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.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public class CycleDetectionBFS {
|
class Main {
|
||||||
|
|
||||||
static class Pair {
|
// helper structure
|
||||||
int first;
|
static class Node{
|
||||||
int second;
|
int node;
|
||||||
|
int parent;
|
||||||
|
|
||||||
Pair(int first, int second) {
|
Node(int node, int parent){
|
||||||
this.first = first;
|
this.node = node;
|
||||||
this.second = second;
|
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;
|
visited[src] = true;
|
||||||
Queue<Pair> q = new LinkedList<>();
|
|
||||||
q.add(new Pair(src, -1));
|
|
||||||
|
|
||||||
while (!q.isEmpty()) {
|
Queue<Node> q = new LinkedList<>();
|
||||||
int node = q.peek().first;
|
q.add(new Node(src, -1));
|
||||||
int parent = q.peek().second;
|
|
||||||
q.remove();
|
|
||||||
|
|
||||||
for (int adjacentNode : adj.get(node)) {
|
while(!q.isEmpty()){
|
||||||
if (!vis[adjacentNode]) {
|
Node cur = q.poll();
|
||||||
vis[adjacentNode] = true;
|
int u = cur.node;
|
||||||
q.add(new Pair(adjacentNode, node));
|
int par = cur.parent;
|
||||||
|
|
||||||
|
for(int v : adj.get(u)){
|
||||||
|
if(!visited[v]){
|
||||||
|
visited[v] = true;
|
||||||
|
q.add(new Node(v, u));
|
||||||
}
|
}
|
||||||
|
else if(v != par){
|
||||||
// everything except this 'else if' statement is normal BFS
|
return true; // cycle
|
||||||
else if (adjacentNode != parent) {
|
|
||||||
return true; // cycle detected
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
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++) {
|
for(int i=0;i<V;i++){
|
||||||
if (!vis[i]) {
|
if(!visited[i]){
|
||||||
if (bfs(i, V, adj, vis)) {
|
if(bfs(i, V, adj, visited)) return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
// ================= DRIVER =================
|
||||||
|
public static void main(String args[]){
|
||||||
|
|
||||||
int V = 5;
|
int V = 5;
|
||||||
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < V; i++) {
|
for(int i=0;i<V;i++) adj.add(new ArrayList<>());
|
||||||
adj.add(new ArrayList<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Undirected graph edges
|
// Undirected graph
|
||||||
adj.get(0).add(1);
|
adj.get(0).add(1);
|
||||||
adj.get(1).add(0);
|
adj.get(1).add(0);
|
||||||
|
|
||||||
|
|
@ -71,12 +72,12 @@ public class CycleDetectionBFS {
|
||||||
adj.get(2).add(3);
|
adj.get(2).add(3);
|
||||||
adj.get(3).add(2);
|
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(1).add(3);
|
||||||
|
|
||||||
adj.get(3).add(4);
|
adj.get(3).add(4);
|
||||||
adj.get(4).add(3);
|
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.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public class CycleDetectionDFS {
|
class Main {
|
||||||
|
|
||||||
static class Pair {
|
// helper structure
|
||||||
|
static class Node{
|
||||||
int node;
|
int node;
|
||||||
int parent;
|
int parent;
|
||||||
|
|
||||||
Pair(int node, int parent) {
|
Node(int node, int parent){
|
||||||
this.node = node;
|
this.node = node;
|
||||||
this.parent = parent;
|
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 u = cur.node;
|
||||||
int parent = current.parent;
|
int par = cur.parent;
|
||||||
|
|
||||||
vis[node] = true;
|
visited[u] = true;
|
||||||
|
|
||||||
for (int adjacentNode : adj.get(node)) {
|
for(int v : adj.get(u)){
|
||||||
|
if(!visited[v]){
|
||||||
if (!vis[adjacentNode]) {
|
if(dfs(new Node(v, u), visited, adj)) return true;
|
||||||
if (dfs(new Pair(adjacentNode, node), vis, adj)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if(v != par){
|
||||||
// everything except this 'else if' statement is normal DFS
|
return true; // cycle
|
||||||
else if (adjacentNode != parent) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
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++) {
|
for(int i=0;i<V;i++){
|
||||||
if (!vis[i]) {
|
if(!visited[i]){
|
||||||
if (dfs(new Pair(i, -1), vis, adj)) {
|
if(dfs(new Node(i, -1), visited, adj)) return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
// ================= DRIVER =================
|
||||||
|
public static void main(String args[]){
|
||||||
|
|
||||||
int V = 5;
|
int V = 5;
|
||||||
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < V; i++) {
|
for(int i=0;i<V;i++) adj.add(new ArrayList<>());
|
||||||
adj.add(new ArrayList<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Undirected graph edges
|
// Undirected graph
|
||||||
adj.get(0).add(1);
|
adj.get(0).add(1);
|
||||||
adj.get(1).add(0);
|
adj.get(1).add(0);
|
||||||
|
|
||||||
|
|
@ -68,12 +64,12 @@ public class CycleDetectionDFS {
|
||||||
adj.get(2).add(3);
|
adj.get(2).add(3);
|
||||||
adj.get(3).add(2);
|
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(1).add(3);
|
||||||
|
|
||||||
adj.get(3).add(4);
|
adj.get(3).add(4);
|
||||||
adj.get(4).add(3);
|
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