MHB Apply Depth-first Search Algorithm Example

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Search
AI Thread Summary
The discussion revolves around the application of the depth-first search (DFS) algorithm, with a focus on understanding its implementation and verifying the correctness of a specific example. The algorithm initializes all nodes as white, marking them gray upon visitation and black once all adjacent nodes are explored. A participant expresses confusion over the order of node visitation, noting a discrepancy between their results and a book's alphabetical approach. Another participant reassures them that the order may vary as long as the algorithm's principles are followed. However, they point out a mistake in the participant's notes, emphasizing that if a node is marked black, all its neighbors must also be black, which is not the case in the provided example. The conversation highlights the importance of correctly tracking node states throughout the DFS process.
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: 108
  • gra.png
    gra.png
    3.5 KB · Views: 105
  • gra.png
    gra.png
    3.6 KB · Views: 92
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: 103
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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top