-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import java.util.ArrayList; | ||
|
||
public class DFS { | ||
Check warning on line 3 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
Check warning on line 3 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
// 1- --3 | ||
// / | \ | ||
// 0 | 5--6 | ||
// \ | / | ||
// 2-- - 4 | ||
|
||
public static void main(String[] args) { | ||
Check warning on line 10 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
Check warning on line 10 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
int vertex = 7; | ||
Check warning on line 11 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
@SuppressWarnings("unchecked") | ||
Check warning on line 12 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
ArrayList<Edge> graph[] = new ArrayList[vertex]; | ||
Check warning on line 13 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
boolean vis[] = new boolean[vertex]; | ||
Check warning on line 14 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
Check warning on line 14 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
|
||
createGraph(graph); | ||
Check warning on line 16 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
|
||
depthFirst(graph, 0, vis); | ||
System.out.println(); | ||
} | ||
|
||
static class Edge { | ||
int src; | ||
int dest; | ||
|
||
public Edge(int s, int d) { | ||
this.src = s; | ||
this.dest = d; | ||
} | ||
} | ||
|
||
public static void createGraph(ArrayList<Edge> graph[]) { | ||
for (int i = 0; i < graph.length; i++) { | ||
graph[i] = new ArrayList<Edge>(); | ||
} | ||
graph[0].add(new Edge(0, 1)); | ||
graph[0].add(new Edge(0, 2)); | ||
|
||
graph[1].add(new Edge(1, 0)); | ||
graph[1].add(new Edge(1, 3)); | ||
|
||
graph[2].add(new Edge(2, 0)); | ||
graph[2].add(new Edge(2, 4)); | ||
|
||
graph[3].add(new Edge(3, 1)); | ||
graph[3].add(new Edge(3, 4)); | ||
graph[3].add(new Edge(3, 5)); | ||
|
||
graph[4].add(new Edge(4, 2)); | ||
graph[4].add(new Edge(4, 3)); | ||
graph[4].add(new Edge(4, 5)); | ||
|
||
graph[5].add(new Edge(5, 3)); | ||
graph[5].add(new Edge(5, 4)); | ||
graph[5].add(new Edge(5, 6)); | ||
|
||
graph[6].add(new Edge(6, 5)); | ||
|
||
} | ||
|
||
public static void depthFirst(ArrayList<Edge> graph[], int curr, boolean vis[]) { | ||
System.out.println(curr); | ||
vis[curr] = true; | ||
for (int i = 0; i < graph[curr].size(); i++) { | ||
Edge e = graph[curr].get(i); | ||
if (vis[e.dest] == false) { | ||
depthFirst(graph, e.dest, vis); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |