Y Even
Y Even
Theory:
A road network can be represented as a graph, where nodes (vertices) correspond to road
junctions, and edges represent the road segments connecting these junctions. Each edge
carries a positive weight, which may represent factors such as distance, time, or cost of
travel. The challenge of determining the most efficient route across this network—whether to
minimize time, distance, or cost—is known as the shortest path problem. This problem is
critical in applications like navigation systems, where finding the optimal route between
locations is essential, or in logistics, where cost efficiency in delivery planning is a priority.
Solving the shortest path problem effectively becomes even more important in large
networks with millions of nodes and edges.
In a weighted graph, each edge has a numerical value or weight. For road networks, nodes
symbolize intersections, edges represent the connecting roads, and weights can indicate
travel time, distance, or cost. For example, if weights represent time, the goal is to minimize
travel time. If they represent distance, the objective is to minimize the total distance covered.
Road networks can be modeled using two types of graphs: undirected graphs, which
represent roads that allow two-way travel, and directed graphs, used for one-way streets
where travel is restricted to a specific direction.
The shortest path problem involves finding a route between two nodes that minimizes the
sum of the edge weights along that path. Two main variations of this problem exist: the
single-source shortest path, where the goal is to find the shortest path from a given node to
all other nodes, and the all-pairs shortest path, where the objective is to find the shortest path
between every pair of nodes. Given the vast size and complexity of road networks, solving
this problem efficiently is critical to ensure computation remains manageable.
Dijkstra's Algorithm is the most widely used solution for graphs with non-negative weights.
Created by Edsger Dijkstra in 1956, the algorithm follows a greedy approach. It works by
selecting the node with the smallest known distance from the source and exploring its
neighbors to find shorter paths. This process continues until all reachable nodes have been
processed.
The algorithm begins by setting the distance from the source node to itself as 0 and to all
other nodes as infinity. A priority queue (min-heap) is used to keep track of nodes based on
their distance from the source. As the algorithm explores each node’s neighbors, it updates
the shortest known distance if a shorter path is found and adds that neighbor to the queue if
its distance was modified. The process terminates when all nodes are visited or when the
queue contains no more nodes with finite distances.
Dijkstra's Algorithm is efficient, with a time complexity of O((V + E) log V), where V
represents the number of nodes and E the number of edges. It performs particularly well on
sparse graphs, where the number of edges is significantly smaller than the number of
possible node connections. While optimal for graphs with positive edge weights, Dijkstra's
Algorithm is not suited for graphs with negative weights.
In real-world road networks, certain roads, such as highways, are more crucial for long-
distance travel. Algorithms can be further optimized by prioritizing these major routes, a
concept known as highway dimension. By focusing on key roads, pathfinding efficiency
improves, especially in large-scale networks.
In conclusion, the shortest path problem is a central issue in transportation and logistics, and
Dijkstra’s Algorithm provides a practical and efficient solution for finding the shortest
paths in graphs with positive edge weights, making it ideal for road networks.
Code:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
typedef pair<int, int> pii; // Pair to represent a node and its distance (distance, node)
// Min-heap priority queue to select the edge with the minimum weight
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, src}); // Push the source node into the queue (distance, node)
while (!pq.empty()) {
int u = pq.top().second; // Get the node with the smallest distance
int d = pq.top().first; // Get the corresponding distance
pq.pop(); // Remove the node from the queue
int main() {
int n, m; // n: number of nodes (junctions), m: number of edges (road segments)
cout << "Enter number of junctions (nodes) and road segments (edges): ";
cin >> n >> m;
cout << "Enter the road segments (node1 node2 weight) one by one:\n";
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w}); // Directed edge from u to v with weight w
adj[v].push_back({u, w}); // For undirected graphs, add the reverse edge
}
// Find shortest path from each node (junction) to all other nodes
for (int src = 0; src < n; ++src) {
vector<int> distances = dijkstra(src, n, adj);
cout << "From junction " << src << ":\n";
for (int i = 0; i < n; ++i) {
cout << "To junction " << i << ": " << distances[i] << endl;
}
cout << "--------------------------\n";
}
return 0;
}
Output :
Learning Outcome:
• Understanding how graphs are represented and the key shortest path algorithms like
Dijkstra's and Bellman-Ford.
• Investigating the concept of highway dimension and its advantages for optimizing
pathfinding.
Analysis and
Design of
Programme B. Tech CSE Course Name
Algorithm
Marking Criteria
Performance (F) 3
Result (G) 2
Total 10