graphs.minimum_spanning_tree_prims2ΒΆ
Primβs (also known as JarnΓkβs) algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex.
AttributesΒΆ
ClassesΒΆ
Graph Undirected Weighted Class |
|
Minimum Priority Queue Class |
FunctionsΒΆ
|
heap helper function get the position of the left child of the current node |
|
heap helper function get the position of the right child of the current node |
|
heap helper function get the position of the parent of the current node |
|
Module ContentsΒΆ
- class graphs.minimum_spanning_tree_prims2.GraphUndirectedWeightedΒΆ
Graph Undirected Weighted Class
Functions: add_node: function to add a node in the graph add_edge: function to add an edge between 2 nodes in the graph
- __len__() intΒΆ
- __repr__() strΒΆ
- add_edge(node1: T, node2: T, weight: int) NoneΒΆ
- add_node(node: T) NoneΒΆ
- connections: dict[T, dict[T, int]]ΒΆ
- nodes: int = 0ΒΆ
- class graphs.minimum_spanning_tree_prims2.MinPriorityQueueΒΆ
Minimum Priority Queue Class
Functions: is_empty: function to check if the priority queue is empty push: function to add an element with given priority to the queue extract_min: function to remove and return the element with lowest weight (highest
priority)
update_key: function to update the weight of the given key _bubble_up: helper function to place a node at the proper position (upward
movement)
- _bubble_down: helper function to place a node at the proper position (downward
movement)
_swap_nodes: helper function to swap the nodes at the given positions
>>> queue = MinPriorityQueue()
>>> queue.push(1, 1000) >>> queue.push(2, 100) >>> queue.push(3, 4000) >>> queue.push(4, 3000)
>>> queue.extract_min() 2
>>> queue.update_key(4, 50)
>>> queue.extract_min() 4 >>> queue.extract_min() 1 >>> queue.extract_min() 3
- __len__() intΒΆ
- __repr__() strΒΆ
- _bubble_down(elem: T) NoneΒΆ
- _bubble_up(elem: T) NoneΒΆ
- _swap_nodes(node1_pos: int, node2_pos: int) NoneΒΆ
- extract_min() TΒΆ
- is_empty() boolΒΆ
- push(elem: T, weight: int) NoneΒΆ
- update_key(elem: T, weight: int) NoneΒΆ
- elements: int = 0ΒΆ
- heap: list[tuple[T, int]] = []ΒΆ
- position_map: dict[T, int]ΒΆ
- graphs.minimum_spanning_tree_prims2.get_child_left_position(position: int) intΒΆ
heap helper function get the position of the left child of the current node
>>> get_child_left_position(0) 1
- graphs.minimum_spanning_tree_prims2.get_child_right_position(position: int) intΒΆ
heap helper function get the position of the right child of the current node
>>> get_child_right_position(0) 2
- graphs.minimum_spanning_tree_prims2.get_parent_position(position: int) intΒΆ
heap helper function get the position of the parent of the current node
>>> get_parent_position(1) 0 >>> get_parent_position(2) 0
- graphs.minimum_spanning_tree_prims2.prims_algo(graph: GraphUndirectedWeighted[prims_algo.T]) tuple[dict[prims_algo.T, int], dict[prims_algo.T, prims_algo.T | None]]ΒΆ
>>> graph = GraphUndirectedWeighted()
>>> graph.add_edge("a", "b", 3) >>> graph.add_edge("b", "c", 10) >>> graph.add_edge("c", "d", 5) >>> graph.add_edge("a", "c", 15) >>> graph.add_edge("b", "d", 100)
>>> dist, parent = prims_algo(graph)
>>> abs(dist["a"] - dist["b"]) 3 >>> abs(dist["d"] - dist["b"]) 15 >>> abs(dist["a"] - dist["c"]) 13
- graphs.minimum_spanning_tree_prims2.TΒΆ