0% found this document useful (0 votes)
17 views15 pages

Graph Anuj

Uploaded by

parthavikhatu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

Graph Anuj

Uploaded by

parthavikhatu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

package graphs1;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.Queue;

public class MainClass {

public static void main(String[] args) {

ArrayList<ArrayList<Integer>> graph = new ArrayList<>();

int V = 4;

for(int i =0; i<V; i++) {

graph.add(new ArrayList<>());

int graphMat[][] = new int[V][V];

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");

// for(int i = 0; i<dist.length; i++) {


// System.out.println(i+" -> "+dist[i]);

// }

static void findPath(ArrayList<ArrayList<Integer>> graph, int src, int dest) {

int V = 4;

boolean vis[] = new boolean[V];

int dist[] = new int[V];

int prec[] = new int[V];

bfs(graph, src, vis, dist, prec);

while (dest != -1) {

System.out.println(dest);

dest = prec[dest];

static int numberOfConnectedComponents(ArrayList<ArrayList<Integer>> graph, int V) {

boolean vis[] = new boolean[V];

int count = 0;

for(int i = 0; i<V; i++) {

if(!vis[i]) {

count++;

// bfs(graph, vis, i);

return count;

static void bfs(ArrayList<ArrayList<Integer>> graph, int src, boolean[] vis, int dist[], int prec[]) {

Queue<Integer> q = new LinkedList<>();


vis[src] = true;

dist[src] = 0;

prec[src] = -1;

q.add(src);

while (!q.isEmpty()) {

int cur = q.poll();

// System.out.println(cur);

for(int neighbor: graph.get(cur)) {

if(!vis[neighbor]) {

q.add(neighbor);

vis[neighbor] = true;

dist[neighbor] = dist[cur] + 1;

prec[neighbor] = cur;

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);

// 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;

public class MainClass {

public static void main(String[] args) {

ArrayList<ArrayList<Integer>> graph = new ArrayList<>();

int V = 8;

for(int i =0; i<V; i++) {

graph.add(new ArrayList<>());

addEdge(graph, 0,1); addEdge(graph, 0,2);

addEdge(graph, 5,2); addEdge(graph, 1,5);

// addEdge(graph, 7,4);

addEdge(graph, 3,6);

boolean vis[] = new boolean[V];

// dfs(graph, vis, 0);

// dfsIterative(graph, vis, 0);

System.out.println(numberOfConnectedComponents(graph, V));

static void dfs(ArrayList<ArrayList<Integer>> graph, boolean vis[], int cur) {

vis[cur] = true;

// System.out.println(cur);

for(int neighbor: graph.get(cur)) {


if(!vis[neighbor]) {

dfs(graph, vis, neighbor);

static int numberOfConnectedComponents(ArrayList<ArrayList<Integer>> graph, int V) {

boolean vis[] = new boolean[V];

int count = 0;

for(int i = 0; i<V; i++) {

if(!vis[i]) {

count++;

dfs(graph, vis, i);

return count;

static void dfsIterative(ArrayList<ArrayList<Integer>> graph, boolean vis[], int cur) {

ArrayDeque<Integer> stack = new ArrayDeque<>();

stack.push(cur);

vis[cur] = true;

while (!stack.isEmpty()) {

int a = stack.pop();

System.out.println(a);

for(int neighbor: graph.get(a)) {

if(!vis[neighbor]) {

stack.push(neighbor);

vis[neighbor] = true;

}
}

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);

3.package graphs3;

import java.util.ArrayDeque;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.Queue;

public class MainClass {

public static void main(String[] args) {

ArrayList<ArrayList<Integer>> graph = new ArrayList<>();

int V = 6;
for(int i =0; i<V; i++) {

graph.add(new ArrayList<>());

addEdge(graph, 0, 2); addEdge(graph, 0, 3);

addEdge(graph, 2, 3); addEdge(graph, 2, 1);

addEdge(graph, 3, 1); addEdge(graph, 5, 1);

addEdge(graph, 4, 1); addEdge(graph, 4, 5);

// topologicalOrdering(graph, V);

topologicalOrderingKahnAlgo(graph, V);

static void topologicalOrderingKahnAlgo(ArrayList<ArrayList<Integer>> graph, int V) {

Queue<Integer> q = new LinkedList<>();

int inDegree[] = new int[V];

for(int i = 0; i<V; i++) {

for(int e: graph.get(i)) {

inDegree[e]++;

for (int i = 0; i<V; i++) {

if (inDegree[i] == 0) {

q.add(i);

while (!q.isEmpty()) {

int cur = q.poll();

System.out.println(cur);

for(int neighbor: graph.get(cur)) {


inDegree[neighbor]--;

if(inDegree[neighbor] == 0) {

q.add(neighbor);

static void topologicalOrdering(ArrayList<ArrayList<Integer>> graph, int V) {

boolean vis[] = new boolean[V];

ArrayDeque<Integer> ansStack = new ArrayDeque<>();

for (int i = 0; i<V; i++) {

if(!vis[i]) {

dfs(graph, vis, i, ansStack);

while (!ansStack.isEmpty()) {

System.out.println(ansStack.pop());

static void dfs(ArrayList<ArrayList<Integer>> graph, boolean vis[], int cur,

ArrayDeque<Integer> ansStack) {

vis[cur] = true;

for(int neighbor: graph.get(cur)) {

if(!vis[neighbor]) {

dfs(graph, vis, neighbor, ansStack);

ansStack.push(cur); //push into the stack, while backtracking

}
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;

public class MainClass {

public static void main(String[] args) {

ArrayList<ArrayList<Pair>> graph = new ArrayList<>();

int V = 6;

for(int i = 0; i<V; i++) {

graph.add(new ArrayList<>());

addEdge(graph, 0, 1, 1); addEdge(graph, 0, 4, 1);

addEdge(graph, 1, 2, 2); addEdge(graph, 4, 1, 2);

addEdge(graph, 3, 4, 2); addEdge(graph, 3, 1, 3);

addEdge(graph, 2, 3, 3); addEdge(graph, 5, 1, 5);

addEdge(graph, 3, 5, 6); addEdge(graph, 5, 4, 6);

addEdge(graph, 5, 0, 9);

System.out.println(mstKruskal(graph));
}

static int mstKruskal(ArrayList<ArrayList<Pair>> graph) {

ArrayList<Edge> edges = new ArrayList<>();

for(int i = 0; i<graph.size(); i++) {

ArrayList<Pair> adj = graph.get(i);

for(Pair p: adj) {

edges.add(new Edge(i, p.v, p.w));

Collections.sort(edges);

int v = graph.size();

DisjointSet ds = new DisjointSet(v);

int ans = 0;

int count = 0;

for (Edge cur: edges) {

int src = cur.src;

int dest = cur.dest;

int findSrc = ds.find(src);

int findDest = ds.find(dest);

if(findSrc != findDest) {

ans += cur.wt;

count++;

if(count == v-1) break;

ds.union(src, dest);

return ans;

}
static class Edge implements Comparable<Edge> {

int src, dest, wt;

public Edge(int src, int dest, int wt) {

this.src = src;

this.dest = dest;

this.wt = wt;

@Override

public int compareTo(Edge that) {

return this.wt - that.wt;

static void addEdge(ArrayList<ArrayList<Pair>> graph, int src, int dest, int wt) {

graph.get(src).add(new Pair(dest, wt));

graph.get(dest).add(new Pair(src, wt));

class Pair {

int v, w;

public Pair(int v, int w) {

this.v = v;

this.w = w;

}
}

5.package graphs5;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.PriorityQueue;

public class MainClass {

public static void main(String[] args) {

ArrayList<ArrayList<Pair>> graph = new ArrayList<>();

int V = 6;

for(int i = 0; i<V; i++) {

graph.add(new ArrayList<>());

addEdge(graph, 0, 1, 1); addEdge(graph, 0, 4, 1);

addEdge(graph, 1, 2, 2); addEdge(graph, 4, 1, 2);

addEdge(graph, 3, 4, 2); addEdge(graph, 3, 1, 3);

addEdge(graph, 2, 3, 3); addEdge(graph, 5, 1, 5);

addEdge(graph, 3, 5, 6); addEdge(graph, 5, 4, 6);

addEdge(graph, 5, 0, 9);

// System.out.println(mstPrims(graph, V));

int ans[] = minDistanceDijkstras(graph, V, 0);

for(int i = 0; i<V; i++) {

System.out.println("0 -> "+i +" = " + ans[i]);

static int[] minDistanceDijkstras(ArrayList<ArrayList<Pair>> graph, int V, int src) {

int ans[] = new int[V];

Arrays.fill(ans, Integer.MAX_VALUE);
ans[src] = 0;

PriorityQueue<Edge> pq = new PriorityQueue<>();

pq.add(new Edge(-1, src, 0));

boolean vis[] = new boolean[V];

while (!pq.isEmpty()) {

Edge edge = pq.poll();

int cur = edge.dest;

if(vis[cur]) continue;

vis[cur] = true;

ArrayList<Pair> curNeighbors = graph.get(cur);

for(Pair neighbor: curNeighbors) {

if(!vis[neighbor.v]) {

if(ans[cur] != Integer.MAX_VALUE && ans[neighbor.v] > ans[cur] + neighbor.w) {

ans[neighbor.v] = ans[cur] + neighbor.w;

pq.add(new Edge(cur, neighbor.v, ans[neighbor.v]));

return ans;

static int mstPrims(ArrayList<ArrayList<Pair>> graph, int V) {

PriorityQueue<Edge> pq = new PriorityQueue<>();

pq.add(new Edge(-1, 0, 0));

boolean vis[] = new boolean[V];

int ans = 0;
while (!pq.isEmpty()) {

Edge cur = pq.poll();

if(vis[cur.dest]) continue;

vis[cur.dest] = true;

ans += cur.wt;

ArrayList<Pair> curNeighbors = graph.get(cur.dest);

for(Pair pair: curNeighbors) {

if(!vis[pair.v]) {

pq.add(new Edge(cur.dest, pair.v, pair.w));

return ans;

static class Edge implements Comparable<Edge> {

int src, dest, wt;

public Edge(int src, int dest, int wt) {

this.src = src;

this.dest = dest;

this.wt = wt;

@Override

public int compareTo(Edge that) {

return this.wt - that.wt;


}

static void addEdge(ArrayList<ArrayList<Pair>> graph, int src, int dest, int wt) {

graph.get(src).add(new Pair(dest, wt));

graph.get(dest).add(new Pair(src, wt));

class Pair {

int v, w;

public Pair(int v, int w) {

this.v = v;

this.w = w;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy