-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.py
28 lines (22 loc) · 794 Bytes
/
Graph.py
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
27
28
class Graph:
def __init__(self):
self.adj_list = {}
def add_edge(self, node1, node2, weight):
if node1 not in self.adj_list:
self.adj_list[node1] = {}
if node2 not in self.adj_list:
self.adj_list[node2] = {}
self.adj_list[node1][node2] = weight
self.adj_list[node2][node1] = weight
def remove_edge(self, node1, node2):
if node1 not in self.adj_list:
return
if node2 not in self.adj_list:
return
del self.adj_list[node1][node2]
del self.adj_list[node2][node1]
def get_neighbors(self, node):
return self.adj_list[node]
def print_graph(self):
for node, neighbors in self.adj_list.items():
print(f"Node {node}: {neighbors}")