-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from whobushra/master
Added graph algorithms in java folder
- Loading branch information
Showing
7 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |