|
| 1 | + |
| 2 | +from typing import Dict, List |
| 3 | + |
1 | 4 | from vertex import Vertex |
2 | 5 | from priorityqueue import PriorityQueue |
3 | 6 |
|
4 | | - |
5 | 7 | class Graph: |
6 | | - def __init__(self): |
7 | | - self._vertices: dict = {} |
8 | | - self._adjacency_map: dict = {} |
9 | | - self._prev: dict = {} |
10 | | - |
11 | | - def add_vertex(self, label: str): |
12 | | - v: Vertex = Vertex(label) |
13 | | - self._vertices[label] = v |
14 | | - self._adjacency_map[label]: list = [] |
15 | | - self._prev[label] = None |
16 | | - |
17 | | - def add_edge(self, label1: str, label2: str, weight: int): |
18 | | - self._adjacency_map[label1].append(Vertex(label2, weight)) |
19 | | - self._adjacency_map[label2].append(Vertex(label1, weight)) |
20 | | - |
21 | | - def prims(self, label: str): |
22 | | - result: str = "" |
23 | | - self._vertices[label].weight = 0 |
24 | | - pq: PriorityQueue = PriorityQueue() |
25 | | - for label in self._vertices: |
26 | | - pq.insert(self._vertices[label]) |
27 | | - while not pq.is_empty(): |
28 | | - current: Vertex = pq.delete_min() |
29 | | - if self._prev[current.label] is not None: |
30 | | - result += self._prev[current.label] + " -> " + current.label + ", " |
31 | | - for neighbour in self._adjacency_map[current.label]: |
32 | | - v: Vertex = self._vertices[neighbour.label] |
33 | | - if neighbour.weight < v.weight: |
34 | | - v.weight = neighbour.weight |
35 | | - self._prev[v.label] = current.label |
36 | | - pq.decrease_key(v.key) |
37 | | - print(result) |
38 | 8 |
|
| 9 | + def __init__(self) -> None: |
| 10 | + self.vertices: Dict[str, Vertex] = dict() |
| 11 | + self.adjacency_map: Dict[str, List[Vertex]] = dict() |
| 12 | + self.prev:Dict[str, str] = dict() |
39 | 13 |
|
40 | 14 |
|
| 15 | + def add_vertex(self, label:str, weight:int=float('inf')): |
| 16 | + self.vertices[label] = Vertex(label) |
| 17 | + self.adjacency_map[label] = [] |
| 18 | + self.prev[label] = None |
41 | 19 |
|
42 | 20 |
|
| 21 | + def add_edge(self, label1:str, label2:str, weight:int): |
| 22 | + self.adjacency_map[label1].append(Vertex(label2, weight)) |
| 23 | + self.adjacency_map[label1].append(Vertex(label1, weight)) |
43 | 24 |
|
| 25 | + |
| 26 | + def prims(self, label:str): |
| 27 | + res:str = '' |
| 28 | + v:Vertex = self.vertices[label] |
| 29 | + v.weight = 0 |
44 | 30 |
|
| 31 | + pq:PriorityQueue = PriorityQueue() |
| 32 | + for k in self.vertices: |
| 33 | + vertex = self.vertices[k] |
| 34 | + pq.insert(vertex) |
45 | 35 |
|
| 36 | + while not pq.is_empty(): |
| 37 | + v = pq.delete_min() |
| 38 | + print('Min is ', v.label) |
| 39 | + if self.prev[v.label] is not None: |
| 40 | + res += f'{self.prev[v.label]} -> {v.label}, ' |
| 41 | + for neighbour in self.adjacency_map[v.label]: |
| 42 | + vertex:Vertex = self.vertices[neighbour.label] |
| 43 | + if neighbour.weight < vertex.weight: |
| 44 | + vertex.weight = neighbour.weight |
| 45 | + self.prev[vertex.label] = v.label |
| 46 | + pq.decrease_key(vertex.index) |
| 47 | + |
| 48 | + print(res) |
0 commit comments