DAA Practical Question
DAA Practical Question
#include<stdio.h>
#include<limits.h>
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;
}
int main() {
#include<stdio.h>
#include<limits.h>
int main() {
int n;
printf("Enter the number of vertices in the graph: ");
#include<stdio.h>
#include<limits.h>
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;
}