# Check if given Path Between Two Nodes of a Graph Represents a Shortest Paths

To check if a given way between two hubs of a chart speaks to a most brief way, one can compare the entirety of edge weights along the given way with the shortest distance between the same combination of hubs by employing a solid most brief way calculation such as Dijkstra's calculation or the Floyd−Warshall calculation. On the off chance that the whole of the edge weights on the given way match the most limited remove, at that point it speaks to a most brief way. Something else: in the event that the entirety of the edge weights is more prominent than the most brief distance, it demonstrates that there exists a shorter way between the two hubs within the chart.

## Methods Used

• Dijsktra’s Algorithm

• Floyd−Warshall Algorithm with Edge Reversal Cost

## Greedy Algorithm

Dijkstra's calculation may be a prevalent chart traversal calculation utilised to discover the most limited way between a source hub and all other hubs in a chart. Within the setting of checking on the off chance that a given way between two hubs speaks to a most limited way, Dijkstra's calculation can be utilised to calculate the most limited separation between those hubs. By running Dijkstra's calculation from the beginning hub, we get the most limited separations for all other hubs. In case the given way matches the most limited distance between the two hubs, at that point it speaks to a substantial and most brief way. Something else: in the event that the given way is longer than the calculated shortest distance, it shows the presence of a shorter way within the chart.

### Algorithm

• Make a shortest path (graph, source, destination):

• Initialise a set gone by to store gone to hubs and a word reference separation to store most limited distances.

• Set the separation of the source hub to boundlessness and all other hubs to boundlessness within the separations dictionary.

• While there are unvisited nodes,

• a. Select the hub with the least separation from the separation word reference and stamp it as visited.

• b. For each neighbouring hub of the current node:

• Calculate the provisional separation by adding the edge weight to the current node's distance.

• If the conditional separation is less than the put−away separation, overhaul the distance.

• If the most brief distance from the source to the goal in separations is break even with the given way length, return genuine (the given way speaks to a most brief way). Something else, return false.

• This calculation utilises Dijkstra's approach to calculate the shortest separations and, after that, compares the most brief distance from the source to the goal with the given way length to decide in the event that it speaks to a most brief way.

### Example

#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;

const int INF = numeric_limits<int>::max();

bool shortestPath(vector<vector<pair<int, int>>>& graph, int source, int destination, int pathLength) {
int numNodes = graph.size();
vector<int> distances(numNodes, INF);
distances[source] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, source);

while (!pq.empty()) {
int u = pq.top().second;
int dist = pq.top().first;
pq.pop();

if (dist > distances[u])
continue;

for (auto& neighbor : graph[u]) {
int v = neighbor.first;
int weight = neighbor.second;

if (dist + weight < distances[v]) {
distances[v] = dist + weight;
pq.emplace(distances[v], v);
}
}
}

return (distances[destination] == pathLength);
}

int main() {
int numNodes = 6;
vector<vector<pair<int, int>>> graph(numNodes);

// Build the graph
graph.emplace_back(1, 2);
graph.emplace_back(2, 5);
graph.emplace_back(3, 4);
graph.emplace_back(4, 1);
graph.emplace_back(3, 2);
graph.emplace_back(4, 3);
graph.emplace_back(5, 6);
graph.emplace_back(5, 2);

int source = 0;
int destination = 5;
int pathLength = 8;

bool isShortestPath = shortestPath(graph, source, destination, pathLength);

if (isShortestPath)
cout << "The given path represents a shortest path." << endl;
else
cout << "The given path does not represent a shortest path." << endl;

return 0;
}



### Output

The given path does not represent a shortest path.


## Floyd−Warshall Algorithm with Edge Reversal Cost

The Floyd−Warshall calculation may be an energetic programming calculation utilised to discover the shortest paths between all pairs of hubs in a chart. Within the setting of checking on the off chance that a given way between two hubs speaks to a most limited way, the Floyd−Warshall calculation can be used to compute the most brief separations between all sets of hubs within the chart. By comparing the most brief distance obtained utilising the calculation with the entirety of edge weights along the given way, we are ready to determine if the given way speaks to a most limited path. If the entirety of the edge weights matches the most brief separation, at that point the given way could be the most limited way between the two hubs within the chart.

### Algorithm

• Make a 2D lattice of measure numNodes x numNodes and initialise it with boundlessness (INF) for all sets of nodes.

• Set the nook−to−nook additives of dist to 0.

• For every coordinated edge (u, v) with weight w in the chart, overhaul dist[u][v] to w and dist[v][u] to w w_reversal, in which w_reversal is the inversion taken via way of means of the edge (v, u).

• Perform the Floyd−Warshall calculation with the taking after settled loops:

• For every midway hub okay from numNodes to 1, do the following:

• For every aggregate of hubs i and j from numNodes to 1, improve dist[i][j] to the least of:

• dist[i][j]

• dist[i] [k] dist[k] [j]

• After the calculation wraps up, dist will contain the most limited separations between all sets of hubs, considering the edge inversion cost.

• To check in the event that a given way between two hubs, source and goal, speaks to a most brief way, compare the length of the given way with distance [source] [destination]. In case they are, the given way speaks to a most limited way.

### Example

#include <iostream>
#include <vector>
using namespace std;

const int INF = 1e9;

void floydWarshall(vector<vector<int>>& graph, int numNodes) {
vector<vector<int>> dist(graph); // Distance matrix initialized with the graph

for (int k = 0; k < numNodes; k++) {
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}

// Printing the shortest distances
cout << "Shortest distances between all pairs of nodes:" << endl;
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
if (dist[i][j] == INF)
cout << "INF ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}

int main() {
int numNodes = 4; // Number of nodes

// Adjacency matrix representation of the graph with edge weights and edge reversal costs
vector<vector<int>> graph = {
{0, 5, INF, 10},
{INF, 0, 3, INF},
{INF, INF, 0, 1},
{INF, INF, INF, 0}
};

floydWarshall(graph, numNodes);

return 0;
}


### Output

Shortest distances between all pairs of nodes:
0 5 8 9
INF 0 3 4
INF INF 0 1
INF INF INF 0


## Conclusion

This article examines how to check on the off chance that a given way between two hubs of a chart speaks to a most limited way. It clarifies two approaches: Dijkstra's Calculation and Floyd−Warshall Calculation with Edge Inversion Fetched. The code usage in C illustrates these calculations. It also gives a brief clarification of the calculations and their utilisation. The article points out to assist perusers in getting the concept of finding the most limited ways in charts and deciding if a given way is without a doubt the most brief.

Updated on: 13-Jul-2023

36 Views 