-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1076 from Riyachauhan11/main
adding graph traversals
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from collections import deque | ||
|
||
class Graph: | ||
def __init__(self): | ||
self.graph = {} | ||
|
||
def add_edge(self, u, v): | ||
if u not in self.graph: | ||
self.graph[u] = [] | ||
if v not in self.graph: | ||
self.graph[v] = [] | ||
self.graph[u].append(v) | ||
self.graph[v].append(u) # For undirected graph | ||
|
||
def bfs(self, start): | ||
visited = set() | ||
queue = deque([start]) | ||
traversal = [] | ||
|
||
visited.add(start) | ||
|
||
while queue: | ||
node = queue.popleft() | ||
traversal.append(node) | ||
for neighbor in self.graph.get(node, []): | ||
if neighbor not in visited: | ||
visited.add(neighbor) | ||
queue.append(neighbor) | ||
|
||
return traversal | ||
|
||
def display(self): | ||
for node, neighbors in self.graph.items(): | ||
print(f"{node}: {neighbors}") | ||
|
||
def menu(): | ||
print("\n--- Graph BFS Menu ---") | ||
print("1. Add Edge") | ||
print("2. Perform BFS") | ||
print("3. Display Graph") | ||
print("4. Exit") | ||
|
||
if __name__ == "__main__": | ||
graph = Graph() | ||
|
||
while True: | ||
menu() | ||
choice = input("Enter choice: ") | ||
|
||
if choice == '1': | ||
u = input("Enter first node: ") | ||
v = input("Enter second node: ") | ||
graph.add_edge(u, v) | ||
print(f"Added edge between {u} and {v}.") | ||
|
||
elif choice == '2': | ||
start = input("Enter start node for BFS: ") | ||
if start in graph.graph: | ||
print("BFS Traversal:", graph.bfs(start)) | ||
else: | ||
print(f"Node {start} does not exist in the graph.") | ||
|
||
elif choice == '3': | ||
print("Graph Adjacency List:") | ||
graph.display() | ||
|
||
elif choice == '4': | ||
print("Exiting...") | ||
break | ||
|
||
else: | ||
print("Invalid choice, try again.") |
67 changes: 67 additions & 0 deletions
67
Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
class Graph: | ||
def __init__(self): | ||
self.graph = {} | ||
|
||
def add_edge(self, u, v): | ||
if u not in self.graph: | ||
self.graph[u] = [] | ||
if v not in self.graph: | ||
self.graph[v] = [] | ||
self.graph[u].append(v) | ||
self.graph[v].append(u) # For undirected graph | ||
|
||
def dfs(self, start): | ||
visited = set() | ||
traversal = [] | ||
|
||
def dfs_util(v): | ||
visited.add(v) | ||
traversal.append(v) | ||
for neighbor in self.graph.get(v, []): | ||
if neighbor not in visited: | ||
dfs_util(neighbor) | ||
|
||
dfs_util(start) | ||
return traversal | ||
|
||
def display(self): | ||
for node, neighbors in self.graph.items(): | ||
print(f"{node}: {neighbors}") | ||
|
||
def menu(): | ||
print("\n--- Graph DFS Menu ---") | ||
print("1. Add Edge") | ||
print("2. Perform DFS") | ||
print("3. Display Graph") | ||
print("4. Exit") | ||
|
||
if __name__ == "__main__": | ||
graph = Graph() | ||
|
||
while True: | ||
menu() | ||
choice = input("Enter choice: ") | ||
|
||
if choice == '1': | ||
u = input("Enter first node: ") | ||
v = input("Enter second node: ") | ||
graph.add_edge(u, v) | ||
print(f"Added edge between {u} and {v}.") | ||
|
||
elif choice == '2': | ||
start = input("Enter start node for DFS: ") | ||
if start in graph.graph: | ||
print("DFS Traversal:", graph.dfs(start)) | ||
else: | ||
print(f"Node {start} does not exist in the graph.") | ||
|
||
elif choice == '3': | ||
print("Graph Adjacency List:") | ||
graph.display() | ||
|
||
elif choice == '4': | ||
print("Exiting...") | ||
break | ||
|
||
else: | ||
print("Invalid choice, try again.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters