-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.java
More file actions
67 lines (52 loc) · 1.6 KB
/
DFS.java
File metadata and controls
67 lines (52 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
import java.io.*;
public class DFS {
// Class for defining the edges
static class Edge{
int src,dest;
public Edge(int source, int dest){
this.src = source;
this.dest = dest;
}
}
// Class for initializing the graph
static class Graph{
List<List<Integer>> list;
public Graph(List<Edge> edges, int size){
list = new ArrayList<>();
for(int i=0;i<size; i++){
list.add(new ArrayList<>());
}
for(Edge e: edges){
list.get(e.src).add(e.dest);
}
}
}
static void DFS(int v, boolean visited[], Graph graph){
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v + " ");
// Recurse for all the vertices adjacent to this vertex
Iterator<Integer> i = graph.list.get(v).listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFS(n, visited, graph);
}
}
public static void main(String[] args) throws IOException {
int nodes = 4;
ArrayList<Edge> edges = new ArrayList<Edge>(){
{
add(new Edge(0,1)); add(new Edge(0, 2));
add(new Edge(1, 2)); add(new Edge(2,0));
add(new Edge(2,3)); add(new Edge(3,3));
}
};
Graph graph = new Graph(edges, nodes);
boolean visited[] = new boolean[nodes];
// Starting from node 2
DFS(2, visited, graph);
}
}