How Do You Write a Function for Depth First Search in Graph Traversal?

In summary, the depth-first search algorithm involves traversing through a graph or tree starting from a specified vertex and visiting all of its adjacent nodes before moving on to the next vertex. This is done recursively until all vertices have been visited. The algorithm also keeps track of the time it takes to visit each node. It can be useful for finding paths and cycles in a graph.
  • #1
Henry R
25
0
Hello... how to write the function of depth first search?

Thank you.
 
Technology news on Phys.org
  • #2
Henry R said:
Hello... how to write the function of depth first search?

Thank you.

Hi! (Wave)

That's the algorithm of depth-first-search:

Code:
Depthfirstsearch(G)
   for each v ∈ V
        color[v]=white
        p[v]=NIL
   time=0
   for each v ∈ V
       if color[v]=white then
          Visit(v)

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
 
  • #3
evinda said:
Hi! (Wave)

That's the algorithm of depth-first-search:

Code:
Depthfirstsearch(G)
   for each v ∈ V
        color[v]=white
        p[v]=NIL
   time=0
   for each v ∈ V
       if color[v]=white then
          Visit(v)

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
Hey! Thanks. But, did u get the code by any websites? Oh okay, I understand. Thanks for helping.
 
Last edited:
  • #4
Henry R said:
Hey! Thanks. But, did u get the code by any websites? Oh okay, I understand. Thanks for helping.

I found it in my notes.. (Smile)
 
  • #5
Depth First Search (DFS) is a popular graph traversal algorithm used in computer science and data structures. It is used to traverse a graph or tree data structure, starting from a given source vertex and visiting all of its adjacent vertices before moving on to the next level of vertices.

To write the function of depth first search, we first need to define the data structure for representing a graph. This can be done using an adjacency list or an adjacency matrix. Once we have the graph represented, we can then write the DFS function.

The basic steps for implementing DFS are as follows:

1. Create a visited array to keep track of the visited vertices.

2. Initialize all vertices as not visited.

3. Create a stack to keep track of the vertices to be visited.

4. Push the source vertex onto the stack.

5. While the stack is not empty, pop a vertex from the stack.

6. If the popped vertex has not been visited, mark it as visited and process it.

7. Find all the adjacent vertices of the popped vertex and push them onto the stack.

8. Repeat steps 5 to 7 until the stack is empty.

The pseudo-code for the DFS function can be written as:

DFS(graph, source):

// Initialize the visited array
visited = [False]*graph.num_vertices

// Create a stack and push the source vertex onto it
stack = [source]

// Loop until the stack is empty
while stack:

// Pop a vertex from the stack
vertex = stack.pop()

// If the vertex has not been visited
if not visited[vertex]:

// Mark it as visited and process it
visited[vertex] = True
process(vertex)

// Find all the adjacent vertices and push them onto the stack
for adjacent_vertex in graph.adjacent_vertices(vertex):
stack.push(adjacent_vertex)

The time complexity of DFS is O(V+E), where V is the number of vertices and E is the number of edges in the graph. This is because we visit each vertex and each edge only once.

In conclusion, DFS is a simple yet powerful algorithm for traversing a graph data structure. By implementing the above steps, we can successfully traverse a graph using DFS.
 

FAQ: How Do You Write a Function for Depth First Search in Graph Traversal?

What is a graph in computer science?

A graph in computer science is a data structure that consists of a set of vertices (or nodes) connected by edges. It is used to represent relationships between different objects or entities.

What is depth first search (DFS)?

Depth first search is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It starts at a chosen vertex and explores its neighbors, then moves to the next unvisited vertex until it has visited all vertices in the graph.

How does DFS differ from breadth first search (BFS)?

DFS and BFS are both graph traversal algorithms, but they differ in the order in which they visit vertices. DFS explores deeper into a graph before backtracking, while BFS explores all neighbors of a vertex before moving to the next level of vertices.

What is the time complexity of DFS?

The time complexity of DFS is O(|V| + |E|), where |V| is the number of vertices and |E| is the number of edges in the graph. This means that the time taken for DFS to traverse a graph is directly proportional to the number of vertices and edges in the graph.

In what applications is DFS commonly used?

DFS is commonly used for solving problems that involve finding a path or cycle in a graph, such as maze solving, topological sorting, and finding connected components. It is also used in many graph algorithms, including minimum spanning tree and shortest path algorithms.

Similar threads

Replies
3
Views
978
Replies
14
Views
331
Replies
2
Views
2K
Replies
1
Views
835
Replies
4
Views
2K
Replies
2
Views
3K
Back
Top