forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.py
37 lines (27 loc) · 878 Bytes
/
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
''' This DFS Algorithm will find out all the possible paths from all nodes in the Graph'''
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list) # create from existing library
def addEdge(self, i, j):
self.graph[i].append(j) # add a new edge
# Actual algorithm of DFS in python
def DFSUtil(self, j, isvisited):
isvisited[j] = True
print(j)
for i in self.graph[j]:
if isvisited[i] == False:
self.DFSUtil(i, isvisited)
def DFS(self, j):
visited = [False] * (len(self.graph))
self.DFSUtil(j, visited)
graph = Graph()
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(1, 2)
graph.addEdge(2, 0)
graph.addEdge(2, 3)
graph.addEdge(3, 3)
for i in range(4):
print("DFS from vertex " + str(i) + ": ")
graph.DFS(i)