mirror of https://github.com/evanferrao/dsa
keep code consistent
This commit is contained in:
parent
046371022d
commit
c583b04984
|
|
@ -1,67 +1,68 @@
|
|||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class CycleDetectionBFS {
|
||||
class Main {
|
||||
|
||||
static class Pair {
|
||||
int first;
|
||||
int second;
|
||||
// helper structure
|
||||
static class Node{
|
||||
int node;
|
||||
int parent;
|
||||
|
||||
Pair(int first, int second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
Node(int node, int parent){
|
||||
this.node = node;
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean bfs(int src, int V, ArrayList<ArrayList<Integer>> adj,boolean[] vis) {
|
||||
public static boolean bfs(int src, int V,
|
||||
ArrayList<ArrayList<Integer>> adj,
|
||||
boolean visited[]){
|
||||
|
||||
vis[src] = true;
|
||||
Queue<Pair> q = new LinkedList<>();
|
||||
q.add(new Pair(src, -1));
|
||||
visited[src] = true;
|
||||
|
||||
while (!q.isEmpty()) {
|
||||
int node = q.peek().first;
|
||||
int parent = q.peek().second;
|
||||
q.remove();
|
||||
Queue<Node> q = new LinkedList<>();
|
||||
q.add(new Node(src, -1));
|
||||
|
||||
for (int adjacentNode : adj.get(node)) {
|
||||
if (!vis[adjacentNode]) {
|
||||
vis[adjacentNode] = true;
|
||||
q.add(new Pair(adjacentNode, node));
|
||||
while(!q.isEmpty()){
|
||||
Node cur = q.poll();
|
||||
int u = cur.node;
|
||||
int par = cur.parent;
|
||||
|
||||
for(int v : adj.get(u)){
|
||||
if(!visited[v]){
|
||||
visited[v] = true;
|
||||
q.add(new Node(v, u));
|
||||
}
|
||||
|
||||
// everything except this 'else if' statement is normal BFS
|
||||
else if (adjacentNode != parent) {
|
||||
return true; // cycle detected
|
||||
else if(v != par){
|
||||
return true; // cycle
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
|
||||
public static boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj){
|
||||
|
||||
boolean[] vis = new boolean[V];
|
||||
boolean visited[] = new boolean[V];
|
||||
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (!vis[i]) {
|
||||
if (bfs(i, V, adj, vis)) {
|
||||
return true;
|
||||
}
|
||||
for(int i=0;i<V;i++){
|
||||
if(!visited[i]){
|
||||
if(bfs(i, V, adj, visited)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ================= DRIVER =================
|
||||
public static void main(String args[]){
|
||||
|
||||
int V = 5;
|
||||
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < V; i++) {
|
||||
adj.add(new ArrayList<>());
|
||||
}
|
||||
for(int i=0;i<V;i++) adj.add(new ArrayList<>());
|
||||
|
||||
// Undirected graph edges
|
||||
// Undirected graph
|
||||
adj.get(0).add(1);
|
||||
adj.get(1).add(0);
|
||||
|
||||
|
|
@ -71,12 +72,12 @@ public class CycleDetectionBFS {
|
|||
adj.get(2).add(3);
|
||||
adj.get(3).add(2);
|
||||
|
||||
adj.get(3).add(1); // creates a cycle
|
||||
adj.get(3).add(1);
|
||||
adj.get(1).add(3);
|
||||
|
||||
adj.get(3).add(4);
|
||||
adj.get(4).add(3);
|
||||
|
||||
System.out.printf(isCycle(V, adj) ? "Cycle detected" : "No cycle detected");
|
||||
System.out.printf(isCycle(V, adj) ? "Cycle detected\n" : "No cycle detected\n");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,64 +1,60 @@
|
|||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class CycleDetectionDFS {
|
||||
class Main {
|
||||
|
||||
static class Pair {
|
||||
// helper structure
|
||||
static class Node{
|
||||
int node;
|
||||
int parent;
|
||||
|
||||
Pair(int node, int parent) {
|
||||
Node(int node, int parent){
|
||||
this.node = node;
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean dfs(Pair current, boolean[] vis, ArrayList<ArrayList<Integer>> adj) {
|
||||
public static boolean dfs(Node cur, boolean visited[],
|
||||
ArrayList<ArrayList<Integer>> adj){
|
||||
|
||||
int node = current.node;
|
||||
int parent = current.parent;
|
||||
int u = cur.node;
|
||||
int par = cur.parent;
|
||||
|
||||
vis[node] = true;
|
||||
visited[u] = true;
|
||||
|
||||
for (int adjacentNode : adj.get(node)) {
|
||||
|
||||
if (!vis[adjacentNode]) {
|
||||
if (dfs(new Pair(adjacentNode, node), vis, adj)) {
|
||||
return true;
|
||||
}
|
||||
for(int v : adj.get(u)){
|
||||
if(!visited[v]){
|
||||
if(dfs(new Node(v, u), visited, adj)) return true;
|
||||
}
|
||||
|
||||
// everything except this 'else if' statement is normal DFS
|
||||
else if (adjacentNode != parent) {
|
||||
return true;
|
||||
else if(v != par){
|
||||
return true; // cycle
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
|
||||
public static boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj){
|
||||
|
||||
boolean[] vis = new boolean[V];
|
||||
boolean visited[] = new boolean[V];
|
||||
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (!vis[i]) {
|
||||
if (dfs(new Pair(i, -1), vis, adj)) {
|
||||
return true;
|
||||
}
|
||||
for(int i=0;i<V;i++){
|
||||
if(!visited[i]){
|
||||
if(dfs(new Node(i, -1), visited, adj)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ================= DRIVER =================
|
||||
public static void main(String args[]){
|
||||
|
||||
int V = 5;
|
||||
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < V; i++) {
|
||||
adj.add(new ArrayList<>());
|
||||
}
|
||||
for(int i=0;i<V;i++) adj.add(new ArrayList<>());
|
||||
|
||||
// Undirected graph edges
|
||||
// Undirected graph
|
||||
adj.get(0).add(1);
|
||||
adj.get(1).add(0);
|
||||
|
||||
|
|
@ -68,12 +64,12 @@ public class CycleDetectionDFS {
|
|||
adj.get(2).add(3);
|
||||
adj.get(3).add(2);
|
||||
|
||||
adj.get(3).add(1); // creates a cycle
|
||||
adj.get(3).add(1);
|
||||
adj.get(1).add(3);
|
||||
|
||||
adj.get(3).add(4);
|
||||
adj.get(4).add(3);
|
||||
|
||||
System.out.printf(isCycle(V, adj) ? "Cycle detected" : "No cycle detected");
|
||||
System.out.printf(isCycle(V, adj) ? "Cycle detected\n" : "No cycle detected\n");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue