Week 12
Week 12
Description:
Depth-first search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before
backtracking. It starts from a source vertex and visits all the vertices in the graph or until the target vertex is found.
DFS operates by recursively visiting all the vertices in a graph, starting from a given source vertex. The algorithm first
visits the source vertex and then explores as far as possible along each of its neighbors. It does this by visiting the
neighbor with the smallest value first and recursively applying the same process to that vertex.
The DFS algorithm continues this process until it has visited all the vertices in the graph or has found the target vertex.
In a directed graph, DFS can be used to find a path between two vertices or to check if a graph is cyclic.
DFS can be implemented using either recursion or an explicit stack. The algorithm has a time complexity of O (V + E),
where V is the number of vertices in the graph and E is the number of edges. It is widely used in various applications
such as web-crawling, garbage collection, and compilers.
DFS Algorithm
Before learning the python code for Depth-First and its output, let us go through the algorithm it follows for the
same. The recursive method of the Depth-First Search algorithm is implemented using stack. A standard Depth-
First Search implementation puts every vertex of the graph into one in all 2 categories: 1) Visited 2) Not Visited. The
only purpose of this algorithm is to visit all the vertex of the graph avoiding cycles.
1. We will start by putting any one of the graph's vertex on top of the stack.
2. After that take the top item of the stack and add it to the visited list of the vertex.
3. Next, create a list of that adjacent node of the vertex. Add the ones which aren't in the visited list of
vertexes to the top of the stack.
DFS pseudocode
The pseudocode for Depth-First Search in python goes as below: In the init() function, notice that we run the DFS
function on every node because many times, a graph may contain two different disconnected part and therefore to
make sure that we have visited every vertex, we can also run the DFS algorithm at every node.
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u] if
v.visited == false
DFS(G,v)
init() {
For each u ∈ G u.visited = false
For each u ∈ G
DFS(G, u)
}
Output
Let us see how the DFS algorithm works with an example. Here, we will use an undirected graph with 5 vertices.
We begin from the vertex P, the DFS rule starts by putting it within the Visited list and putting all its adjacent vertices
within the stack.
Next, we tend to visit the part at the highest of the stack i.e., Q, and head to its adjacent nodes. Since P has already been
visited, we tend to visit R instead.
Vertex R has the unvisited adjacent vertex in T, therefore we will be adding that to the highest of the stack and
visit it.
At last, we will visit the last component S, it does not have any unvisited adjacent nodes, thus we've completed
the Depth First Traversal of the graph.
Breadth-First Search (BFS) is an algorithm used for traversing graphs or trees. Traversing means visiting each
node of the graph. Breadth-First Search is a recursive algorithm to search all the vertices of a graph or a tree.
BFS in python can be implemented by using data structures like a dictionary and lists. Breadth-First
Search in tree and graph is almost the same. The only difference is that the graph may contain cycles, so we
may traverse to the same node again.
BFS Algorithm
Before learning the python code for Breadth-First and its output, let us go through the algorithm it follows
for the same. We can take the example of Rubik’s Cube for the instance. Rubik’s Cube is seen as searching
for a path to convert it from a full mess of colors to a single color. So, comparing the Rubik’s Cube to the
graph, we can say that the possible state of the cube is corresponding to the nodes of the graph and the
possible actions of the cube is corresponding to the edges of the graph.
As breadth-first search is the process of traversing each node of the graph, a standard BFS algorithm
traverses each vertex of the graph into two parts: 1) Visited 2) Not Visited. So, the purpose of the algorithm is
to visit all the vertex while avoiding cycles.
BFS starts from a node, then it checks all the nodes at distance one from the beginning node, then it checks
all the nodes at distance two, and so on. So as to recollect the nodes to be visited, BFS uses a queue.
The steps of the algorithm work as follow:
1. Start by putting any one of the graph’s vertices at the back of the queue.
2. Now 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 those which are not within the visited list to the
rear of the queue.
4. Keep continuing steps two and three till the queue is empty.
Many times, a graph may contain two different disconnected parts and therefore to make sure that we have
visited every vertex, we can also run the BFS algorithm at every node.
BFS pseudocode
Output:
2031
Example
Let us see how this algorithm works with an example. Here, we will use an undirected graph with 5 vertices.
We begin from the vertex P, the BFS algorithmic program starts by putting it within the Visited list and puts all its
adjacent vertices within the stack.
Next, we have a tendency to visit the part at the front of the queue i.e. Q and visit its adjacent nodes. Since P
has already been visited, we have a tendency to visit R instead.
Vertex R has an unvisited adjacent vertex in T, thus we have a tendency to add that to the rear
of the queue and visit S, which is at the front of the queue.
Now, only T remains within the queue since the only adjacent node of S i.e. P is already
visited. We have a tendency to visit it.
Since the queue is empty, we've completed the Traversal of the graph.