Home > Article > Backend Development > Checks whether the path between two nodes in the given graph represents the shortest path
To check if a given path between two centers of a graph conforms to the shortest path, you can combine the entire edge weights along the given path with the same center by using a reliable shortest path The shortest distance between them is calculated using a comparative method, such as the Dijkstra calculation or the Floyd−Warshall calculation. If all edge weights on a given path match the most limited deletion, then it represents the simplest path. Also: If the overall edge weight is more prominent than the shortest distance, it indicates that there is a short distance between the two centers in the graph.
Dijkstra’s algorithm
Floyd−Warshall algorithm with marginal inversion cost
Dijkstra's calculation is perhaps a popular graph traversal calculation used to find the most limited path between a source center and all other centers in a graph. In the case of checking whether a given path between two centers is related to the most finite path, Dijkstra's calculation can be used to calculate the most finite separation between these centers. By running Dijkstra's calculation from the starting hub, we get the most finite intervals for all other hubs. If a given route matches the most limited distance between two hubs, then it represents a substantial and shortest route. Others: If the given route is longer than the calculated shortest distance, it indicates that a shorter route exists in the chart.
Create the shortest path (graph, source, destination):
Initialize a set of "past" to store the distance to the center, and initialize a word reference interval to store the most limited distance.
Set the source hub's spacing to infinity and all other hubs' spacing to infinity in the separator dictionary.
Although there are unvisited nodes,
a. The center with the smallest distance from the separator word reference is selected and marked as visited.
b. For each neighbor hub of the current node:
The temporary interval is calculated by adding the edge weight to the distance of the current node.
If the condition spacing is smaller than the storage spacing, then the inspection distance.
Returns true if the shortest distance from source to destination in separation breaks even with the given path length (the given path represents the shortest path). Otherwise, return false.
This calculation utilizes Dijkstra's method to calculate the shortest interval and then compares the shortest distance from the source to the destination with the given path length to determine if it is the shortest path.
#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[0].emplace_back(1, 2); graph[0].emplace_back(2, 5); graph[1].emplace_back(3, 4); graph[1].emplace_back(4, 1); graph[2].emplace_back(3, 2); graph[3].emplace_back(4, 3); graph[3].emplace_back(5, 6); graph[4].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; }
The given path does not represent a shortest path.
Make a 2D lattice measuring numNodes x numNodes and initialize it to infinity (INF) for all node sets.
Set the corner-to-corner addition of dist to 0.
For each coordination edge (u, v) with weight w in the graph, completely modify dist[u][v] to w and modify dist[v][u] to w w_reversal, where w_reversal is obtained by reversing the edge (v, u).
Perform the Floyd−Warshall calculation after the canned loop:
For each halfway hub from numNodes to 1, do the following:
For each aggregate of hubs i and j from numNodes to 1, refine dist[i][j] to the minimum of the following values:
Distance[i][j]
Distance[i][k]Distance[k][j]
After the calculation is complete, dist will contain the most limited separation between all hub groups, taking into account edge inversion costs.
To check if a given route between two hubs (source and destination) is the shortest route, compare the length of the given route with the distance [source][destination]. If so, the given way is the most limited way.
#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; }
Shortest distances between all pairs of nodes: 0 5 8 9 INF 0 3 4 INF INF 0 1 INF INF INF 0
This article explores how to check whether a given path between two centers of a graph represents the most finite path. It illustrates two methods: the Dijkstra calculation and the Floyd-Warshall calculation for obtaining edge inversions. Code usage in C illustrates these calculations. It also briefly explains calculations and their uses. This article is intended to help readers understand how to find the most limited method in a diagram and determine whether a given method is undoubtedly the simplest.
The above is the detailed content of Checks whether the path between two nodes in the given graph represents the shortest path. For more information, please follow other related articles on the PHP Chinese website!