Skip to content

Commit

Permalink
Merge pull request #62 from sirjan13/master
Browse files Browse the repository at this point in the history
Added DFS code in python
  • Loading branch information
srbcheema1 authored Oct 2, 2017
2 parents 80df4ee + d78be16 commit a5396bb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Python implementation DataStructures/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import collections

class Graph:
def __init__(self):
self.adjList = collections.defaultdict(set)

def addEdge(self, node1, node2):
self.adjList[node1].add(node2)
self.adjList[node2].add(node1)

def dfsHelper(current, graph, visited, visitFunc):
if (visited[current]):
return

visited[current] = True;

visitFunc(current)

for neighbor in graph.adjList[current]:
dfsHelper(neighbor, graph, visited, visitFunc)


def dfs(current, graph, visitFunc):
visited = collections.defaultdict(bool)
dfsHelper(current, graph, visited, visitFunc)

def visitPrint(i):
print(i)

# Testing the depth first search implementation
if __name__ == "__main__":

# Testing on this tree
# 1
# / \
# / \
# 2 3
# / \ / \
# 4 5 6 7

g = Graph()
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(2, 4)
g.addEdge(2, 5)
g.addEdge(3, 6)
g.addEdge(3, 7)

print("Test 1:")
dfs(1, g, visitPrint)

print("\nTest2:")
dfs(2, g, visitPrint)

0 comments on commit a5396bb

Please sign in to comment.