|
| 1 | +import sys |
| 2 | +from collections import defaultdict |
| 3 | + |
| 4 | +def PrimsAlgorithm(l): |
| 5 | + |
| 6 | + nodePosition = [] |
| 7 | + def getPosition(vertex): |
| 8 | + return nodePosition[vertex] |
| 9 | + |
| 10 | + def setPosition(vertex, pos): |
| 11 | + nodePosition[vertex] = pos |
| 12 | + |
| 13 | + def topToBottom(heap, start, size, positions): |
| 14 | + if start > size // 2 - 1: |
| 15 | + return |
| 16 | + else: |
| 17 | + if 2 * start + 2 >= size: |
| 18 | + m = 2 * start + 1 |
| 19 | + else: |
| 20 | + if heap[2 * start + 1] < heap[2 * start + 2]: |
| 21 | + m = 2 * start + 1 |
| 22 | + else: |
| 23 | + m = 2 * start + 2 |
| 24 | + if heap[m] < heap[start]: |
| 25 | + temp, temp1 = heap[m], positions[m] |
| 26 | + heap[m], positions[m] = heap[start], positions[start] |
| 27 | + heap[start], positions[start] = temp, temp1 |
| 28 | + |
| 29 | + temp = getPosition(positions[m]) |
| 30 | + setPosition(positions[m], getPosition(positions[start])) |
| 31 | + setPosition(positions[start], temp) |
| 32 | + |
| 33 | + topToBottom(heap, m, size, positions) |
| 34 | + |
| 35 | + # Update function if value of any node in min-heap decreases |
| 36 | + def bottomToTop(val, index, heap, position): |
| 37 | + temp = position[index] |
| 38 | + |
| 39 | + while(index != 0): |
| 40 | + if index % 2 == 0: |
| 41 | + parent = int( (index-2) / 2 ) |
| 42 | + else: |
| 43 | + parent = int( (index-1) / 2 ) |
| 44 | + |
| 45 | + if val < heap[parent]: |
| 46 | + heap[index] = heap[parent] |
| 47 | + position[index] = position[parent] |
| 48 | + setPosition(position[parent], index) |
| 49 | + else: |
| 50 | + heap[index] = val |
| 51 | + position[index] = temp |
| 52 | + setPosition(temp, index) |
| 53 | + break |
| 54 | + index = parent |
| 55 | + else: |
| 56 | + heap[0] = val |
| 57 | + position[0] = temp |
| 58 | + setPosition(temp, 0) |
| 59 | + |
| 60 | + def heapify(heap, positions): |
| 61 | + start = len(heap) // 2 - 1 |
| 62 | + for i in range(start, -1, -1): |
| 63 | + topToBottom(heap, i, len(heap), positions) |
| 64 | + |
| 65 | + def deleteMinimum(heap, positions): |
| 66 | + temp = positions[0] |
| 67 | + heap[0] = sys.maxsize |
| 68 | + topToBottom(heap, 0, len(heap), positions) |
| 69 | + return temp |
| 70 | + |
| 71 | + visited = [0 for i in range(len(l))] |
| 72 | + Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex |
| 73 | + # Minimum Distance of explored vertex with neighboring vertex of partial tree formed in graph |
| 74 | + Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex |
| 75 | + Positions = [] |
| 76 | + |
| 77 | + for x in range(len(l)): |
| 78 | + p = sys.maxsize |
| 79 | + Distance_TV.append(p) |
| 80 | + Positions.append(x) |
| 81 | + nodePosition.append(x) |
| 82 | + |
| 83 | + TreeEdges = [] |
| 84 | + visited[0] = 1 |
| 85 | + Distance_TV[0] = sys.maxsize |
| 86 | + for x in l[0]: |
| 87 | + Nbr_TV[ x[0] ] = 0 |
| 88 | + Distance_TV[ x[0] ] = x[1] |
| 89 | + heapify(Distance_TV, Positions) |
| 90 | + |
| 91 | + for i in range(1, len(l)): |
| 92 | + vertex = deleteMinimum(Distance_TV, Positions) |
| 93 | + if visited[vertex] == 0: |
| 94 | + TreeEdges.append((Nbr_TV[vertex], vertex)) |
| 95 | + visited[vertex] = 1 |
| 96 | + for v in l[vertex]: |
| 97 | + if visited[v[0]] == 0 and v[1] < Distance_TV[ getPosition(v[0]) ]: |
| 98 | + Distance_TV[ getPosition(v[0]) ] = v[1] |
| 99 | + bottomToTop(v[1], getPosition(v[0]), Distance_TV, Positions) |
| 100 | + Nbr_TV[ v[0] ] = vertex |
| 101 | + return TreeEdges |
| 102 | + |
| 103 | +# < --------- Prims Algorithm --------- > |
| 104 | +n = int(input("Enter number of vertices: ")) |
| 105 | +e = int(input("Enter number of edges: ")) |
| 106 | +adjlist = defaultdict(list) |
| 107 | +for x in range(e): |
| 108 | + l = [int(x) for x in input().split()] |
| 109 | + adjlist[l[0]].append([ l[1], l[2] ]) |
| 110 | + adjlist[l[1]].append([ l[0], l[2] ]) |
| 111 | +print(PrimsAlgorithm(adjlist)) |
0 commit comments