Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e092d09

Browse files
graph
1 parent 2524e74 commit e092d09

File tree

4 files changed

+240
-1
lines changed

4 files changed

+240
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class BipatriteDFSColouring {
5+
static BufferedReader br;
6+
static StringTokenizer st;
7+
8+
static int nextInt() throws IOException {
9+
return Integer.parseInt(next());
10+
}
11+
12+
static String next() throws IOException {
13+
while (st == null || !st.hasMoreTokens()) {
14+
st = new StringTokenizer(br.readLine());
15+
}
16+
return st.nextToken();
17+
}
18+
19+
static long nextLong() throws IOException {
20+
return Long.parseLong(next());
21+
}
22+
23+
public static void main(String args[]) throws IOException {
24+
br = new BufferedReader(new InputStreamReader(System.in));
25+
int t = nextInt();
26+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int vertices = nextInt();
34+
int edges = nextInt();
35+
List<List<Integer>> adj = new ArrayList<>();
36+
for (int i = 0; i < vertices + 1; i++) {
37+
adj.add(new ArrayList<>());
38+
39+
}
40+
41+
for (int i = 0; i < edges; i++) {
42+
int source = nextInt();
43+
int destination = nextInt();
44+
adj.get(source).add(destination);
45+
adj.get(destination).add(source);
46+
47+
}
48+
49+
System.out.println(adj);
50+
int col[] = new int[vertices + 1];
51+
Arrays.fill(col, -1);
52+
System.out.println(dfs(adj, col));
53+
54+
}
55+
56+
static boolean dfs(List<List<Integer>> adj, int[] col) {
57+
for (int i = 1; i < col.length; i++) {
58+
if (col[i] == -1) {
59+
if (!dfs(i, adj, col)) {
60+
return false;
61+
}
62+
63+
}
64+
}
65+
return true;
66+
}
67+
68+
static boolean dfs(int node, List<List<Integer>> adj, int[] col) {
69+
70+
if (col[node] == -1) {
71+
col[node] = 1;
72+
}
73+
74+
for (int it : adj.get(node)) {
75+
if (col[it] == -1) {
76+
col[it] = 1 - col[node];
77+
78+
if (!dfs(it, adj, col))
79+
return false;
80+
} else if (col[node] == col[it]) {
81+
return false;
82+
}
83+
}
84+
85+
return true;
86+
}
87+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
/**
3+
1
4+
9 10
5+
1 2
6+
2 3
7+
3 4
8+
4 5
9+
3 6
10+
6 5
11+
7 2
12+
7 8
13+
8 9
14+
9 5
15+
16+
1 --> 2 --> 3 --> 4 --> 5 <--
17+
^ | ^ |
18+
| ^ | |
19+
7 6 ----------- |
20+
| |
21+
^ |
22+
8 --> 9 ---------------
23+
24+
*/
25+
import java.io.*;
26+
import java.util.*;
27+
28+
class DetectCycleInDirectedGraphDFS {
29+
static BufferedReader br;
30+
static StringTokenizer st;
31+
32+
static int nextInt() throws IOException {
33+
return Integer.parseInt(next());
34+
}
35+
36+
static String next() throws IOException {
37+
while (st == null || !st.hasMoreTokens()) {
38+
st = new StringTokenizer(br.readLine());
39+
}
40+
return st.nextToken();
41+
}
42+
43+
static long nextLong() throws IOException {
44+
return Long.parseLong(next());
45+
}
46+
47+
public static void main(String args[]) throws IOException {
48+
br = new BufferedReader(new InputStreamReader(System.in));
49+
int t = nextInt();
50+
while (t-- > 0) {
51+
input();
52+
}
53+
br.close();
54+
}
55+
56+
public static void input() throws IOException {
57+
int vertices = nextInt();
58+
int edges = nextInt();
59+
List<List<Integer>> adj = new ArrayList<>();
60+
for (int i = 0; i < vertices + 1; i++) {
61+
adj.add(new ArrayList<>());
62+
63+
}
64+
65+
for (int i = 0; i < edges; i++) {
66+
int source = nextInt();
67+
int destination = nextInt();
68+
adj.get(source).add(destination);
69+
}
70+
int vis[] = new int[vertices + 1];
71+
int dfsvis[] = new int[vertices + 1];
72+
System.out.println(adj);
73+
System.out.println(dfs(adj, vis, dfsvis));
74+
}
75+
76+
static boolean dfs(List<List<Integer>> adj, int[] vis, int[] dfsvis) {
77+
for (int i = 0; i < vis.length; i++) {
78+
if (vis[i] == 0) {
79+
if (dfs(i, adj, vis, dfsvis) == true)
80+
return true;
81+
}
82+
}
83+
return false;
84+
}
85+
86+
static boolean dfs(int ind, List<List<Integer>> adj, int[] vis, int[] dfsvis) {
87+
vis[ind] = 1;
88+
dfsvis[ind] = 1;
89+
for (int it : adj.get(ind)) {
90+
if (vis[it] == 0) {
91+
if (dfs(it, adj, vis, dfsvis) == true) {
92+
return true;
93+
}
94+
} else if (dfsvis[it] == 1) {
95+
return true;
96+
}
97+
98+
}
99+
100+
dfsvis[ind] = 0;
101+
return false;
102+
}
103+
104+
}

‎DSAPractice/graph/DetectCycleInUndirectedGraph.java‎ renamed to ‎DSAPractice/graph/DetectCycleInUndirectedGraphbfs.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Node {
1111
}
1212

1313
// using bfs
14-
public class DetectCycleInUndirectedGraph {
14+
public class DetectCycleInUndirectedGraphbfs {
1515
static StringTokenizer st;
1616
static BufferedReader br;
1717

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class TopologicalSortDFS {
5+
static BufferedReader br;
6+
static StringTokenizer st;
7+
8+
static int nextInt() throws IOException {
9+
return Integer.parseInt(next());
10+
}
11+
12+
static String next() throws IOException {
13+
while (st == null || !st.hasMoreTokens()) {
14+
st = new StringTokenizer(br.readLine());
15+
}
16+
return st.nextToken();
17+
}
18+
19+
static long nextLong() throws IOException {
20+
return Long.parseLong(next());
21+
}
22+
23+
public static void main(String args[]) throws IOException {
24+
br = new BufferedReader(new InputStreamReader(System.in));
25+
int t = nextInt();
26+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int vertices = nextInt();
34+
int edges = nextInt();
35+
List<List<Integer>> adj = new ArrayList<>();
36+
for (int i = 0; i < vertices + 1; i++) {
37+
adj.add(new ArrayList<>());
38+
39+
}
40+
41+
for (int i = 0; i < edges; i++) {
42+
int source = nextInt();
43+
int destination = nextInt();
44+
adj.get(source).add(destination);
45+
}
46+
int vis[] = new int[vertices + 1];
47+
}
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /