Beruflich Dokumente
Kultur Dokumente
YASH KUMAR
PSEUDOCODE-
INPUT- total number of vertices, adjacency matrix, starting
node, ending node
OUTPUT- the shortest path and the cost of shortest path
void dijkstra(int a[size][size],int n,int startnode, int endnode)
{
int cost[size][size],distance[size],pr[size];
int visited[size],count,mindistance,next,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]==0)
cost[i][j]=infinite;
else
cost[i][j]=a[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pr[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
18BCE0294
YASH KUMAR
while(count<n-1)
{
mindistance=infinite;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
next=i;
}
visited[next]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[next][i]<distance[i])
{
distance[i]=mindistance+cost[next][i];
pr[i]=next;
}
count++;
}
for(i=0;i<n;i++)
if(i==endnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pr[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
18BCE0294
YASH KUMAR
CODE-
#include<stdio.h>
#define infinite 9999
#define size 10
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
18BCE0294
YASH KUMAR
mindistance=distance[i];
next=i;
}
visited[next]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[next][i]<distance[i])
{
distance[i]=mindistance+cost[next][i];
pr[i]=next;
}
count++;
}
for(i=0;i<n;i++)
if(i==endnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pr[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
int main()
{
int a[size][size],i,j,n,u,des;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
18BCE0294
YASH KUMAR
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
printf("\nEnter the destination node:");
scanf("%d",&des);
dijkstra(a,n,u,des);
return 0;
}
18BCE0294
YASH KUMAR
KNAPSACK PROBLEM
In this dynamic programming problem we have n items each with
an associated weight and value (benefit or profit). The objective is
to fill the knapsack with items such that we have a maximum profit
without crossing the weight limit of the knapsack. Since this is a 0 1
knapsack problem hence we can either take an entire item or reject
it completely. We can not break an item and fill the knapsack.
SOLVING METHOD:
In this problem we have a Knapsack that has a weight limit W.
There are items i1, i2, ..., in each having weight w1, w2, … wn
and some benefit (value or profit) associated with it v1, v2, ...
vn
Our objective is to maximise the benefit such that the total
weight inside the knapsack is at most W.
Since this is a 0 1 Knapsack problem algorithm so, we can
either take an entire item or reject it completely. We can not
break an item and fill the knapsack.
Problem
Assume that we have a knapsack with max weight capacity W = 5
Our objective is to fill the knapsack with items such that the benefit
(value or profit) is maximum.
Following table contains the items along with their value and weight.
ITEM I 1 2 3 4
WEIGHT WT 3 2 4 1
Total items n = 4
Total capacity of the knapsack W = 5
Now we create a value table V[i,w] where, i denotes number of
items and w denotes the weight of the items.
Rows denote the items and columns denote the weight.
18BCE0294
YASH KUMAR
I=0
I=0 0 0 0 0 0 0
set i = n and w = W
C Code:-
#include<stdio.h>
int main(void) {
int val[] = {-1, 100, 20, 60, 40}; //value of the items
return 0;
if(x > y) {
return x;
else {
return y;
}
18BCE0294
YASH KUMAR
int i, w;
{ if(wt[i] <= w) {
else {
V[i][w] = V[i-1][w];
printf("Max Value: %d\n", V[n][W]); //max value that can be put inside the
knapsack