Prim's and Kruskal's Algorithms for Minimum Spanning Tree, Study notes of Algorithms and Programming

An explanation of prim's and kruskal's algorithms for finding the minimum spanning tree (mst) of a graph. The pseudocode for both algorithms, as well as explanations of how they work. Prim's algorithm grows a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree. Kruskal's algorithm grows a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far. Both algorithms are important in graph theory and have applications in various fields, including computer networks and transportation networks.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-kpd
koofers-user-kpd 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1
1
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
Greedy Algorithms
and
Graph Optimization Problems
Dr. Steve Goddard
CSCE 310J
Data Structures & Algorithms
2
CSCE 310J
Data Structures & Algorithms
Giving credit where credit is due:
» Most of slides for this lecture are based on
slides created by Dr. David Luebke, University
of Virginia.
» Some examples and slides are based on lecture
notes created by Dr. Chuck Cusack, UNL, Dr.
Jim Cohoon, University of Virginia, and Dr.
Ben Choi, Louisiana Technical University.
» I have modified them and added new slides
3
Greedy Algorithms
Main Concept: Make the best or greedy choice at any
given step.
» This is what you did before you learned to not to think ahead and
plan
Choices are made in sequence such that
» Each individual choice is best according to some limited “short-
term” criterion, that is not too expensive to evaluate
» Once a choice is made, it cannot be undone!
Even if it becomes evident later that it was a poor choice
Sometimes life is like that
The goal is to make progress by choosing an action that
» Incurs the minimum short-term cost,
» With the hope that a lot of small short-term costs add up to small
overall cost.
4
When to be Greedy
Greedy algorithms apply to problems with
» The greedy choice property: an optimal solution can be obtained
by making the greedy choice at each step.
»Optimal substructures: optimal solutions contain optimal sub-
solutions.
» Many optimal solutions, but we only need one such solution.
Unlike dynamic programming, we do not need to know the
solutions to the sub-problems to make choices.
» Hence, greedy algorithms are often more efficient than dynamic
programming.
Possible drawback:
» Actions with a small short-term cost may lead to a situation, where
further large costs are unavoidable.
5
The Fractional Knapsack
Problem
A thief breaks into a cookie store.
She has a bag that can hold up to Cpounds of cookies.
There are ncookies in the store.
The ith cookie weighs wipounds and is worth vidollars.
She can break the cookies and sell fractions of them.
The thief wants to maximize the value of the cookies she
steals, of course.
How much of each cookie should she steal?
6
Some Greedy Observations
The ith cookie is worth pi= vi/widollars per pound.
The item with the largest pihas the most “bang for
the buck,” so the thief should take as much as she
can of this item.
If the thief takes xpounds of a cookie with pj<pi
instead of cookie i, her profit will be smaller for
the same weight.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Prim's and Kruskal's Algorithms for Minimum Spanning Tree and more Study notes Algorithms and Programming in PDF only on Docsity!

1

http://www.cse.unl.edu/~goddard/Courses/CSCE310J

Greedy Algorithms

and

Graph Optimization Problems

Dr. Steve Goddard

[email protected]

CSCE 310J

Data Structures & Algorithms

2

CSCE 310J

Data Structures & Algorithms

 Giving credit where credit is due:

» Most of slides for this lecture are based on

slides created by Dr. David Luebke, University

of Virginia.

» Some examples and slides are based on lecture

notes created by Dr. Chuck Cusack, UNL, Dr.

Jim Cohoon, University of Virginia, and Dr.

Ben Choi, Louisiana Technical University.

» I have modified them and added new slides

3

Greedy Algorithms

 Main Concept: Make the best or greedy choice at any

given step.

» This is what you did before you learned to not to think ahead and plan 

 Choices are made in sequence such that

» Each individual choice is best according to some limited “short- term” criterion, that is not too expensive to evaluate » Once a choice is made, it cannot be undone!  Even if it becomes evident later that it was a poor choice  Sometimes life is like that 

 The goal is to make progress by choosing an action that

» Incurs the minimum short-term cost, » With the hope that a lot of small short-term costs add up to small overall cost. 4

When to be Greedy

 Greedy algorithms apply to problems with

» The greedy choice property : an optimal solution can be obtained by making the greedy choice at each step. » Optimal substructures : optimal solutions contain optimal sub- solutions. » Many optimal solutions, but we only need one such solution.

 Unlike dynamic programming, we do not need to know the

solutions to the sub-problems to make choices.

» Hence, greedy algorithms are often more efficient than dynamic programming.

 Possible drawback:

» Actions with a small short-term cost may lead to a situation, where further large costs are unavoidable.

5

The Fractional Knapsack

Problem

 A thief breaks into a cookie store.

 She has a bag that can hold up to C pounds of cookies.

 There are n cookies in the store.

 The ith^ cookie weighs wi pounds and is worth vi dollars.

 She can break the cookies and sell fractions of them.

 The thief wants to maximize the value of the cookies she

steals, of course.

 How much of each cookie should she steal?

6

Some Greedy Observations

 The ith^ cookie is worth pi = vi / wi dollars per pound.

 The item with the largest pi has the most “bang for

the buck,” so the thief should take as much as she

can of this item.

 If the thief takes x pounds of a cookie with pj < pi

instead of cookie i , her profit will be smaller for

the same weight.

7

Fractional Knapsack

Problem

 Problem

» Given a set of n objects where object i has value vi per

cookie and weight wi and a knapsack capacity C ,

determine the fractional amount fi of each object i to be

included in the knapsack such that the profit is

maximized while the weight of the included objects

does not exceed the knapsack capacity

 maximize such that

where 0 ≤ fi ≤ 1



n

i

vi fi

1

wf C

n

i

 i i ≤ = 1

8

Strategy: Pick by Density

 Strategy

» Sort objects in non-increasing order of profit pi = vi / wi

 That is, p 1 =v 1 /w 1 ≥ p 2 =v 2 /w 2 ≥ ... ≥ pn =vn/wn

» Consider objects by increasing order of subscript.

» When an object is considered choose a maximal amount

such that the knapsack capacity is not violated.

9

A Greedy Solution

fractionalKnapsack(V, W, capacity, n, KnapSack) { sortByDescendingProfit(V,W,n) KnapSack = 0; capacityLeft = C; for (i = 1; (i <= n) && (capacityLeft > 0); ++i) { if (W[i] < capacityLeft) KnapSack[i] = 1; capacityLeft -= W[i]; else KnapSack[i] = capacityLeft/W[i]; capacityLeft = 0; } } } What is the complexity of this algorithm? 10

Knapsack Example

 The thief’s knapsack holds 15 pounds

 The cookie inventory has the following properties:

pi 3 1.33 1 0.5 8 2 1.2.

wi 4 3 5 6 1 4 10 4

vi 12 4 5 3 8 8 12 1

i 1 2 3 4 5 6 7 8

pi 8 3 2 1.33 1.2 1 0.5.

wi 1 4 4 3 10 5 6 4

vi 8 12 8 4 12 5 3 1

i 6 1 6 2 7 3 4 8

 Same properties sorted by profit

11

Knapsack Example Solution

 The thief takes 1 pound of cookie 5, 4 pounds of cookie 1,

4 pounds of cookie 6, 3 pounds of cookie 2, and 3 pounds

of cookie 7:

 Thus, the profit is 8+ 12 + 8 + 4 + 3.6 = $35.

 Solution property for non-trivial instances

» knapSack = (1, 1,... 1, fj, 0, 0,... 0) with 0 < fj ≤ 1

 Is it Optimal?

pi 8 3 2 1.33 1.2 1 0.5.

wi 1 4 4 3 3 /10 5 6 4

vi 8 12 8 4 12 5 3 1

i 6 1 6 2 7 3 4 8

12

0/1 Knapsack

 fi is restricted to be either 0 or 1

 Does pick by value work?

 Does pick by weight work?

 Does pick by density work?

19

Minimum Spanning Tree

 MSTs satisfy the optimal substructure property:

an optimal tree is composed of optimal subtrees

» Let T be an MST of G with an edge ( u,v ) in the middle

» Removing ( u,v ) partitions T into two trees T 1 and T 2

» Claim: T 1 is an MST of G 1 = (V 1 ,E 1 ), and T 2 is an MST

of G 2 = (V 2 ,E 2 ) ( Do V 1 and V 2 share vertices? Why? )

» Proof: w(T) = w( u,v ) + w(T 1 ) + w(T 2 )

(There can’t be a better tree than T 1 or T 2 , or T would

be suboptimal)

20

Minimum Spanning Tree

 Thm:

» Let T be MST of G, and let A ⊆ T be subtree of T

» Let ( u,v ) be min-weight edge connecting A to V-A

» Then ( u,v ) ∈ T

 Proof: left as an exercise

21

Finding a MST

 Principal greedy methods: algorithms by Prim and Kruskal

 Prim

» Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree  Intermediary solution is a subtree

 Kruskal

» Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far  Intermediary solution is a spanning forest

22

Prim’s Algorithm

MST-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);

Grow a single tree by repeatedly

adding the least cost edge that

connects a vertex in the existing tree

to a vertex not in the existing tree

Intermediary solution is a subtree

23

Prim’s Algorithm

MST-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 10

3

6 4 5

2

9

15

8 Run on example graph

24

Prim’s Algorithm

MST-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 10

3

6 4 5

2

9

15

8 Run on example graph

25

Prim’s Algorithm

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

3

6 4 5

2

9

15

8 Pick a start vertex r

r

26

Prim’s Algorithm

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

3

6 4 5

2

9

15

8 Red vertices have been removed from Q

u

27

Prim’s Algorithm

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

3

6 4 5

2

9

15

8 Red arrows indicate parent pointers

u

28

Prim’s Algorithm

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

3

6 4 5

2

9

15

8

u

29

Prim’s Algorithm

MST-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 10

3

6 4 5

2

9

15

8 u

30

Prim’s Algorithm

MST-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 10

3

6 4 5

2

9

15

8 u

37

Prim’s Algorithm

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

3

6 4 5

2

9

15

8

u

38

Prim’s Algorithm

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

3

6 4 5

2

9

15

8

u

39

Prim’s Algorithm

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

3

6 4 5

2

9

15

8

u

40

Prim’s Algorithm

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

3

6 4 5

2

9

15

8

u

41

Prim’s Algorithm

MST-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 10

3

6 4 5

2

9

15

8

u

42

Prim’s Algorithm

MST-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 10

3

6 4 5

2

9

15

8

u

43

Review: Prim’s Algorithm

MST-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);

What is the hidden cost in this code?

44

Review: Prim’s Algorithm

MST-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; DecreaseKey(v, w(u,v));

45

Review: Prim’s Algorithm

MST-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; DecreaseKey(v, w(u,v));

How often is ExtractMin() called?

How often is DecreaseKey() called?

46

Review: Prim’s Algorithm

MST-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);

What will be the running time?

There are n=|V| ExtractMin calls and

m=|E| DecreaseKey calls. Thus, the

worst case is O(n^2 +m).

The priority Q implementation

has a large impact on

performance.

E.g., O((n+m)lg n) = O(m lg n) using binary heap for Q

Can achieve O(n lg n + m) with Two-pass pairing or Fibonacci heaps

47

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,v}}; Union(FindSet(u), FindSet(v)); }

Grow a tree by repeatedly adding the least

cost edge that does not introduce a cycle

among the edges included so far

Intermediary solution is a spanning

forest

48

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

55

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

56

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

57

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

58

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

59

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

60

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

61

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

62

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

63

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

64

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

65

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

66

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,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

73

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

What will affect the running time?

Let n=|V| and m=|E|

1 Sort

O(n) MakeSet() calls

O(m) FindSet() calls

O(n) Union() calls

74

Kruskal’s Algorithm:

Running Time

 To summarize:

» Sort edges: O(m lg m)

» O(n) MakeSet()’s

» O(m) FindSet()’s

» O(n) Union()’s

 Upshot:

» Best disjoint-set union algorithm makes above 3

operations take O(m⋅α(m,n)), α almost constant

» Overall thus O(m lg m), almost linear w/o sorting

75

Single-Source Shortest Path

 Problem: given a weighted directed graph G, find

the minimum-weight path from a given source

vertex s to another vertex v

» “Shortest-path” = minimum weight

» Weight of path is sum of edges

» E.g., a road map: what is the shortest path from

Minneapolis to Lincoln?

76

Shortest Path Properties

 Again, we have optimal substructure : the shortest

path consists of shortest subpaths:

» Proof: suppose some subpath is not a shortest path

 There must then exist a shorter subpath  Could substitute the shorter subpath for a shorter path  But then overall path is not shortest path. Contradiction

77

Shortest Path Properties

 Define δ(u,v) to be the weight of the shortest path

from u to v

 Shortest paths satisfy the triangle inequality :

δ(u,v) ≤ δ(u,x) + δ(x,v)

 “Proof”:

x

u v

This path is no longer than any other path

78

Shortest Path Properties

 In graphs with negative weight cycles, some

shortest paths will not exist (Why ?):

79

Relaxation

 A key technique in shortest path algorithms is

relaxation

» Idea: for all v , maintain upper bound d[ v ] on δ( s , v )

Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w; }

u v

Relax

u v

Estimated d[u] d[v] v 5 9

u

Relax

u v

New d[v]

80

Dijkstra’s Algorithm

 If no negative edge weights, we can beat BFS

 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]

81

Dijkstra’s Algorithm

Dijkstra(G,s)

for each v ∈∈∈∈ V

d[v] = ∞∞∞∞;

d[s] = 0; S = ∅∅∅∅; Q = V;

while (Q ≠≠≠≠ ∅∅∅∅)

u = ExtractMin(Q);

S = S {u};

for each v ∈∈∈∈ Adj[u]

if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);

Relaxation

Note: this Step

is really a call to Q->DecreaseKey()

B

C

A D

Ex: run the algorithm

82

Dijkstra’s Algorithm

Dijkstra(G,s)

for each v ∈∈∈∈ V

d[v] = ∞∞∞∞;

d[s] = 0; S = ∅∅∅∅; Q = V;

while (Q ≠≠≠≠ ∅∅∅∅)

u = ExtractMin(Q);

S = S {u};

for each v ∈∈∈∈ Adj[u]

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?

What will be the total running time?

83

Analysis of

Dijkstra’s Algorithm

Dijkstra(G,s)

for each v ∈∈∈∈ V

d[v] = ∞∞∞∞;

d[s] = 0; S = ∅∅∅∅; Q = V;

while (Q ≠≠≠≠ ∅∅∅∅)

u = ExtractMin(Q);

S = S {u};

for each v ∈∈∈∈ Adj[u]

if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);

There are n=|V| ExtractMin calls and

m=|E| DecreaseKey calls. Thus, the

worst case is O(n^2 +m).

The priority Q implementation

has a large impact on

performance.

E.g., O((n+m)lg n) = O(m lg n) using binary heap for Q

Can achieve O(n lg n + m) with Fibonacci heaps

84

Dijkstra’s Algorithm

Dijkstra(G,s)

for each v ∈∈∈∈ V

d[v] = ∞∞∞∞;

d[s] = 0; S = ∅∅∅∅; Q = V;

while (Q ≠≠≠≠ ∅∅∅∅)

u = ExtractMin(Q);

S = S (^) {u};

for each v ∈∈∈∈ Adj[u]

if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);

Correctness: we must show that when u is

removed from Q, it has already converged