forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims_minimum_spanning.py
More file actions
53 lines (39 loc) · 1.19 KB
/
prims_minimum_spanning.py
File metadata and controls
53 lines (39 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Prim's Minimum Spanning Tree
Computes the weight of a minimum spanning tree for a connected weighted
undirected graph using a priority queue.
Reference: https://en.wikipedia.org/wiki/Prim%27s_algorithm
Complexity:
Time: O(E log V)
Space: O(V + E)
"""
from __future__ import annotations
import heapq
from typing import Any
def prims_minimum_spanning(
graph_used: dict[Any, list[list[int | Any]]],
) -> int:
"""Return the total weight of the MST using Prim's algorithm.
Args:
graph_used: Adjacency list as ``{node: [[weight, neighbour], ...]}``.
Returns:
Sum of edge weights in the minimum spanning tree.
Examples:
>>> prims_minimum_spanning({1: [[1, 2]], 2: [[1, 1]]})
1
"""
vis: list[Any] = []
heap: list[list[int | Any]] = [[0, 1]]
prim: set[Any] = set()
mincost = 0
while len(heap) > 0:
cost, node = heapq.heappop(heap)
if node in vis:
continue
mincost += cost
prim.add(node)
vis.append(node)
for distance, adjacent in graph_used[node]:
if adjacent not in vis:
heapq.heappush(heap, [distance, adjacent])
return mincost