Skip to content

Commit 1c28611

Browse files
committed
initial commit
0 parents  commit 1c28611

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4778
-0
lines changed

DisjointSet/UnionAndFind.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package DisjointSet;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
public class UnionAndFind {
9+
10+
11+
private static int find(int v,int[] parent) {
12+
13+
if(parent[v] == -1) {
14+
return v;
15+
}else {
16+
return find(parent[v],parent);
17+
}
18+
19+
}
20+
21+
22+
private static void union(int from , int to, int[] parent) {
23+
24+
int from_parent = find(from,parent);
25+
int to_parent = find(to,parent);
26+
27+
parent[from_parent] = to_parent;
28+
29+
}
30+
31+
private static boolean isCyclic(List<Integer> pair,int[] parent) {
32+
33+
34+
int from = pair.get(0);
35+
int to = pair.get(1);
36+
37+
int fromParent = find(from,parent);
38+
int toParent = find(to,parent);
39+
40+
if(fromParent == toParent) {
41+
return true;
42+
}else {
43+
union(fromParent, toParent, parent);
44+
return false;
45+
}
46+
}
47+
48+
49+
public static void main(String[] args) {
50+
51+
Scanner sc = new Scanner(System.in);
52+
53+
int vertices = sc.nextInt();
54+
int edges = sc.nextInt();
55+
56+
57+
int[] parent = new int[vertices];
58+
59+
// initialize all the parents to -1
60+
for(int i = 0 ; i < vertices ; i++) {
61+
parent[i] = -1;
62+
}
63+
64+
List<List<Integer>> edgeList = new ArrayList<>();
65+
66+
for(int i = 0 ; i < edges ; i++) {
67+
68+
List<Integer> pair = new ArrayList<>();
69+
70+
for(int j = 0 ; j < 2 ; j++) {
71+
pair.add(sc.nextInt());
72+
}
73+
74+
edgeList.add(pair);
75+
}
76+
77+
// processing all the edges
78+
for(int i = 0 ; i < edgeList.size() ; i++) {
79+
List<Integer> pair = edgeList.get(i);
80+
81+
if(isCyclic(pair,parent)) {
82+
System.out.println("True");
83+
}else {
84+
System.out.println("false");
85+
}
86+
}
87+
88+
}
89+
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package DisjointSet;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
public class UnionByRankAndFindByPathCompression {
8+
9+
private static int findByCompression(int v, int[] parent) {
10+
// If v is the parent of itself
11+
if (parent[v] == -1) {
12+
// Then v is the representative
13+
return v;
14+
} else {
15+
// Recursively find the representative.
16+
int result = findByCompression(parent[v], parent);
17+
18+
// We cache the result by moving v’s node directly under the representative of this set
19+
parent[v] = result;
20+
21+
// And then we return the result
22+
return result;
23+
}
24+
}
25+
26+
private static void unionByRank(int from_parent, int to_parent, int[] parent, int[] rank) {
27+
28+
// Get the rank of from 's parent
29+
int from_rank = rank[from_parent];
30+
31+
// Get the rank of to’s parent
32+
int to_rank = rank[from_parent];
33+
34+
// If from’s rank is less than to’s rank
35+
if (from_rank < to_rank) {
36+
// Then move from under to
37+
parent[from_parent] = to_parent;
38+
}
39+
// Else if to’s rank is less than from’s rank
40+
else if (to_rank < from_rank) {
41+
// Then move to under from
42+
parent[to_parent] = from_parent;
43+
}
44+
// Else if their ranks are the same
45+
else {
46+
47+
// Then move from under to (does not matter which one goes where)
48+
parent[from_parent] = to_parent;
49+
50+
// And increment the result tree’s
51+
// rank by 1
52+
rank[to_parent]++;
53+
54+
}
55+
56+
}
57+
58+
private static boolean isCyclic(List<Integer> pair, int[] parent, int[] rank) {
59+
60+
61+
int from = pair.get(0);
62+
int to = pair.get(1);
63+
64+
int fromParent = findByCompression(from, parent);
65+
int toParent = findByCompression(to, parent);
66+
67+
if (fromParent == toParent) {
68+
return true;
69+
} else {
70+
unionByRank(fromParent, toParent, parent, rank);
71+
return false;
72+
}
73+
}
74+
75+
76+
public static void main(String[] args) {
77+
78+
Scanner sc = new Scanner(System.in);
79+
80+
int vertices = sc.nextInt();
81+
int edges = sc.nextInt();
82+
83+
84+
int[] parent = new int[vertices];
85+
int[] rank = new int[vertices];
86+
87+
// initialize all the parents to -1
88+
for (int i = 0; i < vertices; i++) {
89+
parent[i] = -1;
90+
}
91+
92+
List<List<Integer>> edgeList = new ArrayList<>();
93+
94+
for (int i = 0; i < edges; i++) {
95+
96+
List<Integer> pair = new ArrayList<>();
97+
98+
for (int j = 0; j < 2; j++) {
99+
pair.add(sc.nextInt());
100+
}
101+
102+
edgeList.add(pair);
103+
}
104+
105+
// processing all the edges
106+
for (int i = 0; i < edgeList.size(); i++) {
107+
List<Integer> pair = edgeList.get(i);
108+
109+
if (isCyclic(pair, parent, rank)) {
110+
System.out.println("True");
111+
} else {
112+
System.out.println("false");
113+
}
114+
}
115+
116+
}
117+
118+
}
119+
120+
121+
122+
123+
124+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package Graphs.AllPairShortestPath;
2+
3+
// All Pair Shortest Path Algorithm
4+
public class FloydWarshall {
5+
6+
private static int vertices;
7+
private static int edges;
8+
9+
private static int[][] adjMatrix;
10+
11+
public FloydWarshall(int nodes) {
12+
vertices = nodes;
13+
edges = 0;
14+
adjMatrix = new int[nodes][nodes];
15+
16+
// initialize the all the diagonals to zero and remaining to infinity
17+
for(int i = 0 ; i < nodes ; i++) {
18+
for (int j = 0 ; j < nodes ; j++) {
19+
if(i == j) {
20+
adjMatrix[i][j] = 0;
21+
}else {
22+
adjMatrix[i][j] = Integer.MAX_VALUE;
23+
}
24+
}
25+
}
26+
27+
}
28+
29+
private static void addEdge(int u , int v , int w) {
30+
adjMatrix[u][v] = w;
31+
edges++;
32+
}
33+
34+
public String toString() {
35+
StringBuilder sb = new StringBuilder();
36+
sb.append(vertices).append(" vertices, ").append(edges).append(" edges ").append("\n");
37+
for(int vertex = 0 ; vertex < vertices ; vertex++) {
38+
sb.append(vertex).append(":");
39+
for(int w : adjMatrix[vertex]) {
40+
sb.append(w).append(" ");
41+
}
42+
sb.append("\n");
43+
}
44+
return sb.toString();
45+
}
46+
47+
private static void allPairShortestPath() {
48+
49+
for(int k = 0 ; k < vertices ; k++) {
50+
for(int i = 0 ; i < vertices ; i++) {
51+
for(int j = 0 ; j < vertices ; j++) {
52+
if(adjMatrix[i][k] == Integer.MAX_VALUE || adjMatrix[k][j] == Integer.MAX_VALUE) {
53+
continue;
54+
}
55+
else if(adjMatrix[i][k] + adjMatrix[k][j] < adjMatrix[i][j]) {
56+
adjMatrix[i][j] = adjMatrix[i][k] + adjMatrix[k][j];
57+
}
58+
}
59+
}
60+
}
61+
62+
// check for negative weight cycle
63+
for(int i = 0; i < vertices ; i++) {
64+
if(adjMatrix[i][i] < 0) {
65+
System.out.println("Negative Edge weight Cycle Exixts");
66+
return;
67+
}
68+
}
69+
70+
71+
}
72+
73+
public static void main(String[] args) {
74+
75+
FloydWarshall floydWarshall = new FloydWarshall(4);
76+
77+
addEdge(0,1,9);
78+
addEdge(0,2,-4);
79+
addEdge(1,0,6);
80+
addEdge(1,3,2);
81+
addEdge(2,1,5);
82+
addEdge(3,2,1);
83+
84+
allPairShortestPath();
85+
System.out.println(floydWarshall);
86+
}
87+
88+
89+
}

Graphs/ConnectedComponents.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package Graphs;
2+
3+
import java.util.LinkedList;
4+
5+
public class ConnectedComponents {
6+
7+
8+
private static LinkedList<Integer>[] adj ;
9+
private static int vertices; // vertices
10+
private int edges; // Edges
11+
private static int[] flag;
12+
13+
public ConnectedComponents(int nodes) {
14+
vertices = nodes;
15+
edges = 0;
16+
adj = new LinkedList[nodes];
17+
for (int i = 0 ; i < nodes ; i++) {
18+
adj[i] = new LinkedList<>();
19+
}
20+
21+
flag = new int[nodes];
22+
}
23+
24+
private void addEdge(int u , int v) {
25+
adj[u].add(v);
26+
adj[v].add(u);
27+
edges++;
28+
}
29+
30+
public String toString() {
31+
StringBuilder sb = new StringBuilder();
32+
33+
sb.append(vertices).append("vertices ").append(edges).append("edges ").append("\n");
34+
for(int v = 0; v < vertices; v++) {
35+
sb.append(v).append(": ");
36+
for(int w : adj[v]) {
37+
sb.append(w).append(" ");
38+
}
39+
sb.append("\n");
40+
}
41+
return sb.toString();
42+
}
43+
44+
private static int connectedComponents() {
45+
int count = 0;
46+
47+
for(int i = 0 ; i < vertices ; i++) {
48+
if(flag[i] == 0) {
49+
dfs(i);
50+
count++;
51+
}
52+
}
53+
54+
return count;
55+
}
56+
57+
58+
private static void dfs(int source) {
59+
60+
flag[source] = 1;
61+
62+
for(int v : adj[source]) {
63+
if(flag[v] == 0) {
64+
dfs(v);
65+
}
66+
}
67+
68+
}
69+
70+
71+
public static void main(String[] args) {
72+
73+
ConnectedComponents cc = new ConnectedComponents(9);
74+
75+
cc.addEdge(0,1);
76+
cc.addEdge(0,3);
77+
cc.addEdge(1,2);
78+
cc.addEdge(2,3);
79+
cc.addEdge(4,5);
80+
cc.addEdge(6,7);
81+
cc.addEdge(6,8);
82+
cc.addEdge(7,8);
83+
84+
System.out.println(cc);
85+
86+
int connectedComponents = connectedComponents();
87+
System.out.println("\n"+ connectedComponents);
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)