DFS Lab
DFS Lab
Activity Outcomes:
This lab teaches you the following topics:
How to represent problems in terms of state space graph
How to find a solution of those problems using Depth First Search.
Instructor Note:
As pre-lab activity, read Chapter 3 from the book (Artificial Intelligence, A Modern Approach by Peter
Norvig, 3rd edition) to know the basics of search algorithms.
1) Useful Concepts:
In the previous lab we studied breadth first search, which is the most naïve way of searching trees. The
path returned by breadth search is not optimal. Uniform cost search always returns the optimal path. In
this lab students will be able to implement a uniform cost search approach. Students will also be able to
apply depth first search to their problems
Pseudocode
DFS-recursive(G, s):
mark s as visited
for all neighbours w of s in Graph G:
if w is not visited:
DFS-recursive(G, w)
Input File
6
4
1 2
2 3
1 3
45
Code
#include <iostream>
#include <vector>
using namespace std;
void dfs(int s) {
visited[s] = true;
for(int i = 0;i < adj[s].size();++i) {
if(visited[adj[s][i]] == false)
dfs(adj[s][i]);
}
}
void initialize() {
for(int i = 0;i < 10;++i)
visited[i] = false;
}
int main() {
int nodes, edges, x, y, connectedComponents = 0;
cin >> nodes; //Number of nodes
cin >> edges; //Number of edges
for(int i = 0;i < edges;++i) {
cin >> x >> y;
//Undirected Graph
adj[x].push_back(y); //Edge from vertex x to vertex y
adj[y].push_back(x); //Edge from vertex y to vertex x
}
Output
Number of connected components: 3
Activity 1:
Consider a toy problem that can be represented as a following graph. How would you represent this
graph in python?
Solution:
Remember a node of a tree can be represented by four attributes. We consider node as a class having
four attributes namely,
1. State of the node
2. Parent of the node
3. Actions applicable to that node
4. The total path cost of that node starting f rom the initial state until that particular node is
reached
We can now implement this class in a dictionary. This dictionary will represent our state space graph.
As we will traverse through the graph, we will keep updating parent and cost of each node.
For the graph in previous activity, imagine node A as starting node and your goal is to reach F.
Keeping depth first search in mind, describe a sequence of actions that you must take to reach that goal
state.
Remember that we can implement depth first search simply by using LIFO approach instead of FIFO
nodes (nodes without
children) in explored set.
Now the function definition is complete which can be called as follows,
Notice the difference in two portions of the code between breadth first search and depth first search. In
the first we just pop out the last entry from the queue and in the 2 nd difference we delete leaf nodes
from the graph.
Calling DFS() will return the following solution, ['A', 'E', 'D']
Activity 2:
Change initial state to D and set goal state as C. What will be resulting path of BF S search? What
will be the sequence of nodes explored?
Solution:
Final path is ['D', 'B', 'A', 'C'] Explored node sequence is D, E, A.
Lab Task 1:
Imagine going from Arad to Bucharest in the following map. Your goal is to minimize the distance
mentioned in the map during your travel. Implement a depth first search to find the corresponding
path.
Lab Task 2:
Generate a list of possible words from a character matrix
Given an M × N boggle board, find a list of all possible words that can be formed by a sequence of
adjacent characters on the board.
We are allowed to search a word in all eight possible directions, i.e., North, West, South, East, North-
East, North-West, South-East, South-West, but a word should not have multiple instances of the same
cell.
Consider the following the traditional 4 × 4
boggle board. If the input dictionary is [START,
NOTE, SAND, STONED], the valid words are
[NOTE, SAND, STONED].
Boggle Board