0% found this document useful (0 votes)
41 views7 pages

Single Source Shortest Paths

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Single Source Shortest Paths

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AIM:-Compare the performance of Single Source Shortest Paths using Greedy method when the graph is

represented by adjacency matrix and adjacency lists

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.

Using Adjacency Matrix

Program:

#include <stdio.h>

#include <limits.h>

#define V 5

#define INF INT_MAX

int minDistance(int dist[], int sptSet[]) {

int min = INF, min_index;

for (int v = 0; v < V; v++) {

if (sptSet[v] == 0 && dist[v] <= min) {

min = dist[v];

min_index = v;

return min_index;

void dijkstraMatrix(int graph[V][V], int src) {

int dist[V];
int sptSet[V];

for (int i = 0; i < V; i++) {

dist[i] = INF;

sptSet[i] = 0;

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {

int u = minDistance(dist, sptSet);

sptSet[u] = 1;

for (int v = 0; v < V; v++) {

if (!sptSet[v] && graph[u][v] && dist[u] != INF &&

dist[u] + graph[u][v] < dist[v]) {

dist[v] = dist[u] + graph[u][v];

printf("Vertex Distance from Source\n");

for (int i = 0; i < V; i++)

printf("%d \t\t %d\n", i, dist[i]);

int main() {

int graph[V][V] = { {0, 10, 0, 30, 100},

{10, 0, 50, 0, 0},

{0, 50, 0, 20, 10},


{30, 0, 20, 0, 60},

{100, 0, 10, 60, 0} };

dijkstraMatrix(graph, 0);

return 0;

OUTPUT:-

Vertex Distance from Source

0 0

1 10

2 60

3 30

4 70

Program-2

Using Adjacency List

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define V 5
#define INF INT_MAX

typedef struct Node {

int dest;

int weight;

struct Node* next;

} Node;

typedef struct Graph {

Node* head[V];

} Graph;

Node* newNode(int dest, int weight) {

Node* n = (Node*)malloc(sizeof(Node));

n->dest = dest;

n->weight = weight;

n->next = NULL;

return n;

Graph* createGraph() {

Graph* graph = (Graph*)malloc(sizeof(Graph));

for (int i = 0; i < V; i++) {

graph->head[i] = NULL;

return graph;

void addEdge(Graph* graph, int src, int dest, int weight) {

Node* n = newNode(dest, weight);


n->next = graph->head[src];

graph->head[src] = n;

n = newNode(src, weight); // for undirected graph

n->next = graph->head[dest];

graph->head[dest] = n;

int minDistance(int dist[], int sptSet[]) {

int min = INF, min_index;

for (int v = 0; v < V; v++) {

if (sptSet[v] == 0 && dist[v] <= min) {

min = dist[v];

min_index = v;

return min_index;

void dijkstraList(Graph* graph, int src) {

int dist[V];

int sptSet[V];

for (int i = 0; i < V; i++) {

dist[i] = INF;

sptSet[i] = 0;

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, sptSet);

sptSet[u] = 1;

Node* temp = graph->head[u];

while (temp != NULL) {

int v = temp->dest;

if (!sptSet[v] && dist[u] != INF &&

dist[u] + temp->weight < dist[v]) {

dist[v] = dist[u] + temp->weight;

temp = temp->next;

printf("Vertex Distance from Source\n");

for (int i = 0; i < V; i++)

printf("%d \t\t %d\n", i, dist[i]);

int main() {

Graph* graph = createGraph();

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:-

Vertex Distance from Source

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.

You might also like