Document
Document
1.
Comparison Chart
to the solution.
A* search search
Definition of Informed search
Traversal means visiting all the nodes of a graph. Breadth first traversal or Breadth first Search
is a recursive algorithm for searching all the vertices of a graph or tree data structure. In this
article, you will learn with the help of examples the BFS algorithm, BFS pseudocode and the
code of the breadth first search algorithm with implementation in C++, C, Java and Python
programs.
BFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list
to the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so to make sure that we cover every
vertex, we can also run the BFS algorithm on every node
BFS example
Let's see how the Breadth First Search algorithm works with an example. We use an undirected
graph with 5 vertices.
We start from vertex 0, the BFS algorithm starts by putting it in the Visited list and putting all its
adjacent vertices in the stack.
Next, we visit the element at the front of queue i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.
4, so we add that to the back of the queue and visit 3, which is at the front of the queue.
Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already visited. We visit
it.
Since the queue is empty, we have completed the Depth First Traversal of the graph.
BFS pseudocode
create a queue Q
while Q is non-empty
BFS code
The code for the Breadth First Search Algorithm with an example is shown below. The code has
been simplified so that we can focus on the algorithm rather than other details.
BFS in Python
5. import collections
6.
7. def bfs(graph, root):
8. visited, queue = set(), collections.deque([root])
9. visited.add(root)
10. while queue:
11. vertex = queue.popleft()
12. for neighbour in graph[vertex]:
13. if neighbour not in visited:
14. visited.add(neighbour)
15. queue.append(neighbour)
16.
17.
18. if __name__ == '__main__':
19. graph = {0: [1, 2], 1: [2], 2: [3], 3: [1,2]}
20. breadth_first_search(graph, 0)
DFS algorithm
Traversal means visiting all the nodes of a graph. Depth first traversal or Depth first Search is a
recursive algorithm for searching all the vertices of a graph or tree data structure. In this article,
you will learn with the help of examples the DFS algorithm, DFS pseudocode and the code of
the depth first search algorithm with implementation in C++, C, Java and Python programs.
DFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two categories:
21. Visited
22. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
23. Start by putting any one of the graph's vertices on top of a stack.
24. Take the top item of the stack and add it to the visited list.
25. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list
to the top of stack.
26. Keep repeating steps 2 and 3 until the stack is empty.
DFS example
Let's see how the Depth First Search algorithm works with an example. We use an undirected
graph with 5 vertices.
We start from vertex 0, the DFS algorithm starts by putting it in the Visited list and putting all its
adjacent vertices in the stack.
Next, we visit the element at the top of stack i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
DFS code
The code for the Depth First Search Algorithm with an example is shown below. The code has
been simplified so that we can focus on the algorithm rather than other details.
DFS in Python
27. def dfs(graph, start, visited=None):
28. if visited is None:
29. visited = set()
30. visited.add(start)
31. print(start)
32. for next in graph[start] - visited:
33. dfs(graph, next, visited)
34. return visited
35.
36. graph = {'0': set(['1', '2']),
37. '1': set(['0', '3', '4']),
38. '2': set(['0']),
39. '3': set(['1']),
40. '4': set(['2', '3'])}
41.
42. dfs(graph, '0')