Skip to content

Commit

Permalink
Merge pull request #339 from Atharvasaraiya/task-4
Browse files Browse the repository at this point in the history
Task 4
  • Loading branch information
Ananyasingh2002 authored Oct 2, 2023
2 parents 94cd71e + 9c81e15 commit 5418f69
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Project/Python Progs/graph.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 5418f69

Please sign in to comment.