0% found this document useful (0 votes)
133 views41 pages

Minimum Spanning Trees: Text Prim's Algorithm

The document discusses two algorithms for finding minimum spanning trees: Prim's algorithm and Kruskal's algorithm. Prim's algorithm is similar to Dijkstra's algorithm for finding the shortest paths. It works by gradually building up the minimum spanning tree, one additional edge at a time. Kruskal's algorithm focuses on edges rather than nodes, sorting the edges by weight and then selecting edges without creating cycles. Both algorithms run through examples on a sample graph to demonstrate how they work step-by-step to find the minimum spanning tree.

Uploaded by

sdawdwsq
Copyright
© © All Rights Reserved
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)
133 views41 pages

Minimum Spanning Trees: Text Prim's Algorithm

The document discusses two algorithms for finding minimum spanning trees: Prim's algorithm and Kruskal's algorithm. Prim's algorithm is similar to Dijkstra's algorithm for finding the shortest paths. It works by gradually building up the minimum spanning tree, one additional edge at a time. Kruskal's algorithm focuses on edges rather than nodes, sorting the edges by weight and then selecting edges without creating cycles. Both algorithms run through examples on a sample graph to demonstrate how they work step-by-step to find the minimum spanning tree.

Uploaded by

sdawdwsq
Copyright
© © All Rights Reserved
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/ 41

Minimum Spanning Trees

Text

Read Weiss, 9.5

Prims Algorithm

Weiss 9.5.1
Similar to Dijkstras Algorithm

Kruskals Algorithm

Weiss 9.5.2
Focuses on edges, rather than nodes

Definition
A Minimum Spanning Tree (MST) is a
subgraph of an undirected graph such that
the subgraph spans (includes) all nodes, is
connected, is acyclic, and has minimum
total edge weight

Algorithm Characteristics
Both Prims and Kruskals Algorithms work
with undirected graphs
Both work with weighted and unweighted
graphs but are more interesting when edges
are weighted
Both are greedy algorithms that produce
optimal solutions

Prims Algorithm
Similar to Dijkstras Algorithm except that
dv records edge weights, not path lengths

Walk-Through
2

10

18

C
3

Initialize array

10
25

2
3

dv

pv

10

H
3

D
25

dv

pv

10
2

K
A

18

C
3

Start with any node, say D

C
D
E
F
G
H

Update distances of
adjacent, unselected nodes

10

H
3

D
25

pv

25

18

10
2

dv

18

K
3

C
C
D

Select node with


minimum distance

10

H
3

D
25

pv

25

18

10
2

dv

18

K
3

C
C
D

G
H

Update distances of
adjacent, unselected nodes

10

H
3

D
25

pv

18

10
2

dv

18

K
3

C
C
D

G
H

Select node with


minimum distance

10

10
25

2
3

dv

pv

18

18

K
A

G
H

Update distances of
adjacent, unselected nodes

10

D
25

2
3

10

dv

pv

18

K
3

C
C

G
H

Select node with


minimum distance

10

D
25

2
3

10

dv

pv

18

K
3

C
C

E
F

Update distances of
adjacent, unselected nodes

10

18

10
25

2
3

K
3

dv

pv

10

E
F

Select node with


minimum distance

10

18

10
25

2
3

K
3

dv

pv

10

Update distances of
adjacent, unselected nodes

10

18

10
25

2
3

K
3

dv

pv

10

Table entries unchanged

Select node with


minimum distance

10

18

10
25

2
3

K
3

dv

pv

10

Update distances of
adjacent, unselected nodes

10

18

10
25

2
3

K
3

dv

pv

Select node with


minimum distance

10

10
25

2
3

dv

pv

18

C
C

Update distances of
adjacent, unselected nodes

10

10
25

2
3

dv

pv

18

C
C

Table entries unchanged

Select node with


minimum distance

10

18

C
D

10
25

2
3

dv

pv

Cost of Minimum
Spanning Tree = dv = 21

2
3

F
A

C
3

2
3

dv

pv

Done

Kruskals Algorithm
Work with edges, rather than nodes
Two steps:
Sort edges by increasing edge weight
Select the first |V| 1 edges that do not
generate a cycle

Walk-Through
F

10

3
6

Consider an undirected, weight graph

4
1

2
3

10

3
6

Sort the edges by increasing edge weight

4
1

2
3

edge

dv

edge

dv

(D,E)

(B,E)

(D,G)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

(E,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Accepting edge (E,G) would create a cycle

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle

10

C
D

4
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Select first |V|1 edges which do not


generate a cycle
3

F
A

C
3

D
1

2
3

edge

dv

(B,E)

(B,F)

(E,G)

(B,H)

(C,D)

(A,H)

(G,H)

(D,F)

(C,F)

(A,B)

(B,C)

(A,F)

10

edge

dv

(D,E)

(D,G)

Done
Total Cost =

dv = 21

not
considered

Detecting Cycles
Use Disjoint Sets (Chapter 8)

You might also like