Professional Documents
Culture Documents
Dijisktras
Dijisktras
def __init__(self):
self.vertices = {}
def get_vertices(self):
return list(self.vertices.keys())
def get_edges(self):
edges = []
for start, connections in self.vertices.items():
for end, weight in connections.items():
edges.append((start, end, weight))
return edges
def display_graph(self):
print("Graph:")
for start, connections in self.vertices.items():
for end, weight in connections.items():
print(f"{start} - {weight} -> {end}")
print()
# Example Usage:
graph = Graph()
vertices = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
for vertex in vertices:
graph.add_vertex(vertex)
graph.add_edge("A", "B", 2)
graph.add_edge("A", "C", 4)
graph.add_edge("B", "C", 1)
graph.add_edge("B", "D", 3)
graph.add_edge("C", "E", 5)
graph.add_edge("D", "F", 2)
graph.add_edge("E", "G", 4)
graph.add_edge("F", "H", 3)
graph.add_edge("G", "I", 2)
graph.add_edge("H", "J", 4)
graph.add_edge("I", "K", 1)
:
graph.add_edge("J", "L", 2)