From e29476556cda2f2db318699aff582122c46ab353 Mon Sep 17 00:00:00 2001 From: DBasu2610 Date: Mon, 30 Sep 2024 12:27:18 +0530 Subject: [PATCH] formatted DSF.java --- Solved-Problems/GraphTheory/DFS.java | 80 +++++++++++++--------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/Solved-Problems/GraphTheory/DFS.java b/Solved-Problems/GraphTheory/DFS.java index aab6142..62efa33 100644 --- a/Solved-Problems/GraphTheory/DFS.java +++ b/Solved-Problems/GraphTheory/DFS.java @@ -1,18 +1,17 @@ package DSA.DSA.SolvedProblems.GraphTheory; - import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class DFS { - // 1- --3 - // / | \ - // 0 | 5--6 - // \ | / - // 2-- - 4 - + // 1- --3 + // / | \ + // 0 | 5--6 + // \ | / + // 2-- - 4 + public static void main(String[] args) { int vertex = 7; ArrayList graph[] = new ArrayList[vertex]; @@ -20,62 +19,59 @@ public static void main(String[] args) { createGraph(graph); - depthFirst(graph,0,vis); + depthFirst(graph, 0, vis); System.out.println(); } - - static class Edge { + static class Edge { int src; int dest; + public Edge(int s, int d) { this.src = s; this.dest = d; - } + } } public static void createGraph(ArrayList graph[]) { for (int i = 0; i < graph.length; i++) { graph[i] = new ArrayList(); } - 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)); - - - + 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 graph[],int curr, boolean vis[]) { + public static void depthFirst(ArrayList 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); + if (vis[e.dest] == false) { + depthFirst(graph, e.dest, vis); } } - } - -} + } +}