






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An analysis of dijkstra's algorithm and kruskal's algorithm, two popular graph algorithms used in computer science. The correctness of both algorithms, their implementation using disjoint sets, and the running time complexity. It also includes examples of running the algorithms and explanations of the concepts involved.
Typology: Slides
1 / 46
This page cannot be seen from the preview
Don't miss anything!







































○ Idea: if were lucky and processes vertices on each shortest path in order, B-F would be done in one pass ○ Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert). ○ Thus: just one pass. Running time: O(V+E)
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);
How many times is ExtractMin() called?
How many times is DecreaseKey() called?
What will be the total running time?
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);
How many times is ExtractMin() called?
How many times is DecraseKey() called?
A: O(E lg V) using binary heap for Q Can acheive O(V lg V + E) with Fibonacci heapsDocsity.com
● Note that d[v] ≥ δ(s,v) ∀v ● Let u be first vertex picked s.t. ∃ shorter path than d[u] ⇒d[u] > δ(s,u) ● Let y be first vertex ∈V-S on actual shortest path from s→u ⇒ d[y] = δ(s,y) ■ 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
s
x
y
u
p
p (^2)
● Note that d[v] ≥ δ(s,v) ∀v ● Let u be first vertex picked s.t. ∃ shorter path than d[u] ⇒d[u] > δ(s,u) ● Let y be first vertex ∈V-S on actual shortest path from s→u ⇒ d[y] = δ(s,y) ● d[u] > δ(s,u) = δ(s,y) + δ(y,u) ( Why? ) = d[y] + δ(y,u) ≥ d[y] But if d[u] > d[y], wouldn't have chosen u. Contradiction.
s
x
y
u
p
p (^2)
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));
}
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));
}
(^2 ) 9
1
5 13
17 25
14 8
21
Run the 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));
}
(^2 ) 9
1
5 13
17 25
14 8
21
Run the 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));
}
(^2 ) 9
1?
5 13
17 25
14 8
21
Run the 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));
}
2? (^19) 9
1
5 13
17 25
14 8
21
Run the 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));
}
(^2 ) 9
1
5 13
17 25
14 8
21
Run the algorithm: