Skip to content

Commit ae4ea87

Browse files
committed
4 graphs
5 trees added Signed-off-by: P SIDHARTH <philkhanasidharth14@gmail.com>
1 parent feb100c commit ae4ea87

File tree

9 files changed

+504
-0
lines changed

9 files changed

+504
-0
lines changed

graphs/java/Bipartite.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Arrays;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
4+
5+
public class Bipartite {
6+
public static void main(String args[]) {
7+
8+
}
9+
public static boolean check(int V, List<Integer>[] adj){
10+
int[] colors = new int[V+1];
11+
Arrays.fill(colors, -1);
12+
13+
for (int i = 0; i <= V; i++) {
14+
if(colors[i] == -1){
15+
if(bfs(colors, adj, i)){
16+
return true;
17+
}
18+
}
19+
}
20+
return false;
21+
}
22+
public static boolean bfs(int[] colors, List<Integer>[] adj, int node){
23+
Queue<Integer> q = new LinkedList<>();
24+
q.add(node);
25+
colors[node] = 0;
26+
while (!q.isEmpty()) {
27+
int polled = q.poll();
28+
29+
for(int neighbor : adj[polled]){
30+
if(colors[neighbor] == -1){
31+
colors[neighbor] = 1 - colors[polled];
32+
q.offer(neighbor);
33+
}else if (colors[node] == colors[neighbor]){
34+
return false;
35+
}
36+
}
37+
}
38+
return true;
39+
}
40+
public static boolean dfs(int[] colors, List<Integer> adj, int node){
41+
colors[node] = true;
42+
43+
for(int neighbor : adj[node]){
44+
if(colors[neighbor] == -1){
45+
colors[neighbor] = 1 - colors[node];
46+
dfs(colors, adj, neighbor);
47+
}else if(colors[node]==colors[neighbor]){
48+
return false;
49+
}
50+
}
51+
return true;
52+
}
53+
}

graphs/java/DetectCycle.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
import java.util.zip.CRC32;
4+
5+
class Pair{
6+
int parent;
7+
int x;
8+
public Pair(int parent, int x){
9+
this.parent = parent;
10+
this.x = x;
11+
}
12+
}
13+
public class DetectCycle{
14+
public static void main(String args[]) {
15+
16+
}
17+
public static boolean isCycle(int V, List<Integer>[] adj){
18+
19+
boolean[] visited = new boolean[V];
20+
for (int i = 0; i < V; i++) {
21+
if(!visited[i]){
22+
if(bfs(i, adj, visited)){
23+
return true;
24+
}
25+
}
26+
}
27+
return false;
28+
}
29+
public static boolean bfs(int node, List<Integer>[] adj, boolean[] visited){
30+
Queue<Pair> q = new LinkedList<>();
31+
q.add(new Pair(node, -1));
32+
visited[node] = true;
33+
while(!q.isEmpty()){
34+
Pair curr = q.poll();
35+
int parent = curr.parent;
36+
int x = curr.x;
37+
38+
for(Integer neighbor: adj[node]){
39+
if(!visited[neighbor]){
40+
visited[neighbor] = true;
41+
q.offer(new Pair(neighbor ,node));
42+
}else if( parent != neighbor){
43+
return true;
44+
}
45+
}
46+
}
47+
return false;
48+
}
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.List;
2+
3+
public class DirectedDetectCycle{
4+
public static void main(String args[]) {
5+
6+
}
7+
public static boolean detech(int V, List<Integer>[] adj){
8+
boolean[] visited = new boolean[V];
9+
boolean[] path = new boolean[V];
10+
11+
for (int i = 0; i < V; i++) {
12+
if(!visited[i]){
13+
if(dfs(i, adj, path, visited)){
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
public static boolean dfs(int i, List<Integer>[] adj, boolean[] path,
21+
boolean[] visited){
22+
visited[i] = true;
23+
path[i] = true;
24+
25+
for(int neighbor : adj[i]){
26+
if(path[neighbor]){
27+
28+
return true;
29+
}
30+
if(!visited[neighbor]){
31+
if(dfs(i, adj, path, visited)){
32+
return true;
33+
}
34+
35+
}
36+
}
37+
path[i] = false;
38+
return false;
39+
}
40+
}

graphs/java/KahnTopo.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
import java.util.Stack;
4+
5+
public class KahnTopo{
6+
public static void main(String ar[]) {
7+
8+
9+
}
10+
public static void topoSort(int V, List<Integer>[] adj){
11+
boolean[] visited = new boolean[V+1];
12+
Stack<Integer> stack = new Stack<>();
13+
14+
for(int i = 0 ; i < V; i++){
15+
if(!visited[i]){
16+
dfs(i, visitedi, stack, adj);
17+
}
18+
}
19+
while(!stack.isEmpty()){
20+
System.out.println(stack.pop());
21+
}
22+
}
23+
public static void dfs(int node, boolean[] visited, Stack<Integer> stack,
24+
List<Integer>[] adj){
25+
visited[node] = true;
26+
27+
for(int neighbor : adj[node]){
28+
if(!visited[neighbor]){
29+
dfs(node, visited, stack, adj);
30+
}
31+
}
32+
stack.add(neighbor);
33+
34+
}
35+
36+
public int[] topoSort(int V, List<Integer> adj[]) {
37+
int[] ans = new int[V];
38+
int[] inDegree = new int[V];
39+
40+
for (int i = 0; i < V; i++) {
41+
for (int it : adj[i]) inDegree[it]++;
42+
}
43+
44+
Queue<Integer> q = new LinkedList<>();
45+
46+
for (int i = 0; i < V; i++) {
47+
if (inDegree[i] == 0) q.add(i);
48+
}
49+
50+
int index = 0; // Index to store elements in ans array
51+
52+
while (!q.isEmpty()) {
53+
int node = q.poll();
54+
55+
ans[index++] = node;
56+
57+
for (int it : adj[node]) {
58+
inDegree[it]--;
59+
if (inDegree[it] == 0) q.add(it);
60+
}
61+
}
62+
return ans;
63+
}
64+
}

trees/java/Boundary.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.*;
2+
3+
class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
TreeNode() {}
8+
TreeNode(int val) { this.val = val; }
9+
TreeNode(int val, TreeNode left, TreeNode right) {
10+
this.val = val;
11+
this.left = left;
12+
this.right = right;
13+
}
14+
}
15+
16+
public class Boundary {
17+
public List<Integer> boundary(TreeNode root) {
18+
List<Integer> res = new ArrayList<>();
19+
20+
if(root == null) return res;
21+
22+
if(!isLeaf(root)){
23+
res.add(root.val);
24+
}
25+
addLeftBoundary(root, res);
26+
addLeaves(root, res);
27+
addRightBoundary(root, res);
28+
29+
return res;
30+
31+
}
32+
33+
public static boolean isLeaf(TreeNode root){
34+
return (root.left == null && root.right == null);
35+
}
36+
37+
public void addLeaves(TreeNode root, List<Integer> res){
38+
if(isLeaf(root)){
39+
res.add(root.val);
40+
return;
41+
}
42+
if(root.left != null){
43+
addLeaves(root.left, res);
44+
}
45+
if(root.right != null){
46+
addLeaves(root.right, res);
47+
}
48+
}
49+
public static void addLeftBoundary(TreeNode node, List<Integer> res){
50+
TreeNode curr = node.left;
51+
while(curr != null){
52+
if(!isLeaf(curr)){
53+
res.add(curr.val);
54+
}
55+
if(curr.left != null){
56+
curr = curr.left;
57+
} else {
58+
curr = curr.right;
59+
}
60+
}
61+
}
62+
63+
public static void addRightBoundary(TreeNode node, List<Integer> res){
64+
65+
TreeNode curr = node.right;
66+
List<Integer> temp = new ArrayList<>();
67+
68+
while(curr != null){
69+
if(!isLeaf(curr)){
70+
temp.add(curr.data);
71+
}
72+
if(curr.right != null){
73+
curr = curr.right;
74+
} else {
75+
curr = curr.left;
76+
}
77+
}
78+
for(int i = temp.size() - 1 ; i >= 0 ; i--){
79+
res.add(temp.get(i));
80+
}
81+
}
82+
}
83+

trees/java/Cousins.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.HashMap;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
4+
5+
class TreeNode {
6+
int val;
7+
TreeNode left;
8+
TreeNode right;
9+
TreeNode() {}
10+
TreeNode(int val) { this.val = val; }
11+
TreeNode(int val, TreeNode left, TreeNode right) {
12+
this.val = val;
13+
this.left = left;
14+
this.right = right;
15+
}
16+
}
17+
18+
19+
class Cousins{
20+
public static void main(String args[]) {
21+
TreeNode root = new TreeNode(5);
22+
root.left = new TreeNode(4);
23+
root.right = new TreeNode(5);
24+
root.left.left= new TreeNode(1);
25+
root.left.right = new TreeNode(10);
26+
root.right.right = new TreeNode(7);
27+
28+
Inorder(root);
29+
System.out.println("______________________");
30+
31+
function(root);
32+
root.val = 0;
33+
root.left.val = 0;
34+
root.right.val = 0;
35+
Inorder(root);
36+
}
37+
public static void function(TreeNode root) {
38+
39+
Queue<TreeNode> q = new LinkedList<>();
40+
q.add(root);
41+
root.val =0;
42+
while(!q.isEmpty()){
43+
int level = q.size();
44+
int levelSum = 0;
45+
46+
Queue<TreeNode> tq = new LinkedList<>(q);
47+
for (int i = 0; i < level; i++) {
48+
TreeNode polled = tq.poll();
49+
levelSum += polled.val;
50+
if (polled.left != null) levelSum += polled.left.val;
51+
if (polled.right != null) levelSum += polled.right.val;
52+
}
53+
54+
for (int i = 0; i < level; i++) {
55+
TreeNode curr = q.poll();
56+
57+
int siblings =0 ;
58+
if(curr.left != null) {
59+
siblings +=curr.left.val;
60+
q.add(curr.left);
61+
}
62+
if(curr.right != null){
63+
siblings += curr.right.val;
64+
q.add(curr.right);
65+
}
66+
67+
if(curr.left != null){
68+
curr.left.val = Math.abs(levelSum - siblings);
69+
}
70+
if(curr.right!= null){
71+
curr.right.val = Math.abs(levelSum - siblings);
72+
}
73+
74+
}
75+
}
76+
}
77+
public static void Inorder(TreeNode root){
78+
/// left to root to right
79+
if(root != null){
80+
Inorder(root.left);
81+
System.out.println(root.val);
82+
Inorder(root.right);
83+
}
84+
}
85+
86+
}

0 commit comments

Comments
 (0)