@@ -13,3 +13,76 @@ def define_edges(graph):
1313return edges
1414
1515print (define_edges (graph ))
16+
17+
18+ # Dict Graphs
19+
20+ graph = {"a" : ["c" ],
21+ "b" : ["c" ,"e" ],
22+ "c" : ["a" ,"b" ,"d" ,"e" ],
23+ "d" : ["c" ],
24+ "e" : ["c" ,"b" ],
25+ "f" : []
26+ }
27+
28+ # import dictionary for graph
29+ from collections import defaultdict
30+
31+ # function for adding edge to graph
32+ graph = defaultdict (list )
33+
34+ def addEdge (graph ,u ,v ):
35+ graph [u ].append (v )
36+
37+ def generate_edges (graph ):
38+ edges = []
39+
40+ # for each node in graph
41+ for node in graph :
42+
43+ # for each neighbour node of a single node
44+ for neighbour in graph [node ]:
45+ # if edge exists then append
46+ edges .append ((node ,neighbour ))
47+ return edges
48+
49+
50+ # function to find path
51+ def find_path (graph ,start ,end ,path = []):
52+ path = path + [start ]
53+ if start == end :
54+ return path
55+ for node in graph [start ]:
56+ if node not in path :
57+ newpath = find_path (graph ,node ,end ,path )
58+ if newpath :
59+ return newpath
60+ return None
61+
62+
63+ # function to generate all possible paths
64+ def find_all_paths (graph ,start ,end ,path = []):
65+ path = path + [start ]
66+ if start == end :
67+ return [path ]
68+ paths = []
69+ for node in graph [start ]:
70+ if node not in path :
71+ newpaths = find_all_paths (graph ,node ,end ,path )
72+ for newpath in newpaths :
73+ paths .append (newpath )
74+ return paths
75+
76+ # function to find the shortest path
77+ def find_shortest_path (graph ,start ,end ,path = []):
78+ path = path + [start ]
79+ if start == end :
80+ return path
81+ shortest = None
82+ for node in graph [start ]:
83+ if node not in path :
84+ newpath = find_shortest_path (graph ,node ,end ,path )
85+ if newpath :
86+ if not shortest or len (newpath )< len (shortest ):
87+ shortest = newpath
88+ return shortest