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 9a474c0

Browse files
graphs
1 parent 0849e09 commit 9a474c0

File tree

7 files changed

+583
-46
lines changed

7 files changed

+583
-46
lines changed

‎DSAPractice/graph/BFS.java‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class BFS {
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 vertex = nextInt();
34+
int edges = nextInt();
35+
List<List<Integer>> adjecencyList = new ArrayList<>();
36+
for (int i = 0; i < vertex + 1; i++) {
37+
adjecencyList.add(new ArrayList<>());
38+
}
39+
for (int i = 0; i < edges; i++) {
40+
int source = nextInt();
41+
int destination = nextInt();
42+
adjecencyList.get(source).add(destination);
43+
adjecencyList.get(destination).add(source);// because this is undirected
44+
}
45+
46+
System.out.println(adjecencyList);
47+
int vis[] = new int[vertex + 1];
48+
bfs(vis, adjecencyList);
49+
}
50+
51+
public static void bfs(int[] vis, List<List<Integer>> adj) {
52+
List<Integer> bfs = new ArrayList<>();
53+
for (int i = 1; i < vis.length; i++) {
54+
if (vis[i] == 0) {
55+
Queue<Integer> trav = new LinkedList<>();
56+
trav.add(i);
57+
vis[i] = 1;
58+
while (!trav.isEmpty()) {
59+
int node = trav.poll();
60+
bfs.add(node);
61+
62+
for (int it : adj.get(node)) {
63+
if (vis[it] == 0) {
64+
vis[it] = 1;
65+
trav.add(it);
66+
67+
}
68+
}
69+
70+
}
71+
}
72+
}
73+
74+
System.out.println("The bfs is:");
75+
System.out.println(bfs);
76+
}
77+
78+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Bipartitebfscolouring {
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+
int vis[] = new int[vertices + 1];
49+
50+
boolean result = bfs(vis, adj, vertices);
51+
System.out.println(result);
52+
}
53+
54+
static boolean bfs(int[] vis, List<List<Integer>> adj, int vertices) {
55+
56+
int colour[] = new int[vertices + 1];
57+
for (int i = 1; i < vertices + 1; i++) {
58+
colour[i] = -1;
59+
}
60+
for (int i = 1; i < vis.length; i++) {
61+
if (vis[i] == 0) {
62+
if (colour[i] == -1) {
63+
if (!bfs(adj, i, colour)) {
64+
return false;
65+
}
66+
67+
}
68+
69+
}
70+
}
71+
return true;
72+
73+
}
74+
75+
static boolean bfs(List<List<Integer>> adj, int node, int[] colour) {
76+
Queue<Integer> q = new LinkedList<>();
77+
q.add(node);
78+
colour[node] = 1;
79+
while (!q.isEmpty()) {
80+
int no = q.poll();
81+
for (int it : adj.get(no)) {
82+
if (colour[it] == -1) {
83+
colour[it] = 1 - colour[no];
84+
q.add(it);
85+
} else if (colour[it] == colour[no]) {
86+
return false;
87+
}
88+
}
89+
}
90+
91+
return true;
92+
}
93+
}

‎DSAPractice/graph/DFS.java‎

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

0 commit comments

Comments
(0)

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