0% found this document useful (0 votes)
26 views49 pages

Algorithm Design and Analysis: Algorithm Design Is A Specific Method To Create A Mathematical

The document discusses algorithm design and analysis. It describes algorithm design as a method for creating mathematical processes to solve problems. It also discusses algorithm analysis which determines the resources like time and storage required by algorithms. Common techniques for analyzing algorithm complexity asymptotically are big O, big Omega, and big Theta notations. Example algorithms and programs for sorting, searching and other problems using techniques like divide-and-conquer are provided.

Uploaded by

Gagandeep Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views49 pages

Algorithm Design and Analysis: Algorithm Design Is A Specific Method To Create A Mathematical

The document discusses algorithm design and analysis. It describes algorithm design as a method for creating mathematical processes to solve problems. It also discusses algorithm analysis which determines the resources like time and storage required by algorithms. Common techniques for analyzing algorithm complexity asymptotically are big O, big Omega, and big Theta notations. Example algorithms and programs for sorting, searching and other problems using techniques like divide-and-conquer are provided.

Uploaded by

Gagandeep Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Algorithm Design And Analysis

ALGORITHM DESIGN AND ANALYSIS


Algorithm design is a specific method to create a mathematical
process in solving problems. Applied algorithm design is algorithm
engineering. Algorithm design is identified and incorporated into
many solution theories of operation research, such as dynamic
programming and divide-and-conquer. Techniques for designing and
implementing algorithm designs are algorithm design patterns, [1] such
as template method pattern and decorator pattern, and uses of data
structures, and name and sort lists. Some current day uses of
algorithm design can be found in internet retrieval processes of web
crawling, packet routing and caching.
In computer science, the analysis of algorithms is the determination of
the amount of resources (such as time and storage) necessary to
execute them. Most algorithms are designed to work with inputs of
arbitrary length. Usually, the efficiency or running time of an
algorithm is stated as a function relating the input length to the
number of steps (time complexity) or storage locations (space
complexity).
Algorithm analysis is an important part of a broader computational
complexity theory, which provides theoretical estimates for the
resources needed by any algorithm which solves a given
computational problem. These estimates provide an insight into
reasonable directions of search for efficient algorithms.
In theoretical analysis of algorithms it is common to estimate their
complexity in the asymptotic sense, i.e., to estimate the complexity
function for arbitrarily large input. Big O notation, Big-omega
notation and Big-theta notation are used to this end. For instance,
binary search is said to run in a number of steps proportional to the
logarithm .
Page | 1
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO PERFORM QUICK SORT USING DIVIDE &


CONQUER TECHNIQUE*/
OUTPUT:

Page | 2
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAMS:
/* PROGRAM TO PERFORM QUICK SORT USING DIVIDE &
CONQUER TECHNIQUE*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
#include<string.h>
#define NULL 0
char lower[10];
char upper[10];
int top=-1;
void display(int s[],int n);
int main()
{
void quicksort(int s[],int n);
int n=10;
clrscr();
int s[]={41,19,65,35,21,73,28,87,52,59};
cout<<"Before Sorting\n\n";
display(s,n);
quicksort(s,n);
cout<<"Afetr Sorting\n\n";
display(s,n);
Page | 3
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

cout<<"\nEnd of the program";


getch();
return 0;
}
void display(int s[],int n)
{for(int i=0;i<n;i++)
cout<<" "<<s[i];
cout<<"\n";}
void quicksort(int s[],int n)
{
int beg,end,loc;
int partition(int s[],int n,int beg ,int end);
if(n>=2)
{
top=top+1;
lower[0]=0; upper[0]=n-1;
}
while(top!=-1)
{
beg=lower[top]; end=upper[top];
top=top-1;
loc=partition(s,n,beg,end);
if(beg<=loc-2)
{
Page | 4
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

top=top+1;
lower[top]=beg; upper[top]=loc-1;
}
if(loc+2<=end)
{
top=top+1;
lower[top]=loc+1;upper[top]=end;
}
}
}
int partition(int s[],int n,int beg,int end)
{
int left,right,loc,temp;
left=beg; right=end; loc=beg;
while(n>=1)
{
while((s[loc]<s[right]) && (loc!=right))
right=right-1;
if(loc==right)
return(loc);
if(s[loc]>s[right])
{
temp=s[loc];
s[loc]=s[right];
Page | 5
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

s[right]=temp;
loc=right;
}
while(s[loc]>=s[left] && loc!=left)
left=left+1;
if(loc==left)
return(loc);
if(s[loc]<s[left])
{
temp=s[loc];
s[loc]=s[left];
s[left]=temp;
loc=left;
}
n=n-1;
}
return 0;
}

Page | 6
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO PERFORM SELECTION SORT USING


DIVIDE & CONQUER TECHNIQUE*/
OUTPUT:

Page | 7
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO PERFORM SELECTION SORT USING


DIVIDE & CONQUER TECHNIQUE*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,k,min,loc,temp;
cout<<"\n------------ SELECTION SORT ------------ \n\n";
cout<<"Enter No. of Elements=";
cin>>n;
cout<<"\nEnter Elements=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(p=1;p<=n-1;p++)
{
min=a[p];
loc=p;
for(k=p+1;k<=n;k++)
{
if(min>a[k])
{
Page | 8
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

min=a[k];
loc=k;
}
}
temp=a[p];
a[p]=a[loc];
a[loc]=temp;
}
cout<<"\nAfter Sorting : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}

Page | 9
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO PERFORM BINARY SEARCH USING DIVIDE


& CONQUER TECHNIQUE*/
OUTPUT:

Page | 10
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO PERFORM BINARY SEARCH USING DIVIDE


& CONQUER TECHNIQUE*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a[100],n,i,flag,item,low,high;
int binser(int a[100],int item,int low,int high);
clrscr();
cout<<"enter the no of elements";
cin>>n;
cout<<"enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"the list is";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"enter the elments to be searched";
cin>>item;
low=0;
Page | 11
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

high=n-1;
flag=binser(a,item,low,high);
cout<<"elements is at["<<flag<<"]location";
getch();
}
int binser(int a[],int item,int low,int high)
{
int m;
m=(low+high)/2;
if(item==a[m])
{
return m;
}
else if(item<a[m])
{
binser(a,item,low,m-1);
}
else
{
binser(a,item,m+1,high);
}
}

Page | 12
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO PRINT FIBONICCI SERIES*/


OUTPUT:

/* PROGRAM TO PRINT FIBONICCI SERIES*/


Page | 13
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

#include<iostream.h>
#include<conio.h>
rec_fibo(int);
void main()
{
clrscr();
int n,i;
cout<<"Enter the total elements in the series : ";
cin>>n;
cout<<"\nThe Fibonacci series is:\n";
for(i=0;i<n;i++)
{
cout<<rec_fibo(i)<<" ";
}
getch();
}
rec_fibo(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return rec_fibo(n-1)+rec_fibo(n-2);
Page | 14
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

Page | 15
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO FORM BINARY SEARCH TREE USING


DYNAMIC PROGRAMING*/
OUTPUT:

Page | 16
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

/* PROGRAM TO FORM BINARY SEARCH TREE USING


DYNAMIC PROGRAMING*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define MAX 10
int find(int i,int j);
void print(int,int);
int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],I,j,k,n,m;
char idnt[7][10];
void main()
{
clrscr();
cout << "enter the no, of identifiers";
cin >>n;
cout <<"enter identifiers";
for(i=1;i<=n;i++)
cin>>idnt[i];
cout <<"enter success propability for identifiers";
for(i=1;i<=n;i++)
cin >>p[i];
cout << "enter failure propability for identifiers";
for(i=0;i<=n;i++)
cin >> q[i];
Page | 17
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

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

w[i][i]=q[i];
c[i][i]=r[i][i]=0;
w[i][i+1]=q[i]+q[i+1]+p[i+1];
r[i][i+1]=i+1;
c[i][i+1]=q[i]+q[i+1]+p[i+1];
}
w[n][n]=q[n];
r[n][n]=c[n][n]=0;
for(m=2;m<=n;m++)

for(i=0;i<=n-m;i++)
{
j=i+m;
w[i][j]=w[i][j-1]+p[j]+q[j];
k=find(i,j);
r[i][j]=k;
c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
}

cout <<"\n";
print(0,n);
getch();
}
int find(int i,int j)

int min=2000,m,l;
Page | 18
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

for(m=i+1;m<=j;m++)
if(c[i][m-1]+c[m][j]<min)
{
min=c[i][m-1]+c[m][j];
l=m;
}
return l;
}
void print(int i,int j)

if(i<j)
puts(idnt[r[i][j]]);
else
return;
print(i,r[i][j]-1);
print(r[i][j],j);
}

Page | 19
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT TRAVELLING SALESMAN


PROBLEM USING GREEDY METHOD.
OUTPUT:

Page | 20
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT TRAVELLING SALESMAN


PROBLEM USING GREEDY METHOD.
#include<iostream.h>
#include<conio.h>
#define max 100
#define infinity 999
int tsp_dp(int c[][max],int tour[],int start,int n);
void main()
{
int i,j,n,c[max][max],tour [max],cost;
clrscr();
cout<<"\n this program demonstrates the tsp problem";
cout<<"enter the no. of cities to traverse";
cin>>n;
cout<<"enter the cost matrix";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>c[i][j];
for(i=0;i<n;i++)
tour[i]=i;
cost=tsp_dp(c,tour,0,n);
cout<<"\nminimum cost"<<cost;
for(i=0;i<n;i++)
cout<<tour[i]+1;
Page | 21
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

cout<<"\n";
getch();
}
int tsp_dp(int c[][max],int tour[],int start,int n)
{
int i,j,k,temp[max],min_tour[max],min_cost,cost;
if(start==n-2)
return c[tour[n-1]][tour[n-1]]+c[tour[n-1]][0];
min_cost=infinity;
for(i=start+1;i<n;i++)
{
for(j=0;j<n;j++)
temp[j]=tour[j];
temp[start+1]=tour[i];
temp[i]=tour[start+1];
if(c[tour[start]][tour[i]]+(cost=tsp_dp(c,temp,start+1,n))<min_cost)
{
min_cost=c[tour[start]][tour[i]]+cost;
for(k=0;k<n;k++)
min_tour[k]=temp[k];
} }
for(i=0;i<n;i++)
tour[i]=min_tour[i];
return (min_cost);
Page | 22
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

Page | 23
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT KNAPSACK PROBLEM USING


GREEDY METHOD
OUTPUT:

Page | 24
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT KNAPSACK PROBLEM USING


GREEDY METHOD
#include<iostream.h>
#include<conio.h>
int w[10],p[10],p_no[10],n,m;
float pwr[10];
template<class t>
void swap(t*a,int i,int j)
{
t temp;
temp=a[j];
a[j]=a[i];
a[i]=temp;
return;
}
void line()
{
for(int i=0;i<80;i++)
cout<<"=";
}
void display()
{
cout<<"\n\t knapsack\n";
line();
Page | 25
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

for(int i=0;i<n;i++)
cout<<p_no[i]<<"\t\t"<<w[i]<<"\t\t"<<p[i]<<"\t\t"<<pwr[i]<<"\n";
}
void knapsack()
{
int u,i,z[100];
u=m;
float x[10];
for(i=0;i<n;i++)
{
if(w[i]>0)
break;
else
{
x[i]=1;
u=u-w[i];
}
}
if(i<=n)
x[i]=(float)u/w[i];
for(i=0;i<n;i++)
{
for(int j=1;j<n;j++)
{
Page | 26
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

if(p_no[i]>p_no[j])
{
swap(p_no,i,j);
swap(x,i,j);
swap(p,i,j);
}
}
}
float opt_sol=0;
cout<<"\nthe order is\n";
cout<<"/";
for(i=0;i<n;i++)
cout<<p_no[i]<<"";
cout<<")";
cout<<"\t(";
for(i=0;i<n;i++)
{
cout<<x[i]<<"";
opt_sol=opt_sol+(p[i]*z[i]);
}
cout<<")";
cout<<"\noptimal solution is:\t"<<opt_sol;
}
int main()
Page | 27
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

{
int i;
cout<<"nter the bag capacity";
cin>>m;
cout<<"nter the no. of items";
cin>>n;

cout<<"\nenter the weights for"<<n<<"items:\t";


for(i=0;i<n;i++)
{
p_no[i]=i+1;
cout<<"\nproduct"<<i+1<<"\nweights:\t";
cin>>w[i];
cout<<"\nprofit:\t";
cin>>p[i];
pwr[i]=(float)p[i]/w[i];
}
display();
for(i=0;i<n;i++)
{
for(int j=j+1;j<n;j++)
{
if(pwr[i]<pwr[j])
{
Page | 28
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

swap(p_no,i,j);
swap(w,i,j);
swap(p,i,j);
}
}
}
display();
knapsack();
getch();
}

Page | 29
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT MINIMUM SPANNING TREE


USING PRIMS ALGORITHM
OUTPUT:

Page | 30
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT MINIMUM SPANNING TREE


USING PRIMS ALGORITHM
#include<iostream.h>
#include<conio.h>
#define s 20
#define inf 32767
void prim(int g[s][s],int node)
{
int q[s],i,j,k;
int md,v1,v2,total=0;
for(i=0;i<node;i++)
{
q[i]=0;
}
cout<<"\n\nminimum spanning tree\n";
q[0]=1;
for(k=0;k<node;k++)
{
md=inf;
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
if((g[i][j]&&q[i]&&q[j])||(!q[i]&&q[j]))
Page | 31
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

{
if(g[i][j]<md)
{
md=g[i][j];
v1=i;
v2=j;
}
}
}
}
cout<<"\'n edge("<<v1<<","<<v2<<")"<<"weight="<<md<<endl;
q[v1]=q[v2]=1;
total=total+md;
}
cout<<"\n total path length:"<<total;
}
void main()
{
int g[s][s],node,v1,v2,len,i,j,n;
cout<<"\n prim algorithm";
cout<<"\nenter the no of nodes in graph";
cin>>node;
cout<<"nenter the no of edges in graph";
cin>>n;
Page | 32
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
g[i][j]=0;
}}
cout<<"\n edge & weight";
for(i=0;i<n;i++)
{
cout<<"\n enter the edges v1 & v2";
cin>>v1>>v2;
cout<<"enter the corresponding weight";
cin>>len;
g[v1][v2]=g[v1][v2]=len;
}
prim(g,node);
getch();
}

Page | 33
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO FIND MINIMUM COST OF SPANNING TREE


USING PRIMS ALGORITHM
OUTPUT:

Page | 34
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO FIND MINIMUM COST OF SPANNING TREE


USING PRIMS ALGORITHM
#include<iostream.h>
#include<conio.h>
int a,b,v,u,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];

int main()
{
cout<<"\n enter the no. of nodes:";
cin>>n;
cout<<"\n enter the adjancy matrix:";
for(i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[i]=1;
cout<<"\n";
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
Page | 35
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0||visited[v]==0)
{
cout<<"\n edge:"<<ne++<<"cost:",a,b,min;
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n minimum cost:"<<mincost;
getch();
return 0;
}

Page | 36
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT KRUSKAL'S ALGORITHM USING


GREEDY METHOD
OUTPUT:

Page | 37
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT KRUSKAL'S ALGORITHM USING


GREEDY METHOD
#include<iostream.h>
#include<conio.h>
#include<process.h>
class kruskal
{
private:
int n,m,noe,graph_edge[100][4];
int tree[10][10],sets[100][10],top[100];
public:
void read_graph();
void initialize_span_t();
void sort_edges();
void algorithm();
int find_node(int);
void print_min_span_t();
};
void kruskal::read_graph()
{
int n;
cout<<"\n This program implements the kruskal algo\n";
cout<<"\n enter the no. of nodes in graph:";
cin>>n;
Page | 38
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

noe=0;
cout<<"\n enter the weights for the following edges:\n";
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout<<"<"<<i<<","<<j<<">:";
int w;
cin>>w;
if(w!=0)
{
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
}
}
}
cout<<"\n The edges in the graph are:\n";
for(i=1;i<=noe;i++)
{
cout<<"<"<<graph_edge[i][1]<<","<<graph_edge[i][2]<<">:"<<graph_edge[i]
[3]<<endl;
}}
Page | 39
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

void kruskal::sort_edges()
{
for(int i=1;i<=noe-1;i++)
{
for(int j=1;j<=noe-1;j++)
{
if(graph_edge[j][3]>graph_edge[j+1][3])
{
int t=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
t=graph_edge[j][2];
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
t=graph_edge[j][3];
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
}
}
}
cout<<"\n After sorting the edges in the graph are:\n";
for(i=1;i<=noe;i++)
{

Page | 40
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

cout<<"<"<<graph_edge[i][1]<<","<<graph_edge[i][2]<<">"<<graph_edge[i]
[3]<<endl;
}
}
void kruskal::algorithm()
{
for(int i=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
cout<<"\n The algorithm starts:\n";
for(i=1;i<=noe;i++)
{
int p1=find_node(graph_edge[i][1]);
int p2=find_node(graph_edge[i][2]);
if(p1!=p2)
{
cout<<"\n The edge included in tree is"<<"<"<<graph_edge[i]
[1]<<","<<graph_edge[i][2]<<","<<endl;
tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
for(int j=1;j<=top[p2];j++)
{
top[p1]++;
Page | 41
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

sets[p1][top[p1]]=sets[p2][j];
}
top[p2]=0;
}
else
{
cout<<"\n Inclusion of the edge"<<graph_edge[i][1]<<"<"<<graph_edge[i]
[2]<<">"<<"forms a cycle so it is removed\n";
}
}
}
int kruskal::find_node(int n)
{
for(int i=1;i<=noe;i++)
{
for(int j=1;j<=top[i];j++)
{
if(n==sets[i][j])
return i;
}
}
return -1;
}
void main()
Page | 42
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

{
clrscr();
kruskal obj;
obj.read_graph();
obj.sort_edges();
obj.algorithm();
getch();
}

Page | 43
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT SINGLE SOURCE SHORTEST


PATH USING GREEDY METHOD
OUTPUT:

Page | 44
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

PROGRAM TO IMPLEMENT SINGLE SOURCE SHORTEST


PATH USING GREEDY METHOD
#include<iostream.h>
#include<conio.h>
void main()
{
int min(int[],int[],int);
int wadj[10][10],i,j;
int n,e,sx,dx,weight;
clrscr();
cout<<"\n enter the no. of vertices and edges in graph:";
cin>>n>>e;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
wadj[i][j]=999;
cout<<"\n Input source,dest vertices and weight of edges:";
for(i=1;i<=e;i++)
{
cin>>sx>>dx>>weight;
wadj[sx][dx]=weight;
wadj[dx][sx]=weight;
}
cout<<"\n Weighted adjancy matrix:";
for(i=1;i<=n;i++)
Page | 45
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

{
for(j=1;j<=n;j++)
cout<<wadj[i][j]<<"\t";
cout<<endl;
}
static int cost[10],path[10],sh[10],s;
cout<<"\n enter the source vertex:";
cin>>s;
for(i=1;i<=n;i++)
sh[i]=0;
for(i=1;i<=n;i++)
if(wadj[s][i]==999)
{
cost[i]=999;
path[i]=0;
}
else
{
cost[i]=wadj[s][i];
path[i]=s;
sh[s]=1;
cost[s]=0;
for(i=1;i<=n;i++)
cout<<cost[i]<<"\t";
Page | 46
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

cout<<endl;
int it_flag=0;
while(it_flag==0)
{
j=min(cost,sh,n);
sh[j]=1;
for(i=1;i<=n;i++)
{
if(sh[i]==0)
{
if(wadj[j][i]!=999)
{
if(cost[j]+wadj[i][j]<cost[i])
{
cost[i]=cost[j]+wadj[j][i];
path[i]=j;
}
}
}
}
it_flag=1;
for(i=1;i<=n;i++)
if(sh[i]==0)
{
Page | 47
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

it_flag=0;
break;
}
}
for(i=1;i<=n;i++)
cout<<"\n Cost of shorted path from source vertex:"<<s<<"to destination
vertex:"<<i<<"is:"<<cost[i];
cout<<"\n enter destination vertex:";
cin>>dx;
cout<<"\n cost of reaching from"<<s<<"to"<<dx<<"="<<cost[dx];
int short_path[10];
int m=path[dx];
i=0;
while(m!=s)
{
short_path[i]=m;
i++;
m=path[m];
}
cout<<s<<"-->";
for(j=i-1;j>=0;j--)
cout<<"\n"<<short_path[j]<<"-->";
cout<<dx;
}
Page | 48
DAV COLLEGE JALANDHAR

Algorithm Design And Analysis

getch();
}
int min(int cost[],int sh[],int n)
{
int v,i,min1=999;
for(i=1;i<=n;i++)
{
if(sh[i]==0)
{
if(cost[i]<min1)
{
min1=cost[i];
v=i;
}
}
}
return(v);
}

Page | 49
DAV COLLEGE JALANDHAR

You might also like