
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dijkstra's Algorithm for Single Source Shortest Path
The D'Esopo?Pape technique works with a single source vertex as a starting point to find the shortest path between that vertex and all other vertices in a directed graph. This method outperforms the conventional Bellman?Ford approach for charts with negative edge weights. During execution, this technique swiftly chooses the vertices that are spaced the closest together using a priority queue.
By iteratively relaxing edges and updating distances when a shorter path is identified, the D'Esopo?Pape method finds the shortest pathways in a graph. The approach optimises efficiency and reduces needless calculations by using a priority queue to choose the vertices with the least tentative distances.Among the many uses for the D'Esopo?Pape algorithm are those in computer networking, transportation planning, and data centre routing.
Methods Used
D'Esopo?pape Algorithm
D'Esopo?Pape Algorithm
The D'Esopo?Pape method iteratively relaxes edges and updates distances to find a graph's shortest pathways. The approach saves superfluous calculations and increases efficiency by selecting vertices with the least tentative distances using a priority queue.
Algorithm
Start with a directed graph having numVertices vertices.
Initialise a distance array distance of size numVertices and set all distances to infinity except the source vertex, which is 0.
Create a priority queue pq for vertices and tentative distances in increasing order. Insert the zero?distance source vertex into pq.
Do this if pq is not empty:
Find the vertex u closest to pq.
For each u?outgoing edge:
v is e's destination vertex.
Update distance[v] to distance[u] + weight if it is less than distance[v]. Insert or update the vertex v with its revised tentative distance in pq.
Print the source vertex's shortest distances.
Example
#include <iostream> #include <vector> #include <queue> #include <climits> // Structure to represent a directed edge struct Edge { int source, destination, weight; }; // Function to compute single-source shortest paths using D'Esopo-Pape algorithm void shortestPaths(const std::vector<Edge>& edges, int numVertices, int source) { std::vector<int> distance(numVertices, INT_MAX); // Initialize distances to infinity distance[source] = 0; // Distance from source to itself is 0 // Priority queue to store vertices with their tentative distances std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq; pq.push(std::make_pair(distance[source], source)); while (!pq.empty()) { int u = pq.top().second; pq.pop(); // Traverse all adjacent vertices of u for (const Edge& edge : edges) { int v = edge.destination; int weight = edge.weight; // Relax the edge if a shorter path is found if (distance[u] + weight < distance[v]) { distance[v] = distance[u] + weight; pq.push(std::make_pair(distance[v], v)); } } } // Print the shortest distances from the source std::cout << "Shortest distances from source " << source << ":\n"; for (int i = 0; i < numVertices; ++i) { std::cout << "Vertex " << i << ": " << distance[i] << '\n'; } } int main() { // Example usage int numVertices = 5; std::vector<Edge> edges = { {0, 1, 10}, {0, 2, 5}, {1, 3, 1}, {2, 1, 3}, {2, 3, 9}, {2, 4, 2}, {3, 4, 4}, {4, 3, 6} }; int source = 0; shortestPaths(edges, numVertices, source); return 0; }
Output
Shortest distances from source 0: Vertex 0: 0 Vertex 1: 3 Vertex 2: 5 Vertex 3: 1 Vertex 4: 2
Conclusion
Finally, the D'Esopo?Pape method solves the directed graph single?source shortest path issue. The method effectively uses a priority queue and iteratively relaxing edges to calculate the shortest paths between a source vertex and all graph vertices.The shortest route algorithms developed by Dijkstra and Bellman?Ford cannot handle negative edge weights. This makes it suited for many real?world situations where edge weights imply costs or penalties.The D'Esopo?Pape algorithm optimises efficiency and accuracy while minimising calculations. Transportation, computing, and financial networks can use it.The D'Esopo?Pape algorithm helps us plan routes, optimise networks, and allocate resources. It solves complicated shortest route problems by handling directed graphs with negative weights well.