-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.py
120 lines (94 loc) · 5.82 KB
/
DFS.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# This code implements a graph data structure and performs data analysis using depth-first search (DFS).
# This code defines two classes, Graph and DataAnalysis, for analyzing data using a graph.
# ----> You can see the task of each class at the beginning. <----
# The Graph class represents an undirected graph and provides methods for adding edges
# and performing depth-first search (DFS) to find a path from a start node to a goal node.
class Graph:
def __init__(self):
self.graph = {}
def add_edge(self, u, v):
if u in self.graph:
self.graph[u].append(v) # Add v to the adjacency list of u
else:
self.graph[u] = [v] # Create a new adjacency list for u with v as the first element
if v in self.graph:
self.graph[v].append(u) # Add u to the adjacency list of v
else:
self.graph[v] = [u] # Create a new adjacency list for v with u as the first element
def dfs(self, start, goal):
visited = set() # Keep track of visited vertices
path = [] # Store the path from start to goal
self._dfs_util(start, goal, visited, path)
return path
def _dfs_util(self, vertex, goal, visited, path):
visited.add(vertex) # Mark the vertex as visited
path.append(vertex) # Add the vertex to the current path
if vertex == goal: # If we have reached the goal vertex
return True
for neighbor in self.graph[vertex]: # Iterate over the neighbors of the current vertex
if neighbor not in visited: # If the neighbor is not visited
if self._dfs_util(neighbor, goal, visited, path): # Recursively call DFS on the neighbor
return True # If the neighbor reaches the goal, return True
path.pop() # If no path from vertex leads to the goal, remove the vertex from the path
return False
# The DataAnalysis class utilizes the Graph class to analyze data by taking user input for the number of edges and
# the edges themselves, and then finding the shortest path between a start node and a goal node.
class DataAnalysis:
def __init__(self):
self.graph = Graph()
def print_graph_shape(self):
print(" *** Graph Shape: ***")
for node, neighbors in self.graph.graph.items():
print(f" {node} -> {', '.join(neighbors)}")
def analyze_data(self):
n = int(input("---> Enter the number of edges: "))
for _ in range(n):
u, v = input("---> Enter an edge (space-separated): ").split()
self.graph.add_edge(u, v)
start_node = input("---> Enter the start node: ")
goal_node = input("---> Enter the goal node: ")
print("***************************************************************************\n")
self.print_graph_shape()
shortest_path = self.graph.dfs(start_node, goal_node)
if shortest_path:
print("\n *** Shortest path:", "->".join(shortest_path))
else:
print(" ( ! No path found. ! )")
# This part of the code is written as an example to show the output of the code.
# According to your needs, you can change or delete this part.
def banner():
print("""
-------------------------------------------------------------------------
| (: *** Welcome *** :) |
| |
| This program implements a graph data structure and performs |
| data analysis using depth-first search (DFS). |
| |
| For Example: |
| ---> Enter the number of edges: 5 |
| ---> Enter an edge (space-separated): A B |
| ---> Enter an edge (space-separated): B C |
| ---> Enter an edge (space-separated): C D |
| ---> Enter an edge (space-separated): A E |
| ---> Enter an edge (space-separated): E F |
| ---> Enter the start node: A |
| ---> Enter the goal node: D |
| *** Graph Shape: *** |
| A -> B, E |
| B -> A, C |
| C -> B, D |
| D -> C |
| E -> A, F |
| F -> E |
| *** Shortest path: A->B->C->D |
| |
-------------------------------------------------------------------------
""")
def main():
banner()
data_analysis = DataAnalysis()
data_analysis.analyze_data()
print("***************************************************************************\n")
if __name__ == "__main__":
main()
# An example of how to use the program is shown.