Professional Documents
Culture Documents
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:
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:
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.