Docsity

Graphs, Representation - Object Oriented Programming and Data Structures - Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Graphs, Representation - Object Oriented Programming and Data Structures - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

MORE GRAPHS

2 3

2 4

3

1

2

3

4

0 1 0 1

0 0 1 0

0 0 0 0

0 1 1 0

1 2 3 4

1

2

3

4

Representations of Graphs

List Matrix

1

2

3

4

Danaus Park

Representing one thing two ways

 In computer science we often build and use multiple

representations of the same data

 For A5 this isn’t really necessary, but in A6 (coming

soon!) you’ll need to work with both explicit graph

representations of the park and with the 2-D form

in order to have a high quality solution

 For a lower quality solution this won’t be needed

 Best solutions might be 100x or more faster…

Shortest Paths in Graphs

 Finding the shortest (min-cost) path in a graph is a problem

that occurs often

 Find the shortest route between Ithaca and West Lafayette, IN

 Result depends on our notion of cost

 Least mileage… or least time… or cheapest

 Perhaps, expends the least power in the butterfly while flying fastest

 Many “costs” can be represented as edge weights

 A butterfly that optimizes to fly in bright sunshine, or to most efficiently

collect a list of flowers, is optimizing over possible path lengths that are

computed using one or perhaps multiple such factors: machine learning

 How do we find a shortest path?

Dijkstra’s shortest-path algorithm

7

Dijsktra describes the algorithm in English:

When he designed it in 1956, most people were programming in

assembly language!

Only one high-level language: Fortran, developed by John

Backus at IBM and not quite finished.

No theory of order-of-execution time —topic yet to be developed.

In paper, Dijsktra says, “my solution is preferred to another one

… “the amount of work to be done seems considerably less.”

Dijkstra, E.W. A note on two problems in Connexion with graphs.

Numerische Mathematik 1, 269–271 (1959).

Dijkstra’s shortest path algorithm

The n (> 0) nodes of a graph numbered 0..n-1.

L[0] = 2

L[1] = 5

L[2] = 6

L[3] = 7

L[4] = 0

v

Each edge has a positive weight.

Some node v be selected as the start node.

Use an array L[0..n-1]: for each node w, store in

L[w] the length of the shortest path from v to w.

weight(v1, v2) is the weight of the edge from node v1 to v2.

Calculate length of shortest path from v to each node.

  1. For a Settled node s, L[s] is length of shortest v  s path.
  2. All edges leaving S go to F.
  3. For a Frontier node f, L[f] is length of shortest v  f path

using only red nodes (except for f)

  1. For a Far- off node b, L[b] = ∞

Frontier

F

Settled

S

Far off

f

f

(edges leaving the black set and

edges from the blue to the red set

are not shown)

  1. L[v] = 0, L[w] > 0 for w ≠ v

The loop invariant

v

  1. For a Settled node s, L[s] is length of shortest v  r path.
  2. All edges leaving S go to F.
  3. For a Frontier node f, L[f] is length of shortest v  f path

using only Settled nodes (except for f).

  1. For a Far- off node b, L[b] = ∞.

Theorem. For a node f in F with minimum L value (over nodes in

F), L[f] is the length of the shortest path from v to f.

Frontier

F

Settled

S

Far off

f

Theorem about the invariant

v f

g

g

Case 1: v is in S.

Case 2: v is in F. Note that L[v] is 0; it has minimum L value

L[g] ≥ L[f]

  1. L[v] = 0, L[w] > 0 for w ≠ v

When does loop stop? When is

array L completely calculated?

while {

  1. For s, L[s] is length of

shortest v  s path.

  1. Edges leaving S go to F.

S F Far off

  1. For f, L[f] is length of

shortest v  f path using

red nodes (except for f).

  1. For b in Far off, L[b] = ∞
  2. L[v] = 0, L[w] > 0 for w ≠ v

For all w, L[w]= ∞; L[v]= 0;

F= { v }; S= { };

Theorem: For a node f in F

with min L value, L[f] is

shortest path length

F ≠ {}

The algorithm

Loopy question 2:

How is progress toward

termination accomplished?

while {

f= node in F with min L value;

Remove f from F, add it to S;

  1. For s, L[s] is length of

shortest v  s path.

  1. Edges leaving S go to F.

S F Far off

  1. For f, L[f] is length of

shortest v  f path using

red nodes (except for f).

  1. For b, L[b] = ∞
  2. L[v] = 0, L[w] > 0 for w ≠ v

For all w, L[w]= ∞; L[v]= 0;

F= { v }; S= { };

Theorem: For a node f in F

with min L value, L[f] is

shortest path length

f

F ≠ {}

The algorithm

Loopy question 3:

f

For all w, L[w]= ∞; L[v]= 0;

F= { v }; S= { };

while F ≠ {} {

f= node in F with min L value;

Remove f from F, add it to S;

for each edge (f,w) {

if (L[w] is ∞) add w to F;

if (L[f] + weight (f,w) < L[w])

L[w]= L[f] + weight(f,w);

About implementation

  1. No need to implement S.
  2. Implement F as a min-heap.
  3. Instead of ∞, use

Integer.MAX_VALUE.

if (L[w] == Integer.MAX_VAL) {

L[w]= L[f] + weight(f,w);

add w to F;

} else L[w]= Math.min(L[w],

L[f] + weight(f,w));

S F

For all w, L[w]= ∞; L[v]= 0;

F= { v };

while F ≠ {} {

f= node in F with min L value;

Remove f from F;

for each edge (f,w) {

if (L[w] == Integer.MAX_VAL) {

L[w]= L[f] + weight(f,w);

add w to F;

else L[w]=

Math.min(L[w], L[f] + weight(f,w));

Execution time

S F

n nodes, reachable from v. e ≥ n-1 edges

n–1 ≤ e ≤ n*n

O(n)

O(n log n)

O(e)

O(n-1)

O(n log n)

O((e-(n-1)) log n)

O(n)

O(n + e)

outer loop:

n iterations.

Condition

evaluated

n+1 times.

inner loop:

e iterations.

Condition

evaluated

n + e times.

Complete graph: O(n

2

log n). Sparse graph: O(n log n)

O(n)

O(1)

2.

1.

1

2

4 3

2.

0.

1.

3.

0.

X

∞

Dijkstra’s Algorithm

2.

1.

1

2

4 3

2.

0.

1.

3.

0.

X

Dijkstra’s Algorithm

∞