0% found this document useful (0 votes)
53 views

DAA Practical Question

Akshay wants to have a party and needs to share the route to his house with his friends. He creates a graph of locations and implements Dijkstra's algorithm to find the shortest paths from each friend's location to his house. The document includes code to implement Dijkstra's algorithm to solve this.

Uploaded by

mishaverma433
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

DAA Practical Question

Akshay wants to have a party and needs to share the route to his house with his friends. He creates a graph of locations and implements Dijkstra's algorithm to find the shortest paths from each friend's location to his house. The document includes code to implement Dijkstra's algorithm to solve this.

Uploaded by

mishaverma433
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

I. After end term examination, Akshay wants to party with his friends.

All his friends are living as paying


guest and it has been decided to first gather at Akshay’s house and then move towards party location. The
problem is that no one knows the exact address of his house in the city. Akshay as a computer science
wizard knows how to apply his theory subjects in his real life and came up with an amazing idea to help his
friends. He draws a graph by looking in to location of his house and his friends’ location (as a node in the
graph) on a map. He wishes to find out shortest distance and path covering that distance from each of his
friend’s location to his house and then whatsapp them this path so that they can reach his house in
minimum time. Akshay has developed the program that implements Dijkstra’s algorithm but not sure
about correctness of results. Can you also implement the same algorithm and verify the correctness of
Akshay’s results?

#include<stdio.h>
#include<limits.h>

#define MAX 1000


#define INF INT_MAX

int distance[MAX], visited[MAX], pred[MAX];

int findMinVertex(int n) {
int minVertex = -1;
for(int i = 0; i < n; i++){
if(!visited[i] && (minVertex == -1 || distance[i] < distance[minVertex])){
minVertex = i;
}
}
return minVertex;
}

void printPath(int vertex) {


if(pred[vertex] == -1) {
printf("%d ", vertex + 1);
return;
}

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


printPath(pred[vertex]);
printf("%d ", vertex + 1);
}

void dijkstra(int graph[MAX][MAX], int n, int src) {


for(int i = 0; i < n; i++) {
distance[i] = INF;
visited[i] = 0;
pred[i] = -1;
}
distance[src] = 0;
for(int i = 0; i < n; i++) {
int minVertex = findMinVertex(n);
visited[minVertex] = 1;
for(int j = 0; j < n; j++) {
if(graph[minVertex][j] != 0 && !visited[j]) {
int currD = distance[minVertex] + graph[minVertex][j];
if(currD < distance[j]) {
distance[j] = currD;
pred[j] = minVertex;
}
}
}
}
for(int i = 0; i < n; i++) {
printf("%d : ", i + 1);
printPath(i);
printf(": %d\n", distance[i]);
}
}

int main() {

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


int n;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &n);
int graph[MAX][MAX];
printf("Enter the adjacency matrix of the graph:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
int src;
printf("Enter the source vertex (Akshay's house): ");
scanf("%d", &src);
src--; // Adjust for 0 indexing
dijkstra(graph, n, src);
return 0;
}

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


OUTPUT

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


II. After end term examination, Akshay wants to party with his friends. All his friends are living as paying
guest and it has been decided to first gather at Akshay’s house and then move towards party location. The
problem is that no one knows the exact address of his house in the city. Akshay as a computer science
wizard knows how to apply his theory subjects in his real life and came up with an amazing idea to help his
friends. He draws a graph by looking in to location of his house and his friends’ location (as a node in the
graph) on a map. He wishes to find out shortest distance and path covering that distance from each of his
friend’s location to his house and then whatsapp them this path so that they can reach his house in
minimum time. Design an algorithm and implement it using a program to solve problem using Bellman-
Ford's shortest path algorithm.

#include<stdio.h>
#include<limits.h>

#define MAX 1000


#define INF INT_MAX

int distance[MAX], pred[MAX];

void printPath(int vertex) {


if(pred[vertex] == -1) {
printf("%d ", vertex + 1);
return;
}
printPath(pred[vertex]);
printf("%d ", vertex + 1);
}

void bellmanFord(int graph[MAX][MAX], int n, int src) {


int i, j, k;
for(i = 0; i < n; i++) {
distance[i] = INF;
pred[i] = -1;
}
Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964
distance[src] = 0;

for(i = 0; i < n-1; i++) {


for(j = 0; j < n; j++) {
for(k = 0; k < n; k++) {
if(graph[j][k] != 0 && distance[j] != INF && distance[j] + graph[j][k] < distance[k]) {
distance[k] = distance[j] + graph[j][k];
pred[k] = j;
}
}
}
}

for(i = 0; i < n; i++) {


for(j = 0; j < n; j++) {
if(graph[i][j] != 0 && distance[i] != INF && distance[i] + graph[i][j] < distance[j]) {
printf("Graph contains negative weight cycle\n");
return;
}
}
}

for(i = 0; i < n; i++) {


printf("%d : ", i + 1);
printPath(i);
printf(": %d\n", distance[i]);
}
}

int main() {
int n;
printf("Enter the number of vertices in the graph: ");

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


scanf("%d", &n);
int graph[MAX][MAX];
printf("Enter the adjacency matrix of the graph:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
int src;
printf("Enter the source vertex (Akshay's house): ");
scanf("%d", &src);
src--; // Adjust for 0 indexing
bellmanFord(graph, n, src);
return 0;
}

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


OUTPUT

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


III. Given a directed graph with two vertices ( source and destination). Design an algorithm and implement
it using a program to find the weight of the shortest path from source to destination with exactly k edges
on the path.

#include<stdio.h>
#include<limits.h>

#define MAX_V 100


#define MAX_K 100
#define INF INT_MAX

int min(int a, int b) {


return (a < b)? a : b;
}

void shortestPath(int graph[MAX_V][MAX_V], int V, int src, int dst, int K) {


int dp[MAX_V][MAX_V][MAX_K+1];

for (int e = 0; e <= K; e++) {


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
dp[i][j][e] = INF;

if (e == 0 && i == j)
dp[i][j][e] = 0;
if (e == 1 && graph[i][j] != INF)
dp[i][j][e] = graph[i][j];
if (e > 1) {
for (int a = 0; a < V; a++) {
if (graph[i][a] != INF && i != a && j != a && dp[a][j][e-1] != INF)
dp[i][j][e] = min(dp[i][j][e], graph[i][a] + dp[a][j][e-1]);
}
}
Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964
}
}
}

if (dp[src][dst][K] == INF)
printf("No path of length %d is available\n", K);
else
printf("Weight of shortest path from (%d,%d) with %d edges : %d\n", src+1, dst+1, K, dp[src][dst][K]);
}

int main() {
int V;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &V);
int graph[MAX_V][MAX_V];
printf("Enter the adjacency matrix of the graph:\n");
for(int i = 0; i < V; i++) {
for(int j = 0; j < V; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j)
graph[i][j] = INF;
}
}
int src, dst, K;
printf("Enter the source and destination vertex numbers: ");
scanf("%d %d", &src, &dst);
printf("Enter the number of edges (k): ");
scanf("%d", &K);
src--; dst--; // Adjust for 0 indexing
shortestPath(graph, V, src, dst, K);
return 0;
}

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964


OUTPUT

Nimisha Verma Section:B Class Rno.:43 University Rno.:2018964

You might also like