You are on page 1of 56

1.

2.

Program to draw a line using DDA line algorithm.


Program for implementing Bresenhams algorithm for line
generation.
3. Algorithm & program of insertion sort.
4. Algorithm & program of merge sort.
5. Algorithm & program of quick sort.
6. Algorithm & program of heap sort.
7. Algorithm & program of radix sort.
8. Algorithm & program of bucket sort.
9. Algorithm & program of counting sort.
10. Algorithm & program of linear search.
11. Algorithm & program of binary search.
12. Algorithm & program of BFS.
13. Algorithm & program of DFS.
14. Algorithm & program to find minimum spanning tree using
prims algorithm.
15. Algorithm & program to find optimal solution for a knapsack
problem using greedy method.
16. Algorithm & program to implement Dijkstras algorithm.

/* Program of DDA line drawing. */


#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
void lineDDA(int, int, int, int);
void main() {
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter the starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter the ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineDDA(x1, y1, xn, yn);
getch();
}
void lineDDA(int x1, int y1, int xn, int yn) {
int dx, dy, m, i;
m = (yn-y1)/(xn-x1);
for (i=x1; i<=xn; i++) {
if (m <= 1) {
dx = 1;
dy = m * dx;
2

}
else {
dy = 1;
dx = dy / m;
}
x1 = x1 + dx;
y1 = y1 + dy;
putpixel(x1, y1, RED);
delay(20);
}
}

Output:-

/* Program of Bresanhem line drawing . */


# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2) {
if(p < 0) {
x=x+1;
4

y=y;
p = p + 2 * (dy);
}
else {
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

Output:-

/* Program and algorithm of insertion sort. */


Algo:
Input :- An array A[0:n-1] of n element.
Output :- An array A[0:n-1] of n element in acceding order.
For(i=1 to n-1) do
{
v =A[i];
j =(i-1);
while (j>=0) and (A[j]>v) do
{
A[j+1]=A[j];
J=j-1;
A[j]=v;
}
}

Program:
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[5] = { 25, 18, 32, 14, 3 } ;
int i, j, k, temp ;
clrscr();
printf ( "\t\t\t\t Insertion Sort\n\n" ) ;
printf ( "Array before sorting:\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;
6

for ( i = 1 ; i <= 4 ; i++ )


{
for ( j = 0 ; j < i ; j++ )
{
if ( arr[j] > arr[i] )
{
temp = arr[j] ;
arr[j] = arr[i] ;
for ( k = i ; k > j ; k-- )
arr[k] = arr[k - 1] ;
arr[k + 1] = temp ;
}
}
}
printf ( "\n\n" ) ;
printf ( "Array after sorting:\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch();
}

Output:-

/* Program and algorithm of merge sort. */


Algo:
Heap(item[],N)
{
Build_heap(item,N);
Heapify(item,N)
}
Build_heap(item[],N)
{
for(i=1;i<N;i++)
{
initialize child by 1;

do set parent is equal to (child-1)/2;


if item[parent]<item[child] then
swap item[parent and item[child]]
update child by parent;
while (parent!=0)
Heapify (item[],N)
{
for(I=N-1;I>0;I--)
{
swap item[0] and item[i];
Build heap [item, I];
}
9

}
}
}

Program:
#include<stdio.h>
#include<conio.h>
#define max 100
void mergesort(int low,int high);
void merge(int low,int mid,int high);
int a[max],n;
void main()
{
int i,k;
clrscr();
printf("\t\t\t\t Merge Sort\n\n");
printf("Enter the no of element\n");
scanf("%d",&n);
printf("\nEnter the %d value\n",n);
for(k=0;k<=n-1;k++)
{
scanf("%d",&a[k]);
}
mergesort(0,n-1);
10

printf("\nSorted list is\n");


for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
void mergesort(int low,int high)
{
int mid;
if(low!=high)
{ mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
} }
void merge(int low,int mid,int high)
{ int h=low,i=low,j=mid+1,b[100];
while((h<=mid) && (j<=high))
{ if(a[h]<=a[j])
{ b[i++]=a[h++];
}
else
{ b[i++]=a[j++];
11

}}
while(h<=mid)
b[i++]=a[h++];
while(j<=high)
b[i++]=a[j++];
for(h=low;h<=high;h++)
a[h]=b[h];
}

Output:

12

/* Program and algorithm of quick sort. */


Algo:
Quick(int arr[],int low,int up)
{
int piv,temp,left,right;
left=low;
right=up;
piv=low; /*take first element of sublist as pivot */
if(low>=up)
/*loop till pivot is placed ar its proper place */
{
/* compare from right to left */
while(arr[piv]<=arr[right] && piv=right)
right=right-1;
if(piv==right)
pivot_place=true;
if(arr[piv]>arr[right]])
{
temp=arr[piv];
piv=right;
}
/* Compare from left to rigth */
while(arr[piv]>=arr[left] && left!=piv)
left=left+1;
if(piv==left)
13

pivot_place=true;
if(arr[piv]<arr[left])
{
temp=arr[piv];
arr[piv]=arr[left];
arr[left]=temp;
display(arr,low,up);
Quick(arr,low,piv-1);
Quick(arr,piv+1,up);
}
/* End of Quick */
display(int arr[],int low,int up)
{
int i;
for(i=low to i=up)
pivot(%d,a[i]);
}

14

Program:
#include<stdio.h>
#include<conio.h>
void quicksort ( int *, int, int ) ;
int split ( int *, int, int ) ;
void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i ;
clrscr();
printf ( "\t\t\t\t Quick Sort\n\n" ) ;
printf ( "Array before sorting:\n") ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
quicksort ( arr, 0, 9 ) ;
printf ( "\n" ) ;
printf ( "Array after sorting:\n") ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch();
}
void quicksort ( int a[ ], int lower, int upper )
{
int i ;
15

if ( upper > lower )


{
i = split ( a, lower, upper ) ;
quicksort ( a, lower, i - 1 ) ;
quicksort ( a, i + 1, upper ) ;
}
}
int split ( int a[ ], int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
16

}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}

Output:-

17

/* Program and algorithm of Heap sort. */


Algo:
Heap(item[],N)
{
Build_heap(item,N);
Heapify(item,N)
}
Build_heap(item[],N)
{
for(i=1;i<N;i++)
{
initialize child by 1;
do set parent is equal to (child-1)/2;
if item[parent]<item[child] then
swap item[parent and item[child]]
update child by parent;
while (parent!=0)
Heapify (item[],N)
{
for(I=N-1;I>0;I--)
{
swap item[0] and item[i];
Build heap [item, I];
}
}
18

}
}

Program:
#include <stdio.h>
#include <conio.h>
void makeheap ( int [ ], int ) ;
void heapsort ( int [ ], int ) ;
void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i ;
clrscr();
printf ( "\t\t\t\t Heap Sort\n\n" ) ;
makeheap ( arr, 10 ) ;
printf ( "Before Sorting:\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
heapsort ( arr, 10 ) ;
printf ( "\n" ) ;
printf ( "After Sorting:\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch();
19

}
/* creates heap from the tree*/
void makeheap ( int x[ ], int n )
{
int i, val, s, f ;
for ( i = 1 ; i < n ; i++ )
{
val = x[i] ;
s=i;
f=(s-1)/2;
while ( s > 0 && x[f] < val )
{
x[s] = x[f] ;
s=f;
f=(s-1)/2;
}
x[s] = val ;
}
}
/* sorts heap */
void heapsort ( int x[ ], int n )
{
int i, s, f, ivalue ;
for ( i = n - 1 ; i > 0 ; i-- )
20

{
ivalue = x[i] ;
x[i] = x[0] ;
f=0;
if ( i == 1 )
s = -1 ;
else
s=1;
if ( i > 2 && x[2] > x[1] )
s=2;
while ( s >= 0 && ivalue < x[s] )
{
x[f] = x[s] ;
f=s;
s=2*f+1;
if ( s + 1 <= i - 1 && x[s] < x[s + 1] )
s++ ;
if ( s > i - 1 )
s = -1 ;
}
x[f] = ivalue ;
}
}

21

Output:-

22

/* Program and algorithm of radix sort. */


Algo:
Radix(A,d)
for i 1 to d
use a sutable sort to sort array on digit i.

Program:
#include<stdio.h>
#include<conio.h>
radix_sort(int array[], int n);
void main()
{
int array[100],n,i;
clrscr();
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
printf("\nEnter the elements to be sorted: \n");
for(i = 0 ; i < n ; i++ )
{
printf("\tArray[%d] = ",i);
scanf("%d",&array[i]);
}
printf("\nArray Before Radix Sort:"); //Array Before Radix Sort
for(i = 0; i < n; i++)
23

{
printf("%8d", array[i]);
}
printf("\n");
radix_sort(array,n);
printf("\nArray After Radix Sort: "); //Array After Radix Sort
for(i = 0; i < n; i++)
{
printf("%8d", array[i]);
}
printf("\n");
getch();
}
radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0 ; i<n ; i++)
{
if(arr[i] > large)
{
24

large = arr[i];
}
while(large > 0)
{
num++;
large = large/10;
}
for(passes=0 ; passes<num ; passes++)
{
for(k=0 ; k<10 ; k++)
{
buck[k] = 0;
}
for(i=0 ; i<n ;i++)
{
l = ((arr[i]/div)%10);
bucket[l][buck[l]++] = arr[i];
}
i=0;
for(k=0 ; k<10 ; k++)
{
for(j=0 ; j<buck[k] ; j++)
{
arr[i++] = bucket[k][j];
25

}
}
div*=10;
}
}
}

Output:-

26

/* Program and algorithm of bucket sort. */


Algo:
Bucket_sort[A]
n length (A)
for i=1 to n do
insert A[i] into list B[n[i]]
for i=1 to n-1 do
sort list B with insertion sort
Concatenate the list B[0], B[1] _ _ _ B[n-1] together in order.

Program:
#include<stdio.h>
#include<conio.h>
void Bucket_Sort(int array[], int n)
{
int i,j;
int count[15];

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


{
count[i] = 0;
}
for(i=0; i < n; i++)
27

{
(count[array[i]])++;
}
for(i=0,j=0; i < n; i++)
{
for(; count[i]>0;(count[i])--)
{
array[j++] = i;
}
}
for (i = 0;i < n;i++)
{ printf("%d ", array[i]);
}
printf("\n");
}
void main()
{ int array[100];
int num;
int i;
clrscr();
printf("Enter How many Numbers : ");
scanf("%d",&num);
printf("\nEnter the %d elements to be sorted:\n",num);
for(i = 0; i < num; i++ )
28

{ scanf("%d",&array[i]);
}
printf("\nThe array of elements before sorting : \n");
for (i = 0;i < num;i++)
{ printf("%d ", array[i]);
}
printf("\n\nThe array of elements after sorting : \n");
Bucket_Sort(array, num);
getch();
}

Output:-

29

/* Program and algorithm of count sort. */


Algo:
Counting_sort(A,B,K)
for i0 to k
do c[i]0
for j1 to length[A];
do c[A[j]]c[A[i]]+1
for j1 to k
do c[i]c[i]+c[i-1]
for jlength[A] down to 1
do B[c[A[j]]A[j]
c[A[j]]c[A[j]]-1

Program:
#include<stdio.h>
#include<conio.h>
int Counting_sort(int A[], int k, int n)
{
int i, j;
int B[15], C[100];
for(i = 0; i <= k; i++)
C[i] = 0;
for(j =1; j <= n; j++)
C[A[j]] = C[A[j]] + 1;
30

for(i = 1; i <= k; i++)


C[i] = C[i] + C[i-1];
for(j = n; j >= 1; j--)
{

B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;

}
printf("\nThe Sorted array is :\n");
for(i = 1; i <= n; i++)
printf("\t%d",B[i]);
}
Void main()
{
int n,i,k = 0, A[15];
printf("-----------------------------------------------------------\n");
printf("-----------------------------------------------------------\n\n");
printf("\t\tCOUNTING SORT ALGORITHM\n\n\n\n");
printf("Enter the number of input : ");
scanf("%d",&n);
printf("\n\nEnter the elements to be sorted :\n");
for ( i = 1; i <= n; i++)
{
scanf("%d",&A[i]);
if(A[i] > k)
{
31

k = A[i];
}
}
Counting_sort(A, k, n);
getch();
}

Output:-

32

/* Program of Linear Search. */


#include<stdio.h>
#include<conio.h>
void main(){
int a[10],i,n,m,c=0;
clrscr();
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("\nEnter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("\nEnter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("\nThe number %d is not in the list",m);
else
printf("\n\n\n\t\t\tThe number %d is found",m);
33

getch();
}

Output:-

34

/* Program of Binary Search. */


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n=0,i,j,temp;
int beg,end,mid,target;
clrscr();
printf("Enter the total number=");
scanf("%d",&n);
printf("\nEnter the array elements=");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
beg=0;
end=n;
mid=(beg+end)/2;
printf("\nEnter the number to be searched=");
scanf("%d",&target);
while(beg<=end && a[mid]!=target)
{ if(target<a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
35

}
if(a[mid]==target)
{ printf("\n\nThe number is found at position %2d",mid+1);
}
else
{ printf("\n\nThe number is not found");
}
getch();
}

Output:-

36

/* Program and algorithm of BFS. */


Algo:
Input A graph G(V,E)
Output Same graph G with vertices mark with consecutive integer
in order they are visited by BFS.

for every vertex in (V) marked it with 0 as a mark of being unvisited


count=0;
for each v in V do
if v is marked with 0
BFS(v);
BFS (v) count=count+1
Assign count to v and initialized queue with v while queue is not
empty do
for each vertex w in V which is adjacent to v do
if w is marked with 0
count=count+1
assign count to w and add w to the list and delete v from the
front of the list.

37

Program:
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{ for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{ visited[q[f]]=1;
bfs(q[f++]);
}}
void main()
{ int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
38

scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}

Output:-

39

/* Program of and algorithm DFS. */


Algo:
Input A graph G(V,E)
Output Same graph G with vertices mark with consecutive integer
in order they are visited by DFS.

for every vertex in (V) marked it with 0 as a mark of being unvisited


count=0;
for each v in V do
if v is marked with 0
DFS(v);

DFS(v) Recursive call for DFS for the vertex which is adjacent to v.
count =count+1
marked v with count
for each vertex w in V which is adjacent to v do
if w is marked with 0
DFS(w)

40

Program:
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define VERTEXCOUNT 6
#define VISITED 1
struct node
{
int data;
struct node *next;
};
int visitVertex[VERTEXCOUNT];
struct node * createNode(int data)
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void depthFirstSearch(int index, struct node *vertex[])
{
struct node *temp;
/* mark the unvisted node as visited and print it to console */
41

visitVertex[index - 1] = VISITED;
temp = vertex[index - 1];
printf("VV(Visited Vertex): %d\n", index);
while (temp != NULL)
{

if (visitVertex[temp->data - 1] == VISITED)
temp = temp->next;
else
depthFirstSearch(temp->data, vertex);
}
}
void deleteNodes(struct node *myNode)
{
struct node *temp;
while (myNode != NULL)
{
temp = myNode;
myNode = myNode->next;
free(temp);
}
}
void main()
{
42

struct node *vertex[VERTEXCOUNT], *newnode;


int index = 0;
clrscr();
/* Create adjacency list for the graph */
newnode = createNode(2);
vertex[0] = newnode;
newnode->next = createNode(3);
newnode = createNode(1);
vertex[1] = newnode;
newnode->next = createNode(4);
newnode->next->next = createNode(6);
newnode = createNode(1);
vertex[2] = newnode;
newnode->next = createNode(5);
newnode->next->next = createNode(6);
newnode = createNode(2);
vertex[3] = newnode;
newnode->next = createNode(6);
newnode = createNode(3);
vertex[4] = newnode;
newnode->next = createNode(6);
newnode = createNode(2);
vertex[5] = newnode;
newnode->next = createNode(3);
43

newnode->next->next = createNode(4);
newnode->next->next->next = createNode(5);
/* depth first search operation */
depthFirstSearch(1, vertex);
while (index < VERTEXCOUNT)
{
deleteNodes(vertex[index]);
index++;
}
getch();
}

Output:-

44

/* Program of prims to find the minimum spanning tree. */


#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
45

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)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}

46

Output:-

47

/* Program of Knapsack Fractional Problem. */


Algo:
/* p[1--- n ] , w[1--- n] contains the profit and weight respectively of the
n objects which are ordered such that p[i]/w[i]>=p[i+1]/w[i+1]
that is per unit of weight. m is the capacity of knapsack and x[1--- n] is
the solution vector */

for i=1 to n do
{
x[i]=0.0;
u=m;
for i=1 to n do
{
if(w[i]>u) then break
x[i]=1.0;
u=u-w[i];
}
if(i<=n) than
x[i]=u/w[i];
}

48

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i,u;
int p[20],w[20];
float x[20];
float optimal=0.0;
clrscr();
printf("Enter number of objects=");
scanf("%d",&n);
printf("\n\nEnter capacity of knapsack=");
scanf("%d",&m);
printf("\n\nEnter profit in decreasing order pi/wi=");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("Enter Weight is decreasing order pi/wi=");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
x[i]=0.0;
u=m;
for(i=1;i<=m;i++)
49

{
if(w[i]>4)
break;
else
x[i]=1.0;
u=u-w[i];
}
if(i<=n)
x[i]=(float)u/w[i];
printf("\n\nThe value of x are=");
for(i=1;i<=n;i++)
printf("%f ",x[i]);
for(i=1;i<=n;i++)
optimal=optimal+p[i]*x[i];
printf("%f ",optimal);
getch();
}

50

Output:-

51

/* Dijkestra program and algorithm to find the shortest path. */


Algo:
Dijkestra(G,W,S)
initialize_single_source(G,S)
S
Q v[G]
while Q!=
do v Extract_MIN(Q)
S S U {u}
for each vertex v Adj[u]
do Relax(u,v,W)
initialixe_single_source(G,S)
for each vertex u v[G]
do d[v]
[v] NIL
d[S] 0
when v v[S]
Relax(u,v,W)
if d[v]>d[u]+W(u,v)
then d[v] d[u]+W(u,v)
[v] u

52

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define INFINITY 9999
void main()
{
int vertexCount, **edgeLength, **res;
int i, j, k;
clrscr();
printf("Enter the no of vertices:");
scanf("%d", &vertexCount);
edgeLength = (int **)calloc(sizeof(int), vertexCount);
res = (int **)calloc(sizeof(int), vertexCount);
for (i = 0; i < vertexCount; i++)
{
edgeLength[i] = (int *)calloc(sizeof(int), vertexCount);
res[i] = (int *)calloc(sizeof(int), vertexCount);
}
for (i = 0; i < vertexCount; i++)
{
for (j = 0; j < vertexCount; j++)
{
printf("Edge weight %d to %d(0 if no edge):", i + 1, j + 1);
53

scanf("%d", &edgeLength[i][j]);
if (edgeLength[i][j] == 0)
{
res[i][j] = INFINITY;
}
else
{
res[i][j] = edgeLength[i][j];
}
}
}
printf("Adjacent matrix for edge weights:\n");
for (i = 0; i < vertexCount; i++)
{
for (j = 0; j < vertexCount; j++)
{
printf("%3d", edgeLength[i][j]);
}
printf("\n");
}
/* Calculate shortest path from each vertex to every other vertices */
for (i = 0; i < vertexCount; i++)
{
for (j = 0; j < vertexCount; j++)
54

{
for (k = 0; k < vertexCount; k++)
{
if (res[j][k] > res[j][i] + res[i][k])
{
res[j][k] = res[j][i] + res[i][k];
}
}
}
}
printf("\nShortest path between vertices\n");
for (i = 0; i < vertexCount; i++)
{
for (j = 0; j < vertexCount; j++)
{
if (res[i][j] == INFINITY)
printf("%3d", 0);
else
printf("%3d", res[i][j]);
}
printf("\n");
}
getch();
}
55

Output:-

56