You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Depth-first search is an algorithm for traversing or searching tree or graph data structures.
6
+
The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.
7
+
So the basic idea is to start from the root or any arbitrary node and mark the node and move to the adjacent unmarked node and continue this loop until there is no unmarked adjacent node.
8
+
Then backtrack and check for other unmarked nodes and traverse them.
9
+
Finally, print the nodes in the path.
10
+
*/
11
+
12
+
/*Algorithm:
13
+
1. Create a recursive function that takes the index of the node and a visited array.
14
+
2. Mark the current node as visited and print the node.
15
+
3. Traverse all the adjacent and unmarked nodes and call the recursive function with the index of the adjacent node.
16
+
*/
17
+
18
+
publicclassDFS {
19
+
privateintV;
20
+
privateLinkedList<Integer> adj[];
21
+
22
+
DFS(intv){
23
+
V = v;
24
+
adj = newLinkedList[v];
25
+
for(inti = 0; i < v; ++i){
26
+
adj[i] = newLinkedList<>();
27
+
}
28
+
}
29
+
30
+
voidaddEdge(intv, intw){
31
+
adj[v].add(w);
32
+
}
33
+
34
+
voidDFSUtil(intv, boolean[] visited){
35
+
visited[v] = true;
36
+
System.out.print(v + " ");
37
+
38
+
for (intn : adj[v]) {
39
+
if (!visited[n])
40
+
DFSUtil(n, visited);
41
+
}
42
+
}
43
+
44
+
voidDFS(intv){
45
+
boolean[] visited = newboolean[V];
46
+
DFSUtil(v, visited);
47
+
}
48
+
49
+
publicstaticvoidmain(String[] args) {
50
+
DFSg = newDFS(4);
51
+
52
+
g.addEdge(0, 1);
53
+
g.addEdge(0, 2);
54
+
g.addEdge(1, 2);
55
+
g.addEdge(2, 0);
56
+
g.addEdge(2, 3);
57
+
g.addEdge(3, 3);
58
+
59
+
System.out.println("Following is Depth First Traversal " + "(starting from vertex 2)");
* Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
8
+
Note: In a path, no cell can be visited more than one time.
9
+
10
+
Example 1:
11
+
12
+
Input:
13
+
N = 4
14
+
m[][] = {{1, 0, 0, 0},
15
+
{1, 1, 0, 1},
16
+
{1, 1, 0, 0},
17
+
{0, 1, 1, 1}}
18
+
Output:
19
+
DDRDRR DRDDRR
20
+
Explanation:
21
+
The rat can reach the destination at
22
+
(3, 3) from (0, 0) by two paths - DRDDRR
23
+
and DDRDRR, when printed in sorted order
24
+
we get DDRDRR DRDDRR.
25
+
Example 2:
26
+
Input:
27
+
N = 2
28
+
m[][] = {{1, 0},
29
+
{1, 0}}
30
+
Output:
31
+
-1
32
+
Explanation:
33
+
No path exists and destination cell is
34
+
blocked.*/
35
+
36
+
/*
37
+
* Approach =>
38
+
* 1. Start from the initial index (i.e. (0,0)) and look for the valid moves through the adjacent cells in the order Down->Left->Right->Up (to get the sorted paths) in the grid.
39
+
* 2. If the move is possible, then move to that cell while storing the character corresponding to the move(D,L,R,U) and again start looking for the valid move until the last index (i.e. (n-1,n-1)) is reached.
40
+
* 3. Also, keep on marking the cells as visited and when we traversed all the paths possible from that cell,
41
+
* then unmark that cell for other different paths and remove the character from the path formed.
42
+
* 4. As the last index of the grid(bottom right) is reached, then store the traversed path.
0 commit comments