forked from phpduke/Algorithms-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleGraphTemplate.py
More file actions
50 lines (39 loc) · 1.41 KB
/
SimpleGraphTemplate.py
File metadata and controls
50 lines (39 loc) · 1.41 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from collections import deque, defaultdict
class Graph:
def __init__(self, directed=False):
self.adj_list = defaultdict(list)
self.directed = directed
def add_edge(self, u, v):
self.adj_list[u].append(v)
if not self.directed:
self.adj_list[v].append(u)
def bfs(self, start):
# Dictionary to store distance from start node
distance = {node: float('inf') for node in self.adj_list}
distance[start] = 0
queue = deque([start])
while queue:
current_node = queue.popleft()
for neighbor in self.adj_list[current_node]:
if distance[neighbor] == float('inf'): # Unvisited node
distance[neighbor] = distance[current_node] + 1
queue.append(neighbor)
return distance
def print_graph(self):
for key, value in self.adj_list.items():
print(f"{key}: {value}")
# Example usage:
if __name__ == "__main__":
g = Graph(directed=False)
g.add_edge(1, 2)
g.add_edge(1, 3)
g.add_edge(2, 4)
g.add_edge(3, 5)
g.add_edge(4, 5)
# Print the adjacency list (for debugging)
g.print_graph()
# Perform BFS starting from node 1
distances = g.bfs(1)
print("\nDistances from node 1:")
for node, dist in distances.items():
print(f"Node {node}: Distance {dist}")