Graph Anuj
Graph Anuj
package graphs1;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
int V = 4;
graph.add(new ArrayList<>());
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 0, 3);
addEdge(graph, 1, 2);
// addEdgeMat(graphMat, 0, 1, 3);
// addEdgeMat(graphMat, 0, 2, 8);
// addEdgeMat(graphMat, 0, 3, 1);
// addEdgeMat(graphMat, 1, 2, 5);
// addEdgeMat(graphMat, 3, 2, 3);
findPath(graph, 3, 1);
// System.out.println("printing distance");
// }
int V = 4;
System.out.println(dest);
dest = prec[dest];
int count = 0;
if(!vis[i]) {
count++;
return count;
static void bfs(ArrayList<ArrayList<Integer>> graph, int src, boolean[] vis, int dist[], int prec[]) {
dist[src] = 0;
prec[src] = -1;
q.add(src);
while (!q.isEmpty()) {
// System.out.println(cur);
if(!vis[neighbor]) {
q.add(neighbor);
vis[neighbor] = true;
dist[neighbor] = dist[cur] + 1;
prec[neighbor] = cur;
if(graph.get(a).contains(b)) return;
graph.get(a).add(b);
graph.get(b).add(a);
// static void addEdgeMat(int graphMat[][], int src, int dest, int wt) {
// graphMat[src][dest] = wt;
// }
}
2.package graphs2;
import java.util.ArrayDeque;
import java.util.ArrayList;
int V = 8;
graph.add(new ArrayList<>());
// addEdge(graph, 7,4);
addEdge(graph, 3,6);
System.out.println(numberOfConnectedComponents(graph, V));
vis[cur] = true;
// System.out.println(cur);
int count = 0;
if(!vis[i]) {
count++;
return count;
stack.push(cur);
vis[cur] = true;
while (!stack.isEmpty()) {
int a = stack.pop();
System.out.println(a);
if(!vis[neighbor]) {
stack.push(neighbor);
vis[neighbor] = true;
}
}
if(graph.get(a).contains(b)) return;
graph.get(a).add(b);
graph.get(b).add(a);
3.package graphs3;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
int V = 6;
for(int i =0; i<V; i++) {
graph.add(new ArrayList<>());
// topologicalOrdering(graph, V);
topologicalOrderingKahnAlgo(graph, V);
for(int e: graph.get(i)) {
inDegree[e]++;
if (inDegree[i] == 0) {
q.add(i);
while (!q.isEmpty()) {
System.out.println(cur);
if(inDegree[neighbor] == 0) {
q.add(neighbor);
if(!vis[i]) {
while (!ansStack.isEmpty()) {
System.out.println(ansStack.pop());
ArrayDeque<Integer> ansStack) {
vis[cur] = true;
if(!vis[neighbor]) {
}
static void addEdge(ArrayList<ArrayList<Integer>> graph, int a, int b) {
if(graph.get(a).contains(b)) return;
graph.get(a).add(b);
// graph.get(b).add(a);
4.package graphs4;
import disjointSets.DisjointSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
int V = 6;
graph.add(new ArrayList<>());
addEdge(graph, 5, 0, 9);
System.out.println(mstKruskal(graph));
}
for(Pair p: adj) {
Collections.sort(edges);
int v = graph.size();
int ans = 0;
int count = 0;
if(findSrc != findDest) {
ans += cur.wt;
count++;
ds.union(src, dest);
return ans;
}
static class Edge implements Comparable<Edge> {
this.src = src;
this.dest = dest;
this.wt = wt;
@Override
static void addEdge(ArrayList<ArrayList<Pair>> graph, int src, int dest, int wt) {
class Pair {
int v, w;
this.v = v;
this.w = w;
}
}
5.package graphs5;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
int V = 6;
graph.add(new ArrayList<>());
addEdge(graph, 5, 0, 9);
// System.out.println(mstPrims(graph, V));
Arrays.fill(ans, Integer.MAX_VALUE);
ans[src] = 0;
while (!pq.isEmpty()) {
if(vis[cur]) continue;
vis[cur] = true;
if(!vis[neighbor.v]) {
return ans;
int ans = 0;
while (!pq.isEmpty()) {
if(vis[cur.dest]) continue;
vis[cur.dest] = true;
ans += cur.wt;
if(!vis[pair.v]) {
return ans;
this.src = src;
this.dest = dest;
this.wt = wt;
@Override
static void addEdge(ArrayList<ArrayList<Pair>> graph, int src, int dest, int wt) {
class Pair {
int v, w;
this.v = v;
this.w = w;