0% found this document useful (0 votes)
5 views

Lec 10 - Graph Algorithms (2)

This document discusses graph algorithms, specifically focusing on Minimum Spanning Trees (MST) and the algorithms used to find them, namely Kruskal's and Prim's algorithms. It outlines the problem of connecting houses in a town with roads at minimal repair costs, defining the concepts of spanning trees and minimum spanning trees. The document also provides examples of Kruskal's algorithm, detailing the steps involved in selecting edges to form an MST without creating cycles.

Uploaded by

laptop2112006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lec 10 - Graph Algorithms (2)

This document discusses graph algorithms, specifically focusing on Minimum Spanning Trees (MST) and the algorithms used to find them, namely Kruskal's and Prim's algorithms. It outlines the problem of connecting houses in a town with roads at minimal repair costs, defining the concepts of spanning trees and minimum spanning trees. The document also provides examples of Kruskal's algorithm, detailing the steps involved in selecting edges to form an MST without creating cycles.

Uploaded by

laptop2112006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

CS311

Algorithm Design and


Analysis
LECTURE 10 : GRAPH ALGORITHMS (2)
Contents
▪ Minimum Spanning Tree
▪Kruskal’s Algorithm
▪Prim’s Algorithm
▪Kruskal’s Algorithm Versus Prim’s Algorithm

ALGORITHM DESIGN AND ANALYSIS 2


Problem
▪A town has a set of houses and a set of 8 7
b c d
roads
4 9
2
▪A road connects 2 and only 2 houses
a 11 i 4 14 e
▪A road connecting houses u and v has a
repair cost w(u, v) 8 7 6 10
h g f
1 2
Goal: Repair roads such that:
1. Everyone stays connected, i.e., can reach every house from all other houses
2. Total repair cost is minimum

ALGORITHM DESIGN AND ANALYSIS 3


Problem
8 7 8 7
b c d b c d
4 9 4 9
2 2
a 11 i 4 14 e a 11 i 14 e
4
7 6 7 6
8 10 8 10
h g f g g f
1 2 1 2

A connected, undirected graph where Vertices = houses, Edges = roads.

A weight w(u, v) on each edge (u, v)  E


Find T  E such that: 1. T connects all vertices
2. w(T) = Σ(u,v)T w(u, v) is minimized

ALGORITHM DESIGN AND ANALYSIS 4


Minimum Spanning Trees
Spanning Tree
◦ A tree (i.e., connected, acyclic graph) which contains all the vertices of the
graph
Minimum Spanning Tree
◦ Spanning tree with the minimum sum of weights
◦ Number of edges in a MST= |V| - 1 8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
g g f
1 2

ALGORITHM DESIGN AND ANALYSIS 5


Minimum spanning tree
Let’s think about simple MSTs on this graph:

1 1 1
a b a b a b
5 5 5
2 4 2 4 2 4

c 3
d c 3
d c 3
d

Not spanning ➔ d is not in T. Not a tree ➔ has a cycle.

ALGORITHM DESIGN AND ANALYSIS 6


Minimum spanning tree
1 1 1
a b a b a b
3 3 3
2 4 2 4 2 4

c 3
d c 3
d c 3
d

Spanning Tree Spanning Tree Spanning Tree


Cost = 8 Cost = 8 Cost = 6
MST

ALGORITHM DESIGN AND ANALYSIS 7


Kruskal’s Algorithm
▪ Kruskal’s algorithm is a “greedy” algorithm.

▪ Creates a forest of trees.

▪ Initially the forest consists of n single node trees (and no edges).

▪ At each step, we add one safe edge (the cheapest one) so that
tree1
it joins two trees together.
▪ Two steps: u
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not generate a cycle v
tree2

ALGORITHM DESIGN AND ANALYSIS 8


Example:
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
Example:
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10

Accepting edge (E,G) would create a cycle


Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
4
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
4
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
4
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1
(C,D) 3  (A,H) 5 
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
F C edge dv edge dv

A 3 (D,E) 1  (B,E) 4 
4
(D,G) 2  (B,F) 4 
5
B D (E,G) 3  (B,H) 4 
H 1
(C,D) 3  (A,H) 5 
2
3
G E
(G,H)
(C,F)
(B,C)
3
3
4



(D,F)
(A,B)
(A,F)
6
8
10
}
not
considered

Done
Total Cost =  dv = 21
Kruskal’s Algorithm Implementation

ALGORITHM DESIGN AND ANALYSIS 23


Kruskal’s Algorithm Complexity
Since E = O(V2),
we have lg E=O(2 lg V) = O(lg V)
O(V)
O(E)
O(E lg E)

O(lg E) O(E lg E)
O(lg E)

The total running time of Kruskal’s algorithm is O(E log E) = O(E log V)
ALGORITHM DESIGN AND ANALYSIS 24
Prim’s Algorithm
▪ Prim’s algorithm has the property that the edges in the set A always form a single tree.
▪ The tree starts from an arbitrary root vertex r and grows until it spans all the vertices in V
▪ Each step adds to the tree A a light edge (a safe edge).
▪ When the algorithm terminates, the edges in A form a minimum spanning tree.
▪ Prim’s algorithm is a “greedy” algorithm

ALGORITHM DESIGN AND ANALYSIS 25


Prim’s Algorithm

ALGORITHM DESIGN AND ANALYSIS 26


Example: We model the situation as a network, then the problem is to find the
minimum connector for the network using Prim’s algorithm.

B 5 C

3
4
8 6

8
A F D
7
5
4 2

E
Prim’s Algorithm
Select any vertex

B 5 A
C
Select the shortest
3 edge connected to
4
8 6 that vertex

8 AB 3
A D
7 F

5
4
2

E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 AE 4
4
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 ED 2
4
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 DC 4
4
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 EF 5
4
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm
All vertices have been
connected.
B 5
C The solution is
3 AB 3
4
8 6 AE 4
ED 2
8 DC 4
A D EF 5
7 F

5
4 Total weight of tree: 18
2

E
Prim’s Algorithm Complexity
Total time: O(V lg V + E lg V) = O(E lg V)
O(V)

O(V)

O(V lg V)

O(E)
O(E lg V)
ALGORITHM DESIGN AND ANALYSIS 34
Kruskal’s Versus Prim’s Algorithms
Kruskal’s algorithm Prim’s algorithm

1. Select the shortest edge in a 1. Select any vertex


network
2. Select the shortest edge
2. Select the next shortest edge connected to that vertex
which does not create a cycle
3. Select the shortest edge
3. Repeat step 2 until all vertices connected to any vertex
have been connected already connected

4. Repeat step 3 until all


vertices have been
connected
Kruskal’s Versus Prim’s Algorithms
▪ Both algorithms will always give solutions with the same length.

▪ They will usually select edges in a different order.

▪ Occasionally they will use different edges – this may happen when you have to choose
between edges with the same length.

▪ In this case there is more than one minimum connector for the network.

ALGORITHM DESIGN AND ANALYSIS 36


Questions

ALGORITHM DESIGN AND ANALYSIS 37

You might also like