Skip to content

Commit

Permalink
add graph depth first search algo
Browse files Browse the repository at this point in the history
  • Loading branch information
priyankchheda committed Feb 4, 2021
1 parent 2c9e998 commit 973362d
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions graph/depth_first_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
""" Depth First Search Graph Traversal Algorithm
Depth-first search (DFS) is an algorithm for traversing or searching
graph data structures. The algorithm starts at the root node (selecting
some arbitrary node as the root node in the case of a graph) and explores
as far as possible along each branch before backtracking.
Time Complexity: O(V+E)
"""

from collections import deque


def depth_first_search_recursion(graph, root):
""" Depth first search graph traversal algorithm - iterative approach """
def dfs_util(graph, vertex, visited):
visited.add(vertex)
print(vertex, end=" ")

for neighbour in graph[vertex]:
if neighbour not in visited:
dfs_util(graph, neighbour, visited)

visited = set()
dfs_util(graph, root, visited)


def depth_first_search_iteration(graph, root):
""" Depth first search graph traversal algorithm - iterative approach """
visited = set()
stack = deque([root])
visited.add(root)

while stack:
vertex = stack.pop()
print(vertex, end=" ")

for neighbour in graph[vertex]:
if neighbour not in visited:
visited.add(neighbour)
stack.append(neighbour)


def main():
""" operational function """
graph = {
"A": ["B", "C", "D"],
"B": ["E"],
"C": ["F", "G"],
"D": ["H"],
"E": ["I"],
"F": ["J"],
"G": [],
"H": [],
"I": [],
"J": []
}
print("Depth First Traversal (Recursion): ", end="")
depth_first_search_recursion(graph, 'A')
print()

print("Depth First Traversal (Iteration): ", end="")
depth_first_search_iteration(graph, 'A')
print()


if __name__ == "__main__":
main()

0 comments on commit 973362d

Please sign in to comment.