Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.
-
Upload
cecily-singleton -
Category
Documents
-
view
216 -
download
0
Transcript of Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.
![Page 1: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/1.jpg)
Minimum Spanning Tree in Graph
- Week 13
![Page 2: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/2.jpg)
2
Problem: Laying Telephone Wire
Central office
![Page 3: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/3.jpg)
3
Wiring: Naive Approach
Central office
Expensive!
![Page 4: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/4.jpg)
4
Wiring: Better Approach
Central office
Minimize the total length of wire connecting ALL customers
![Page 5: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/5.jpg)
5
Spanning trees Suppose you have a connected undirected graph:
Connected: every node is reachable from every other node Undirected: edges do not have an associated direction
...then a spanning tree of the graph is a connected subgraph which contains all the vertices and has no cycles.
A connected,undirected graph
Four of the spanning trees of the graph
![Page 6: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/6.jpg)
All 16 of its Spanning TreesComplete Graph
![Page 7: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/7.jpg)
7
Minimum-cost spanning trees
Suppose you have a connected undirected graph with a weight (or cost) associated with each edge.
The cost of a spanning tree would be the sum of the costs of its edges.
A minimum-cost spanning tree is a spanning tree that has the lowest cost.
A B
E D
F C
16
19
21 11
33 14
1810
6
5
A connected, undirected graph
A B
E D
F C
1611
18
6
5
A minimum-cost spanning tree
![Page 8: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/8.jpg)
8
Minimum Spanning Tree (MST)
it is a tree (i.e., it is acyclic)
it covers all the vertices V contains |V| - 1 edges
the total cost associated with tree edges is the minimum among all possible spanning trees
not necessarily unique.
A minimum spanning tree is a subgraph of an undirected weighted graph G, such that
Tree = connected graph without cycles
![Page 9: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/9.jpg)
10
How Can We Generate a MST?
a
ce
d
b2
45
9
6
4
5
5
a
ce
d
b2
45
9
6
4
5
5
![Page 10: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/10.jpg)
11
Finding minimum spanning trees
There are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms.
Kruskal’s algorithm: Start with no nodes or edges in the spanning tree, and repeatedly add the cheapest edge that does not create a cycle
Here, we consider the spanning tree to consist of edges only
Prim’s algorithm: Start with any one node in the spanning tree, and repeatedly add the cheapest edge, and the node it leads to, for which the node is not already in the spanning tree.
Here, we consider the spanning tree to consist of both nodes and edges
![Page 11: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/11.jpg)
12
Kruskal’s algorithm
The steps are:
1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, (1). Extract the cheapest edge from the priority queue, (2). If it forms a cycle, reject it. Else add it to the forest. Adding it to the forest will join two trees together.
Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.
![Page 12: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/12.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Complete Graph
![Page 13: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/13.jpg)
1
4
2
5
2
5
4
3
4
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
A AB D
B B
B
C D
J C
C
E
F
D
D H
J E G
F FG I
G GI J
H J JI
![Page 14: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/14.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them
makes the algorithm easier to visualize)
![Page 15: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/15.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 16: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/16.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 17: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/17.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 18: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/18.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 19: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/19.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 20: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/20.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Cycle
Don’t Add Edge
![Page 21: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/21.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 22: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/22.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 23: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/23.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 24: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/24.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Cycle
Don’t Add Edge
![Page 25: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/25.jpg)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
![Page 26: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/26.jpg)
4
1
2
2 1
3
32
4
A
B C
D
E F
G
HI
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Minimum Spanning Tree Complete Graph
![Page 27: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/27.jpg)
Visualization of Kruskal's algorithm
“repeatedly add the cheapest edge that does not create a cycle”
![Page 28: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/28.jpg)
Time complexity of Kruskal's Algorithm
Running Time = O(E log E) (E = # edges)
Testing if an edge creates a cycle can be slow unless a complicated data structure called a “union-find” structure is used.
This algorithm works best, of course, if the number of edges is kept to a minimum.
![Page 29: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/29.jpg)
30
Prim’s algorithm
The steps are:
1. Initialize a tree with a single node arbitrarily chose from graph. 2. Repeat until all nodes are in the tree: (1). Find the node from the graph with the smallest connecting edge to the tree, (2). Add it to the tree
Every step will have joined one node, so that at the end we will have one new graph with all the nodes and it will be a minimum spanning tree of the original graph.
![Page 30: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/30.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Complete Graph
![Page 31: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/31.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 32: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/32.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 33: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/33.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 34: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/34.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 35: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/35.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 36: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/36.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 37: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/37.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 38: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/38.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 39: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/39.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 40: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/40.jpg)
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 41: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/41.jpg)
4
1
2
2 1
3
32
4
A
B C
D
E F
G
HI
J
Complete Graph Minimum Spanning Tree
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
HI
J
![Page 42: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/42.jpg)
Unlike Kruskal’s, Prim’s algorithm doesn’t need to see all of the graph at once. It can deal with it one piece at a time.
It also doesn’t need to worry if adding an edge will create a cycle since this algorithm deals primarily with the nodes, and not the edges.
Summary of Prim's Algorithm
![Page 43: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/43.jpg)
Time complexity of Prim's Algorithm
Running Time = O(E + V log V) (E = #edges, V = #nodes)
![Page 44: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/44.jpg)
45
Time Complexity Review
Kruskal’s algorithm: O(e log e) Prim’s algorithm: O( e log v)
Kruskal’s algorithm is preferable on sparse graphs, i.e., where e is very small compared to the total number of possible edges.
Prim’s algorithm is easy to implemented, but the number of vertices needs to be kept to a minimum in addition to the number of edges.
![Page 45: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/45.jpg)
46
1. How does Google quickly find web pages that contain a search term? Answer
2. How can a subsequence of DNA be quickly found within the genome? Answer
3. How does your operating system track which memory (disk or RAM) is free? Answer
4. How to generate a Maze game? Answer, Example implementation
5. Etc…
Slide 11 on the Week 1 – Introduction of the class “Example applications of data structures and algorithms”
![Page 46: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/46.jpg)
47
Mazes Typically,
Every location in a maze is reachable from the starting location
There is only one path from start to finish
If the cells are “vertices” and the open doors between cells are “edges,” this describes a spanning tree
Since there is exactly one path between any pair of cells, any cells can be used as “start” and “finish”
This describes a spanning tree
![Page 47: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/47.jpg)
48
Mazes as spanning trees
While not every maze is a spanning tree, most can be represented as such.
The nodes are “places” within the maze.
There is exactly one cycle-free path from any node to any other node.
![Page 48: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/48.jpg)
49
Building a maze I
This algorithm requires two sets of cells
the set of cells already in the spanning tree, IN
the set of cells adjacent to the cells in the spanning tree (but not in it themselves), called the FRONTIER
Start with all walls present
Pick any cell and put it into IN (red)
• Put all adjacent cells, that aren’t in IN, into FRONTIER (blue)
![Page 49: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/49.jpg)
50
Building a maze II
Repeatedly do the following:
Remove any one cell C from FRONTIER and put it in IN
Erase the wall between C and some one adjacent cell in IN
Add to FRONTIER all the cells adjacent to C that aren’t in IN (or in FRONTIER already)
• Continue until there are no more cells in FRONTIER
• When the maze is complete (or at any time), choose the start and finish cells
![Page 50: Minimum Spanning Tree in Graph - Week 13. 2 Problem: Laying Telephone Wire Central office.](https://reader030.fdocuments.us/reader030/viewer/2022032705/56649db55503460f94aa6ca0/html5/thumbnails/50.jpg)
Generating a maze by Prim’s algorithm
Prim's algorithm has many applications, such as in the generation of this maze, which applies Prim's algorithm to a randomly weighted grid graph.
Click on to see the Demo.
51