-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.py
More file actions
54 lines (47 loc) · 1.12 KB
/
Copy pathq1.py
File metadata and controls
54 lines (47 loc) · 1.12 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
'''check if path exists between source and destination('directed and acyclic graph)'''
'''
f__>e
^
|
a__>b__>d__>g__>h
| |
^ ^
c i
'''
'''
source="a"
destination="g"
#algorithm:
-if src==dest,return true
-use a bool var to keep track of the dfs call
-make call to the neighbour
-return answer
'''
graph={
"a":["b","c"],
"b":["f","d"],
"c":[],
"d":["g","i"],
"e":["h"],
"f":["e"],
"g":["h"],
"h":[],
"i":[]
}
def path(src,dest,graph):
if src==dest:
return True
ans=False
for neighbour in graph[src]:
ans=ans or path(neighbour,dest,graph)
# print(path(neighbour,dest,graph),":",neighbour,end=",")
if ans==True:
print("Thus path exist:",neighbour)
else:
print("path not exist:",neighbour)
# return ans
# print(path(neighbour,dest,graph),":",neighbour,end=",")
print("Check path existence:")
src=input("enter the source:")
dest=(input("enter the destination:"))
print(path(src,dest,graph))