Skip to content

Commit 7b7919b

Browse files
committed
Minimum spanning tree - undirected graph
1 parent 061074a commit 7b7919b

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ but it also contains other data structures, algorithms and problems.
108108
- [Priority Queue implementation](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/dijkstra/priority-queue-impl-adjacency-map)
109109
- [Matrix implementation](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/dijkstra/matrix-impl)
110110
- [Minimum Spanning Tree](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/minimum-spanning-tree)
111+
- [Minimum Spanning Tree in undirected graph]()
112+
- perform breadth first search or depth first search and record previous edges
111113
- [Prim's algorithm - Minimum Spanning Tree in undirected weighted graph](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/minimum-spanning-tree/prims-algorithm)
112114
- [Is graph bipartite](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/is-graph-bipartite)
113115

Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from queue import Queue
2+
3+
4+
class Graph:
5+
def __init__(self):
6+
self.vertices: list = []
7+
self.adjacency_list: dict = {}
8+
self.prev: dict = {}
9+
self.distance: dict = {}
10+
self.colors: dict = {}
11+
12+
def add_vertex(self, label: str):
13+
self.vertices.append(label)
14+
self.adjacency_list[label]: list = []
15+
self.prev[label] = None
16+
self.distance[label] = 0
17+
self.colors[label] = "white"
18+
19+
def add_edge(self, label1: str, label2: str):
20+
self.adjacency_list[label1].append(label2)
21+
self.adjacency_list[label2].append(label1)
22+
23+
def minimum_spanning_tree(self, label: str) -> list: # this is breadth first search
24+
min_edges: list = []
25+
q: Queue = Queue()
26+
q.enqueue(label)
27+
self.colors[label] = "gray"
28+
while not q.is_empty():
29+
tmp: str = q.dequeue()
30+
for neighbour in self.adjacency_list[tmp]:
31+
if self.colors[neighbour] == "white":
32+
min_edges.append([tmp, neighbour])
33+
self.prev[neighbour] = tmp
34+
self.distance[neighbour] = self.distance[tmp] + 1
35+
self.colors[neighbour] = "gray"
36+
q.enqueue(neighbour)
37+
self.colors[tmp] = "black"
38+
return min_edges
39+
40+
def return_path(self, label: str) -> str:
41+
if self.prev[label] is None:
42+
return label
43+
else:
44+
return self.return_path(self.prev[label]) + " -> " + label
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from graph import Graph
2+
3+
graph = Graph()
4+
5+
my_vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
6+
# add vertices
7+
for i in range(len(my_vertices)):
8+
graph.add_vertex(my_vertices[i])
9+
10+
graph.add_edge('A', 'B')
11+
graph.add_edge('A', 'C')
12+
graph.add_edge('A', 'D')
13+
graph.add_edge('C', 'D')
14+
graph.add_edge('C', 'G')
15+
graph.add_edge('D', 'G')
16+
graph.add_edge('D', 'H')
17+
graph.add_edge('B', 'E')
18+
graph.add_edge('B', 'F')
19+
graph.add_edge('E', 'I')
20+
21+
edges: list = graph.minimum_spanning_tree("A")
22+
print(edges)
23+
print(graph.return_path("H"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Queue:
2+
def __init__(self):
3+
self.queue = []
4+
5+
def enqueue(self, item):
6+
self.queue.insert(0, item)
7+
8+
def dequeue(self):
9+
return self.queue.pop()
10+
11+
def size(self):
12+
return len(self.queue)
13+
14+
def is_empty(self):
15+
return self.queue == []

0 commit comments

Comments
 (0)