Horje
Time and Space Complexity of Depth First Search (DFS)

The time complexity of Depth First Search (DFS) is O(V + E), where V is the number of vertices and E is the number of edges. The space complexity is O(V) for a recursive implementation.

Depth First Search (DFS)

Below are the detailed explanation of Time and Space Complexity of Depth First Search (DFS):

Time Complexity of Depth First Search (DFS):

Best Case of Depth First Search: O(V + E)

  • The best-case time complexity of DFS occurs when the target node is found quickly after exploring only a few vertices and edges.
  • In this scenario, the algorithm may terminate early without having to visit all vertices and edges.
  • Thus, the time complexity is O(V + E), where V is the number of vertices and E is the number of edges.

Average Case of Depth First Search: O(V + E)

  • The average-case time complexity of DFS is also O(V + E).
  • This complexity holds across various graph structures and densities.
  • DFS explores vertices and edges in a depth-first manner, which typically results in linear time complexity proportional to the sum of vertices and edges.

Worst Case of Depth First Search: O(V + E)

  • The worst-case time complexity of DFS also remains O(V + E).
  • In the worst case, DFS explores all vertices and edges reachable from the source node.
  • The algorithm systematically traverses each edge and explores all vertices in a depth-first manner.
  • Therefore, the time complexity is O(V + E) as it traverses all vertices and edges in the graph.

Auxiliary Space Complexity of Depth First Search (DFS):

The auxiliary space complexity of Depth-First Search (DFS) algorithm is O(V), where V is the number of vertices in the graph, this is due to the recursion stack or visited array.

Here’s why the auxiliary space complexity is O(V):

Recursion Stack:

  • DFS uses a function call stack to track vertices.
  • Space complexity depends on the maximum recursion depth.
  • Maximum depth in a graph with V vertices is O(V).

Visited Array (Optional):

  • May use a visited array to mark visited vertices.
  • Size equals the number of vertices (V).



Reffered: https://www.geeksforgeeks.org


Algorithms

Related
Time and Space Complexity of Insertion Sort Time and Space Complexity of Insertion Sort
Randomized Algorithms Randomized Algorithms
Building Data Pipelines with Google Cloud Dataflow: ETL Processing Building Data Pipelines with Google Cloud Dataflow: ETL Processing
How to Use Docker Content Trust to Verify Docker Container Images How to Use Docker Content Trust to Verify Docker Container Images
Design and Analysis of Algorithms Design and Analysis of Algorithms

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13