Graph Theory(Depth First Search)

Hello, guys in this article I will speak about the important searching way which is used in graph theory.

 Depth-first-search, ok first what is it? and how it works?
Here we have a photo which describes how this algorithm work:
  





In this algorithm, we move from the root of the tree to the depth level then we move up again and down again. as you see in the photo;
  

now one will ask me ok how can I implement that?

I will write an Implementation in C++ language


void dfs(int node)
{
visited[node] = true;

for(int i=0;i< adj[node];i++)
{
int child = adj[node][i];
if (!visited[child]) // To avoid cyclic behavior
dfs(child);
}

}
 
 let's talk about Dfs application which is Dfs EdgeClassification.
(notice that it works in Directed graph)

what is it???
let's look first.


ok in this photo you will notice four types of edges.
the first one is tree Edge: we called that edge is a tree edge when we move from parent to child
(like 1  -> 2)

the second one is Back edge: we called that edge is a Back edge when this edge makes cycle by moving 
from a deep level to a superficial level  
(like 4 -> 2)

the third one is forward edge: we called that edge is a forward edge when this edge moves from superficial node to deep one which is a child to the first one. 
(like 1->8)

the fourth one is cross edge: we called that edge is a cross edge when this edge moves from node to another node in another branch  
 (like 6  -> 3)













Comments

Popular posts from this blog

Leet code (Reverse Integer)

Graph Theory(adjacency matrix,adjacency list)

Recursion(Part 1)