0% found this document useful (0 votes)
240 views68 pages

Minimum Spanning Tree

The document discusses two algorithms for finding minimum spanning trees: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm starts with each vertex as its own tree and merges trees together based on the lowest weight edges that connect trees. Prim's algorithm starts from a single vertex and extends the tree by adding the closest edge to a new vertex at each step. Examples are provided to illustrate the steps of each algorithm.

Uploaded by

_Nishant_
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views68 pages

Minimum Spanning Tree

The document discusses two algorithms for finding minimum spanning trees: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm starts with each vertex as its own tree and merges trees together based on the lowest weight edges that connect trees. Prim's algorithm starts from a single vertex and extends the tree by adding the closest edge to a new vertex at each step. Examples are provided to illustrate the steps of each algorithm.

Uploaded by

_Nishant_
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 68

CSCI203 Algorithm & Data

Structure

Tutorial 5
Sionggo Japit
[email protected]

Minimum Spanning Tree


What is a spanning tree and minimum spanning
tree?
In a mathematical graph theory, from a given
connected, undirected graph, a spanning tree
of that graph is a subgraph which is a tree that
connects all the vertices together. A minimum
spanning tree is a spanning tree with weight
less than or equal to the weight of every other
spanning tree.
CSCI203 - Algorithm & Data Structure

Minimum Spanning Tree

4
3
4

5
4

6
3

CSCI203 - Algorithm & Data Structure

Minimum Spanning Tree


Two algorithms for finding Minimum
Spanning Tree:
Kruskals Algorithm
Prims Algorithm

Kruskals Algorithm
Kruskals algorithms for finding a minimum
spanning tree:
Kruskal's algorithm starts with an empty set S and
selects at each stage the shortest edge that has
been neither selected nor rejected.

Kruskals Algorithm
Kruskals algorithm
Create a forest F (a set of trees), where each vertex in the
graph is a separate tree
Create a set S containing all the edges in the graph, and
arrange them in ascending order.
While S is nonempty
Remove an edge with minimum weight from S
If that edge connects two different trees, then add it to
the forest, combining two trees into a single tree
otherwise discard that edge
Stop when the graph is connected.

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 0
1

3
5
4

{1} {2} {3} {4} {5} {6} {7}


1

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example
Step 1

{1, 2} {3} {4} {5} {6} {7}

5
4

{1, 2}

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 2

{1, 2, 3} {4} {5} {6} {7}

5
4

{2, 3}

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 3

{4, 5}

{1, 2, 3} {4, 5} {6} {7}

5
4

3
6

8
4

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 4

{6, 7}

{1, 2, 3} {4, 5} {6, 7}

5
4

3
6

6
3

8
4

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 5

{1, 4}

{1, 2, 3, 4, 5} {6, 7}

5
4

3
6

6
3

8
4

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 5

{2, 5}

{1, 2, 3, 4, 5} {6, 7} - rejected

8
4

5
4

4
3

6
3

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm: An Example

Step 5

{4, 7}

{1, 2, 3, 4, 5, 6, 7} - done

8
4

5
4

6
3

S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5

2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }

Kruskals Algorithm
Kruskals Algorithm
type node = record
node_number: integer
type edge = record
start: ^node
end: ^node
length: integer

Kruskals Algorithm

Kruskals Algorithm
Function Kruskal(N[1..n]: ^node, E[1..e]: ^edge)
sort E by increasing length
S = {}
for i =1 to n do
set[i] = {N[i]}
i=0
repeat
i = i+1
u = E[i]^.start
v = E[i]^.end
uset = find u in set[]
vset = find v in set[]
if uset vset then
merge(uset, vset)
add E[i] to S
until S contains n 1 edges
return S

Prims Algorithm
Prims algorithms for finding a minimum
spanning tree:

Prims algorithm starts at a given node and


at each stage selects into S the shortest
edge that extends the graph to a new
node.

Prims Algorithm
Prims algorithm
Let O be a set of nodes and S a set of edges
Initially O contains the first node of N and S is
empty
At each step look for the shortest edge {u, v}
in E such that u O and v O
Add {u, v} to S
Add v to O
Repeat until O = N
Note that, at each step, S is a minimum
spanning tree on the nodes in O

Prims Algorithm
Prims Algorithm: An Example
Step 0
1

{1}
2

5
4

6
3

Prims Algorithm
Prims Algorithm: An Example
Step 1

{1, 2}

3
5
4

{1, 2}

6
3

Prims Algorithm
Prims Algorithm: An Example
Step 2

{2, 3}

{1, 2, 3}

5
4

6
3

Prims Algorithm
Prims Algorithm: An Example
Step 3

{1, 4}

3
5
4

{1, 2, 3, 4}

Prims Algorithm
Prims Algorithm: An Example
Step 4

{4, 5}

{1, 2, 3, 4, 5}

5
4

3
6

8
4

Prims Algorithm
Prims Algorithm: An Example
Step 5

{4, 7}

{1, 2, 3, 4, 5, 7}

5
7

3
6

8
4

5
4

Prims Algorithm
Prims Algorithm: An Example
Step 5

{7, 6}

{1, 2, 3, 4, 5, 6, 7} done

5
4

8
4

6
3

Prims Algorithm
Prims Algorithm

Function Prim(L[1..n, 1..n])


// L[i, j] is the length of the edge from node i to node j L[I, j] = if no edge
S = {}
for i = 2 to n do
nearest[i] = 1
mindist[i] = L[i, 1]
repeat n 1 times
min =
for j = 2 to n do
if 0 < mindist[j] < min then
min = mindist[j]
k=j
add {nearest[k], k} to S
mindist[k] = 1
for j = 2 to n do
if L[j, k] < mindist[j] then
mindist[j] = L[j, k]
nearest[j] = k
return S

Prims Algorithm
k

Prims Algorithm: at start


L=

nearest =

mindist =

S = {}

Prims Algorithm
Prims Algorithm: after iteration 1
L=

nearest =

mindist =

-1 2

S = {{1, 2}}

Prims Algorithm
Prims Algorithm: after iteration 2
L=

nearest =

mindist =

-1

-1

S = {{1, 2}, {2, 3}}

Prims Algorithm
Prims Algorithm: after iteration 3
L=

nearest =

mindist =

-1

-1

-1

S = {{1, 2}, {2, 3}, {1, 4}}

Prims Algorithm
Prims Algorithm: after iteration 4
L=

nearest =

mindist =
1

-1 2

-1 3

-1 4

-1 5

S = {{1, 2}, {2, 3}, {1, 4}, {4, 5}}

Prims Algorithm
Prims Algorithm: after iteration 5
L=

nearest =

mindist =

-1

-1

-1

-1

-1

S = {{1, 2}, {2, 3}, {1, 4}, {4, 5}, {4, 7}}

Prims Algorithm
Prims Algorithm: after iteration 6
L=

nearest =

mindist =

-1

-1

-1

-1

-1

-1

S = {{1, 2}, {2, 3}, {1, 4}, {4, 5}, {4, 7}, {7, 6}}

Minimum Spanning Tree


Find the MST using:

Kruskals algorithm

1
0

Prims algorithm

3
3

1
3

1
2
3

2
7

3
5

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0, 2}, {1}, {3}, {4}, {5}, {6}, {7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0, 2} {1, 3}, {4}, {5}, {6}, {7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0, 2} {1, 3, 7}, {4}, {5}, {6}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0, 2} {1, 3, 4, 7}, {5}, {6}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 7}, {5}, {6}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 7}, {5}, {6}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 7}, {5}, {6}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 6, 7}, {5}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3), Rejected
3: (6 7),
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3), Rejected
3: (6 7), Rejected
3: (0 5),
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3), Rejected
3: (6 7), Rejected
3: (0 5), Rejected
3: (5 7) }

Minimum Spanning Tree


Kruskals Algorithm
1

F = {0,1, 2, 3, 4, 5, 6, 7}

1
0

3
3

1
2
3

2
7

S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7)

Rejected
Rejected

Rejected
Rejected
Rejected
Rejected
Rejected
Rejected
Rejected }

Minimum Spanning Tree


Prims Algorithm
1

O = {0}

1
0

S={ }

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0}

1
0

S = { 0, 1}, {0, 2}, {0, 5}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0}

1
0

S = { 0, 1}, {0, 2}, {0, 5}

3
3

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,2}

1
0

S = { 0, 1}, {0, 5}, {2, 1}, {2, 5}, {2, 3},


{2, 7}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,1, 2}

1
0

S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {1, 3}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,1, 2, 3}

1
0

S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {3, 7}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,1, 2, 3, 7}

1
0

S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {7, 4}, {7, 5}, {7,6}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,1, 2, 3, 4, 7}

1
0

S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {7, 5}, {7,6}, {4, 6}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,1, 2, 3, 4, 6, 7}

1
0

S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {7, 5}, {7,6}, {6, 5}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
1

O = {0,1, 2, 3, 4, 5, 6, 7}

1
0

S = {0, 5}, {2, 1}, {2, 3}, {2, 7}, {1, 4}, {3,
4}, {7, 5}, {7,6}, {6, 5}

1
2
3

2
7

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
0
0
0
0
0

M
-1
2
1

0
1

2
3
4
5
6
7

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
2
0
2
0
2

M
-1
2
-1
3

0
1

2
3
4
5
6
7

(0, 2)

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
1
1
2
0
2

M
-1
-1
-1
1
3
2

0
1

2
3
4
5
6
7

(0, 1)

(0, 2)

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
1
3
2
0
3

M
-1
-1
-1
-1
2
2

0
1

2
3
4
5
6
7

(0, 1)

(0, 2)
(1, 3)

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
1
7
2
7
3

M
-1
-1
-1
-1
1
2
3
-1

0
1

2
3
4
5
6
7

(0, 1)

(0, 2)
(1, 3)

(3, 7)

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
1
7
2
4
3

M
-1
-1
-1
-1
-1
2
2
-1

0
1

2
3
4
5
6
7

(0, 1)

(0, 2)
(1, 3)
(7, 4)

(3, 7)

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
1
7
2
4
3

M
-1
-1
-1
-1
-1
-1
2
-1

0
1

2
3
4
5
6
7

(0, 1)

(0, 2)
(1, 3)
(7, 4)
(2, 5)
(3, 7)

Minimum Spanning Tree


Prims Algorithm
0 1 2 3 4 5 6 7
0 2 1 3
1 2 3 1 3
2 1 3 3 2 2
3 1 3 2 1
4 3 2 2 1
5 3 2 2 3
6 2 2 3

7 2 1 1 3 3

N
0
0
0
1
7
2
4
3

M
-1
-1
-1
-1
-1
-1
-1
-1

0
1

(0, 1)

2
3
4
5
6
7

(0, 2)
(1, 3)
(7, 4)
(2, 5)
(4, 6)
(3, 7)

You might also like