Single Source Shortest Paths
Single Source Shortest Paths
Description:-
In the Single Source Shortest Path (SSSP) problem, we aim to find the shortest path from a source node
to all other nodes in a graph. One of the most common algorithms to solve this problem is Dijkstra's
algorithm, which uses a Greedy approach. Dijkstra's algorithm can be implemented using either an
Adjacency Matrix or Adjacency List to represent the graph, and the choice between these
representations affects the performance of the algorithm significantly.
Program:
#include <stdio.h>
#include <limits.h>
#define V 5
min = dist[v];
min_index = v;
return min_index;
int dist[V];
int sptSet[V];
dist[i] = INF;
sptSet[i] = 0;
dist[src] = 0;
sptSet[u] = 1;
int main() {
dijkstraMatrix(graph, 0);
return 0;
OUTPUT:-
0 0
1 10
2 60
3 30
4 70
Program-2
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 5
#define INF INT_MAX
int dest;
int weight;
} Node;
Node* head[V];
} Graph;
Node* n = (Node*)malloc(sizeof(Node));
n->dest = dest;
n->weight = weight;
n->next = NULL;
return n;
Graph* createGraph() {
graph->head[i] = NULL;
return graph;
graph->head[src] = n;
n->next = graph->head[dest];
graph->head[dest] = n;
min = dist[v];
min_index = v;
return min_index;
int dist[V];
int sptSet[V];
dist[i] = INF;
sptSet[i] = 0;
dist[src] = 0;
sptSet[u] = 1;
int v = temp->dest;
temp = temp->next;
int main() {
addEdge(graph, 0, 1, 10);
addEdge(graph, 0, 3, 30);
addEdge(graph, 0, 4, 100);
addEdge(graph, 1, 2, 50);
addEdge(graph, 2, 3, 20);
addEdge(graph, 2, 4, 10);
addEdge(graph, 3, 4, 60);
dijkstraList(graph, 0);
return 0;
OUTPUT:-
0 0
1 10
2 60
3 30
4 70
Result:
For most real-world applications (where graphs are often sparse), the adjacency list representation
combined with a priority queue (min-heap) is the better choice for implementing Dijkstra's algorithm.