Networking - 1 ASSIGNMENT NO.1 1 OBJECTIVE To obtain the...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 ASSIGNMENT NO.1 1. OBJECTIVE :- To obtain the shortest distance between every two pair of vertices of the following weighted graph. 2. SOURCE CODE :- #include<stdio.h> #include<conio.h> #define inf 9999 typedef struct { int val; int status; }graph; int min(int,int); void main() { clrscr(); int a[20][20],n,i,j,h,s,d,start,end,p,c=0; char ch; graph g[10][10]; int flag[10],path[10]; clrscr(); printf("Enter the number of vertices: "); scanf("%d",&n); for(i=0;i<n;i++) a[i][i]=0; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) if(i!=j) { printf("\nVertex %d to Vertex %d:\n",i+1,j+1); printf("Is there any edge(y/n)? "); fflush(stdin);ch=getche(); printf("\n"); if(ch=='y' || ch=='Y') a[i][j]=1; else a[i][j]=inf; a[j][i]=a[i][j]; } } printf("\nEnter the starting vertex : "); scanf("%d",&s);start=s-1; printf("Enter the destination vertex : "); scanf("%d",&d);end=d-1; for(i=0;i<n;i++) { g[0][i].val=inf; g[0][i].status=0; flag[i]=0; } g[0][start].val=0;g[0][start].status=1;flag[start]=1; for(i=1;flag[end]!=1;i++) { for(j=0,h=inf;j<n;j++) { if(flag[j]==0) 2 { g[i][j].val=min(g[i-1][j].val,g[i- 1][start].val+a[start][j]); if(g[i][j].val<=h) { h=g[i][j].val; p=j; } } else g[i][j].val=g[i-1][j].val; } g[i][p].status=1; flag[p]=1; start=p; } printf("\n\nShortest Path length = %d",g[i-1][end].val); path[c++]=end;p=end; for(j=i-2;j>=0;j--) if(g[j][p].val!=g[j+1][p].val) { for(int k=0;k<n && g[j][k].status!=1;k++); path[c++]=k;p=k; } printf("\nShortest Path: "); for(i=c-1;i>=0;i--)...
View Full Document

This note was uploaded on 08/08/2011 for the course CS 310 taught by Professor Aartisingh during the Spring '11 term at National Institute of Technology, Calicut.

Page1 / 6

Networking - 1 ASSIGNMENT NO.1 1 OBJECTIVE To obtain the...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online