Upload
ain-ul-moiz-khawaja
View
286
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
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.
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
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);
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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);
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]
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
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
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 …
Final Table
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
Dijkstra’s Algorithm: Idea
At each step:1) Pick closest
unknown vertex2) Add it to known
vertices3) Update distances
39
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
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
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 ??
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 ??
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 ??
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 ??
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 ??
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
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
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
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
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
QUIZ
1. Find the BREATH-FIRST spanning tree and depth-first spanning tree of the graph GA shown above.