Graphs in Java
Graphs in Java
import java.util.*;
}
}
import java.util.*;
// Test case 1
System.out.println("Test Case 1:");
int n1 = 4;
int m1 = 3;
int[][] adj1 = new int[n1 + 1][n1 + 1];
for (int i = 0; i < m1; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
adj1[u][v] = 1;
adj1[v][u] = 1;
}
printGraph(adj1);
// Test case 2
System.out.println("\nTest Case 2:");
int n2 = 5;
int m2 = 4;
int[][] adj2 = new int[n2 + 1][n2 + 1];
for (int i = 0; i < m2; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
adj2[u][v] = 1;
adj2[v][u] = 1;
}
printGraph(adj2);
// scanner.close();
}
System.out.println("\nGraph:");
for (int i = 1; i < adj.length; i++) {
System.out.print(i + ": ");
for (int j = 1; j < adj[i].length; j++) {
if (adj[i][j] == 1) {
System.out.print(j + " ");
}
}
System.out.println();
}
System.out.println();
}
}
import java.util.*;
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.close();
}
}
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.close();
}
static class Edge {
int destination;
int weight;
ArrayList < Integer > bfs = new ArrayList < > ();
boolean vis[] = new boolean[V];
Queue < Integer > q = new LinkedList < > ();
q.add(0);
vis[0] = true;
while (!q.isEmpty()) {
Integer node = q.poll();
bfs.add(node);
return bfs;
}
// DFS
class Solution {
// Function to return a list containing the DFS traversal of the graph.
public static void dfs(int node, boolean vis[], ArrayList<ArrayList<Integer>>
adj,
ArrayList<Integer> ls) {
Number of provinces
class Solution {
// dfs traversal function
private static void dfs(int node,
ArrayList<ArrayList<Integer>> adjLs ,
int vis[]) {
vis[node] = 1;
for(Integer it: adjLs.get(node)) {
if(vis[it] == 0) {
dfs(it, adjLs, vis);
}
}
}
static int numProvinces(ArrayList<ArrayList<Integer>> adj, int V) {
ArrayList<ArrayList<Integer>> adjLs = new ArrayList<ArrayList<Integer>>();
for(int i = 0;i<V;i++) {
adjLs.add(new ArrayList<Integer>());
}
import java.util.*;
class Solution {
private static void dfs(int node, int vis[], Stack<Integer> st,
ArrayList<ArrayList<Integer>> adj) {
vis[node] = 1;
for (int it : adj.get(node)) {
if (vis[it] == 0)
dfs(it, vis, st, adj);
}
st.push(node);
}
//using bfs
import java.util.*;
class Solution {
public ArrayList<Integer> topoSort(int V, ArrayList<Integer>[] adj) {
int[] inDegree = new int[V];
Queue<Integer> queue = new LinkedList<>();
return result;
}
}
adj[2].add(3);
adj[3].add(1);
adj[4].add(0);
adj[4].add(1);
adj[5].add(0);
adj[5].add(2);
//https://www.geeksforgeeks.org/problems/shortest-path-in-undirected-graph/1
class Pair {
int first, second;
Pair(int _first, int _second) {
this.first = _first;
this.second = _second;
}
}
//User function Template for Java
class Solution {
private void topoSort(int node, ArrayList < ArrayList < Pair >> adj,
int vis[], Stack < Integer > st) {
//This is the function to implement Topological sort.
vis[node] = 1;
for (int i = 0; i < adj.get(node).size(); i++) {
int v = adj.get(node).get(i).first;
if (vis[v] == 0) {
topoSort(v, adj, vis, st);
}
}
st.add(node);
}
public int[] shortestPath(int N, int M, int[][] edges) {
ArrayList < ArrayList < Pair >> adj = new ArrayList < > ();
for (int i = 0; i < N; i++) {
ArrayList < Pair > temp = new ArrayList < Pair > ();
adj.add(temp);
}
//We create a graph first in the form of an adjacency list.
dist[0] = 0;
while (!st.isEmpty()) {
int node = st.peek();
st.pop();
// BFS Implementation
Queue<Integer> q = new LinkedList<>();
q.add(src);
while(!q.isEmpty()) {
int node = q.peek();
q.remove();
for(int it : adj.get(node)) {
if(dist[node] + 1 < dist[it]) {
dist[it] = 1 + dist[node];
q.add(it);
}
}
}
// Updated shortest distances are stored in the resultant array ‘ans’.
// Unreachable nodes are marked as -1.
for(int i = 0;i<n;i++) {
if(dist[i] == 1e9) {
dist[i] = -1;
}
}
return dist;
}
}
class Pair{
int node;
int distance;
public Pair(int distance,int node){
this.node = node;
this.distance = distance;
}
}
//User function Template for Java
class Solution
{
//Function to find the shortest distance of all the vertices
//from the source vertex S.
static int[] dijkstra(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj, int
S)
{
// Create a priority queue for storing the nodes as a pair {dist, node
// where dist is the distance from source to the node.
PriorityQueue<Pair> pq =
new PriorityQueue<Pair>((x,y) -> x.distance - y.distance);
// Now, pop the minimum distance node first from the min-heap
// and traverse for all its adjacent nodes.
while(pq.size() != 0) {
int dis = pq.peek().distance;
int node = pq.peek().node;
pq.remove();