forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPathsFromSourceToTarget.py
More file actions
37 lines (33 loc) · 1.03 KB
/
AllPathsFromSourceToTarget.py
File metadata and controls
37 lines (33 loc) · 1.03 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
#All Paths From Source to Target
#https://leetcode.com/problems/all-paths-from-source-to-target/description/
class dfs(object):
def __init__(self, input_matrix):
self.input_matrix = input_matrix
def solution(self, source, destination, path):
if source == destination:
print path
return
for next_node in self.input_matrix[source]:
path.append(next_node)
self.solution(next_node, destination, path)
#Back track
path.pop()
'''
Cannot really thik of a way to do it in bfs
class bfs(object):
def __init__(self, input_matrix):
self.input_matrix = input_matrix
def solution(self, source, destination, path):
q = []
q.append(source)
path = []
while q:
top - = q.pop(0)
if(top == destination):
print path
continue
'''
#Main Program
matrix = [[1,2], [3], [3], []]
obj = dfs(matrix)
obj.solution(0, 3, [0])