@@ -15,22 +15,26 @@ def add_vertex(self, label: str = None, weight: int = float("inf")):
1515
1616 def add_edge (self , label1 : str = None , label2 : str = None , weight : int = float ("inf" )):
1717 self ._adjacency_map [label1 ][label2 ] = Vertex (label2 , weight )
18+ self ._adjacency_map [label2 ][label1 ] = Vertex (label1 , weight )
1819
19- def dijkstra (self , label : str ):
20+ def prims (self , label : str ):
21+ result : str = ""
2022 self ._vertices [label ].set_weight (0 )
2123 pq : PriorityQueue = PriorityQueue ()
2224 for vertex_label in self ._vertices :
2325 pq .insert (self ._vertices [vertex_label ])
2426 while not pq .is_empty ():
2527 vertex : Vertex = pq .delete_min ()
28+ if self ._prev [vertex .get_label ()] is not None :
29+ result += self ._prev [vertex .get_label ()] + " -> " + vertex .get_label () + ", "
2630 for neighbour_label in self ._adjacency_map [vertex .get_label ()]:
2731 neighbour_from_adjacency_map : Vertex = self ._adjacency_map [vertex .get_label ()][neighbour_label ]
2832 neighbour_from_vertices : Vertex = self ._vertices [neighbour_label ]
29- if neighbour_from_vertices .get_weight () > \
30- vertex .get_weight () + neighbour_from_adjacency_map .get_weight ():
31- neighbour_from_vertices .set_weight (vertex .get_weight () + neighbour_from_adjacency_map .get_weight ())
33+ if neighbour_from_vertices .get_weight () > neighbour_from_adjacency_map .get_weight ():
34+ neighbour_from_vertices .set_weight (neighbour_from_adjacency_map .get_weight ())
3235 self ._prev [neighbour_from_vertices .get_label ()] = vertex .get_label ()
3336 pq .decrease_key (neighbour_from_vertices .get_key ())
37+ print (result )
3438
3539 def return_path (self , end_label : str = None ) -> str :
3640 if self ._prev [end_label ] is None :
0 commit comments