Download 9.py from Madhan-Alagarsamy/Python: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/datasets/Madhan-Alagarsamy/Python/resolve/main/9.py
- Command line
-
hf download hf://datasets/Madhan-Alagarsamy/Python/9.py
-
curl -L -o 9.py https://huggingface.co/datasets/Madhan-Alagarsamy/Python/resolve/main/9.py
3.58 kB
| import heapq | |
| import itertools | |
| # ---------------- Priority Queue ---------------- | |
| class AdaptableHeapPriorityQueue: | |
| def __init__(self): | |
| self._heap = [] | |
| self._counter = itertools.count() | |
| def add(self, key, value): | |
| entry = [key, next(self._counter), value] | |
| heapq.heappush(self._heap, entry) | |
| return entry | |
| def update(self, entry, newkey, newvalue): | |
| entry[0] = newkey | |
| entry[2] = newvalue | |
| heapq.heapify(self._heap) | |
| def remove_min(self): | |
| while self._heap: | |
| key, _, value = heapq.heappop(self._heap) | |
| return key, value | |
| raise KeyError("Priority queue is empty") | |
| def is_empty(self): | |
| return len(self._heap) == 0 | |
| # ---------------- Graph Classes ---------------- | |
| class Vertex: | |
| def __init__(self, label): | |
| self.label = label | |
| def __hash__(self): | |
| return hash(self.label) | |
| def __str__(self): | |
| return str(self.label) | |
| class Edge: | |
| def __init__(self, u, v, w): | |
| self._u = u | |
| self._v = v | |
| self._w = w | |
| def opposite(self, x): | |
| return self._v if x == self._u else self._u | |
| def element(self): | |
| return self._w | |
| def endpoints(self): | |
| return (self._u, self._v) | |
| class Graph: | |
| def __init__(self): | |
| self._adj = {} | |
| def add_vertex(self, label): | |
| v = Vertex(label) | |
| self._adj[v] = [] | |
| return v | |
| def add_edge(self, u, v, w): | |
| e = Edge(u, v, w) | |
| self._adj[u].append(e) | |
| self._adj[v].append(e) | |
| def vertices(self): | |
| return self._adj.keys() | |
| def incident_edges(self, v): | |
| return self._adj[v] | |
| # ---------------- Prim-Jarník Algorithm ---------------- | |
| def MST_PrimJarnik(g): | |
| d = {} | |
| tree = [] | |
| pq = AdaptableHeapPriorityQueue() | |
| pqlocator = {} | |
| for v in g.vertices(): | |
| if len(d) == 0: | |
| d[v] = 0 | |
| else: | |
| d[v] = float('inf') | |
| pqlocator[v] = pq.add(d[v], (v, None)) | |
| while not pq.is_empty(): | |
| key, value = pq.remove_min() | |
| u, edge = value | |
| del pqlocator[u] | |
| if edge is not None: | |
| tree.append(edge) | |
| for link in g.incident_edges(u): | |
| v = link.opposite(u) | |
| if v in pqlocator: | |
| wgt = link.element() | |
| if wgt < d[v]: | |
| d[v] = wgt | |
| pq.update(pqlocator[v], d[v], (v, link)) | |
| return tree | |
| print("\Minimum Spanning Tree") | |
| print("\t---------------------------------") | |
| n = int(input("Enter number of vertices: ")) | |
| m = int(input("Enter number of edges: ")) | |
| g = Graph() | |
| vertices = [] | |
| for i in range(n+1): | |
| vertices.append(g.add_vertex(i)) | |
| print("Enter vertices of each edges and its weight as: u v weight") | |
| for _ in range(m): | |
| u, v, w = map(int, input().split()) | |
| g.add_edge(vertices[u], vertices[v], w) | |
| mst = MST_PrimJarnik(g) | |
| print("\nEdges in the Minimum Spanning Tree:") | |
| total = 0 | |
| for e in mst: | |
| u, v = e.endpoints() | |
| print(f"{u} -- {v} (weight = {e.element()})") | |
| total += e.element() | |
| print("Total weight of MST:", total) | |