Category: Shortest Path

Implementation of Johnson’s algorithm for all-pairs shortest paths

import java.util.ArrayList;import java.util.Arrays;  public class Graph {    private static class Neighbour {        int destination;        int weight;          Neighbour(int destination, int weight)        {            this.destination = destination;            this.weight = weight;        }    }      private int vertices;    private final ArrayList        adjacencyList;                  public Graph(int vertices)    {        this.vertices = vertices;          adjacencyList = new ArrayList(vertices);        for (int i = 0; i < vertices; i++)            adjacencyList.add(new ArrayList());    }                  public Graph(int vertices, int[][] adjacencyMatrix)    {        this(vertices);          for (int i = 0; i < vertices; i++) {            for (int j = […]

Continue Reading

Shortest distance between two nodes in Graph by reducing weight of an edge by half

  #include using namespace std;  vector graph[100001];  vector edges;  void add_edge(int u, int v, int w){    graph[u].push_back({ v, w });    graph[v].push_back({ u, w });    edges.push_back({ u, v, w });}  vector dijsktras(int src, int N){            vector dis(N, INT_MAX);      vector vis(N, false);              priority_queue        pq;    pq.push({ 0, src });    dis[src] = 0;              while (!pq.empty()) {                  auto cur = pq.top();        pq.pop();                  int node = cur.second;        int weight = cur.first;                  if (vis[node])            continue;        vis[node] = true;                          for (auto child : graph[node]) {                                                              if (dis[child.first]                > […]

Continue Reading

Difference between the shortest and second shortest path in an Unweighted Bidirectional Graph

#include using namespace std;  void get_edges(int s, vector& edges, vector p){    if (s == -1)        return;    get_edges(p[s], edges, p);    edges.push_back(s);}  void dist_helper(vector graph, vector& d,                 int v1, int v2, int n){        vector v(n, 0);          queue q;    q.push(make_pair(0, 0));    v[0] = 1;          while (!q.empty()) {        auto a = q.front();        q.pop();        for (int i : graph[a.first]) {            if ((i == v1 && a.first == v2)                || (i == v2 && a.first == v1))                continue;            if (v[i] […]

Continue Reading

Shortest Path Properties

Shortest Path PropertiesThe shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized. The shortest path between any two nodes of the graph can be founded using many algorithms, such as Dijkstra’s algorithm, Bellman-Ford […]

Continue Reading

Number of shortest paths in an Undirected Weighted Graph

import java.io.*;import java.util.*;class GFG {          static class Node implements Comparator {                  public int node;                          public int cost;          public Node() {}                  public Node(int node, int cost)        {            this.node = node;            this.cost = cost;        }                  @Override        public int compare(Node node1, Node node2)        {            if (node1.cost < node2.cost)                return -1;            if (node1.cost > node2.cost)                return 1;            return 0;        }    }              static void addEdge(ArrayList adj,                        int x, int y, int w)    {        adj.get(x).add(new Node(y, w));        adj.get(y).add(new Node(x, w));    }              static void dijkstra(ArrayList adj,                         int src, int […]

Continue Reading