You are on page 1of 7

DFS

Descriotion:

Depth-first search is an algorithm for traversing or searching tree or 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.

So the basic idea is to start from the root or any arbitrary node and mark the node and
move to the adjacent unmarked node and continue this loop until there is no unmarked
adjacent node. Then backtrack and check for other unmarked nodes and traverse them.
Finally, print the nodes in the path

Code:
// DFS algorithm in C++

#include <iostream>
#include <list>
using namespace std;

class Graph {
int numVertices;
list<int> *adjLists;
bool *visited;

public:
Graph(int V);
void addEdge(int src, int dest);
void DFS(int vertex);
};

// Initialize graph
Graph::Graph(int vertices) {
numVertices = vertices;
adjLists = new list<int>[vertices];
visited = new bool[vertices];
}
// Add edges
void Graph::addEdge(int src, int dest) {
adjLists[src].push_front(dest);
}

// DFS algorithm
void Graph::DFS(int vertex) {
visited[vertex] = true;
list<int> adjList = adjLists[vertex];

cout << vertex << " ";

list<int>::iterator i;
for (i = adjList.begin(); i != adjList.end(); ++i)
if (!visited[*i])
DFS(*i);
}

int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);

g.DFS(2);

return 0;
}

Examples: 1

Desciption: In this example if we run this code, the code can give us timestamps
according to the given input vertex. When the code is running if any vertex is not visited
yet then the color of the vertex will be white, if the vertex is visited but of its branches is
not discovered yet then the color of the vertex will be gray, and finally if the all the
branches the any vartex’s is discovered then the color of the vertex will be black.

Graph:
Psudocode:
Data: color[V], time,
prev[V],d[V], f[V]

DFS(G) // where prog starts


{
for each vertex u ∈ V
{
color[u] = WHITE;
prev[u]=NIL;
f[u]=inf; d[u]=inf;
}
time = 0;
for each vertex u ∈ V
if (color[u] == WHITE)
DFS_Visit(u);
}
DFS_Visit(u)
{
color[u] = GREY;
time = time+1;
d[u] = time;
for each v ∈ Adj[u]
{
if(color[v] == WHITE){
prev[v]=u;
DFS_Visit(v);}
}
color[u] = BLACK;
time = time+1;
f[u] = time;
}

Input:
G = (V, E) (No source vertex given!)

Goal:
Explore the edges of G to “discover” every vertex in V starting at the most
current visited node
Search may be repeated from multiple sources

Output:
2 timestamps on each vertex.
d[v] = discovery time
f[v] = finishing time (done with examining v’s adjacency list)
Depth-first forest

Example: 2

Description: Suppose there is graph where direction of rutes between Dhaka,


Rangpur, Barishal, Bhola, Chittagong is given. We can find rutes from one place to
another using dfs algorithm

Graph:
0 4

Dhaka Sylhe
t

Barishal

1 3

Bhola Chittagon
g

Psudocode:

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)
}

Input:
We take dhaka or 0 as an input which will be a source vertex

Goal:
Find the different routes of the districts which is in the graph.
Output:
We will get a timestamp or path according to the input district. The output will be
0,4,2,3,1 or Dhaka,sylhet, barishal, bhola

You might also like