Lab05-DAA
Lab05-DAA
1. Find the minimum number of scalar multiplication needed for chain of matrix
Code:
#include <stdio.h>
#include <limits.h>
int main() {
int n;
printf("Enter number of matrices: ");
scanf("%d", &n);
int p[n+1];
printf("Enter dimensions: ");
for (int i = 0; i <= n; i++)
scanf("%d", &p[i]);
2. Implement all pair of Shortest path for a graph (Floyed- Warshall Algorithm)
Code:
#include <stdio.h>
#define INF 99999
printf("Shortest distances:\n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
printf("INF ");
else
printf("%d ", dist[i][j]);
}
printf("\n");
}
}
int main() {
int V, i, j;
int graph[10][10];
printf("Enter number of vertices: ");
scanf("%d", &V);
printf("Enter adjacency matrix (INF = 99999):\n");
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
scanf("%d", &graph[i][j]);
floydWarshall(graph, V);
return 0;
}
Code:
#include <stdio.h>
#include <limits.h>
#define MAX 10
int visited[MAX], cost = 0, minCost = INT_MAX, n;
int graph[MAX][MAX];
int main() {
printf("Enter number of cities: ");
scanf("%d", &n);
4. Implement Single Source shortest Path for a graph ( Dijkstra , Bellman Ford Algorithm):
Code:
#include <stdio.h>
#define INF 9999
#define MAX 100
int minDistance() {
int min = INF, index;
for (int i = 0; i < n; i++)
if (!visited[i] && dist[i] < min) {
min = dist[i];
index = i;
}
return index;
}
int main() {
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter adjacency matrix (use 0 for no edge):\n");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &graph[i][j]);
int source;
printf("Enter source vertex: ");
scanf("%d", &source);
dijkstra(source);
return 0;
}
Code:
#include <stdio.h>
#define SIZE 4
int main() {
int puzzle[SIZE][SIZE];
int i, j, row = 0, col = 0;
char move;
while (1) {
printPuzzle(puzzle);
printf("Move tile (W/A/S/D): ");
scanf(" %c", &move);
if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE) {
puzzle[row][col] = puzzle[newRow][newCol];
puzzle[newRow][newCol] = 0;
row = newRow;
col = newCol;
} else {
printf("Invalid move!\n");
}
}
return 0;
}