CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds...
Transcript of CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds...
![Page 1: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/1.jpg)
CMSC 132: Object-Oriented Programming II
Minimum Spanning Trees
1
![Page 2: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/2.jpg)
Minimum spanning tree • Given: Undirected graph G with positive edge weights (connected). • Definition: A spanning tree of G is a subgraph T that is connected
and acyclic. • Goal: Find a min weight spanning tree.
2
Graph G
![Page 3: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/3.jpg)
Minimum spanning tree • Given: Undirected graph G with positive edge weights (connected). • Definition: A spanning tree of G is a subgraph T that is connected
and acyclic. • Goal: Find a min weight spanning tree.
3
Spanning Tree T: cost = 4+6+8+5+11+9+7 = 50
![Page 4: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/4.jpg)
MST of random graph
4https://who.rocq.inria.fr/Nicolas.Broutin/gallery.html
![Page 5: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/5.jpg)
Simplifying assumptions • Simplifying assumptions.
• Edge weights are distinct. • Graph is connected.
• Consequence. MST exists and is unique.
5
![Page 6: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/6.jpg)
MST Algorithms• Greedy Algorithms: Prim’s and Kruskal’s.
• Both Prim’s and Kruskal’s 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
6
![Page 7: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/7.jpg)
Kruskal's algorithm
• Minimum-spanning-tree algorithm• Consider edges in ascending order of weight.• Add next edge to tree T unless doing so would create
a cycle. • If the graph is not connected, then it finds a minimum
spanning forest
7
![Page 8: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/8.jpg)
Kruskal's algorithm Demo
8
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Consider an undirected, weight graph
![Page 9: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/9.jpg)
Kruskal's algorithm Demo
9
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (E,1,D)
![Page 10: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/10.jpg)
Kruskal's algorithm Demo
10
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (H,3,G)
![Page 11: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/11.jpg)
Kruskal's algorithm Demo
11
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (E,4,B)
![Page 12: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/12.jpg)
Kruskal's algorithm Demo
12
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (F,4,B)
![Page 13: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/13.jpg)
Kruskal's algorithm Demo
13
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (G,4,E)
![Page 14: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/14.jpg)
Kruskal's algorithm Demo
14
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (F,5,C)
![Page 15: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/15.jpg)
Kruskal's algorithm Demo
15
8
1
A
HB
F
E
D
C
G4
5
4
6
1315
5
48
14
3
10
Add Edge (B,8,A)
![Page 16: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/16.jpg)
Kruskal's algorithm Demo
16
1
A
HB
F
E
D
C
G4
4
5
48
3
Cost: 3+4+1+4+8+4+5 = 29
![Page 17: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/17.jpg)
Kruskal’s Algorithm Demo
17
![Page 18: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/18.jpg)
Kruskal’s Algorithm Demo
18
![Page 19: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/19.jpg)
19
Prim's algorithm• Builds the tree one vertex at a time• Starts from an arbitrary starting vertex• Each step adds the cheapest possible connection from
the tree to another vertex.
![Page 20: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/20.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G3
2
4
6
315
9
48
14
3
10
![Page 21: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/21.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (E,1,D)
![Page 22: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/22.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (D,2,G)
![Page 23: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/23.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (D,3,C)
![Page 24: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/24.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (G,3,H)
![Page 25: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/25.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (E,4,B)
![Page 26: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/26.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (F,4,B)
![Page 27: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/27.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
2
4
6
315
9
48
14
3
10
Add Edge (H,5,A)
![Page 28: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/28.jpg)
Prim's algorithm Demo
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G2
4
34
3
Cost: 5+3+2+3+1+4+4 = 20
![Page 29: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/29.jpg)
Prim’s Algorithm Demo
29
![Page 30: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4](https://reader035.fdocuments.in/reader035/viewer/2022070914/5fb52c169a53d508bf578423/html5/thumbnails/30.jpg)
Prim’s Algorithm Demo
30