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.
Examples
Example 1:
Input:
adj = [[2, 3, 1],
[0],
[0, 4],
[0],
[2]]
Output:
[0, 2, 4, 3, 1]
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
0
and visit1
β Final Output:[0, 2, 4, 3, 1]
Example 2:
Input:
adj = [[1, 2],
[0, 2],
[0, 1, 3, 4],
[2],
[2]]
Output:
[0, 1, 2, 3, 4]
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
2
and 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
0
and 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.
Code (C++)
class Solution {
public:
vector<int> dfs(vector<vector<int>>& adj) {
int V = adj.size();
vector<int> res, vis(V, 0);
function<void(int)> traverse = [&](int v) {
vis[v] = 1;
res.push_back(v);
for (int u : adj[v])
if (!vis[u]) traverse(u);
};
for (int i = 0; i < V; i++)
if (!vis[i]) traverse(i);
return res;
}
};
Code (Java)
class Solution {
public ArrayList<Integer> dfs(ArrayList<ArrayList<Integer>> adj) {
ArrayList<Integer> r = new ArrayList<>();
boolean[] v = new boolean[adj.size()];
for (int i = 0; i < adj.size(); i++) if (!v[i]) go(i, adj, v, r);
return r;
}
void go(int i, ArrayList<ArrayList<Integer>> a, boolean[] v, ArrayList<Integer> r) {
v[i] = true;
r.add(i);
for (int j : a.get(i)) if (!v[j]) go(j, a, v, r);
}
}
Code (Python)
class Solution:
def dfs(self, adj):
r, v = [], [False] * len(adj)
def go(i):
v[i] = True
r.append(i)
for j in adj[i]:
if not v[j]:
go(j)
for i in range(len(adj)):
if not v[i]:
go(i)
return r
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