You are on page 1of 12

Ex.

No:6
IMPLEMENT AND ANALYZE MILTISTAGE GRAPHS
Dt: USING DYNAMIC PROGRAMMING APPROACH

AIM:
To develop a c program for the implementation of Multistage graph.

ALGORITHM:

F graph (graph G, int k, int n, int p[])


{
float cost [max size], int d[max size], r;
cost[n]=0.0
for (int j=n-1;j>=1;j--)
{
Let r be a vertex such that is an edge of G and c[j][r]+cosr[r] is minimum;
Cost[j] = c[j][r] + cost [r]; D[j]=r;
}
P[1] =1,p[k]=n
for(j=2;j<k-1;j++)
p[j] = d[p(j-1)];
}

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
int i;
int stages,stages_vertices[MAX],c[MAX][MAX];
int cost[MAX],p[MAX],n;
Get_min(int,int);
void Forward(int);
int create();
void display();
int Get_min(int s,int n)
{
int min=9999;
int min_vertex;
for(i=0;i<n;i++)
{
if(min>(c[s][i]+cost[i]))
{
min=c[s][i]+cost[i];
min_vertex=i;
}
}
return min_vertex;
}
void Forward(int n)
{
int i,r;
int d[20];
for(i=0;i<n;i++)
cost[i]=0;
for(i=n-2;i>=0;i--)
{
r=Get_min(i,n);
cost[i]=c[i][r]+cost[r];
d[i]=r;
}
p[0]=0;
p[stages-1]=n-1;
for(i=1;i<stages-1;i++)
p[i]=d[p[i-1]];
}
int create()
{
int i,j,m,p,no_of_vertices=0;
printf("\nEnter no of vertices:");
scanf("%d",&no_of_vertices);
printf("\nEnter no of stages:");
scanf("%d",&stages);
for(i=0;i<no_of_vertices;i++)
for(j=0;j<no_of_vertices;j++)
c[i][j]=9999;
for(i=0;i<stages;i++)
{
printf("\nEnter no of vertices in stage%d:",i+1);
scanf("%d",&stages_vertices[i]);
}
i=0;
j=stages_vertices[0];
for(m=0;m<stages;m++)
{
j=i+stages_vertices[m];
for(i=0;i<j;i++)
{
for(p=0;p<stages_vertices[m+1];p++)
{
printf("\nEnter cost for%d to%d:",i+1,p+j+1);
scanf("%d",&c[i][p+j]);
}
}
}
return no_of_vertices;
}
void display()
{
int i;
printf("Shortest path is...\n");
for(i=0;i<stages-1;i++)
printf("%d",p[i]+1);
printf("%d",p[i]+1);
}
void main()
{
int n;
clrscr();
n=create();
clrscr();
Forward(n);
display();
getch();
}
OUTPUT:

CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20

RESULT:
Thus the program to implement the multistage graph has been executed and
verified.
Ex. No:7
IMPLEMENT AND ANALYZE ALL PAIR SHORTEST
Dt: PATH USING DYNAMIC PROGRAMMING

AIM:
To develop a c program for the All pair shortest path using Dynamic
programming.

ALGORITHM:

Floyd (W[1…n,1….n])
Implements Floyd’s algorithm for the all pair shortest paths problem.
Input: The weight matrix W of a graph with no negative-length cycle.
Output: The distance matrix of the shortest paths lengths.
D ← W // is not necessary if W can be overwritten
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
D[i, j] ← min [D[i,j],D[i,k]+D[k,j]]
return D

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define INF 999
#define nV 4
int i,j;
void printMatrix(int matrix[][nV]);
void floydWarshall(int graph[][nV])
{
int matrix[nV][nV],i,j,k;
for(i=0;i<nV;i++)
for(j=0;j<nV;j++)
matrix[i][j]=graph[i][j];
for(k=0;k<nV;k++)
{
for(i=0;i<nV;i++)
{
for(j=0;j<nV;j++)
{
if(matrix[i][k]+matrix[k][j]<matrix[i][j])
matrix[i][j]=matrix[i][k]+matrix[k][j];
}
}
}
printMatrix(matrix);
}
void printMatrix(int matrix[][nV])
{
for(i=0;i<nV;i++)
{
for(j=0;j<nV;j++)
{
if(matrix[i][j]==INF)
printf("%4s","INF");
else
printf("%6d",matrix[i][j]);
}
printf("\n");
}
}
void main()
{
int graph[nV][nV]={{0,3,INF,5},{2,0,INF,4},{INF,1,0,INF},{INF,INF,2,0}};
clrscr();
floydWarshall(graph);
getch();
}

OUTPUT:

CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20

RESULT:
Thus the program to implement all pair shortest path was executed and verified.
Ex. No:8
IMPLEMENT AND ANALYZE KNAPSACK PROBLEM
Dt: USING GREEDY METHOD

AIM:
To develop a c program to implement knapsack problem using Greedy
method

ALGORITHM:

Fractional knapsack (S,W);


Input: Set S of items, such that each item i ∈ s has a positive benefit bi
and a positive weight wi; positive maximum total weight W.
Output: Amount xi of each item i ∈ s that maximizes the total benefit
while not exceeding the maximum total weight W.
for each item i ∈ s do
xi ← 0
𝒃𝒊
vi ← 𝒘𝒊 {value index of item i}
w←0 {total weigth}
while w < W do
remove from S an item i with highest value index
a ← min{wi,W-w} (more than W-w causes a weight overflow)
xi ← a
w ← w+a
PROGRAM:
#include<stdio.h>
#include<conio.h>
void knapsack(float capacity, int n, float weight[], float profit[])
{
float x[20], totalprofit,y;
int i,j;
y=capacity;
totalprofit=0;
for(i=0;i<n;i++)
x[i]=0.0;
for(i=0;i<n;i++)
{
if(weight[i]>y)
break;
else
{
x[i]=1.0;
totalprofit=totalprofit+profit[i];
y=y-weight[i];
}
}
if(i<n)
x[i]=y/weight[i];
totalprofit=totalprofit+(x[i]*profit[i]);
printf("The selected elements are \n");
for(i=0;i<n;i++)
if(x[i]==1.0)
printf("\n Profit is %f with weight %f",profit[i],weight[i]);
else if (x[i]>0.0)
printf("\n%f part of profit %f with weight %f",x[i],profit[i],weight[i]);
printf("\n Total profit for %d objects with capacity
%f=%f\n\n",n,capacity,totalprofit);
}
void main()
{
float weight[20],profit[20],ratio[20],t1,t2,t3;
int n;
float capacity;
int i,j;
clrscr();
printf("Enter number of objects:");
scanf("%d",&n);
printf("\n Enter the capacity of knapsack:");
scanf("%f",&capacity);
for(i=0;i<n;i++)
{
printf("\nEnter %d(th) profit:",(i+1));
scanf("%f",&profit[i]);
printf("\nEnter %d(th) weigth:",(i+1));
scanf("%f",&weight[i]);
ratio[i]=profit[i]/weight[i];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(ratio[i]>ratio[j])
{
t1=ratio[i];
ratio[i]=ratio[j];
ratio[j]=t1;
t2=weight[i];
weight[i]=weight[j];
weight[j]=t2;
t3=profit[i];
profit[i]=profit[j];
profit[j]=t3;
}
}
knapsack(capacity,n,weight,profit);
getch();
}
OUTPUT:

CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20

RESULT:
Thus the program to implement knapsack problem using Greedy Method
was executed and verified.

You might also like