Introd#tion: Algorithms can be used to find the best route between a given source and destination. There are different algorithms having different efficiency of finding the shortest route (e.g. Dijkstras algorithm, Floyd and arshall algorithm. !tc.", while some algorithm just finds any route to the destination (e.g. a DF# or a $F# algorithm". %n this re&ort, the DF# algorithm and Floyds algorithm is im&lemented using ' language. Depth -irst Search Description: A De&th First #earch (DF#" is an algorithm that finds a sim&le &ath in a gra&h. A sim&le &ath is a &ath from source to destination that runs through a se(uence of edges from one verte) to another without running through the same verte) twice. The algorithm starts from the first edge that the source verte) is connected to, and runs through the adjacent vertices and edges till it gets to the destination. %f the chosen &ath does not lead to the destination, it runs back one edge and tries the ne)t edge that that verte) is connected to. Fi#re : A Graph Procedure * + , - . / 0 1 Step %. %nitiali2e all vertices to the ready visited 3 4. Step +. 5ush the starting verte) A onto #TA'6 and change the status of A to visited3 *.
Step ( . Traverse to all vertices connected to the verte) * along one side, Step *. %f all the vertices are not visited, &o& the to& verte) of #TA'6 until the unvisited verte) is found. Step &. Traverse to all unvisited vertices on the other side.
Step &. 7e&eat #te&s , and - until #TA'6 is em&ty. So#rce /ode 8include9stdio.h: 8include9conio.h: int a;+4<;+4<,reach;+4<,n= int main("> int i,j,count34= clrscr("= &rintf(?@n!nter no of vertices A ?"= scanf(?Bd?,Cn"= for(i3*=i93n=iDD" for(j3*=j93n=jDD"> reach;i<34= a;i<;j<34= E &rintf(?@n!nter adjacency matri) A @n?"= for(i3*=i93n=iDD" for(j3*=j93n=jDD" scanf(?Bd?,Ca;i<;j<"= dfs(*"= for(i3*=i93n=iDD" if(reach;i<" countDD= getch("= return(4"= E void dfs(int v" > int i= reach;v<3*= for(i3*=i93n=iDD" if(a;v<;i<CCFreach;i<"> &rintf(?@nBdG:Bd?,v,i"= dfs(i"= E E OUT0UT Floyds Alorith! The FloydHarshall algorithm com&ares all &ossible &aths through the gra&h between each &air of vertices. 0roced#re: Find the length of the shortest &ath between every &air of vertices 7e&resent eighted directed gra&h by adjacent matri) IonGe)istent edges may be assigned a value infinite. The distance between % and j verte) and sum of verte) between %,k and k,j is com&ared and the shortest distance distance is selected.
So#rce /ode 8include9conio.h: int min(int,int"= void floyds(int &;*4<;*4<,int n" > int i,j,k= for(k3*=k93n=kDD" for(i3*=i93n=iDD" for(j3*=j93n=jDD" if(i33j" &;i<;j<34= else &;i<;j<3min(&;i<;j<,&;i<;k<D&;k<;j<"= E int min(int a,int b" > if(a9b" return(a"= else return(b"= E void main(" > int &;*4<;*4<,w,n,e,u,v,i,j= &rintf(?@n !nter the number of verticesA@n?"= scanf(?Bd?,Cn"= &rintf(?@n !nter the number of edgesA@n?"= scanf(?Bd?,Ce"= for(i3*=i93n=iDD" > for(j3*=j93n=jDD" &;i<;j<3JJJ= E for(i3*=i93e=iDD" > &rintf(?@n !nter the end vertices of edgeBd with its weight @n?,i"= scanf(?BdBdBd?,Cu,Cv,Cw"= &;u<;v<3w= E &rintf(?@n Katri) of in&ut dataA@n?"= for(i3*=i93n=iDD" > for(j3*=j93n=jDD" &rintf(?Bd @t?,&;i<;j<"= &rintf(?@n?"= E floyds(&,n"= &rintf(?@n The shortest &aths areA@n?"= for(i3*=i93n=iDD" for(j3*=j93n=jDD" > if(iF3j" &rintf(?@n 9Bd,Bd:3Bd?,i,j,&;i<;j<"= E getch("= E OUT0UT