0% found this document useful (0 votes)
4 views3 pages

day5

The document contains two programming assignments: one for implementing the Matrix Chain Multiplication problem to find the minimum number of scalar multiplications needed, and another for the All-Pairs Shortest Path problem using Floyd-Warshall's algorithm. Both assignments include C code implementations and user input prompts for matrix dimensions and graph edges. The code demonstrates dynamic programming and graph algorithms in C.
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)
4 views3 pages

day5

The document contains two programming assignments: one for implementing the Matrix Chain Multiplication problem to find the minimum number of scalar multiplications needed, and another for the All-Pairs Shortest Path problem using Floyd-Warshall's algorithm. Both assignments include C code implementations and user input prompts for matrix dimensions and graph edges. The code demonstrates dynamic programming and graph algorithms in C.
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/ 3

Assignment 4

1. Implement Matrix Chain Multiplication problem: Find the minimum number of scalar
multiplications needed for chain of matrix.

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

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


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

int main() {
int n;
printf("Enter the number of matrices: ");
scanf("%d", &n);
int p[n];
printf("Enter the dimensions of matrices (as an array of size %d):\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
int result = matrixChainOrder(p, n);
printf("Minimum number of scalar multiplications: %d\n", result);
return 0;
}
output:
2}1. Implement all pair shortest path problem using Floyd Warshall’s algorithm.
#include <stdio.h>
#define INF 99999
#define MAX_VERTICES 10

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


int dist[MAX_VERTICES][MAX_VERTICES], i, j, k;

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


for (j = 0; j < V; j++) {
dist[i][j] = graph[i][j];
}
}

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


for (i = 0; i < V; i++) {
for (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 between every pair of vertices:\n");


for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][j] == INF)
printf("INF ");
else
printf("%d ", dist[i][j]);
}
printf("\n");
}
}

int main() {
int graph[MAX_VERTICES][MAX_VERTICES], V, E, i, u, v, weight;
printf("Enter the number of vertices: ");
scanf("%d", &V);

printf("Enter the number of edges: ");


scanf("%d", &E);

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


for (int j = 0; j < V; j++) {
if (i == j)
graph[i][j] = 0;
else
graph[i][j] = INF;
}
}

printf("Enter each edge (u, v, weight):\n");


for (i = 0; i < E; i++) {
scanf("%d %d %d", &u, &v, &weight);
graph[u][v] = weight;
}

floydWarshall(graph, V);

return 0;
}
output:

You might also like