Skip to content

Commit ab67f0d

Browse files
committed
merge
1 parent 1f2eb1c commit ab67f0d

File tree

3 files changed

+348
-1
lines changed

3 files changed

+348
-1
lines changed
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
text_representation:
5+
extension: .md
6+
format_name: markdown
7+
format_version: '1.3'
8+
jupytext_version: 1.13.8
9+
kernelspec:
10+
display_name: Python 3
11+
language: python
12+
name: python3
13+
---
14+
15+
# Minimum Spanning Tree using Prim's Algorithm
16+
17+
18+
We all have travelled by road from one city to another. But, ever wondered how they decided where to create the route and what path to choose? If one will get the job to connect 5 cities via road, then, the naive approach will be to start connecting from one city and continue doing that until one covers all destinations. But, that's not the optimal solution, not only one should cover all nodes but also at the lowest cost possible (minimum length of the road) and for that <b>Minimum Spanning Trees</b> are constructed using Prim's and Kruskal's algorithm.
19+
20+
When each and every node of a graph is connected to each other without forming any cycle, it is known as the <b>Spanning Tree.</b> A graph $G$ having $n$ nodes will have spanning trees with $n$ nodes and $n-1$ edges. Thus, as its name indicates, Minimum Spanning Tree is the tree with the shortest possible distance covered among all other spanning trees.<br> Let's look at the following example to understand this better.
21+
22+
23+
## Minimum Spanning Tree Problem
24+
25+
26+
Suppose there are 5 cities (A, B, C, D and E) that needs to be connected via road. Now, there can be more than one path connecting one city to another but our goal is to find the one having the shortest distance.
27+
28+
<b>ASSUMPTION</b>: Distance taken is imaginary.
29+
30+
The following graph depicts our situation in this case.
31+
32+
```python
33+
# importing libraries
34+
import networkx as nx
35+
import matplotlib.pyplot as plt
36+
37+
roads = nx.Graph()
38+
39+
# adding weighted edges
40+
roads.add_weighted_edges_from(
41+
[
42+
("A", "B", 1),
43+
("A", "C", 7),
44+
("B", "C", 1),
45+
("B", "D", 4),
46+
("B", "E", 3),
47+
("C", "E", 6),
48+
("D", "E", 2),
49+
]
50+
)
51+
52+
# layout of the graph
53+
position = {"A": (0.5, 2), "B": (0, 1), "C": (1, 1), "D": (0, 0), "E": (1, 0)}
54+
pos = nx.spring_layout(
55+
roads, pos=position, weight="weight", fixed=["A", "B", "C", "D", "E"]
56+
)
57+
fig = plt.figure(figsize=(5, 5))
58+
59+
# drawing customised nodes
60+
nx.draw(
61+
roads,
62+
pos,
63+
with_labels=True,
64+
node_size=900,
65+
node_color="#DF340B",
66+
font_color="white",
67+
font_weight="bold",
68+
font_size=14,
69+
node_shape="s",
70+
)
71+
72+
# adding edge labels
73+
nx.draw_networkx_edge_labels(
74+
roads,
75+
pos,
76+
edge_labels=nx.get_edge_attributes(roads, "weight"),
77+
font_size=12,
78+
);
79+
80+
```
81+
82+
Now, in order to find the minimum spanning tree, this notepad will cover the Prim's algorithm.
83+
Let's understand it in detail.
84+
85+
86+
## Prim's Algorithm
87+
Prim's algorithm uses greedy approach to find the minimum spanning tree.That means, in each iteration it finds an edge which has the minimum weight and add it to the growing spanning tree.
88+
89+
The time complexity of the Prim’s Algorithm is $O((V+E) \text{ log} V)$ because each vertex is inserted in the priority queue only once and insertion in priority queue take logarithmic time.
90+
91+
<b>Algorithm Steps</b>
92+
1. Select any arbitrary node as the root node and add it to the tree. Spanning tree will always cover all nodes so any node can be a root node.
93+
2. Select the node having the minimum edge weight among the outgoing edges of the nodes present in the tree. Ensure the node is not already present in the spanning tree.
94+
3. Add the selected node and edge to the tree.
95+
4. Repeat steps 2 and 3 until all nodes are covered.
96+
5. The final graph will represent the Minimum Spanning Tree
97+
98+
99+
### Example solution
100+
Let's get back to the example and find its minimum spanning tree using Prim's algorithm. Before moving forward, here are a few notations that one should remember:
101+
- Red nodes represent unvisited vertices while green nodes represent visited vertices.
102+
- Edges of minimum spanning tree are represented in purple color.
103+
104+
```python
105+
# converting graph to dictionary
106+
road_list = roads._adj
107+
108+
# infinity is assigned as the maximum edge weight + 1
109+
inf = 1 + max([w['weight'] for u in road_list.keys() for (v,w) in road_list[u].items()])
110+
111+
# initialising dictionaries
112+
(visited, distance, TreeEdges) = ({}, {}, [])
113+
```
114+
115+
### Step 1
116+
Suppose the road construction starts from city A, so A is the source node. The distance of other cities is assumed to be unknown, so all other visited vertices are marked as 'not visited' and the distance as infinite (which equals 8 in this case).
117+
118+
```python
119+
# assigning infinite distance to all nodes and marking all nodes as not visited
120+
for v in road_list.keys():
121+
(visited[v], distance[v]) = (False, inf) # false indicates not visited
122+
visited['A'] = True
123+
distance['A']=0
124+
125+
# plotting graph
126+
# Nudge function is created to show node labels outside the node
127+
def nudge(pos, x_shift, y_shift):
128+
return {n: (x + x_shift, y + y_shift) for n, (x, y) in pos.items()}
129+
130+
pos_nodes = nudge(pos, 0.025, 0.16) # shift the layout
131+
fig= plt.figure(figsize=(5, 5))
132+
133+
# assigning green color to visited nodes and red to unvisited.
134+
node_colors = ["#4EAD27" if visited[n] == True else "#DF340B" for n in visited]
135+
labels = {v:distance[v] for v in distance}
136+
137+
# plotting the base graph
138+
nx.draw(
139+
roads,
140+
pos,
141+
with_labels=True,
142+
node_size=900,
143+
node_color=node_colors,
144+
font_color="white",
145+
font_weight="bold",
146+
font_size=14,
147+
node_shape="s",
148+
)
149+
# adding node labels
150+
nx.draw_networkx_labels(
151+
roads,
152+
pos= pos_nodes,
153+
labels= labels,
154+
font_size= 14,
155+
font_color='blue'
156+
)
157+
# adding edge labels
158+
nx.draw_networkx_edge_labels(
159+
roads,
160+
pos,
161+
edge_labels=nx.get_edge_attributes(roads, "weight"),
162+
font_size=12,
163+
);
164+
```
165+
166+
### Step 2
167+
Now, the next step is to assign distances to A's neighbouring cities and the distance is equal to the edge weight. This needs to be done in order to find the minimum spanning tree. The distance will be updated as `minimum(current weight, new edge weight)`.<br>
168+
Here, the following nodes will get updated:
169+
- B : `min(1, 8) = 1`
170+
- C : `min(7, 8) = 7`
171+
172+
```python
173+
# updating weights of A's neighbour
174+
for (v, w) in road_list["A"].items():
175+
distance[v] = w["weight"]
176+
# plotting graph
177+
fig = plt.figure(figsize=(5, 5))
178+
179+
node_colors = ["#4EAD27" if visited[n] == True else "#DF340B" for n in visited]
180+
labels = {v: distance[v] for v in distance}
181+
182+
# plotting the base graph
183+
nx.draw(
184+
roads,
185+
pos,
186+
with_labels=True,
187+
node_size=900,
188+
node_color=node_colors,
189+
font_color="white",
190+
font_weight="bold",
191+
font_size=14,
192+
node_shape="s",
193+
)
194+
# adding node labels
195+
nx.draw_networkx_labels(
196+
roads, pos=pos_nodes, labels=labels, font_size=14, font_color="blue"
197+
)
198+
# adding edge labels
199+
nx.draw_networkx_edge_labels(
200+
roads,
201+
pos,
202+
edge_labels=nx.get_edge_attributes(roads, "weight"),
203+
font_size=12,
204+
);
205+
206+
```
207+
208+
### Step 3 & Step 4
209+
After updating the distance in the previous step, it's time to find the next node with the minimum distance. To do this, iterate across the neighbours of the visited nodes and find the node with the minimum distance. Then update its distance and mark it as visited.
210+
211+
```python
212+
# initialising the required dictionaries for plotting graphs
213+
visited_list = []
214+
distance_list = []
215+
edge_list = []
216+
217+
# iterating through every node's neighbour
218+
for i in road_list.keys():
219+
(mindist, nextv) = (inf, None)
220+
for u in road_list.keys():
221+
for (v, w) in road_list[u].items():
222+
d = w["weight"]
223+
224+
# updating the minimum distance
225+
if visited[u] and (not visited[v]) and d < mindist:
226+
(mindist, nextv, nexte) = (d, v, (u, v, d))
227+
if nextv is None: # all nodes have been visited
228+
break
229+
visited[nextv] = True
230+
visited_list.append(visited.copy())
231+
# adding the next minimum distance edge to the spanning tree
232+
TreeEdges.append(nexte)
233+
edge_list.append(TreeEdges.copy())
234+
235+
# updating the new minimum distance
236+
for (v, w) in road_list[nextv].items():
237+
d = w["weight"]
238+
if not visited[v]:
239+
distance[v] = min(distance[v], d)
240+
distance_list.append(distance.copy())
241+
242+
```
243+
244+
Let's understand each iteration and plot the graph!
245+
246+
<b>Figure 1</b><br>
247+
B has the minimum distance of 1 unit from Node A and hence got added in the spanning tree. The next step is to update the distance of B's neighbour, which are as follows:
248+
- A : Already visited
249+
- C : `min(7, 1)` = 1
250+
- D : `min(8, 4)` = 4
251+
- E : `min(8, 3)` = 3
252+
253+
<b>Figure 2</b><br>
254+
The next node with the minimum distance is C with a distance of 1 unit, so now C will get added to the spanning tree, and it's neighbours will get updated:
255+
- A : Already visited
256+
- B : Already visited
257+
- E : `min(3, 6)` = 3
258+
259+
<b>Figure 3</b><br>
260+
Among the last 2 nodes, E has the minimum distance of 3 units. So, E will get added to the spanning tree, and its neighbours will get updated:
261+
- B : Already visited
262+
- C : Already visited
263+
- D : `min(4, 2)` = 2
264+
265+
<b>Figure 4</b><br>
266+
The final node D, with a distance of 2 units, got connected to the minimum spanning tree. This figure illustrates the final Minimum Spanning Tree of the example.
267+
268+
```python
269+
fig, axes = plt.subplots(2, 2, figsize=(15,15))
270+
c=0
271+
for v,d,ax,edges in zip(visited_list,distance_list,axes.ravel(), edge_list):
272+
c+=1
273+
ax.set_title("Figure "+str(c), fontsize=16)
274+
node_colors = ["#4EAD27" if v[n] == True else "#DF340B" for n in v]
275+
labels = {k:d[k] for k in d}
276+
nx.draw(
277+
roads,
278+
pos,
279+
with_labels=True,
280+
node_size=900,
281+
node_color=node_colors,
282+
font_color="white",
283+
font_weight="bold",
284+
font_size=14,
285+
node_shape="s",
286+
ax= ax
287+
)
288+
nx.draw_networkx_edges(
289+
roads,
290+
pos,
291+
edgelist=edges,
292+
width=3,
293+
edge_color="#823AAF",
294+
ax=ax,
295+
)
296+
297+
nx.draw_networkx_labels(
298+
roads,
299+
pos= pos_nodes,
300+
labels= labels,
301+
font_size= 14.5,
302+
font_color='blue',
303+
ax= ax
304+
)
305+
nx.draw_networkx_edge_labels(
306+
roads,
307+
pos,
308+
edge_labels=nx.get_edge_attributes(roads, "weight"),
309+
font_size=12,
310+
ax=ax
311+
);
312+
313+
```
314+
315+
### Step 5
316+
The final output of the program is stored as a list of tuples in `TreeEdges` as shown below.
317+
318+
```python
319+
print(TreeEdges)
320+
```
321+
322+
## NetworkX Implementation
323+
324+
The above code is a basic implementation of Prim's algorithm with the time complexity of $ O (mn)$, which further can be improved to $O((V+E) \text{ log} V)$ with the help of priority queues. Here's the good part, with the help of NetworkX functions, one can implement it in $O((V+E) \text{ log} V)$ without even writing the whole code.<br>
325+
NetworkX provides various [Tree](https://networkx.org/documentation/stable/reference/algorithms/tree.html#) functions to perform difficult operations, and [minimum_spanning_tree()](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.mst.minimum_spanning_tree.html#networkx.algorithms.tree.mst.minimum_spanning_tree) is one of them. Not only this, you can also find the maximum spanning tree with the help of the [maximum_spanning_tree()](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.mst.maximum_spanning_tree.html#networkx.algorithms.tree.mst.maximum_spanning_tree) function.<br>
326+
The following code uses NetworkX function and gives us the same output as our code.
327+
328+
```python
329+
MST= nx.minimum_spanning_tree(roads, algorithm= 'prim')
330+
print(sorted(MST.edges(data=True)))
331+
```
332+
333+
## Applications of Prim's Algorithm
334+
- It is used to solve travelling salesman problem.
335+
- As said earlier, used in network for roads and rail tracks connecting all the cities.
336+
- Path finding algorithms used in artificial intelligence.
337+
- Cluster analysis
338+
- Game development
339+
- Maze generation
340+
341+
There are many other similar applications present in this world. Whenever there's a need to find a cost-effective method to connect nodes (it can be anything), there's a high chance of Prim's algorithm playing its role in the solution.
342+
343+
344+
## Reference
345+
R. C. Prim "Shortest connection networks and some generalizations." The bell system technical journal, Volume: 36, Issue: 6, (Nov. 1957): 1389-1401<br>
346+
https://ia800904.us.archive.org/18/items/bstj36-6-1389/bstj36-6-1389.pdf

content/algorithms/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ maxdepth: 1
1010
assortativity/correlation
1111
dag/index
1212
flow/dinitz_alg
13+
MST Prim/MST Prim
1314
```

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# nx-guides is tested on the development branch of NetworkX
2-
git+git://github.com/networkx/networkx@main
2+
git+https://github.com/networkx/networkx@main
33

44
# Scientific Python (see networkx/requirements/default.txt)
55
numpy

0 commit comments

Comments
 (0)