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

Lab05-DAA

The document contains implementations of various algorithms including Matrix Chain Multiplication, Floyd-Warshall for shortest paths, Traveling Salesman Problem, Dijkstra's and Bellman-Ford algorithms for single source shortest paths, and a 15 Puzzle game. Each section includes C code that demonstrates how to execute the respective algorithm. The document serves as a practical guide for understanding and applying these algorithms in programming.

Uploaded by

studyscience19
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)
3 views

Lab05-DAA

The document contains implementations of various algorithms including Matrix Chain Multiplication, Floyd-Warshall for shortest paths, Traveling Salesman Problem, Dijkstra's and Bellman-Ford algorithms for single source shortest paths, and a 15 Puzzle game. Each section includes C code that demonstrates how to execute the respective algorithm. The document serves as a practical guide for understanding and applying these algorithms in programming.

Uploaded by

studyscience19
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/ 5

Lab-05

1. Find the minimum number of scalar multiplication needed for chain of matrix

Code:

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

int MatrixChainOrder(int p[], int n) {


int m[n][n];

for (int i = 1; i < n; i++)


m[i][i] = 0;

for (int L = 2; L < n; L++) {


for (int i = 1; i < n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = INT_MAX;
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n-1];
}

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]);

printf("Minimum number of multiplications is %d\n", MatrixChainOrder(p, n+1));


return 0;
}

2. Implement all pair of Shortest path for a graph (Floyed- Warshall Algorithm)

Code:
#include <stdio.h>
#define INF 99999

void floydWarshall(int graph[10][10], int V) {


int dist[10][10];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];

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


for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];

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;
}

3. Implement Traveling Salesman Problem

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

#define MAX 10
int visited[MAX], cost = 0, minCost = INT_MAX, n;
int graph[MAX][MAX];

void tsp(int pos, int count, int costSoFar, int start) {


if (count == n && graph[pos][start]) {
int totalCost = costSoFar + graph[pos][start];
if (totalCost < minCost)
minCost = totalCost;
return;
}
for (int i = 0; i < n; i++) {
if (!visited[i] && graph[pos][i]) {
visited[i] = 1;
tsp(i, count + 1, costSoFar + graph[pos][i], start);
visited[i] = 0;
}
}
}

int main() {
printf("Enter number of cities: ");
scanf("%d", &n);

printf("Enter cost matrix:\n");


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &graph[i][j]);

for (int i = 0; i < n; i++) visited[i] = 0;


visited[0] = 1;
tsp(0, 1, 0, 0);

printf("Minimum cost of TSP: %d\n", minCost);


return 0;
}

4. Implement Single Source shortest Path for a graph ( Dijkstra , Bellman Ford Algorithm):

Code:
#include <stdio.h>
#define INF 9999
#define MAX 100

int graph[MAX][MAX], dist[MAX], visited[MAX], n;

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;
}

void dijkstra(int start) {


for (int i = 0; i < n; i++) {
dist[i] = INF;
visited[i] = 0;
}
dist[start] = 0;

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


int u = minDistance();
visited[u] = 1;
for (int v = 0; v < n; v++)
if (!visited[v] && graph[u][v] && 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 < n; i++)
printf("%d \t %d\n", i, dist[i]);
}

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;
}

5. Implement 15 Puzzle Problem

Code:

#include <stdio.h>

#define SIZE 4

void printPuzzle(int puzzle[SIZE][SIZE]) {


printf("Current puzzle state:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (puzzle[i][j] == 0) printf(" ");
else printf("%2d ", puzzle[i][j]);
}
printf("\n");
}
}

int main() {
int puzzle[SIZE][SIZE];
int i, j, row = 0, col = 0;

printf("Enter the puzzle (0 for empty tile):\n");


for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++) {
scanf("%d", &puzzle[i][j]);
if (puzzle[i][j] == 0) {
row = i;
col = j;
}
}

char move;
while (1) {
printPuzzle(puzzle);
printf("Move tile (W/A/S/D): ");
scanf(" %c", &move);

int newRow = row, newCol = col;


if (move == 'W' || move == 'w') newRow--;
else if (move == 'S' || move == 's') newRow++;
else if (move == 'A' || move == 'a') newCol--;
else if (move == 'D' || move == 'd') newCol++;
else break;

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;
}

You might also like