Apply Depth-first Search Algorithm Example

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Search
Click For Summary
SUMMARY

The discussion focuses on the implementation of the Depth-First Search (DFS) algorithm, specifically addressing the correct traversal order of nodes in a graph. The algorithm initializes all nodes to white, visits each node, marks it gray upon visitation, and finally turns it black after all its neighbors are visited. A key point raised is the discrepancy between the user's traversal starting from node "A" to "G" versus the book's alphabetical order from "A" to "B". The consensus is that the final state should have all nodes black, indicating complete visitation.

PREREQUISITES
  • Understanding of graph theory concepts, particularly nodes and edges.
  • Familiarity with the Depth-First Search algorithm and its implementation.
  • Basic knowledge of algorithmic time complexity.
  • Experience with programming constructs such as loops and conditionals.
NEXT STEPS
  • Implement the Depth-First Search algorithm in Python using recursion.
  • Explore graph representation techniques, such as adjacency lists and matrices.
  • Learn about the iterative implementation of Depth-First Search using a stack.
  • Study the applications of Depth-First Search in solving problems like maze traversal and topological sorting.
USEFUL FOR

Students, computer science enthusiasts, and software developers interested in algorithms, particularly those focusing on graph traversal techniques and their applications in programming.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

I want to apply the algorithm of the depth-first search at an example.

Code:
Depthfirstsearch(G)
   for each v ∈ V
        color[v]<-white
        p[v]<-Ø
   time<-Ø
   for each u ∈ V
       if color[u]=white then
          Visit(u)

Code:
Visit(u)
  color[u]<-gray
  time<-time+1
  d[u]<-time
  for each v ∈ Adj[u]
       if color[v]=white then
          p[v]<-u
          Visit(v)
  color[u]<-black
  time<-time+1
  f[u]<-time

This is the example,at which I want to apply the algorithm:

View attachment 3064

According to my notes,it is like that:

View attachment 3066

but..I found the following:

View attachment 3067

Could you tell me which of them is right? Have I done something wrong? (Thinking)
 

Attachments

  • gra.png
    gra.png
    3.2 KB · Views: 124
  • gra.png
    gra.png
    3.5 KB · Views: 122
  • gra.png
    gra.png
    3.6 KB · Views: 106
Technology news on Phys.org
The book's solution is alphabetical, i.e. starts at node "A" and then proceeds to "B". Yours went from "A" to "G".

I don't think it really matters? It shouldn't if you've got the principle of it. I glanced at your code and it looks fine to me.
 
jza said:
The book's solution is alphabetical, i.e. starts at node "A" and then proceeds to "B". Yours went from "A" to "G".

I don't think it really matters? It shouldn't if you've got the principle of it. I glanced at your code and it looks fine to me.

I think I have done some mistakes.. (Wasntme) I tried it again now.

After $a$,I started again with the node $g$ and now I got:

View attachment 3077

Could you tell me if it is right now? (Thinking)
 

Attachments

  • gra.png
    gra.png
    3.5 KB · Views: 118
evinda said:
I think I have done some mistakes.. (Wasntme) I tried it again now.

After $a$,I started again with the node $g$ and now I got:

https://www.physicsforums.com/attachments/3077

Could you tell me if it is right now? (Thinking)

Hey evinda! ;)

I believe you are right.
The final state is where all nodes are black.

The algorithm works by initializing all nodes to white.
Then each of those nodes is visited and traced to its neighbors.
When a node is visited, it is set to gray, and when all neighbors have been visited, it is set to black.
When all nodes have been visited, they are all black. (Smile)

I believe the state in your notes cannot occur.
Since $g$ is black, it has been visited.
This implies that all of its neighbors have also been visited and should also be black. But $b$ is not black. :eek:
 
I like Serena said:
Hey evinda! ;)

I believe you are right.
The final state is where all nodes are black.

The algorithm works by initializing all nodes to white.
Then each of those nodes is visited and traced to its neighbors.
When a node is visited, it is set to gray, and when all neighbors have been visited, it is set to black.
When all nodes have been visited, they are all black. (Smile)

I believe the state in your notes cannot occur.
Since $g$ is black, it has been visited.
This implies that all of its neighbors have also been visited and should also be black. But $b$ is not black. :eek:

Great!Thank you very much! (Smirk)
 

Similar threads

Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K