diff --git a/Project/Python Progs/graph.py b/Project/Python Progs/graph.py new file mode 100644 index 00000000..06084f4a --- /dev/null +++ b/Project/Python Progs/graph.py @@ -0,0 +1,26 @@ +class Graph: + def __init__(self): + self.graph = {} + + def add_vertex(self, vertex): + if vertex not in self.graph: + self.graph[vertex] = [] + + def add_edge(self, vertex1, vertex2): + if vertex1 in self.graph and vertex2 in self.graph: + self.graph[vertex1].append(vertex2) + self.graph[vertex2].append(vertex1) + + def display(self): + for vertex, neighbors in self.graph.items(): + print(f"{vertex}: {neighbors}") + +# Example usage: +if __name__ == "__main__": + graph = Graph() + graph.add_vertex("A") + graph.add_vertex("B") + graph.add_vertex("C") + graph.add_edge("A", "B") + graph.add_edge("B", "C") + graph.display()