Python/graphs/minimum_spanning_tree_prims.py

137 lines
4.8 KiB
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
import sys
from collections import defaultdict
2019-10-05 05:14:13 +00:00
class Heap:
def __init__(self):
self.node_position = []
2018-10-19 12:48:28 +00:00
def get_position(self, vertex):
return self.node_position[vertex]
2019-10-05 05:14:13 +00:00
def set_position(self, vertex, pos):
self.node_position[vertex] = pos
2018-10-19 12:48:28 +00:00
def top_to_bottom(self, heap, start, size, positions):
2018-10-19 12:48:28 +00:00
if start > size // 2 - 1:
return
else:
if 2 * start + 2 >= size:
smallest_child = 2 * start + 1
2018-10-19 12:48:28 +00:00
else:
if heap[2 * start + 1] < heap[2 * start + 2]:
smallest_child = 2 * start + 1
2018-10-19 12:48:28 +00:00
else:
smallest_child = 2 * start + 2
if heap[smallest_child] < heap[start]:
temp, temp1 = heap[smallest_child], positions[smallest_child]
heap[smallest_child], positions[smallest_child] = (
heap[start],
positions[start],
)
2018-10-19 12:48:28 +00:00
heap[start], positions[start] = temp, temp1
temp = self.get_position(positions[smallest_child])
self.set_position(
positions[smallest_child], self.get_position(positions[start])
)
self.set_position(positions[start], temp)
2018-10-19 12:48:28 +00:00
self.top_to_bottom(heap, smallest_child, size, positions)
2018-10-19 12:48:28 +00:00
# Update function if value of any node in min-heap decreases
def bottom_to_top(self, val, index, heap, position):
2018-10-19 12:48:28 +00:00
temp = position[index]
2019-10-05 05:14:13 +00:00
while index != 0:
parent = int((index - 2) / 2) if index % 2 == 0 else int((index - 1) / 2)
2018-10-19 12:48:28 +00:00
if val < heap[parent]:
heap[index] = heap[parent]
position[index] = position[parent]
self.set_position(position[parent], index)
2018-10-19 12:48:28 +00:00
else:
heap[index] = val
position[index] = temp
self.set_position(temp, index)
2018-10-19 12:48:28 +00:00
break
index = parent
else:
heap[0] = val
position[0] = temp
self.set_position(temp, 0)
2018-10-19 12:48:28 +00:00
def heapify(self, heap, positions):
2018-10-19 12:48:28 +00:00
start = len(heap) // 2 - 1
for i in range(start, -1, -1):
self.top_to_bottom(heap, i, len(heap), positions)
2018-10-19 12:48:28 +00:00
def delete_minimum(self, heap, positions):
2018-10-19 12:48:28 +00:00
temp = positions[0]
heap[0] = sys.maxsize
self.top_to_bottom(heap, 0, len(heap), positions)
2018-10-19 12:48:28 +00:00
return temp
def prisms_algorithm(adjacency_list):
"""
>>> adjacency_list = {0: [[1, 1], [3, 3]],
... 1: [[0, 1], [2, 6], [3, 5], [4, 1]],
... 2: [[1, 6], [4, 5], [5, 2]],
... 3: [[0, 3], [1, 5], [4, 1]],
... 4: [[1, 1], [2, 5], [3, 1], [5, 4]],
... 5: [[2, 2], [4, 4]]}
>>> prisms_algorithm(adjacency_list)
[(0, 1), (1, 4), (4, 3), (4, 5), (5, 2)]
"""
heap = Heap()
visited = [0] * len(adjacency_list)
nbr_tv = [-1] * len(adjacency_list) # Neighboring Tree Vertex of selected vertex
# Minimum Distance of explored vertex with neighboring vertex of partial tree
# formed in graph
distance_tv = [] # Heap of Distance of vertices from their neighboring vertex
positions = []
2018-10-19 12:48:28 +00:00
for vertex in range(len(adjacency_list)):
distance_tv.append(sys.maxsize)
positions.append(vertex)
heap.node_position.append(vertex)
2018-10-19 12:48:28 +00:00
tree_edges = []
2018-10-19 12:48:28 +00:00
visited[0] = 1
distance_tv[0] = sys.maxsize
for neighbor, distance in adjacency_list[0]:
nbr_tv[neighbor] = 0
distance_tv[neighbor] = distance
heap.heapify(distance_tv, positions)
2018-10-19 12:48:28 +00:00
for _ in range(1, len(adjacency_list)):
vertex = heap.delete_minimum(distance_tv, positions)
2018-10-19 12:48:28 +00:00
if visited[vertex] == 0:
tree_edges.append((nbr_tv[vertex], vertex))
2018-10-19 12:48:28 +00:00
visited[vertex] = 1
for neighbor, distance in adjacency_list[vertex]:
if (
visited[neighbor] == 0
and distance < distance_tv[heap.get_position(neighbor)]
):
distance_tv[heap.get_position(neighbor)] = distance
heap.bottom_to_top(
distance, heap.get_position(neighbor), distance_tv, positions
)
nbr_tv[neighbor] = vertex
return tree_edges
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__": # pragma: no cover
# < --------- Prims Algorithm --------- >
edges_number = int(input("Enter number of edges: ").strip())
adjacency_list = defaultdict(list)
for _ in range(edges_number):
edge = [int(x) for x in input().strip().split()]
adjacency_list[edge[0]].append([edge[1], edge[2]])
adjacency_list[edge[1]].append([edge[0], edge[2]])
print(prisms_algorithm(adjacency_list))