2018-10-19 12:48:28 +00:00
|
|
|
import sys
|
|
|
|
from collections import defaultdict
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def prisms_algorithm(l): # noqa: E741
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
node_position = []
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
def get_position(vertex):
|
2022-10-12 22:54:20 +00:00
|
|
|
return node_position[vertex]
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
def set_position(vertex, pos):
|
2022-10-12 22:54:20 +00:00
|
|
|
node_position[vertex] = pos
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
def top_to_bottom(heap, start, size, positions):
|
2018-10-19 12:48:28 +00:00
|
|
|
if start > size // 2 - 1:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if 2 * start + 2 >= size:
|
|
|
|
m = 2 * start + 1
|
|
|
|
else:
|
|
|
|
if heap[2 * start + 1] < heap[2 * start + 2]:
|
|
|
|
m = 2 * start + 1
|
|
|
|
else:
|
|
|
|
m = 2 * start + 2
|
|
|
|
if heap[m] < heap[start]:
|
|
|
|
temp, temp1 = heap[m], positions[m]
|
|
|
|
heap[m], positions[m] = heap[start], positions[start]
|
|
|
|
heap[start], positions[start] = temp, temp1
|
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
temp = get_position(positions[m])
|
|
|
|
set_position(positions[m], get_position(positions[start]))
|
|
|
|
set_position(positions[start], temp)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
top_to_bottom(heap, m, size, positions)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
# Update function if value of any node in min-heap decreases
|
2020-03-04 12:40:28 +00:00
|
|
|
def bottom_to_top(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:
|
2018-10-19 12:48:28 +00:00
|
|
|
if index % 2 == 0:
|
2019-10-05 05:14:13 +00:00
|
|
|
parent = int((index - 2) / 2)
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
parent = int((index - 1) / 2)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
if val < heap[parent]:
|
|
|
|
heap[index] = heap[parent]
|
|
|
|
position[index] = position[parent]
|
2020-03-04 12:40:28 +00:00
|
|
|
set_position(position[parent], index)
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
|
|
|
heap[index] = val
|
|
|
|
position[index] = temp
|
2020-03-04 12:40:28 +00:00
|
|
|
set_position(temp, index)
|
2018-10-19 12:48:28 +00:00
|
|
|
break
|
|
|
|
index = parent
|
|
|
|
else:
|
|
|
|
heap[0] = val
|
|
|
|
position[0] = temp
|
2020-03-04 12:40:28 +00:00
|
|
|
set_position(temp, 0)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
def heapify(heap, positions):
|
|
|
|
start = len(heap) // 2 - 1
|
|
|
|
for i in range(start, -1, -1):
|
2020-03-04 12:40:28 +00:00
|
|
|
top_to_bottom(heap, i, len(heap), positions)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def delete_minimum(heap, positions):
|
2018-10-19 12:48:28 +00:00
|
|
|
temp = positions[0]
|
|
|
|
heap[0] = sys.maxsize
|
2020-03-04 12:40:28 +00:00
|
|
|
top_to_bottom(heap, 0, len(heap), positions)
|
2018-10-19 12:48:28 +00:00
|
|
|
return temp
|
|
|
|
|
|
|
|
visited = [0 for i in range(len(l))]
|
2022-10-12 22:54:20 +00:00
|
|
|
nbr_tv = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex
|
2020-06-16 08:09:19 +00:00
|
|
|
# Minimum Distance of explored vertex with neighboring vertex of partial tree
|
|
|
|
# formed in graph
|
2022-10-12 22:54:20 +00:00
|
|
|
distance_tv = [] # Heap of Distance of vertices from their neighboring vertex
|
|
|
|
positions = []
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
for x in range(len(l)):
|
|
|
|
p = sys.maxsize
|
2022-10-12 22:54:20 +00:00
|
|
|
distance_tv.append(p)
|
|
|
|
positions.append(x)
|
|
|
|
node_position.append(x)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
tree_edges = []
|
2018-10-19 12:48:28 +00:00
|
|
|
visited[0] = 1
|
2022-10-12 22:54:20 +00:00
|
|
|
distance_tv[0] = sys.maxsize
|
2018-10-19 12:48:28 +00:00
|
|
|
for x in l[0]:
|
2022-10-12 22:54:20 +00:00
|
|
|
nbr_tv[x[0]] = 0
|
|
|
|
distance_tv[x[0]] = x[1]
|
|
|
|
heapify(distance_tv, positions)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(1, len(l)):
|
2022-10-12 22:54:20 +00:00
|
|
|
vertex = delete_minimum(distance_tv, positions)
|
2018-10-19 12:48:28 +00:00
|
|
|
if visited[vertex] == 0:
|
2022-10-12 22:54:20 +00:00
|
|
|
tree_edges.append((nbr_tv[vertex], vertex))
|
2018-10-19 12:48:28 +00:00
|
|
|
visited[vertex] = 1
|
|
|
|
for v in l[vertex]:
|
2022-10-12 22:54:20 +00:00
|
|
|
if visited[v[0]] == 0 and v[1] < distance_tv[get_position(v[0])]:
|
|
|
|
distance_tv[get_position(v[0])] = v[1]
|
|
|
|
bottom_to_top(v[1], get_position(v[0]), distance_tv, positions)
|
|
|
|
nbr_tv[v[0]] = vertex
|
|
|
|
return tree_edges
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-10-15 19:10:35 +00:00
|
|
|
if __name__ == "__main__": # pragma: no cover
|
2019-07-17 04:07:25 +00:00
|
|
|
# < --------- Prims Algorithm --------- >
|
|
|
|
n = int(input("Enter number of vertices: ").strip())
|
|
|
|
e = int(input("Enter number of edges: ").strip())
|
|
|
|
adjlist = defaultdict(list)
|
|
|
|
for x in range(e):
|
2020-05-22 06:10:11 +00:00
|
|
|
l = [int(x) for x in input().strip().split()] # noqa: E741
|
2019-10-05 05:14:13 +00:00
|
|
|
adjlist[l[0]].append([l[1], l[2]])
|
|
|
|
adjlist[l[1]].append([l[0], l[2]])
|
2022-10-12 22:54:20 +00:00
|
|
|
print(prisms_algorithm(adjlist))
|