-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_traversal.py
More file actions
63 lines (52 loc) · 2.06 KB
/
graph_traversal.py
File metadata and controls
63 lines (52 loc) · 2.06 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
51
52
53
54
55
56
57
58
59
60
61
62
63
import itertools
from termcolor import colored
class GraphTraversal:
"""
Problem Type: Graph Traversal, Path Finding
Problem Statement:
Given a graph represented as an adjacency list, a starting node, and an ending node, find all possible paths from the start node to the end node.
Parameters:
graph (Dict[Any, List[Any]]): The graph represented as an adjacency list.
Methods:
find_paths(start, end): Finds all possible paths from the start node to the end node.
__repr__(): Returns a string representation of the graph.
"""
def __init__(self, graph):
self.graph = graph
def find_paths(self, start, end, path=[]):
"""
Find all possible paths from the start node to the end node.
Parameters:
start (Any): The starting node for the path finding.
end (Any): The ending node for the path finding.
path (List[Any], optional): The current path being traversed. Defaults to an empty list.
Returns:
List[List[Any]]: A list of all possible paths from the start node to the end node.
"""
path = path + [start]
if start == end:
return [path]
if start not in self.graph:
return []
paths = []
for node in self.graph[start]:
if node not in path:
newpaths = self.find_paths(node, end, path)
for p in newpaths:
paths.append(p)
return paths
def __repr__(self):
graph_repr = "\n".join([colored(f"{key}: {value}", 'yellow') for key, value in self.graph.items()])
return colored(f"GraphTraversal(graph={{\n{graph_repr}\n}})", 'cyan')
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
graph_traversal = GraphTraversal(graph)
print(colored("All paths from A to F:", 'blue'), colored(graph_traversal.find_paths('A', 'F'), 'green')) # Output: [['A', 'B', 'E', 'F'], ['A', 'C', 'F']]
print(colored(repr(graph_traversal), 'magenta'))