From 94b2e723d4e31a1c2a17092112e679ca67a7a23a Mon Sep 17 00:00:00 2001 From: Riya Chauhan <96919050+Riyachauhan11@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:56:46 +0530 Subject: [PATCH 1/4] Create Menu_driven_code_DFS_Traversal.py --- .../Graph/Menu_driven_code_DFS_Traversal.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py diff --git a/Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py b/Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py new file mode 100644 index 0000000000..8c6c702e04 --- /dev/null +++ b/Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py @@ -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.") From 92bd6bd842af83bf9a56db101dd133b2a071d5ab Mon Sep 17 00:00:00 2001 From: Riyachauhan11 Date: Mon, 4 Nov 2024 15:27:32 +0000 Subject: [PATCH 2/4] updating Project-Structure.md --- Project-Structure.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Project-Structure.md b/Project-Structure.md index 52b16916d1..7577974a14 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -102,6 +102,7 @@ * [Unique-Paths](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/unique-paths.py) * Graph * [Graph Cloning](Algorithms_and_Data_Structures/Graph/Graph_Cloning.py) + * [Menu Driven Code Dfs Traversal](Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py) * Heapsort * [Heapsortvisualizer](Algorithms_and_Data_Structures/HeapSort/heapsortvisualizer.py) * [Timecomplexity1](Algorithms_and_Data_Structures/HeapSort/timecomplexity1.py) From 741201a2c266fecee43a6ef72747a62af77a975a Mon Sep 17 00:00:00 2001 From: Riya Chauhan <96919050+Riyachauhan11@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:59:21 +0530 Subject: [PATCH 3/4] Create Menu_driven_code_BFS_Traversal.py --- .../Graph/Menu_driven_code_BFS_Traversal.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py diff --git a/Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py b/Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py new file mode 100644 index 0000000000..c6a015eef9 --- /dev/null +++ b/Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py @@ -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.") From b7c09cc0ad957350c7ce461d55b1047e08e632bb Mon Sep 17 00:00:00 2001 From: Riyachauhan11 Date: Mon, 4 Nov 2024 15:30:27 +0000 Subject: [PATCH 4/4] updating Project-Structure.md --- Project-Structure.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Project-Structure.md b/Project-Structure.md index 7577974a14..bdbd0c02e5 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -102,6 +102,7 @@ * [Unique-Paths](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/unique-paths.py) * Graph * [Graph Cloning](Algorithms_and_Data_Structures/Graph/Graph_Cloning.py) + * [Menu Driven Code Bfs Traversal](Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py) * [Menu Driven Code Dfs Traversal](Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py) * Heapsort * [Heapsortvisualizer](Algorithms_and_Data_Structures/HeapSort/heapsortvisualizer.py)