mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
4d0c830d2c
* ci(pre-commit): Add ``flake8-builtins`` additional dependency to ``pre-commit`` (#7104) * refactor: Fix ``flake8-builtins`` (#7104) * fix(lru_cache): Fix naming conventions in docstrings (#7104) * ci(pre-commit): Order additional dependencies alphabetically (#7104) * fix(lfu_cache): Correct function name in docstring (#7104) * Update strings/snake_case_to_camel_pascal_case.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/stacks/next_greater_element.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update digital_image_processing/index_calculation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update graphs/prim.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update hashes/djb2.py Co-authored-by: Christian Clauss <cclauss@me.com> * refactor: Rename `_builtin` to `builtin_` ( #7104) * fix: Rename all instances (#7104) * refactor: Update variable names (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ci: Create ``tox.ini`` and ignore ``A003`` (#7123) * revert: Remove function name changes (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename tox.ini to .flake8 * Update data_structures/heap/heap.py Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> * refactor: Rename `next_` to `next_item` (#7104) * ci(pre-commit): Add `flake8` plugin `flake8-bugbear` (#7127) * refactor: Follow `flake8-bugbear` plugin (#7127) * fix: Correct `knapsack` code (#7127) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
117 lines
3.8 KiB
Python
117 lines
3.8 KiB
Python
import sys
|
|
from collections import defaultdict
|
|
|
|
|
|
def prisms_algorithm(l): # noqa: E741
|
|
|
|
node_position = []
|
|
|
|
def get_position(vertex):
|
|
return node_position[vertex]
|
|
|
|
def set_position(vertex, pos):
|
|
node_position[vertex] = pos
|
|
|
|
def top_to_bottom(heap, start, size, positions):
|
|
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
|
|
|
|
temp = get_position(positions[m])
|
|
set_position(positions[m], get_position(positions[start]))
|
|
set_position(positions[start], temp)
|
|
|
|
top_to_bottom(heap, m, size, positions)
|
|
|
|
# Update function if value of any node in min-heap decreases
|
|
def bottom_to_top(val, index, heap, position):
|
|
temp = position[index]
|
|
|
|
while index != 0:
|
|
if index % 2 == 0:
|
|
parent = int((index - 2) / 2)
|
|
else:
|
|
parent = int((index - 1) / 2)
|
|
|
|
if val < heap[parent]:
|
|
heap[index] = heap[parent]
|
|
position[index] = position[parent]
|
|
set_position(position[parent], index)
|
|
else:
|
|
heap[index] = val
|
|
position[index] = temp
|
|
set_position(temp, index)
|
|
break
|
|
index = parent
|
|
else:
|
|
heap[0] = val
|
|
position[0] = temp
|
|
set_position(temp, 0)
|
|
|
|
def heapify(heap, positions):
|
|
start = len(heap) // 2 - 1
|
|
for i in range(start, -1, -1):
|
|
top_to_bottom(heap, i, len(heap), positions)
|
|
|
|
def delete_minimum(heap, positions):
|
|
temp = positions[0]
|
|
heap[0] = sys.maxsize
|
|
top_to_bottom(heap, 0, len(heap), positions)
|
|
return temp
|
|
|
|
visited = [0 for i in range(len(l))]
|
|
nbr_tv = [-1 for i in range(len(l))] # 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 = []
|
|
|
|
for x in range(len(l)):
|
|
p = sys.maxsize
|
|
distance_tv.append(p)
|
|
positions.append(x)
|
|
node_position.append(x)
|
|
|
|
tree_edges = []
|
|
visited[0] = 1
|
|
distance_tv[0] = sys.maxsize
|
|
for x in l[0]:
|
|
nbr_tv[x[0]] = 0
|
|
distance_tv[x[0]] = x[1]
|
|
heapify(distance_tv, positions)
|
|
|
|
for _ in range(1, len(l)):
|
|
vertex = delete_minimum(distance_tv, positions)
|
|
if visited[vertex] == 0:
|
|
tree_edges.append((nbr_tv[vertex], vertex))
|
|
visited[vertex] = 1
|
|
for v in l[vertex]:
|
|
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
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
# < --------- 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):
|
|
l = [int(x) for x in input().strip().split()] # noqa: E741
|
|
adjlist[l[0]].append([l[1], l[2]])
|
|
adjlist[l[1]].append([l[0], l[2]])
|
|
print(prisms_algorithm(adjlist))
|