-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.py
More file actions
26 lines (21 loc) · 680 Bytes
/
graph.py
File metadata and controls
26 lines (21 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Graph:
def __init__(self):
self.vertices = {}
def add_vertex(self, vertex):
if vertex not in self.vertices:
self.vertices[vertex] = []
def add_edge(self, vertex1, vertex2):
if vertex1 in self.vertices and vertex2 in self.vertices:
self.vertices[vertex1].append(vertex2)
self.vertices[vertex2].append(vertex1)
def display(self):
for vertex in self.vertices:
print(f"{vertex}: {', '.join(map(str, self.vertices[vertex]))}")
# Example Usage:
graph = Graph()
graph.add_vertex(1)
graph.add_vertex(2)
graph.add_vertex(3)
graph.add_edge(1, 2)
graph.add_edge(2, 3)
graph.display()