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

Assignment 4 Solution

The document contains three C programming assignments: the first implements Dijkstra's algorithm for finding the shortest path in a graph with 6 vertices, the second implements the fractional knapsack problem, and the third finds the minimum cost spanning tree using Prim's algorithm. Each assignment includes code snippets, explanations of the algorithms, and sample outputs. The document serves as a guide for implementing these algorithms in C programming.

Uploaded by

bastenvan389
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 views8 pages

Assignment 4 Solution

The document contains three C programming assignments: the first implements Dijkstra's algorithm for finding the shortest path in a graph with 6 vertices, the second implements the fractional knapsack problem, and the third finds the minimum cost spanning tree using Prim's algorithm. Each assignment includes code snippets, explanations of the algorithms, and sample outputs. The document serves as a guide for implementing these algorithms in C programming.

Uploaded by

bastenvan389
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/ 8

Assignment-4

1. Write a C program to implement single source shortest path (Dijkstra) algorithm for
the following graph with 6 vertices using dynamic programming.

Solution:
#include <stdio.h>
#define INFINITY 9999
#define MAX 10

void dijkstra(int Graph[MAX][MAX], int n, int start);

int main() {
int Graph[MAX][MAX], n, u;

n = 6;

// Initializing the graph


Graph[0][0] = 0; Graph[0][1] = 2; Graph[0][2] = 4; Graph[0][3] = 0; Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[1][0] = 0; Graph[1][1] = 0; Graph[1][2] = 1; Graph[1][3] = 7; Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[2][0] = 0; Graph[2][1] = 0; Graph[2][2] = 0; Graph[2][3] = 0; Graph[2][4] = 3;
Graph[2][5] = 0;
Graph[3][0] = 0; Graph[3][1] = 0; Graph[3][2] = 0; Graph[3][3] = 0; Graph[3][4] = 0;
Graph[3][5] = 1;
Graph[4][0] = 0; Graph[4][1] = 0; Graph[4][2] = 0; Graph[4][3] = 2; Graph[4][4] = 0;
Graph[4][5] = 5;
Graph[5][0] = 0; Graph[5][1] = 0; Graph[5][2] = 0; Graph[5][3] = 0; Graph[5][4] = 0;
Graph[5][5] = 0;

u = 0;
dijkstra(Graph, n, u);

return 0;
}
void dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;

// Create cost matrix


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];
}
}

// Initialize distance, predecessor, and visited


for (i = 0; i < n; i++) {
distance[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}

distance[start] = 0;
visited[start] = 1;
count = 1;

// Dijkstra's algorithm
while (count < n - 1) {
mindistance = INFINITY;

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


if (distance[i] < mindistance && !visited[i]) {
mindistance = distance[i];
nextnode = i;
}
}

visited[nextnode] = 1;

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


if (!visited[i]) {
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
}
}

count++;
}

// Print the distance and path


for (i = 0; i < n; i++) {
if (i != start) {
printf("\nDistance from source to %d = %d", i, distance[i]);
printf("\nPath = %d", i);
j = i;
do {
j = pred[j];
printf(" <-%d", j);
} while (j != start);
}
}
}
Output:-

2. Write a C Program to implement fractional knapsack for the following instance of


elements.
Display the total profit also.
Number of Element: 4
Size of Sack: 9
Solution:
#include <stdio.h>

int main() {
float temp, temp1, s;
float x[20];
int n, i, j, k;

printf("How many elements?\n");


scanf("%d", &n);

struct knap {
int profit;
int size;
};

struct knap a[n], c[n];

// Input profit and size


for (i = 0; i < n; i++) {
printf("Enter Profit & Size of element #%d:\n", i + 1);
scanf("%d%d", &a[i].profit, &a[i].size);
}

// Sort by profit/size ratio in ascending order


for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if ((float)a[j].profit / a[j].size > (float)a[j + 1].profit / a[j + 1].size) {
// Swap profits
temp = a[j].profit;
a[j].profit = a[j + 1].profit;
a[j + 1].profit = temp;

// Swap sizes
temp1 = a[j].size;
a[j].size = a[j + 1].size;
a[j + 1].size = temp1;
}
}
}

// Copy in descending order


printf("Values of Profit & associated Size according to decreasing P/S ratio:\n");
for (i = n - 1, k = 0; i >= 0; i--, k++) {
c[k].profit = a[i].profit;
c[k].size = a[i].size;
}

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


printf("Profit = %d Size = %d\n", c[i].profit, c[i].size);
}

printf("Enter size of the bag:\n");


scanf("%f", &s);

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


x[i] = 0.0;
}

float U = s;
float P = 0.0;

// Fractional Knapsack logic


for (i = 0; i < n; i++) {
if (c[i].size <= U) {
x[i] = 1.0;
printf("Fraction taken of element #%d = %f\n", i + 1, x[i]);
U = U - c[i].size;
printf("Remaining Size of sack after filling with element #%d: %f\n", i + 1, U);
P = P + c[i].profit;
printf("Current Profit after filling with element #%d: %f\n", i + 1, P);
} else if (c[i].size > U) {
float m = U / c[i].size;
x[i] = m;
printf("Fraction taken of element #%d = %f\n", i + 1, x[i]);
P = P + (x[i] * c[i].profit);
break;
}
}
printf("Max Profit = %f\n", P);
return 0;
}

3. Write a C Program to find minimum cost spanning tree using Prim’s Algorithm for the
following graph.

#include <stdio.h>

int u, v, n, i, j, ne = 1;
int visited[10] = {0}, min, mincost = 0, cost[10][10];

int main() {
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999; // Representing no edge
}
}

visited[1] = 1;
printf("\n");

while (ne < n) {


min = 999;

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


if (visited[i]) {
for (j = 1; j <= n; j++) {
if (!visited[j] && cost[i][j] < min) {
min = cost[i][j];
u = i;
v = j;
}
}
}
}

if (!visited[v]) {
printf("\nEdge %d: (%d %d) cost: %d", ne++, u, v, min);
mincost += min;
visited[v] = 1;
}

cost[u][v] = cost[v][u] = 999; // Mark edge as used


}

printf("\nMinimum cost = %d\n", mincost);

return 0;
}

You might also like