Dijkstra's algorithm: Difference between revisions

From Rosetta Code
Content added Content deleted
(fix page structure. add wikipedia description of algorithium. Needs test case.)
m (fix up a little bit)
Line 1: Line 1:
{{drafttask|algorithm}}{{Wikipedia}}
{{draft task|Classic CS problems}}{{Wikipedia}}
'''Dijkstra's algorithm''', conceived by Dutch [[wp:computer scientist|computer scientist]] [[wp:Edsger Dijkstra|Edsger Dijkstra]] in 1956 and published in 1959, is a [[wp:graph search algorithm|graph search algorithm]] that solves the single-source [[wp:shortest path problem|shortest path problem]] for a [[wp:graph (mathematics)|graph]] with nonnegative [[wp:edge (graph theory)|edge]] path costs, producing a [[wp:shortest path tree|shortest path tree]]. This algorithm is often used in [[wp:routing|routing]] and as a subroutine in other graph algorithms.
'''Dijkstra's algorithm''', conceived by Dutch [[wp:computer scientist|computer scientist]] [[wp:Edsger Dijkstra|Edsger Dijkstra]] in 1956 and published in 1959, is a [[wp:graph search algorithm|graph search algorithm]] that solves the single-source [[wp:shortest path problem|shortest path problem]] for a [[wp:graph (mathematics)|graph]] with nonnegative [[wp:edge (graph theory)|edge]] path costs, producing a [[wp:shortest path tree|shortest path tree]]. This algorithm is often used in [[wp:routing|routing]] and as a subroutine in other graph algorithms.


For a given source [[wp:vertex (graph theory)|vertex]] (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path first is widely used in network [[wp:routing protocol|routing protocol]]s, most notably [[wp:IS-IS|IS-IS]] and [[wp:OSPF|OSPF]] (Open Shortest Path First).
For a given source [[wp:vertex (graph theory)|vertex]] (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path first is widely used in network [[wp:routing protocol|routing protocol]]s, most notably [[wp:IS-IS|IS-IS]] and [[wp:OSPF|OSPF]] (Open Shortest Path First).
=={{header|java}}==
=={{header|Java}}==
<lang java>import java.io.*;
<lang java>import java.io.*;
import java.util.*;
import java.util.*;

Revision as of 23:48, 8 December 2011

Dijkstra's algorithm is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at Dijkstra's algorithm. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1956 and published in 1959, is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.

For a given source vertex (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path first is widely used in network routing protocols, most notably IS-IS and OSPF (Open Shortest Path First).

Java

<lang java>import java.io.*; import java.util.*;

class Graph { int MAXNODES=50; int MAX1=150; int INFINITY=5000; int i,j,finall=0; int smalldist,newdist,k,s,d,current,n,distcurr; int weight[][]=new int[MAXNODES][MAXNODES]; int distance[]=new int[MAXNODES]; int visit[]=new int[MAXNODES]; int precede[]=new int[MAXNODES]; int path[]=new int[MAX1];


/*function to print the result*/ void Display_Result() { i=d; path[finall]=d; finall++; while(precede[i] != s) { j = precede[i]; i = j; path[finall] = i; finall++; } path[finall] = s; System.out.println("\nThe shortest path followed is : \n\n"); for(i=finall;i>0;i--) System.out.print("\t\t( "+ path[i]+" ->"+path[i-1]+" ) with cost = "); System.out.print(weight[path[i]][path[i-1]]+"\n\n"); System.out.println("For the Total Cost = "+distance[d]); }/*end Display_Result*/

/*function to read number*/ int getNumber() { String str; int ne=0; InputStreamReader input=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(input); try { str=in.readLine(); ne=Integer.parseInt(str); } catch(Exception e) { System.out.println("I/O Error"); } return ne; }/*end getNumber*/

/*function for shortest path*/ void SPA() { System.out.print("\nEnter the number of nodes (Less than 50) in the matrix : "); n=getNumber();

System.out.print("\nEnter the cost matrix : \n\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) { System.out.print("\nCost "+(i+1)+"--"+(j+1)+" : "); weight[i][j]=getNumber(); }


System.out.print("\nEnter the source node : "); s=getNumber();

System.out.print("\nEnter the destination node : "); d=getNumber();

for (i=0;i<n;i++) { distance[i]=INFINITY; precede[i]=INFINITY; } distance[s]=0; current=s; visit[current]=1; while(current!=d) { distcurr=distance[current]; smalldist=INFINITY; for(i=0; i<n; i++) { if(visit[i]==0) { newdist=distcurr+weight[current][i]; if(newdist<distance[i]) { distance[i]=newdist; precede[i]=current; } if(distance[i]< smalldist) { smalldist=distance[i]; k=i; } } } current = k; visit[current]=1; } Display_Result(); }

}/*end SPA*/


class Dijkstra { public static void main(String args[]) { Graph g=new Graph(); g.SPA(); } }</lang>