0% found this document useful (0 votes)
71 views13 pages

Introduction To Algorithms: Concrete Example Concrete Example

The document discusses Kruskal's algorithm for finding a minimum spanning tree (MST) in a graph. Kruskal's algorithm is a greedy algorithm that works as follows: 1) Sort the edges by weight from lowest to highest. 2) Add the lowest weight edge that does not create a cycle to the MST. 3) Repeat step 2 until n-1 edges are added, where n is the number of vertices. The algorithm uses disjoint sets to track connected components and check for cycles in constant time per edge added.
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)
71 views13 pages

Introduction To Algorithms: Concrete Example Concrete Example

The document discusses Kruskal's algorithm for finding a minimum spanning tree (MST) in a graph. Kruskal's algorithm is a greedy algorithm that works as follows: 1) Sort the edges by weight from lowest to highest. 2) Add the lowest weight edge that does not create a cycle to the MST. 3) Repeat step 2 until n-1 edges are added, where n is the number of vertices. The algorithm uses disjoint sets to track connected components and check for cycles in constant time per edge added.
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/ 13

Concrete example

Introduction to Algorithms
Imagine: You wish to connect all the computers in an
office building using the least amount of cable

- Each vertex in a graph G represents a computer


- Each edge represents the amount of cable needed to
connect all computers

Chapter 23: Minimum Spanning Trees

Spanning Tree Minimum Spanning


p g Tree
„ Input:
„ A spanning tree of G is a subgraph which
‰ Undirected connected graph G = (V, E) and weight
‰ is tree (acyclic)
function w : E→R,
‰ and connect all the vertices in V.

„ Output:
‰ A Minimum
Mi i i ttree T : tree
spanning t that
th t connects
t allll
the vertices and minimizes w(T ) = ∑ w(u, v)
(u ,v )∈T

„ Greedy Algorithms
‰ Kruskal’s algorithm

‰ Prim’s algorithm

‰ Spanning tree has only |V| - 1 edges.


edges
Example: MST Example

Kruskal'ss Algorithm
Kruskal Kruskal'ss Algorithm
Kruskal
INPUT:
„ Edge based algorithm „
‰ edge-weighted graph G = (V, E), with |V| = n

„ OUTPUT:
‰ a spanning tree A of G
„ Greedy strategy: „ touches all vertices,
„ has n-1 edges
„ off minimum
i i costt ( = ttotal
t l edge
d weight)
i ht)
‰ From the remaining edges, select a least-cost
„ Algorithm:
edge that does not result in a cycle when added
‰ Start with A empty,
to the set of already selected edges ‰ Add the edges one at a time, in increasing weight order

‰ An edge is accepted if it connects vertices of distinct trees


(if the edge does not form a cycle in A)
‰ Repeat |V|-1 times ‰ until A contains n-1 edges
Kruskal'ss Algorithm
Kruskal Data Structures For Kruskal’s Algorithm
g

MST Kruskal(G,w)
MST-Kruskal(
„ Does the addition of an edge (u, v) to T
1 A ← ∅ result in a cycle?
2 for each vertex v ∈ V[G] do
3 Set(v) //creates set containing v (for initialization)
Make-Set(
Make „ Each component
p of T is a tree.
4 sort the edges of E ‰ When u and v are in the
5 for each (u,v)∈E do
6 Fi d S t(u) ≠ Find-Set(
if Find-Set( Fi d S t(v) then th // diff
different
t „ component the addition of the edge (u, v)
same component,
component creates a cycle. 1 3
7 A ← A ∪ {(u,v)} 7 4
2
8 Union(Set(u),Set(v)) // merge
9 return A 2 4
„ different components, the addition of the edge (u, v)
does not create a cycle. 5 7
6 3

6 8

Data Structures For Kruskal’s Algorithm


g Data Structures For Kruskal’s Algorithm
g
„ Each component of T is defined by the „ When an edge (u, v) is added to T, the two
vertices in the component
component. components that have vertices u and v combine
to become a single component
„ Represent each component as a set of
„ In our set representation of components, the set
vertices.
vertices
that has vertex u and the set that has vertex v
‰ {1, 2, 3, 4}, {5, 6}, {7, 8} are united.
„ Two vertices are in the same component iff
ff ‰ {1, 2, 3, 4} + {5, 6} Î {1, 2, 3, 4, 5, 6}
they are in the same set of vertices.
1 3 5 7
1 3 5 7 7 4 6 3
7 2
4 6 3
2 2 4 6 8
2 4 6 8
Kruskal’ss Algorithm
Kruskal
2 19 2 19
Kruskal’ss Algorithm
Kruskal
2 19 2 19
9 9 9 9
14 17 14 17 14 17 14 17
8 25 8 25 8 25 8 25
5 5 5 5?
21 13 1 21 13 1 21 13 1 21 13 1

2 19 2 19
2 19 2 19
9 9
14 17 14 17 9 9
8 25 8 25 14 17 14 17
5 5 8 25 8? 25
5 5
21 13 1 21 13 1?
21 13 1 21 13 1

2 19 2? 19
2 19 2 19
9 9
14 17 14 17 9 9?
8 25 8 25 14 17 14 17
5 5 25 8 25
5 5
21 13 1 21 13 1
21 13 1 21 13 1

Kruskal’ss Algorithm
Kruskal
2 19 2 19
Kruskal’ss Algorithm
Kruskal
2 19? 2 19
9 9 9 9
14 17 14 17 14 17 14 17
8 25 8 25 8 25 8 25
5 5 5 5
21 13 1 21 13? 1 21 13 1 21? 13 1

2 19 2 19 2 19 2 19
9 9 9 9
14 17 14
14? 17 14 17 14 17
8 25 8 25 8 25? 8 25
5 5 5 5

21 13 1 21 13 1 21 13 1 21 13 1

2 19 2 19 2 19
9 9 9
14 17 14 17? 14 17
8 25 8 25 8 25
5 5 5
21 13 1 21 13 1 21 13 1
5 5
A B A B
4 6 2 4 6 2

2 D 3 2 D 3
C C

3 1 2 3 1 2
E F E F
4 4

5 5
A B A B
4 6 2 4 6 2

2 D 3 2 D 3
C C

3 1 2 3 1 2
E F E F
4 4
5 5
A B A B
4 6 2 4 6 2

2 D 3 2 D 3 cycle!!
C C

3 1 2 3 1 2
E F E F
4 4

5 5
A B A B
4 6 2 4 6 2

2 D 3 2 D 3
C C

3 1 2 3 1 2
E F E F
4 4
Kruskal'ss Algorithm
Kruskal
minimum-
u spspanning
g tree
ee MST-Kruskal(G,w)
1 A ← ∅
2 for each vertex v ∈ V[G] do // takes O(V)
3 (v)
Make-Set(
4 sort the edges of E // takes O(E lg E)
A B // takes O(E)
5 for each (u,v)∈E, in nondecreasing of weight do
2
6 if Find-Set(u) ≠ Find-Set(v) then
7 A ← A ∪ {(u,v)}
2 D 8 Union(Set(u),Set(
) Set(v))
C 9 return A

3 1 2
E F

Running Time of Kruskal


Kruskal’ss Algorithm Prim’ss Algorithm
Prim
Kruskal’s Algorithm
g consists of two stages.
g
„
‰ Initializing the set A in line 1 takes O(1) time.
„ The tree starts from an arbitrary root vertex r
‰ Sorting the edges by weight in line 4.
and grows until the tree spans all the vertices
„ takes O(E lg E) in V
V.
‰ Performing
„ |V| MakeSet() operations for loop in lines 2-3.
2-3 „ At each step
step,
„ |E| FindSet(), for loop in lines 5-8.
„ |V| - 1 Union(), for loop in lines 5-8.
‰ Adds only edges that are safe for A.
„ which takes O(V + E) ‰ When algorithm terminates
terminates, edges in A form MST
MST.
„ The total running time is
‰ O(E lg E) „ Vertex based algorithm.
‰ We have lg │E│ = O(lg V) because # of E = V-1

‰ So total running time becomes O(E lg V). „ Grows one tree T, one vertex at a time
Prim’ss Algorithm
Prim Prim’ss Algorithm
Prim
MST-Prim(G,w,r) //G: graph with weight w and a root vertex r
„ Lines 1-5 set the key of each vertex to ∞ (except root r,
1 for each u ∈ V[G]{
2 key[u] ← ∞ whose key is set to 0 first vertex processed).
processed) Also
Also, set
3 p[u] ← NULL // parent of u parent of each vertex to NULL, and initialize min-priority
} queue Q to contain all vertices.
4 key[r] ← 0
5 Q = BuildMinHeap(V,key); // Q – vertices out of T
6 while Q ≠ ∅ do „ Line 7 identifies a vertex u є Q
7 u ← ExtractMin(Q) // making u part of T
8 for each v ∈ Adj[u] do
9 if v ∈ Q and w(u,v)
( , ) < key[v]
y[ ] then updating „ Removing u from set Q adds it to set Q-V of vertices in tree,
10 p[v] ← u keys thus adding (u, p[ u]) to A.
11 key[v] ← w(u,v)
„ The for loop of lines 8-11
8 11 update key and p fields of every
• For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree. vertex v adjacent to u but not in tree.
• key [v]= ∞ if there is no edge and p [v] names parent of v in tree.
• When
Wh algorithm
l i h terminatesi the
h min-
min
i -priority
i i queue Q is i empty.

Run on example graph Run on example graph


6 4 6 ∞ 4
5 9 5 9
∞ ∞ ∞

14 2 14 2
10 10
15 15
∞ ∞ ∞
3 8 3 ∞ 8

key[u] = ∞
Run on example graph Run on example graph
6 ∞ 4 6 ∞ 4
5 9 5 9
∞ ∞ ∞ ∞ ∞ ∞

14 2 14 2
10 10
15 15
r 0 ∞ ∞ u 0 ∞ ∞
3 ∞ 8 3 ∞ 8

Pick a start vertex r Red vertices have been removed from Q

Run on example graph Run on example graph


6 ∞ 4 6 ∞ 4
5 9 9
∞ ∞ ∞ 14 5
∞ ∞

14 2 14 2
10 10
15 15
u 0 ∞ ∞ u 0 ∞ ∞
3 3 8 3 3 8

Red arrows indicate parent pointers


Run on example graph Run on example graph
6 ∞ 4 6 ∞ 4
5 9 5 9
14 ∞ ∞ 14 ∞ ∞

14 2 14 2
10 10
15 15
0 ∞ ∞ 0 8 ∞
3 3 8 3 3 8
u u

Extract min from Q


Extract_min

Run on example graph Run on example graph


6 ∞ 4 6 ∞ 4
5 9 5 9
10 ∞ ∞ 10 ∞ ∞

14 2 14 2
10 10
15 15
0 8 ∞ 0 8 ∞
3 3 8 3 3 8
u u
Run on example graph Run on example graph
6 ∞ 4 6 ∞ 4
5 9 5 9
10 2 ∞ 10 2 ∞

14 2 14 2
10 10
15 15
0 8 ∞ 0 8 15
3 3 8 3 3 8
u u

Run on example graph Run on example graph


6 ∞ 4
u
6 ∞ 4
u
5 9 5 9
10 2 ∞ 10 2 9

14 2 14 2
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8
Run on example graph Run on example graph
4 u 4 u
6 4 6 4
5 9 5 9
10 2 9 5 2 9

14 2 14 2
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8

Run on example graph Run on example graph


u
4 u 4
6 4 6 4
5 9 5 9
5 2 9 5 2 9

14 2 14 2
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8
Run on example graph Run on example graph
u
6 4 4 6 4 4
5 9 5 9
5 2 9 5 2 9

14 2 14 2 u
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8

You might also like