52

Graphs > Discrete structures , Data Structures & Algorithums

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Graphs > Discrete structures , Data Structures & Algorithums
Page 2: Graphs > Discrete structures , Data Structures & Algorithums

Minimum Spanning Tree: Prim's AlgorithmPrim's algorithm for finding an MST is a

greedy algorithm. Start by selecting an arbitrary vertex, include

it into the current MST. Grow the current MST by inserting into it the

vertex closest to one of the vertices already in current MST.

Page 3: Graphs > Discrete structures , Data Structures & Algorithums

Minimum Spanning TreeProblem: given a connected, undirected,

weighted graph, find a spanning tree using edges that minimize the total weight

1410

3

6 4

5

2

9

15

8

Page 4: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Page 5: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

1410

3

6 45

2

9

15

8

Run on example graph

Page 6: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

1410

3

6 45

2

9

15

8

Run on example graph

Page 7: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

0

1410

3

6 45

2

9

15

8

Pick a start vertex r

r

Page 8: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

0

1410

3

6 45

2

9

15

8

Black vertices have been removed from Q

u

Page 9: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

0

3

1410

3

6 45

2

9

15

8

Black arrows indicate parent pointers

u

Page 10: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

14

0

3

1410

3

6 45

2

9

15

8

u

Page 11: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

14

0

3

1410

3

6 45

2

9

15

8u

Page 12: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

14

0 8

3

1410

3

6 45

2

9

15

8u

Page 13: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

10

0 8

3

1410

3

6 45

2

9

15

8u

Page 14: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

10

0 8

3

1410

3

6 45

2

9

15

8u

Page 15: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

10 2

0 8

3

1410

3

6 45

2

9

15

8u

Page 16: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

10 2

0 8 15

3

1410

3

6 45

2

9

15

8u

Page 17: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

10 2

0 8 15

3

1410

3

6 45

2

9

15

8

u

Page 18: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

10 2

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 19: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

5 2

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 20: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 21: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 22: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 23: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 24: Graphs > Discrete structures , Data Structures & Algorithums

Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 25: Graphs > Discrete structures , Data Structures & Algorithums

Review: Prim’s AlgorithmMST-Prim(G, w, r)

Q = V[G];

for each u Q key[u] = ; key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;

key[v] = w(u,v);

Page 26: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm

Similar to (BFS) Breadth-First SearchGrow a tree gradually, advancing from vertices taken

from a queue

Also similar to Prim’s algorithm for MSTUse a priority queue keyed on d[v]

Page 27: Graphs > Discrete structures , Data Structures & Algorithums

Shortest Path for Weighted Graphs

Given a graph G = (V, E) with edge costs c(e), and a vertex s V, find the shortest (lowest cost) path from s to every vertex in V

Assume: only positive edge costs

Page 28: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm for Single Source Shortest PathSimilar to breadth-first search, but uses a

heap instead of a queue:Always select (expand) the vertex that has a

lowest-cost path to the start vertex Correctly handles the case where the lowest-

cost (shortest) path to a vertex is not the one with fewest edges

Page 29: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra, Edsger Wybe

Legendary figure in computer science; was a professor at University of Texas.

Supported teaching introductory computer courses without computers (pencil and paper programming)

Supposedly wouldn’t (until very late in life) read his e-mail; so, his staff had to print out messages and put them in his box.

E.W. Dijkstra (1930-2002)

1972 Turning Award Winner, Programming Languages, semaphores, and …

Page 30: Graphs > Discrete structures , Data Structures & Algorithums
Page 31: Graphs > Discrete structures , Data Structures & Algorithums
Page 32: Graphs > Discrete structures , Data Structures & Algorithums
Page 33: Graphs > Discrete structures , Data Structures & Algorithums
Page 34: Graphs > Discrete structures , Data Structures & Algorithums
Page 35: Graphs > Discrete structures , Data Structures & Algorithums
Page 36: Graphs > Discrete structures , Data Structures & Algorithums
Page 37: Graphs > Discrete structures , Data Structures & Algorithums

Final Table

Page 38: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm: IdeaAdapt BFS to handle

weighted graphs

Two kinds of vertices:– Finished or known

vertices• Shortest distance

hasbeen computed

– Unknown vertices• Have tentative

distance

38

Page 39: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm: Idea

At each step:1) Pick closest

unknown vertex2) Add it to known

vertices3) Update distances

39

Page 40: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm: Pseudocode

Initialize the cost of each node to

Initialize the cost of the source to 0

While there are unknown nodes left in the graphSelect an unknown node b with the lowest costMark b as knownFor each node a adjacent to b

a’s cost = min(a’s old cost, b’s cost + cost of (b, a))a’s prev path node = b

40

Page 41: Graphs > Discrete structures , Data Structures & Algorithums

Important FeaturesOnce a vertex is made known, the cost of the

shortest path to that node is knownWhile a vertex is still not known, another

shorter path to it might still be found

41

Page 42: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

42

A B

DC

F H

E

G

0

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A 0

B ??

C ??

D ??

E ??

F ??

G ??

H ??

Page 43: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

43

A B

DC

F H

E

G

0 2

4

1

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B <=2 A

C <=1 A

D <=4 A

E ??

F ??

G ??

H ??

Page 44: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

44

A B

DC

F H

E

G

0 2

4

1

12

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B <=2 A

C Y 1 A

D <=4 A

E <=12 C

F ??

G ??

H ??

Page 45: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

45

A B

DC

F H

E

G

0 2 4

4

1

12

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B Y 2 A

C Y 1 A

D <=4 A

E <=12 C

F <=4 B

G ??

H ??

Page 46: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

46

A B

DC

F H

E

G

0 2 4

4

1

12

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E <=12 C

F <=4 B

G ??

H ??

Page 47: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

47

A B

DC

F H

E

G

0 2 4 7

4

1

12

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E <=12 C

F Y 4 B

G ??

H <=7 F

Page 48: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

48

A B

DC

F H

E

G

0 2 4 7

4

1

12

8

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E <=12 C

F Y 4 B

G <=8 H

H Y 7 F

Page 49: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

49

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E <=11 G

F Y 4 B

G Y 8 H

H Y 7 F

Page 50: Graphs > Discrete structures , Data Structures & Algorithums

Dijkstra’s Algorithm in action

50

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

1

9

2

4

Vertex Visited? Cost Found by

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 F

Page 51: Graphs > Discrete structures , Data Structures & Algorithums

The efficiency of the Dijskras’s algorithm is analyzed by the iteration of the

loop structures. The while loop iteration n – 1 times to visit the minimum

weighted edge. Potentially loop must be repeated n times to examine every

vertices in the graph. So the time complexity is O(n2).

Time Complexity of Dijskrs Algorithm

Page 52: Graphs > Discrete structures , Data Structures & Algorithums

QUIZ

1. Find the BREATH-FIRST spanning tree and depth-first spanning tree of the graph GA shown above.