CS 332: Algorithms: Dijkstra's Algorithm Disjoint-Set Union
CS 332: Algorithms: Dijkstra's Algorithm Disjoint-Set Union
CS 332: Algorithms: Dijkstra's Algorithm Disjoint-Set Union
Dijkstras Algorithm
Disjoint-Set Union
Homework 5
Check web page this afternoon
Dijkstras Algorithm
Dijkstra(G)
B
2
10
for each v V
4
3
A
D
d[v] = ;
d[s] = 0; S = ; Q = V;
5
1
C
while (Q )
u = ExtractMin(Q); Ex: run the algorithm
S = S U {u};
for each v u->Adj[]
if (d[v] > d[u]+w(u,v))
Relaxation
Note: this
d[v] = d[u]+w(u,v);
Step
is really a
call to Q->DecreaseKey()
Dijkstras Algorithm
Dijkstra(G)
How many times is
for each v V
ExtractMin() called?
d[v] = ;
d[s] = 0; S = ; Q = V;
How many times is
while (Q )
u = ExtractMin(Q); DecreaseKey() called?
S = S U {u};
for each v u->Adj[]
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);
Dijkstras Algorithm
Dijkstra(G)
How many times is
for each v V
ExtractMin() called?
d[v] = ;
d[s] = 0; S = ; Q = V;
How many times is
while (Q )
u = ExtractMin(Q); DecraseKey() called?
S = S U {u};
for each v u->Adj[]
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);
Dijkstras Algorithm
Dijkstra(G)
for each v V
d[v] = ;
d[s] = 0; S = ; Q = V;
while (Q )
u = ExtractMin(Q);
S = S U{u};
for each v u->Adj[]
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);
Because d[x] is set correctly for y's predecessor x S on the shortest path, and
When we put x into S, we relaxed (x,y), giving d[y] the correct value
Kruskals Algorithm
Kruskal()
{
T = ;
for each v V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
9
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
1?
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2?
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
9
14
17
T = ;
8
25
5?
for each v V
MakeSet(v);
21
13
1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8?
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
9?
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13?
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14?
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17?
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19?
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21?
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25?
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v V
MakeSet(v);
21
13
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
for T:
Assume algorithm is wrong: result is not an MST
Then algorithm adds a wrong edge at some point
If it adds a wrong edge, there must be a lower weight edge
arguments
Kruskals Algorithm
What will affect the running time?
Kruskal()
{
T = ;
for each v V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
What will affect the running time?
Kruskal()
1 Sort
{
O(V) MakeSet() calls
T = ;
O(E) FindSet() calls
for each v V
O(V) Union() calls
(Exactly how many Union()s?)
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Upshot:
Best disjoint-set union algorithm makes above 3
set:
copy
1 element
Union(S2, S3)
copy
2 elements
n-1 elements
O(n2)
using probability
With our new Union(), any individual element is copied
at most lg n times when forming the complete set from
1-element sets
Worst case: Each time copied, element in smaller set
1st time
resulting set size
2nd time
4
The End