add directed cycle detection and bipartite graphs

This commit is contained in:
Evan Ferrao 2026-02-15 17:47:11 +05:30 committed by GitHub
parent fdb2d87fb1
commit a13ce0b5d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,134 @@
// https://leetcode.com/problems/is-graph-bipartite/
import java.util.*;
import java.io.*;
class BipartiteGraphUsingBFSandDFS {
// BFS
public static boolean bfs(int node, int color, int colorArray[], ArrayList<ArrayList<Integer>> adj){
Queue<Integer> q = new LinkedList<>();
q.add(node);
colorArray[node] = color;
while(!q.isEmpty()){
Integer currNodeode = q.poll();
for(Integer it : adj.get(currNodeode)){
// if(!visited[it]){
// visited[it] = true;
// q.add(it);
// }
if (colorArray[it]==-1){ // not colored yet (not visited basically)
colorArray[it] = 1 - colorArray[currNodeode];
q.add(it);
} else if (colorArray[it]==colorArray[currNodeode]){
return false;
}
}
}
return true;
}
// DFS
public static boolean dfs(int node, int color, int colorArray[], ArrayList<ArrayList<Integer>> adj){
colorArray[node] = color;
for (Integer it: adj.get(node)){
if (colorArray[it]==-1){ // not colored yet (not visited basically)
if (dfs(it,1-color, colorArray, adj)==false) return false;
} else if (colorArray[it]==color){
return false;
}
}
return true;
}
public static boolean isBipartite(int graph[][]){
int V = graph.length;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i=0;i<V;i++) adj.add(new ArrayList<>());
for(int u=0; u<V; u++){
for(int v : graph[u]){
adj.get(u).add(v);
}
}
int colorArray[] = new int[V];
Arrays.fill(colorArray, -1);
for(int i=0;i<V;i++){
if(colorArray[i] == -1){
// choose one
if(dfs(i, 0, colorArray, adj)==false) return false;
// if(!bfs(i, 0, colorArray, adj)) return false;
}
}
return true;
}
// DRIVER
public static void main(String args[]){
// ============ CASE 1 ============
{
int graph[][] = {
{1,2,3},
{0,2},
{0,1,3},
{0,2}
};
System.out.printf("Case 1: %s\n",
isBipartite(graph) ? "Bipartite" : "Not Bipartite");
}
// ============ CASE 2 ============
{
int graph[][] = {
{1,3},
{0,2},
{1,3},
{0,2}
};
System.out.printf("Case 2: %s\n",
isBipartite(graph) ? "Bipartite" : "Not Bipartite");
}
// ============ CASE 3 ============
{
int graph[][] = {
{1},
{0,2},
{1,3},
{2}
};
System.out.printf("Case 3: %s\n",
isBipartite(graph) ? "Bipartite" : "Not Bipartite");
}
// ============ CASE 4 ============
{
int graph[][] = {
{},
{},
{}
};
System.out.printf("Case 4: %s\n",
isBipartite(graph) ? "Bipartite" : "Not Bipartite");
}
}
}

View File

@ -0,0 +1,136 @@
import java.util.*;
import java.io.*;
class CycleDetectionDirectedGraphDFS {
// DFS
public static boolean dfsCheck(int node, boolean visited[],
boolean pathVis[],
ArrayList<ArrayList<Integer>> adj){
visited[node] = true;
pathVis[node] = true;
for(int next : adj.get(node)){
if(!visited[next]){
if(dfsCheck(next, visited, pathVis, adj)) return true;
}
else if(pathVis[next]){
return true;
}
}
pathVis[node] = false;
return false;
}
public static boolean isCyclic(int V, ArrayList<ArrayList<Integer>> adj){
boolean visited[] = new boolean[V];
boolean pathVis[] = new boolean[V];
for(int i=0;i<V;i++){
if(!visited[i]){
if(dfsCheck(i, visited, pathVis, adj)) return true;
}
}
return false;
}
// helper to create adjacency list
public static ArrayList<ArrayList<Integer>> makeGraph(int V){
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i=0;i<V;i++) adj.add(new ArrayList<>());
return adj;
}
// DRIVER
public static void main(String args[]){
// ================= CASE 1 =================
{
int V = 6;
ArrayList<ArrayList<Integer>> adj = makeGraph(V);
/*
0 → 1 → 2 → 3
↑ ↓
└─────┘
4 → 5
*/
adj.get(0).add(1);
adj.get(1).add(2);
adj.get(2).add(3);
adj.get(3).add(1);
adj.get(4).add(5);
System.out.printf("Case 1: %s\n",
isCyclic(V, adj) ? "Cycle detected" : "No cycle");
}
// ================= CASE 2 =================
{
int V = 4;
ArrayList<ArrayList<Integer>> adj = makeGraph(V);
/*
0 → 1 → 2 → 3
No cycle
*/
adj.get(0).add(1);
adj.get(1).add(2);
adj.get(2).add(3);
System.out.printf("Case 2: %s\n",
isCyclic(V, adj) ? "Cycle detected" : "No cycle");
}
// ================= CASE 3 =================
{
int V = 3;
ArrayList<ArrayList<Integer>> adj = makeGraph(V);
/*
0 → 1
1 → 2
2 → 0
*/
adj.get(0).add(1);
adj.get(1).add(2);
adj.get(2).add(0);
System.out.printf("Case 3: %s\n",
isCyclic(V, adj) ? "Cycle detected" : "No cycle");
}
// ================= CASE 4 =================
{
int V = 5;
ArrayList<ArrayList<Integer>> adj = makeGraph(V);
/*
disconnected, no cycle
0 → 1
2 → 3
4 alone
*/
adj.get(0).add(1);
adj.get(2).add(3);
System.out.printf("Case 4: %s\n",
isCyclic(V, adj) ? "Cycle detected" : "No cycle");
}
}
}