When we try to search the shortest route, There are a lot of way to search.
Today, we take a look two represented search algorithm, DFS and BFS.

First DFS is Depth-First Search.
It start from the root, search whole branch before move to the next branch.

Simply DFS same as the way to find the route in the maze with one’s hand on the wall.
If the traveller arrive the dead end, turn around and find other way on the latest fork.
We usually use this way to search every node.

DFS is recursive format so it should check the visited node to avoid an infinite loof.

We can implement DFS using stack or recursive function.

Pseudo code

void DFS(Node root)
  if root == null: // if the root is null, return
    return
  else:
    visit(root)
    root.visited = true // check the root as visited
    for each Node n in root.adjacent:
      if n.visited == false: // keep search on the unvisited root
        DFS(n)

Python code
stack

Class Node:
  def __init__(node, data):
      node.data = data
      node.child = []

def dfs(node):
    stack = [node, ]
    while True:
        if len(stack) == 0:
            return None
        node = stack.pop()
        print(node.data)
        if node.data == Target:
            return node
        stack.extend(node.child)

recursive

Class Node:
  def __init__(node, data):
      node.data = data
      node.child = []
      node.visited = False

def dfs(node):
    if node.visited == True:
        return
    print(node.data)
    node.visited = True
    if node.data == Target:
        return node
    for child in node.child:
        dfs(child)

BFS is Breadth-First search
It start from the root, visit near nodes first and then go further node.
We usually use this algorithm to find shortest route between two nodes.

To avoid an infinite loof, we should check visited node.
It use the FIFO(First-in First-out). repeated queue structure is used to here.
It is similar to prim, Dijkstra

Pseudo code

void BFS(Node node)
  Queue queue = new queue
  if root.visited == true
  queue.enqueue(root)

  while !queue.isEmpty()
    Node node = queue.dequeue()
    visit(node)

    for each Node n in node.adjacent
      if n.visited == False
        n.visited = True
        queue.enqueue

Python code
queue

Class Node:
  def __init__(node, data):
      node.data = data
      node.child = []

def bfs(node):
    queue = [node, ]
    while True:
        if len(queue) == 0:
            return None
        node = queue.pop(0)
        print(node.data)
        if node.data == Target:
            return node
        queue.extend(node.child)

It is quiet similar to DFS but we could not implement BFS as recursive structure.
Those two search algorithm are quiet primitive method.
But it is worth to use them when we solve some algorithm problem.

According to stackoverflow, we need to choose proper way to solve the problem.

Simply, DFS might cause timeout on problem which have very rare solutions and deep tree.
BFS might cause insufficient memory error on problem which have wide tree.

My personal preference is like this

  1. bfs : find shortest path on simple graph
  2. dfs : find all possible solutions include complicated graph, tree and so on

To implement properly we need to practice, write pseudo code first and implement the algorithm following the flow.

Thank you

Comments