Analysis of Dijkstra's Algorithm and Kruskal's Algorithm with Disjoint Set Union, Slides of Computer Science

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

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Dijkstra’s Algorithm
Disjoint-Set Union
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download Analysis of Dijkstra's Algorithm and Kruskal's Algorithm with Disjoint Set Union and more Slides Computer Science in PDF only on Docsity!

Algorithms

Dijkstra’s Algorithm

Disjoint-Set Union

Homework 5

● Check web page this afternoon…

Review: DAG Shortest Paths

● Problem: finding shortest paths in DAG

■ Bellman-Ford takes O(VE) time.

■ Do better using topological sort.

○ 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)

Review: Dijkstra’s Algorithm

● If no negative edge weights, we can beat BF

● Similar to breadth-first search

■ Grow a tree gradually, advancing from vertices

taken from a queue

● Also similar to Prim’s algorithm for MST

■ Use a priority queue keyed on d[v]

Dijkstra’s 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);

How many times is ExtractMin() called?

How many times is DecreaseKey() called?

What will be the total running time?

Dijkstra’s 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);

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

Correctness Of Dijkstra's Algorithm

● 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)

Correctness Of Dijkstra's Algorithm

● 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’s 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));

}

Kruskal’s 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’s 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’s 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’s 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’s 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: