You are on page 1of 46

Index

S.NO.
1

PROGRAM NAME
To write an algorithm and a program to implement Bubble Sort

SIGNATURES

To write an algorithm and a program to implement Linear Search

To write an algorithm and a program to implement Binary Search

To write an algorithm and a program to implement Quick Sort

To write an algorithm and a program to implement breadth first search

To write an algorithm and a program to implement depth first search

To write an algorithm and a program to implement Counting Sort

To write an algorithm and a program to implement matrix chain multiplication

To write an algorithm and a program to implement activity selection problem

10

To write an algorithm and a program to implement longest common subsequence

11

To write an algorithm and a program to implement Dijkstras algo for single source shortest path.

Program No-1
AIM: To write an algorithm and a program to implement Bubble Sort.

ALGORITHM

(Bubble Sort) BUBBLE (DATA, N) Here DATA is an array with N elements. This algorithm sorts the elements in DATA.

1. Repeat Steps 2 and 3 for K=1 to N-1. 2. Set PTR := 1. [Initializes pass pointer PTR.] 3. Repeat while PTR<= N-K: [Execute Pass.] (a.) If DATA[PTR] > DATA[PTR+1], then: Interchange DATA[PTR] and DATA[PTR+1]. [END of If structure.] (b.) Set PTR := PTR+1. [END of inner loop.] [END of inner loop.] 4. Exit.

PROGRAM TO IMPLEMENT BUBBLE SORT

#include<stdio.h> #include<conio.h> void main() { int a[30],i,j,temp,n; clrscr(); printf("enter the number of elements in the array");

scanf("%d",&n); printf("enter the elements of the array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("the sorted array is\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); }

OUTPUT:
enter the number of elements in the array5 enter the elements of the array5 4 3 2 1 the sorted array is 1 2 3 4 5

Program No-2
AIM: To write an algorithm and a program to implement Linear Search.
ALGORITHM

(Linear Search) LINEAR (DATA, N, ITEM, LOC) Here DATA is a given array of N elements, and ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC :=0 if the search is unsuccessful.

1. [Insert ITEM at the end of DATA.] Set DATA[N+1] :=ITEM. 2. [Initialize counter.] Set LOC :=1. 3. [Search for ITEM.] Repeat while DATA[LOC]!= ITEM: Set LOC:=LOC+1. [End of loop.] 4. [Successful?] If LOC = N+1, then: Set LOC :=0. 5. Exit.

PROGRAM TO IMPLEMENT LINEAR SEARCH

#include<stdio.h> #include<conio.h> void main() { int a[50],i,n,item,loc,c=0; clrscr(); printf("enter the number of elements in the array"); scanf("%d",&n); printf("enter the elements of the array");

for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("enter the element you want to search"); scanf("%d",&item); for(i=0;i<n;i++) { if(a[i]==item) { c=1; loc=i; } } if(c==1) { printf("item is at location=%d",loc+1); } else { printf("item not in the list"); } getch(); }

OUTPUT:
enter the number of elements in the array5 enter the elements of the array11 32 14 15 20 enter the element you want to search32 item is at location=2

enter the number of elements in the array5 enter the elements of the array21 1 67 54 34 enter the element you want to search90 item not in the list

Program No-3
AIM: To write an algorithm and a program to implement Binary Search.

ALGORITHM

(Binary Search) BINARY (DATA, LB, UB, ITEM, LOC) Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of information. The variables BEG, END and MID denote, respectively, the beginning, end and middle locations of a segment of elements of DATA. This algorithm finds the location of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or sets LOC = NULL.

1. [Initialize segment variables.] Set BEG := LB, END :=UB and MID = INT((BEG+END)/2). 2. Repeat Steps 3 and 4 while BEG <= END and DATA[MID]!= ITEM. 3. If ITEM<DATA[MID], then Set END := MID-1. Else: Set BEG := MID+1. [END of If structure.] 4. Set MID := INT((BEG + END)/2). [END of Step2 loop.] 5. If DATA[MID] = ITEM, then: Set LOC := MID. Else: Set LOC :=NULL. [END of If structure.] 6. Exit.

PROGRAM TO IMPLEMENT BINARY SEARCH

#include<stdio.h> #include<conio.h> void main() { int a[50],i,n,item,loc,high,low=0,mid; clrscr(); printf("enter the number of elements in the array"); scanf("%d",&n); printf("enter the elements in the array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("enter the item you want to search"); scanf("%d",&item); high=n; mid=((low+high)/2); while(low<=high&&a[mid]!=item) { if(item<a[mid]) { high=mid-1; } else { low=mid+1; } mid=((low+high)/2); }

if(a[mid]==item) { printf("item is present at location %d",mid+1); } else { printf("item not in the list"); } getch(); }

OUTPUT:
enter the number of elements in the array6 enter the elements in the array76 54 34 23 17 5 enter the item you want to search23 item is present at location 4

enter the number of elements in the array5 enter the elements in the array5 4 3 2 1 enter the item you want to search6 item not in the list

Program No-4
AIM: To write an algorithm and a program to implement Quick Sort.

ALGORITHM

QUICKSORT(A,p,r) 1. if p<r 2. 3. 4. then q <- PARTITION(A,p,r) QUICKSORT(A,p,q-1) QUICKSORT(A,q+1,r)

PARTITION(A,p,r) 1. x <- A[r] 2. i <- p-1 3. for j <- p to r-1 4. 5. 6. do if A[j] <= x then i <- i +1 exchange A[i] <-> A[j]

7. exchange A[i+1] <-> A[r] 8. return i+1

PROGRAM TO IMPLEMENT QUICK SORT

#include<stdio.h> #include<conio.h> void swap(int a[],int i,int j) { int k;

k=a[i]; a[i]=a[j]; a[j]=k; } int split(int a[],int first,int last) { int right,left,v,temp; v=a[first]; left=last; right=first+1; do { while(right<=left) { if(a[right]<=v) right++; else break; } while(right<=left) { if(a[left]>v) left--; else break; } if(right<=left) { swap(a,right,left); right++; left; } }

while(right<=left); swap(a,first,left); return(left); } void qsort(int a[],int first,int last) { int s=0; if(first<last) { s=split(a,first,last); qsort(a,first,(s-1)); qsort(a,(s+1),last); } } void main() { int a[20],i,n; clrscr(); fflush(stdin); printf("\nHow many elements you want to sort"); scanf("%d",&n); printf("\nenter the values"); for(i=0;i<n;i++) scanf("%d",&a[i]); qsort(a,0,n-1); printf("\nsorted list is"); for(i=0;i<n;i++) printf("\n%d\n",a[i]); getch(); }

OUTPUT:
How many elements you want to sort5

enter the values21 1 43 15 9

sorted list is 1 9 15 21 43

Program No-5
AIM: To write an algorithm and a program to implement breadth first search.
ALGORITHM

BFS(G,s) 1.for each vertex u v[G]-{s}

5. color[s]GRAY 6. d[s]0 7. [s]NIL

9. ENQUEUE(Q,s) 10. while Q 11. 12. 13. 14. do uDEQUEUE(Q) for each v Adj[u] do if color[v]= WHITE

16. 17. 18. color[u]BLACK ENQUEUE(Q,v)

PROGRAM TO IMPEMENT BREADTH FIRST SEARCH

# include<iostream.h> # include<conio.h> # define size 20 # define T 1 # define F 0

Struct edge { int terminal; Edge *next; };

Struct vertex { int visit; int vertex _no; char info; int path-length; Edge *edge_ptr; };

Stuct Q { int info; Q * next; };

class breadth _first_search { public:void table(int,int matrix[size][size],vertex vert[size]); Edge *insert_vertex(int,edge*);

void bfs (int,vertex vert[size]); void input(int,int mat[size][size]); void output(int number,int mat[size][size]); Q *insert_queue(int vertex_no,Q*first); Q *delete_queue(int *vertex_no,Q*first); };

//insert vertex into connectivity list

Edge*breadth_first_search::insert_vertex(int vertex_no,edge*first) { Edge*newl,*current; newl=new(edge); newl->terminal=vertex_no; newl->next=null; if(!first) return(newl); for(current=first;current->next;current=current->next); current->next=newl; return(first); } //insert values into queues

Q*breadth_first_search::insert_queue(int vertex_no,Q*first) { Q*new,*current; newl=new(Q); newl->info=vertex_no; newl->next=null; if(!first) return(newl);

for(current =first;current->next;current=current->next); current->next=newl; return(first); }

Q*breadth_first_search::delete_queue(int*vertex_no,q*first) { Q*previous: if(!first) return(null); *vertex_no=first->info; previous=first; first=first->next; delete(previous); return(first); }

//initializing enteries

void breadth_first_search::table(int vertex_num,int matrix[size][size],vertex vert[size]) { for(int i=0;i<vertex_num;i++) { vert[i].visit=f; vert[i].vertex_no=i+1; vert[i].info=A+i; vert[i].path_length =0; vert[i].edge_ptr=null; } For(i=0;i<vertex_num;i++) For(int j=0;j<vertex_num;j++)

If(matrix[i][j]>0) Vert[i].edge_ptr=insert_vertex(j,vert[i].edge_ptr); }

//computing path length

void bredth_first_search::BFS(int index,vertex vert[size]) { Q *queue=null; edge*link; vert[index].visit=t; queue=insert_queue(index,queue); while(queue) { queue=delete_queue(&index,queue); for(link=vert[index].edge_ptr;link;link=link->next) { if(vert[link->terminal}.visit==f) { vert[link->terminal}.visit=t; vert[link>terminal].path_lenght=vert[index].path_lenght+1; queue=insert_queue(link->terminal,queue); } } } }

//input function to read adjacency matrix

void breadth_first_search::input(int number,int mat[size][size])

{ cout<<\n input the adjacency matrix\n; for(int i=0;i<number;i++) { for(int j=0;j<number;j++) { cin>>mat[i][j]; } cout<<\n; } }

// output function to display adjacency matrix void breadth_first_search::output(int number,int mat[size][size]) { cout<<\n adjacency matrix\n; for(int i=0;i<number;i++) { for(int j=0;j<number;j++) { cout<< <<mat[i][j]; } cout<<\n; } }

\\function main void main() { clrscr(); breadth_first_search B_F_S;

int number,index; int mat[size][size]; vertex vert[size]; Edge*list; cout<<\ninput the no. of vertices in the graph:; cin>>number; B_F_S.input(number,mat); B_F_S.output(number,mat); B_F_S.table(number,mat,vert); cout<<\n input the starting vertex0-<<number-1<<:; cin>>index; B_F_S.BFS(index,vert); cout<<\n path length of the vertex from<<vert[index].info; cout<<\n vertex length vertex connectivity\n; for(int i=0;i<number;i++) { cout<<\n <<vert[i].info<< <<vert[i].path_lenght<< ; for(list=vert[i].edge_ptr;list;list=list->next) { cout<< ; putch(list->terminal+A); } } getch(); }

OUTPUT:
input the no. of vertices in the graph:8 input the adjacency matrix 01000100 01000010 00001011 00010001 11000000 00110001 00011010 10001001

adjacency matrix 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1

input the starting vertex 0-7 path length of the vertex from A vertex length vertex connectivity A B C D E F G H 0 1 2 2 3 1 2 2 BF BG EGH DH AB CDH DEG AEH

Program No-6
AIM: To write an algorithm and a program to implement depth first search.

ALGORITHM

DFS G(v, e)

1. list L = empty 2. tree T = empty 3. choose a starting vertex x 4. search(x) 5. while(L is not empty) 6. remove edge (v, w) from beginning of L 7. if w not yet visited 8. add (v, w) to T 9. search(w) 10.search(vertex v) 11. visit v 12. for each edge (v, w) 13. add edge (v, w) to the beginning of L

PROGRAM TO IMPEMENT DEPTH FIRST SEARCH

#include<stdio.h> #include<conio.h> # define white 0 # define gray 1 # define black 2

int a[10][10]; int n,i,j,s[10],f[10],color[10],time,u; void d_f_s(int u) { color[u]=gray; time=time+1; s[u]=time; for(j=1;j<=n;j++) { if((a[u][j]!=0)&&(color[j]==white)) d_f_s(j); } color[u]=black; time=time+1; f[u]=time; } void parenthesis() { for(i=1;i<=time;i++) { for(j=1;j<=n;j++) { if(i==s[j]) printf("(%d",j); if(i==f[j]) printf("%d)",j); } } } void main() {

clrscr(); printf("enter the no. of vertices :"); scanf("%d",&n); printf("\nENTER THE ADJACENCY MATRIX OF GRAPH \n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("\n THE ADJACENCY MATRIX OF GRAPH \n"); for(i=1;i<=n;i++) { color[i]=white; f[i]=0; s[i]=0; for(j=1;j<=n;j++) { printf("%d ",a[i][j]); } printf("\n"); } time=0; for(i=1;i<=n;i++) { if(color[i]==white) d_f_s(i); } printf("\NAFTER APPLYING D-F-S ALGORITHM\n");

printf("VERTEX STIME FTIME\n"); for(i=1;i<=n;i++) { printf("%d } printf("\n\n PARENTHESIS STRUCTURE IS AS FOLLOWS :\n"); parenthesis(); getch(); } %d %d\n",i,s[i],f[i]);

OUTPUT:
enter the no. of vertices :4

ENTER THE ADJACENCY MATRIX OF GRAPH 0 1 1 0

1 0 0 1

1 0 0 0

0 1

0 0

enter the source vertex :2 THE ADJACENCY MATRIX OF GRAPH 0 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0

AFTER APPLYING BFS ALGORITHM OUTPUT IS :

PATH TO BE FOLLOWED TO REACH THE VERTEX 1 is LENGTH=1 PATH TO BE FOLLOWED TO REACH THE VERTEX 2 is LENGTH=0 PATH TO BE FOLLOWED TO REACH THE VERTEX 3 is LENGTH=2 PATH TO BE FOLLOWED TO REACH THE VERTEX 4 is LENGTH=1

:--2--1

:--2

:--2--1--3

:--2--4

Program No-7
AIM: To write an algorithm and a program to implement Counting Sort.
ALGORITHM 1. for i 0 to k 2. do C[i] 0 do C[A[j]] C[A[j]]+1 do C[i] C[i]+C[i-1] do B[C[A[j]]] A[j] C[A[j]] C[A[j]]-1 3. for j 1 to length[A] 4. 5. for i 1 to k 6. 7. for j length[A] down to 1 8. 9.

PROGRAM TO IMPLEMENT COUNTING SORT

#include<stdio.h> #include<conio.h> void main() { int a[30],b[30],c[30],k,n,I,j; clrscr(); printf(how many numbers do u want to sort\n\n); scanf(%d,&n); printf(\n enter the numbers\n\n); for(i=1;i<=n;i++)

scanf(%d,&a[i]); k=a[1]; for(i=2;i<=n;i++) if(k=a[1]) k=a[i]; for(i=0;i<=k;j++) c[i]=0; for(j=1;j<=n;j++) c[a[j]=c[a[j]]+1; for(j=n;j>=1;j--) { b[c[a[j]]=a[j]; c[a[j]]=c[a[j]]-1; } printf(\n the sorted list by counting sorts is \n); for(i=1;i<=n;i++) printf(\n%d,b[i]); getch(); }

OUTPUT:
how many numbers do u want to sort 5 enters the numbers 10 12 56 96 85

the sorted list by counting sorts is 10 12 56 85 96

Program No-8
AIM: To write an algorithm and a program to implement matrix chain multiplication
ALGORITHM

MATRIX MULTIPLY(A, B) 1. if columns[A] != rows[B] 2. 3. 4. 5. 6. 7. 8. return C then errorincompatible dimensions else for i 1 to rows[A] do for j 1 to columns[B] do c[i,j] 0 for k 1 to columns[A] do C[i,j] C[i,j]+A[i,k].B[k,j]

PROGRAM TO IMPLEMENT MATRIX CHAIN MULTIPLICATION

#include<stdio.h> #include<conio.h> int s[10][10]; void printoptimalparens (int,int); void main() { long int m[10][10],q;

int i,j,k,n=6,p[7],l; clrscr();

printf(enter the value for the dimensions\n); for(i=0;i<=n;i++) { scanf(%d,&p[i]); } for(i=1;i<=n;i++) { m[i][j]=0; } for(l=2;l<=n;l++) { for(i=1;i<=(n-l+1);i++) { j=i+l-1; m[i][j]=999999999; for(k=I;k<=(j-1);k++) { q=m[i][k]+m[k+1][j]+(p[i-1]*p[k]*p[j]); If(q<m[i][j]) { m[i][j]=q; s[i][j]=k; } } } } clrscr(); printf(the minimum mo. Of scalar multiplication m[i][j] is\n\n); for(i=1;i<=n;i++) { for(j=i;j<=n;j++)

{ printf(%d\t,m[i][j]); printf(\n); } } printf(\n auxillary table records the index of k achieved); printf(optimal cost in computing m[i][j]\n\n); for(i=1;i<n;i++) { for(j=1;j<=n;j++) { printf(%d\t,s[i][j]); printf(\n); } } printf(\n the optimal solution is\n\n); print optimal parens (1,n); getch(); } void print optimal parens (int i,int j) { if(i=j) printf(A%d,i); else { printf(( ); print optimal parens (i,s[i][j]); print optimal parens (s[i][j]+1,j); printf() ); } }

OUTPUT:
enter the values for dimensions 20 3 6 15 20 10 12 the minimum no. of scalar multiplication m[i][j] is 0 360 0 270 1170 2370 2370 2850 1170 1770 2130

0 1800 3000 3720 0 3000 4800 0 2400 0 auxillary table records the index of k achieved the optimal cost in computing m[i][j] 0 1 1 1 1 1 0 2 3 4 5 0 3 4 5 0 4 5 0 5 the optimal solution is (A1((((A2 A3)A4)A5)A6))

Program No-9
AIM: To write an algorithm and a program to implement activity selection problem.

ALGORITHM

Activity selector(s,f) 1. nlength[s] 2. A {a1} 3. i 1 4. for m 2 to n 5. 6. 7. 8. return A do if sm>=fi then A A U {am} i m

PROGRAM TO IMPLEMENT ACTIVITY SELECTION PROBLEM

# include<stdio.h> # include<conio.h> void main() { int s[30],f[30],i,m,n; clrscr(); printf(how many activites\n); scanf(%d,&n); printf(\n enter the starting time for activites\n); for(i=1;i<=n;i++)

scanf(%d,&s[i]); printf(\n enter the finishing time for the activites\n); for(i=1;i<=n;i++); scanf(%d,&f[i]); printf(\n the activity order is \n\n); printf(A1); i=1; for(m=2;m<=n;m++) if(s[m]>=f[i]) { printf(\tA%d,m); i=m; } getch(); }

OUTPUT:
how many activites 2 enter the starting time for activites 2 3 enter the finishing time for the activites 4

the activity order is A1 A2

Program No-10
AIM: To write an algorithm and a program to implement longest common subsequence.
ALGORITHM 1. LCS-LENGTH(X,Y) 2. mlength[X] 3. nlength[Y] 4. for i1 to m i. do c[i,0]0 5. for j0 to n i. do c[0,j]0 6. for i1 to m i. do for j1 to n
1.

do if xi =yj a. then c[i,j]c[i-1,j-1]+1 i. b[i,j] b. else if c[i-1,j]c[i,j-1] i. then c[i,j]c[i-1,j] 1. b[i,j] ii. else c[i,j]c[i,j-1] 1. b[i,j]

7. return c and b PRINT-LCS(b,X,i,j) 1. If i=0 or j=0 i. then return 2. if b[i,j]= i. then PRINT-LCS(b,X,i-1,j-1) ii. print xi

3. elseif b[i,j]= i. then PRINT-LCS(b,X,i-1,j) 4. else PRINT-LCS(b,X,i,j-1)

PROGRAM TO IMPLEMENT LONGEST COMMON SUBSEQUENCE

#include<stdio.h> #include<conio.h> void print(int,int); char x[10],y[10]; int b[10][10]; void main() { int m,n,i,j,c[10][10]; clrscr(); printf("Enter the length of first sequence:="); fflush(stdin); scanf("%d",&m); printf("Enter the first sequence:=");

for(i=1;i<=m;i++) { fflush(stdin); scanf("%c",&x[i]); } printf("Enter the length of second sequence:="); fflush(stdin); scanf("%d",&n); printf("Enter the second sequence:="); for(i=1;i<=n;i++) { fflush(stdin);

scanf("%c",&y[i]); } for(i=1;i<=m;i++) { c[i][0]=0; } for(j=0;j<=n;j++) { c[0][j]=0; } for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { if(x[i]==y[j]) { c[i][j]=c[i-1][j-1]+1; b[i][j]=10; } else if(c[i-1][j]>=c[i][j-1]) { b[i][j]=20; } else { c[i][j]=c[i][j-1]; b[i][j]=30; } } }

printf("The longest common subsequence is:"); print(m,n); getch(); } void print(int i,int j) { if(i==0||j==0) { return; } if(b[i][j]==10) { print(i-1,j-1); printf("%c",x[i]); } else if(b[i][j]==20) { print(i-1,j); } else print(i,j-1); }

OUTPUT:
Enter the length of first sequence:=3 Enter the first sequence:=A B C Enter the length of second sequence:=5

Enter the second sequence:=A S D B D The longest common subsequence is:AB

Program No-11
AIM: To write an algorithm and a program to implement Dijkstras algo for single source shortest path.

ALGORITHM

1 function Dijkstra(Graph, source): 2 3 4 5 6 7 8 for each vertex v in Graph: dist[v] := infinity previous[v] := undefined dist[source] := 0 Q := copy(Graph) while Q is not empty: u := extract_min(Q) // Distance from s to s // Set of all unvisited vertices // The main loop // Remove best vertex from priority queue; returns // Initializations // Unknown distance function from s to v

source on first iteration 9 10 11 12 13 for each neighbor v of u: alt = dist[u] + length(u, v) if alt < dist[v] dist[v] := alt previous[v] := u // Relax (u,v)

PROGRAM TO IMPLEMENT DIJKSTRAS ALGO FOR SINGLE SOURCE SHORTEST PATH

#include<stdio.h> #include<conio.h> int a[10][10],d[10],i,j,temp,s,n,temp1,c[10],pre[10]; void printpath(int k)

{ if(k==s) printf("--%d",s); else if(pre[k]==0) printf("no path from source to vertex"); else { printpath(pre[k]); printf("--%d",k); } } void main() {

clrscr(); printf("enter the no. of vertices :"); scanf("%d",&n); printf("\nENTER THE ADJACENCY MATRIX OF GRAPH \n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("enter the source vertex :"); scanf("%d",&s); printf("\n THE ADJACENCY MATRIX OF GRAPH \n"); for(i=1;i<=n;i++)

{ d[i]=999; c[i]=0; pre[i]=0; for(j=1;j<=n;j++) { printf("%d ",a[i][j]); } printf("\n"); } d[s]=0; temp=s; for(j=1;j<=n;j++) { for(i=1;i<=n;i++) { if((a[temp][i]!=0)&&((d[temp]+a[temp][i])<d[i])) { d[i]=d[temp]+a[temp][i]; pre[i]=temp; temp1=i; } } c[temp]=1; temp=temp1; for(i=1;i<=n;i++) { if((i!=s)&&(c[i]!=1)&&(d[i]<d[temp])) {temp=i;} } }

printf("\nAFTER APPLYING DIJKSTRA ALGORITHM OUTPUT IS :\n"); for(i=1;i<=n;i++) { if(i!=s) { printf("\nshortest distance from source to %d :%d and path followed is ",i,d[i]); printpath(i); } } getch(); }

OUTPUT:
enter the no. of vertices :3

ENTER THE ADJACENCY MATRIX OF GRAPH 0 10 5

0 0 2

0 3 0

enter the source vertex :1 THE ADJACENCY MATRIX OF GRAPH

0 10 5 0 0 2 0 3 0

AFTER APPLYING DIJKSTRA ALGORITHM OUTPUT IS :

shortest distance from source to 2 :8 and path followed is --1--3--2 shortest distance from source to 3 :5 and path followed is --1--3

You might also like