CS 332: Algorithms. Topological Sort Minimum Spanning Trees. Review: BreadthFirst Search. BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v u>adj {
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue (duh); initialize to s
while (Q not empty) {
u = RemoveTop(Q);
for each v u>adj {
if (v>color == WHITE)
v>color = GREY;
v>d = u>d + 1;
v>p = u;
Enqueue(Q, v);
}
u>color = BLACK;
}
}
v>d represents depth in tree
v>p represents parent in tree
David Luebke 23/10/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = YELLOW;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
Review: DFS CodeDavid Luebke 33/10/2014
sourcevertex
d f
1 12
8 11

2  7
9 10
3  4
5  6

David Luebke 163/10/2014
sourcevertex
d f
1 12
8 11
13
2  7
9 10
3  4
5  6

David Luebke 173/10/2014
sourcevertex
d f
1 12
8 11
13
2  7
9 10
3  4
5  6
14
David Luebke 183/10/2014
sourcevertex
d f
1 12
8 11
13
2  7
9 10
3  4
5  6
1415
David Luebke 193/10/2014
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
David Luebke 203/10/2014
David Luebke 213/10/2014
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
Tree edges
Back edges
Forward edges
Cross edges
David Luebke 223/10/2014
David Luebke 233/10/2014
David Luebke 243/10/2014
David Luebke 253/10/2014
David Luebke 263/10/2014
Underwear
Socks
Watch
Pants
Shoes
Shirt
Belt
Tie
Jacket
Socks
Underwear
Pants
Shoes
Watch
Shirt
Belt
Tie
Jacket
David Luebke 283/10/2014
TopologicalSort()
{
Run DFS
When a vertex is finished, output it
Vertices are output in reverse topological order
}
David Luebke 293/10/2014
David Luebke 303/10/2014
6
4
5
9
14
2
10
15
3
8
David Luebke 313/10/2014
6
4
5
9
14
2
10
15
3
8
David Luebke 323/10/2014
A
6
4
5
9
H
B
C
14
2
10
15
G
E
D
3
8
F
David Luebke 333/10/2014
David Luebke 353/10/2014
David Luebke 363/10/2014
David Luebke 373/10/2014
MSTPrim(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);
David Luebke 383/10/2014
MSTPrim(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);
6
4
9
5
14
2
10
15
3
8
Run on example graph
David Luebke 393/10/2014
MSTPrim(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);
6
4
9
5
14
2
10
15
3
8
Run on example graph
David Luebke 403/10/2014
MSTPrim(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);
6
4
9
5
14
2
10
15
r
0
3
8
Pick a start vertex r
David Luebke 413/10/2014
MSTPrim(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);
6
4
9
5
14
2
10
15
u
0
3
8
Red vertices have been removed from Q
David Luebke 423/10/2014
MSTPrim(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);
6
4
9
5
14
2
10
15
u
0
3
8
3
Red arrows indicate parent pointers
David Luebke 433/10/2014
MSTPrim(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);
6
4
9
5
14
14
2
10
15
u
0
3
8
3
David Luebke 443/10/2014
MSTPrim(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);
6
4
9
5
14
14
2
10
15
0
3
8
3
u
David Luebke 453/10/2014
MSTPrim(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);
6
4
9
5
14
14
2
10
15
0
8
3
8
3
u
David Luebke 463/10/2014
MSTPrim(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);
6
4
9
5
10
14
2
10
15
0
8
3
8
3
u
David Luebke 473/10/2014
MSTPrim(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);
6
4
9
5
10
14
2
10
15
0
8
3
8
3
u
David Luebke 483/10/2014
MSTPrim(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);
6
4
9
5
10
2
14
2
10
15
0
8
3
8
3
u
David Luebke 493/10/2014
MSTPrim(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);
6
4
9
5
10
2
14
2
10
15
0
8
15
3
8
3
u
David Luebke 503/10/2014
u
MSTPrim(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);
6
4
9
5
10
2
14
2
10
15
0
8
15
3
8
3
David Luebke 513/10/2014
u
MSTPrim(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);
6
4
9
5
10
2
9
14
2
10
15
0
8
15
3
8
3
David Luebke 523/10/2014
u
MSTPrim(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);
4
6
4
9
5
10
2
9
14
2
10
15
0
8
15
3
8
3
David Luebke 533/10/2014
u
MSTPrim(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);
4
6
4
9
5
5
2
9
14
2
10
15
0
8
15
3
8
3
David Luebke 543/10/2014
u
MSTPrim(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);
4
6
4
9
5
5
2
9
14
2
10
15
0
8
15
3
8
3
David Luebke 553/10/2014
u
MSTPrim(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);
4
6
4
9
5
5
2
9
14
2
10
15
0
8
15
3
8
3
David Luebke 563/10/2014
u
MSTPrim(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);
4
6
4
9
5
5
2
9
14
2
10
15
0
8
15
3
8
3
David Luebke 573/10/2014
MSTPrim(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);
4
6
4
9
5
5
2
9
u
14
2
10
15
0
8
15
3
8
3
David Luebke 583/10/2014
MSTPrim(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);
What is the hidden cost in this code?
David Luebke 593/10/2014
MSTPrim(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;
DecreaseKey(v, w(u,v));
David Luebke 603/10/2014
MSTPrim(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;
DecreaseKey(v, w(u,v));
How often is ExtractMin() called?
How often is DecreaseKey() called?
David Luebke 613/10/2014
MSTPrim(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);
What will be the running time?A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)
David Luebke 623/10/2014
David Luebke 633/10/2014