Skip to content

Commit

Permalink
Merge pull request #543 from whobushra/master
Browse files Browse the repository at this point in the history
Added graph algorithms in java folder
  • Loading branch information
MukulCode authored Oct 31, 2022
2 parents d762602 + c59765c commit a13c1f5
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 0 deletions.
7 changes: 7 additions & 0 deletions java/Graphs/desktop.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[LocalizedFileNames]
primsAlgoOptimised.java[email protected],0
shortestDistanceUndirectedGraph.java[email protected],0
shortestPathInDag.java[email protected],0
topologicalSortBfs.java[email protected],0
topologicalSortDfs.java[email protected],0
primsAlgoBrute.java[email protected],0
50 changes: 50 additions & 0 deletions java/Graphs/primsAlgoBrute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Graphs;

import java.util.*;

public class primsAlgoBrute {

public static void primsBrute(ArrayList<ArrayList<Node>> adj, int N) {
int key[] = new int[N];
int parent[] = new int[N];
boolean mstSet[] = new boolean[N];

for (int i = 0; i < N; i++) {
key[i] = Integer.MAX_VALUE;
}
key[0] = 0;
parent[0] = -1;

// Main for loop for N-1 edges and N vertex
for (int i = 0; i < N - 1; i++) {

int mini = Integer.MAX_VALUE;
int u = 0;

// for finding minimum value in the keys array for N-1 times
for (int v = 0; v < N; v++) {
if (mstSet[v] == false && key[v] < mini) {
mini = key[v];
u = v;
}
}

mstSet[u] = true;

// Iterating adjacent nodes of the min value node

for (Node it : adj.get(u)) {
if (mstSet[it.getV()] == false && it.getWeight() < key[it.getV()]) {
parent[it.getV()] = u;
key[it.getV()] = it.getWeight();
}
}
}

// printing the nodes along with their respective parent node which are present
// in the Minimum spamming tree.
for (int i = 0; i < N; i++) {
System.out.println(parent[i] + "-" + i);
}
}
}
38 changes: 38 additions & 0 deletions java/Graphs/primsAlgoOptimised.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Graphs;

import java.util.*;

public class primsAlgoOptimised {

public static void primsOptimised(ArrayList<ArrayList<Node>> adj,int N){
int key[]=new int[N];
int parent[]=new int[N];
boolean mstSet[]=new boolean[N];

for(int i=0;i<N;i++){
key[i]=Integer.MAX_VALUE;
}

key[0]=0;
parent[0]=-1;

PriorityQueue<Node> pq=new PriorityQueue<>(N,new Node());
pq.add(new Node(key[0],0));

for(int i=0;i<N-1;i++){
int u=pq.poll().getV();
mstSet[u]=true;

for(Node it: adj.get(u)){
if(mstSet[it.getV()]==false && it.getWeight()<key[it.getV()]){
key[it.getV()]=it.getWeight();
pq.add(new Node(it.getV(),key[it.getV()]));
}
}
}

for(int i=1;i<N;i++){
System.out.println(parent[i] + " - "+ i);
}
}
}
28 changes: 28 additions & 0 deletions java/Graphs/shortestDistanceUndirectedGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Graphs;

import java.util.*;

public class shortestDistanceUndirectedGraph {

public static int[] findDist(ArrayList<ArrayList<Integer>> adj, int V, int source) {

int dist[] = new int[V];
Queue<Integer> q = new LinkedList<>();

for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
}
dist[source] = 0;
q.add(source);

while (!q.isEmpty()) {
int node = q.poll();

for (Integer it : adj.get(node)) {
dist[it] = Math.min(dist[node] + 1, dist[it]);
q.add(it);
}
}
return dist;
}
}
61 changes: 61 additions & 0 deletions java/Graphs/shortestPathInDag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package Graphs;

import java.util.*;

public class shortestPathInDag {

public static int[] findShortestPath(ArrayList<ArrayList<Pair>> adj, int V, int source) {
int dist[] = new int[V];

for (int i = 0; i < V; i++)
dist[i] = Integer.MAX_VALUE;
dist[source] = 0;

Stack<Integer> s = new Stack<>();
findtopoSort(adj, V, s);

while (!s.isEmpty()) {
int node = s.pop();

if (dist[node] != Integer.MAX_VALUE) {
for (Pair it : adj.get(node)) {
if (dist[node] + it.weight < dist[it.node]) {
dist[it.node] = dist[node] + it.weight;
}
}
}
}
return dist;
}

public static void findtopoSort(ArrayList<ArrayList<Pair>> adj, int V, Stack<Integer> s) {
boolean vis[] = new boolean[V];

for (int i = 0; i < V; i++) {
if (vis[i] == false) {
dfs(i, vis, s, adj);
}
}
}

public static void dfs(int node, boolean vis[], Stack<Integer> s, ArrayList<ArrayList<Pair>> adj) {
vis[node] = true;
for (Pair it : adj.get(node)) {
if (vis[it.node] == false) {
dfs(it.node, vis, s, adj);
}
}
s.push(node);
}

}

class Pair {
int node;
int weight;

Pair(int node, int weight) {
this.node = node;
this.weight = weight;
}
}
37 changes: 37 additions & 0 deletions java/Graphs/topologicalSortBfs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Graphs;
import java.util.*;

public class topologicalSortBfs {

public int[] topoSort(ArrayList<ArrayList<Integer>> adj, int N) {

int topo[] = new int[N];
int indegree[] = new int[N];

for (int i = 0; i < N; i++) {
for (Integer it : adj.get(i)) {
indegree[it]++;
}
}

Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < N; i++) {
if (indegree[i] == 0) {
q.add(i);
}
}

int ind = 0;
while (!q.isEmpty()) {
Integer node = q.poll();
topo[ind++]=node;
indegree[node]--;
if (indegree[node] == 0) {
q.add(node);
}
}

return topo;
}

}
35 changes: 35 additions & 0 deletions java/Graphs/topologicalSortDfs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Graphs;

import java.util.*;

public class topologicalSortDfs {

ArrayList<Integer> topologicalSort(ArrayList<ArrayList<Integer>> adj, int N) {
int[] vis = new int[N];
Stack<Integer> s = new Stack<>();

for (int i = 0; i < N; i++) {
if (vis[i] == 0) {
dfs(i, vis, s, adj);
}
}
ArrayList<Integer> ans = new ArrayList<>();

while (!s.isEmpty()) {
ans.add(s.pop());
}
return ans;
}

public void dfs(int node, int[] vis, Stack<Integer> s, ArrayList<ArrayList<Integer>> adj) {

vis[node] = 1;
for (Integer it : adj.get(node)) {
if (vis[it] == 0) {
dfs(it, vis, s, adj);
}
}
s.push(node);
}

}

0 comments on commit a13c1f5

Please sign in to comment.