Apply Depth-first Search Algorithm Example

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Search
In summary, 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.
  • #1
evinda
Gold Member
MHB
3,836
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: 84
  • gra.png
    gra.png
    3.5 KB · Views: 79
  • gra.png
    gra.png
    3.6 KB · Views: 69
Technology news on Phys.org
  • #2
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.
 
  • #3
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: 75
  • #4
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:
 
  • #5
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)
 

FAQ: Apply Depth-first Search Algorithm Example

What is a depth-first search algorithm?

A depth-first search algorithm is a graph traversal method that starts at a designated node and explores as far as possible along each branch before backtracking to explore other branches. It is commonly used to search for a path between two nodes in a graph.

How does a depth-first search algorithm work?

A depth-first search algorithm works by maintaining a stack of visited nodes and their adjacent nodes. It starts at a designated node and visits its first unvisited adjacent node. It then adds that node to the stack and repeats the process until all nodes have been visited or a target node is found. If a target node is not found, the algorithm backtracks by popping nodes off the stack and visiting their unvisited adjacent nodes.

What is an example of applying a depth-first search algorithm?

An example of applying a depth-first search algorithm is finding a path between two cities on a map. The cities would be represented as nodes in a graph and the roads connecting them would be represented as edges. The algorithm would start at one city and explore its adjacent cities, adding them to the stack. If the target city is not found, it would backtrack and explore other adjacent cities until the target city is found or all cities have been visited.

What is the time complexity of a depth-first search algorithm?

The time complexity of a depth-first search algorithm is O(V + E), where V is the number of vertices (nodes) in the graph and E is the number of edges. This means that the algorithm will visit each vertex and each edge once, making it a linear time algorithm.

What are the advantages of using a depth-first search algorithm?

One advantage of using a depth-first search algorithm is that it requires less memory compared to other graph traversal methods, such as breadth-first search. This is because it only needs to store the current path being explored in the stack, while other methods may need to store all visited nodes in a queue. Additionally, depth-first search can often find a solution or path more quickly in certain types of graphs, such as a maze, where there may be many dead ends to backtrack from.

Similar threads

Back
Top