πDay 1. DFS of Graph π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given a connected undirected graph represented by a 2D adjacency list adj[][], where adj[i] represents the list of vertices connected to vertex i.
Perform a Depth First Search (DFS) traversal starting from vertex 0, visiting vertices from left to right as per the given adjacency list.
Return a list containing the DFS traversal of the graph.
Note: Traverse nodes in the same order as given in the adjacency list.
Note: Sorry for uploading late, my exam is going on.
π Example Walkthrough:
Example 1:
Input:
adj = [[2, 3, 1],
[0],
[0, 4],
[0],
[2]]Output:
Explanation:
The DFS traversal proceeds as follows:
Start at vertex
0β Output:[0]Visit
2(first neighbor of0) β Output:[0, 2]Visit
4(first neighbor of2) β Output:[0, 2, 4]Backtrack to
2, then backtrack to0, visit3β Output:[0, 2, 4, 3]Backtrack to
0and visit1β Final Output:[0, 2, 4, 3, 1]
Example 2:
Input:
Output:
Explanation:
The DFS traversal proceeds as follows:
Start at vertex
0β Output:[0]Visit
1(first neighbor of0) β Output:[0, 1]Visit
2(first neighbor of1) β Output:[0, 1, 2]Visit
3(first neighbor of2) β Output:[0, 1, 2, 3]Backtrack to
2and visit4β Final Output:[0, 1, 2, 3, 4]
Constraints:
$1 \leq$
adj.size()$\leq 10^4$$1 \leq$
adj[i][j]$\leq 10^4$
π― My Approach:
Recursive DFS (Using Lambda Function)
Algorithm Steps:
Maintain a visited array to track visited nodes.
Implement DFS using recursion and a lambda function.
Start DFS traversal from node
0and recursively visit neighbors in the given order.If a node is unvisited, continue DFS.
Store the DFS traversal sequence in a list.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(V + E), since each vertex and edge is visited once.
Expected Auxiliary Space Complexity: O(V), as we store the visited array and recursive function calls.
π Solution Code
Code (C++)
β‘ Alternative Approaches
π 2οΈβ£ Iterative DFS Approach (Using Stack)
Algorithm Steps:
Use a stack to perform Depth-First Search iteratively.
For each unvisited vertex, push it to the stack and mark it visited.
Process the top element and push its unvisited neighbors in reverse order to maintain DFS order.
Repeat the process until the stack is empty.
π Complexity Analysis:
β Time Complexity: O(V + E) - Each vertex and edge are processed once.
β Space Complexity: O(V) - Due to the stack used in the iterative approach.
β Why This Approach?
It eliminates the risk of stack overflow due to recursion, making it suitable for graphs with a large depth.
π 3οΈβ£ Recursive DFS without Lambda (Traditional Approach)
Algorithm Steps:
Use a helper function for recursion.
Start from an unvisited vertex, mark it as visited, and add to the result.
Recursively call the function for each unvisited neighbor.
π Complexity Analysis:
β Time Complexity: O(V + E) - Each vertex and edge are processed once.
β Space Complexity: O(V) - Due to the recursive call stack.
β Why This Approach?
The traditional recursive approach is simple and intuitive, but it risks stack overflow for deep recursion.
π Comparison of Approaches
Approach
β±οΈ Time Complexity
ποΈ Space Complexity
β Pros
β οΈ Cons
Recursive DFS (Lambda)
π’ O(V + E)
π‘ O(V)
Compact code with lambda functions
Potential stack overflow for deep recursion
Iterative DFS (Stack)
π’ O(V + E)
π‘ O(V)
No recursion issues, avoids stack overflow
Slightly more complex than recursive
Recursive DFS (Traditional)
π’ O(V + E)
π‘ O(V)
Simple and intuitive recursive approach
Risk of stack overflow for large graphs
β Best Choice?
Use Recursive DFS with Lambda for compact and readable code when graph depth is manageable.
Use Iterative DFS to avoid recursion issues when the graph has a large depth.
The Traditional Recursive DFS is good for simple cases but should be avoided for deep recursion.
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated