Skip to content

Commit ff05116

Browse files
Added Code
1 parent 0f4f3ee commit ff05116

File tree

4 files changed

+292
-19
lines changed

4 files changed

+292
-19
lines changed

34. Graph 03 - Undirected Graph Problems/Bipartite Graph.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
1-
def is_cycle(node,visited,parent,flip):
2-
global arr,n
1+
def isBipertite(arr, n):
2+
# Code here
3+
for i in range(n):
4+
ans = check(arr,n,i)
5+
if not ans:
6+
return False
7+
return True
8+
39

4-
visited[node] = flip
5-
flip = (flip+1)%2
10+
def check(arr,n,src):
611

7-
subarr = arr[node]
8-
for c in range(n):
9-
if subarr[c]==1 and visited[c]==0:
10-
return is_cycle(c,visited,node,flip)
11-
elif subarr[c]==1 and c!=parent:
12-
if visited[c]==visited[node]:
13-
return False
12+
13+
color = [-1]*n
14+
color[src] = 1
15+
16+
queue = []
17+
queue.append(src)
18+
19+
while len(queue)!=0:
20+
21+
u = queue.pop()
22+
23+
if arr[u][u]==1:
24+
return False
25+
26+
for i in range(n):
27+
28+
if arr[u][i]==1 and color[i]==-1:
29+
color[i] = (color[u]+1)%2
30+
queue.append(i)
1431

32+
elif arr[u][i]==1 and color[i]==color[u]:
33+
return False
34+
1535
return True
16-
17-
1836

1937

20-
def is_bipartite(arr):
21-
visited = [0]*len(arr)
22-
return is_cycle(0,visited,-1,1)
38+
graph = [[0, 1, 0, 1],
39+
[1, 0, 1, 0],
40+
[0, 1, 0, 1],
41+
[1, 0, 1, 0]
42+
]
2343

44+
print(isBipertite(graph,4))
2445

2546

26-
n = 3
27-
arr = [[0, 1, 0],[0, 0, 1], [1, 0, 0]]
28-
print(is_bipartite(arr))
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.*;
2+
3+
public class Bipartite_Graph {
4+
5+
class Graph{
6+
7+
ArrayList<ArrayList<Integer>> arr;
8+
int V;
9+
10+
Graph(int V){
11+
this.V =V;
12+
this.arr = new ArrayList<>();
13+
for(int i=0;i<V;i++) {
14+
arr.add(new ArrayList<>());
15+
}
16+
}
17+
18+
public void add_edge(int x, int y) {
19+
this.arr.get(x).add(y);
20+
this.arr.get(y).add(x);
21+
}
22+
23+
public void display() {
24+
25+
for(int i=0; i<V; i++) {
26+
System.out.print(String.valueOf(i));
27+
28+
for(int j=0;j<this.arr.get(i).size(); j++) {
29+
System.out.print(" -> "+this.arr.get(i).get(j));
30+
}
31+
System.out.println();
32+
}
33+
}
34+
35+
public boolean cycle_helper(int node,int[] visited, int parent) {
36+
37+
visited[node] = 1;
38+
for(int c : this.arr.get(node)) {
39+
if(visited[c]!=1) {
40+
return cycle_helper(c, visited, node);
41+
} else if(c!=parent) {
42+
return true;
43+
}
44+
}
45+
return false;
46+
47+
}
48+
49+
public boolean contains_cycle() {
50+
int[] visited = new int[this.V];
51+
52+
return cycle_helper(0,visited,-1);
53+
}
54+
55+
56+
57+
}
58+
59+
60+
public static void main(String[] args) {
61+
// TODO Auto-generated method stub
62+
63+
int[] arr = new int[] {0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0};
64+
65+
66+
67+
}
68+
69+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.ArrayList;
2+
3+
4+
public class Cycle_Detection_Undirected_Graph {
5+
6+
class Graph{
7+
8+
ArrayList<ArrayList<Integer>> arr;
9+
int V;
10+
11+
Graph(int V){
12+
this.V =V;
13+
this.arr = new ArrayList<>();
14+
for(int i=0;i<V;i++) {
15+
arr.add(new ArrayList<>());
16+
}
17+
}
18+
19+
public void add_edge(int x, int y) {
20+
this.arr.get(x).add(y);
21+
this.arr.get(y).add(x);
22+
}
23+
24+
public void display() {
25+
26+
for(int i=0; i<V; i++) {
27+
System.out.print(String.valueOf(i));
28+
29+
for(int j=0;j<this.arr.get(i).size(); j++) {
30+
System.out.print(" -> "+this.arr.get(i).get(j));
31+
}
32+
System.out.println();
33+
}
34+
}
35+
36+
public boolean cycle_helper(int node,int[] visited, int parent) {
37+
38+
visited[node] = 1;
39+
for(int c : this.arr.get(node)) {
40+
if(visited[c]==0) {
41+
42+
boolean cycle_mila = cycle_helper(c, visited, node);
43+
if(cycle_mila)
44+
return true;
45+
46+
} else if(visited[c]==1 && c!=parent) {
47+
return true;
48+
}
49+
}
50+
return false;
51+
52+
}
53+
54+
public boolean contains_cycle() {
55+
int[] visited = new int[this.V];
56+
57+
return cycle_helper(0,visited,-1);
58+
}
59+
60+
61+
62+
}
63+
64+
public static void main(String[] args) {
65+
// TODO Auto-generated method stub
66+
67+
Graph g = new Cycle_Detection_Undirected_Graph().new Graph(5);
68+
69+
g.add_edge(0, 1);
70+
g.add_edge(2, 1);
71+
g.add_edge(2, 3);
72+
g.add_edge(3, 4);
73+
g.add_edge(4, 0);
74+
75+
System.out.println(g.contains_cycle());
76+
77+
}
78+
79+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import java.util.*;
2+
3+
class Graph2{
4+
5+
ArrayList<ArrayList<Integer>> arr;
6+
int V;
7+
8+
Graph2(int V){
9+
this.V =V;
10+
this.arr = new ArrayList<>();
11+
for(int i=0;i<V;i++) {
12+
arr.add(new ArrayList<>());
13+
}
14+
}
15+
16+
public void add_edge(int x, int y) {
17+
this.arr.get(x).add(y);
18+
this.arr.get(y).add(x);
19+
}
20+
21+
public int shortest_cycle() {
22+
23+
int[] dist = new int[V];
24+
int[] visited = new int[V];
25+
26+
Queue<Integer> q = new LinkedList<>();
27+
int cycle = Integer.MAX_VALUE;
28+
29+
for(int node=0;node<V;node++) {
30+
dist = new int[V];
31+
visited = new int[V];
32+
33+
visited[i] = 1;
34+
35+
q.add(node);
36+
int d=0;
37+
while(!q.isEmpty()) {
38+
d+=1;
39+
int rem = q.remove();
40+
for(int child : this.arr.get(rem)) {
41+
42+
43+
if (visited[child]!=1) {
44+
q.add(child);
45+
dist[child] = d;
46+
visited[child] = 1;
47+
} else {
48+
49+
if(dist[child]>=dist[rem]) {
50+
cycle = Math.min(cycle, dist[rem]+dist[child]+1);
51+
}
52+
}
53+
54+
}
55+
}
56+
57+
}
58+
59+
return cycle;
60+
}
61+
62+
void display(int[] arr) {
63+
for(int i=0;i<arr.length;i++) {
64+
System.out.print(arr[i]+", ");
65+
}
66+
System.out.println();
67+
}
68+
69+
70+
public void display() {
71+
72+
for(int i=0; i<V; i++) {
73+
System.out.print(String.valueOf(i));
74+
75+
for(int j=0;j<this.arr.get(i).size(); j++) {
76+
System.out.print(" -> "+this.arr.get(i).get(j));
77+
}
78+
System.out.println();
79+
}
80+
}
81+
}
82+
83+
84+
public class Shortest_Cycle_Undirected_Graph {
85+
86+
public static void main(String[] args) {
87+
// TODO Auto-generated method stub
88+
89+
Graph2 g = new Graph2(7);
90+
g.add_edge(0, 6);
91+
g.add_edge(0, 5);
92+
g.add_edge(1, 5);
93+
g.add_edge(1, 6);
94+
g.add_edge(2, 6);
95+
g.add_edge(3, 4);
96+
g.add_edge(4, 1);
97+
g.add_edge(3, 2);
98+
g.add_edge(2, 1);
99+
100+
//g.display();
101+
102+
System.out.println(g.shortest_cycle());
103+
104+
105+
}
106+
107+
}

0 commit comments

Comments
 (0)