0% found this document useful (0 votes)
10 views9 pages

ria exp 6

The document details an experiment on the Single Source Shortest Path Problem using Dijkstra's and Bellman-Ford algorithms. It explains the theory, implementation, and time complexity of both algorithms, highlighting that Dijkstra's is efficient for dense graphs with non-negative weights, while Bellman-Ford can handle negative weights but is slower. The conclusion emphasizes the importance of selecting the appropriate algorithm based on the graph's characteristics.

Uploaded by

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

ria exp 6

The document details an experiment on the Single Source Shortest Path Problem using Dijkstra's and Bellman-Ford algorithms. It explains the theory, implementation, and time complexity of both algorithms, highlighting that Dijkstra's is efficient for dense graphs with non-negative weights, while Bellman-Ford can handle negative weights but is slower. The conclusion emphasizes the importance of selecting the appropriate algorithm based on the graph's characteristics.

Uploaded by

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

Name: Ria Talsania

UID: 2023300242

Experiment No. 6

Date of Submission 12/3/25

AIM: Experiment on Single Source Shortest Path Problem

Program 1

Problem: 1.​ To find the shortest path from a single source to all other nodes in a
weighted graph using Dijkstra’s algorithm
2.​ Calculate time complexity for it.

Theory: Dijkstra’s Algorithm is a greedy approach to finding the shortest paths


from a single source to all other vertices in a weighted graph with
non-negative edge weights. The algorithm initializes all distances to
infinity, except for the source (set to 0). At each step, it selects the node
with the smallest known distance, updates its neighbors’ distances if a
shorter path is found, and continues until all nodes are processed.

Algorithm :

i.​ Initialize all distances to infinity (except source)


ii.​ Keep track of visited nodes with an array
iii.​ After each iteration, select unvisited node with least distance
iv.​ Update distance of neighbours according to current node
v.​ Repeat V-1 times (to ensure all vertices are visited)

Program: #include <stdio.h>


#include <limits.h>

#define V 5

int md(int dist[], int visited[]) {


int min = INT_MAX, minindex;
for (int v = 0; v < V; v++) {
if (visited[v] == 0 && dist[v] <= min) {
min = dist[v], minindex = v;
}
}

return minindex;
}

void dj(int graph[V][V], int start) {


int dist[V];
int visited[V];

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


dist[i] = INT_MAX;
visited[i] = 0;
}

dist[start] = 0;

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

int u = md(dist, visited);

visited[u] = 1;

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

if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]


+ graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
printf("Vertex \t Distance from Source\n");
for (int i = 0; i < V; i++) {
printf("%d \t %d\n", i, dist[i]);
}

int main() {

int graph[V][V] = {
{0, 0, 8, 0, 2},
{0, 0, 0, 2, 7},
{8, 0, 0, 1, 5},
{0, 2, 1, 0, 3},
{2, 7, 5, 3, 0}
};
dj(graph, 0);

return 0;
}

Example 1

Output:
Observations:

The time complexity of Dijkstra’s algorithm (using an array for visited nodes) is primarily
determined by two key operations: finding the minimum-distance unvisited node and relaxing
edges. In each iteration, the algorithm searches for the unvisited node with the smallest known
distance, which takes O(V) time. Since this selection process repeats for V vertices, it contributes
O(V²) to the overall complexity. This approach is suitable for dense graphs where the number of
edges approaches V², but less efficient for sparse graphs compared to the priority queue-based
approach.

Program 2

Problem: 1.​ To find the shortest path from a single source to all other nodes in a
weighted graph using Bellman-Ford’s
2.​ Calculate time complexity for it.

Theory: Bellman-Ford Algorithm is a dynamic programming approach to


finding the shortest paths from a single source to all other vertices in a
weighted graph, including those with negative edge weights. Unlike
Dijkstra’s algorithm, it can handle negative weight cycles by detecting if
distances continue to decrease after V-1 iterations. The algorithm relaxes all
edges V-1 times to ensure the shortest paths are computed.

i.​ Initialize all distances to infinity (except source)


ii.​ For each edge (src,dest,w) update dist[dest] = dist[src]+ w if smaller
iii.​ Repeat V-1 times
iv.​ Run 1 more iteration to check for negative weight cycles. If
distance decreases, negative cycle exists

Program: #include <stdio.h>


#include <limits.h>

#define V 6
#define E 8

struct Edge {
int src, dest, weight;
};

void printTable(int step, int dist[]) {


printf("Step %d:\n", step);
printf("Vertex Cost\n");
for (int i = 0; i < V; i++) {
if (dist[i] == INT_MAX)
printf("%d --\n", i);
else
printf("%d %d\n", i, dist[i]);
}
printf("\n");
}

void BellmanFord(struct Edge edges[], int src) {


int dist[V];

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


dist[i] = INT_MAX;
dist[src] = 0;

printf("\nBellman Ford's Algorithm:\n");

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


for (int j = 0; j < E; j++) {
int u = edges[j].src;
int v = edges[j].dest;
int weight = edges[j].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
}
}
// printTable(i, dist);
}

for (int j = 0; j < E; j++) {


int u = edges[j].src;
int v = edges[j].dest;
int weight = edges[j].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
printf("Graph contains a negative-weight cycle!\n");
return;
}
}

printf("Final Shortest Paths from Source (%d):\n", src);


printf("Vertex Cost\n");
for (int i = 0; i < V; i++) {
if (dist[i] == INT_MAX)
printf("%d --\n", i);
else
printf("%d %d\n", i, dist[i]);
}

int main() {

struct Edge edges[E] = {


{0, 1, 4}, {0, 2, 5}, {1, 3, -2}, {1, 4, 3},
{2, 3, 3}, {2, 5, 1}, {3, 4, 2}, {5, 1, -3}
};

BellmanFord(edges, 0);
return 0;
}

Example 1

Output:
Observations:
The time complexity of the Bellman-Ford algorithm is primarily influenced by two operations: edge
relaxation and negative cycle detection. The algorithm performs V - 1 iterations, where V is the
number of vertices, and in each iteration, it relaxes all E edges. Consequently, the overall time
complexity is O(V × E). While Bellman-Ford is well-suited for graphs with fewer edges and those
containing negative weights, it is less efficient than Dijkstra’s algorithm for dense graphs.

Conclusion:
From this experiment, I learned about the efficiency and applicability of Dijkstra’s and Bellman-Ford’s
algorithms for shortest path calculations. Dijkstra’s algorithm, with a time complexity of O(V²) when using
an array, is highly efficient for dense graphs but cannot handle negative weights. On the other hand,
Bellman-Ford, with a complexity of O(V × E), can accommodate negative weights and detect negative
cycles but is significantly slower for large graphs. The choice between these algorithms depends on the
graph’s structure—Dijkstra is preferable for non-negative weighted graphs, while Bellman-Ford is essential
when negative weights or cycle detection is required.

You might also like