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: 110
  • gra.png
    gra.png
    3.5 KB · Views: 106
  • 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: 105
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)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top