Graph Theory-directed Weighted Graph Shortest Path (Dijkstra) Algorithm

Source: Internet
Author: User

Find the shortest path between two vertices

The rule always sends an agent to the next city. The agent's work is to record the minimum fee information from the source point to other cities.
Calculate the cost and construct an array to keep the shortest distance from the source point to other vertices. This distance varies during Algorithm Execution. Until the end, it stores the true shortest distance from the source point.

Package COM. mapbar. structure;/*** class graph_shortstra.java * description Dijkstra shortest path algorithm, which replaces the priority queue with an array to store the shortest path * company mapbar * Author chenll E-mail: chenll@mapbar.com * version 1.0 * Date 03:46:15 * // Save the distance from the starting point to the current point and the parent node class distpar {public int distance; Public int parvert; public distpar (int pv, int dis) {This. distance = DIS; this. parvert = PV ;}/// defines the vertex class vertex {public char label; Public Boolean isintree; Public vertex (char label) {This. label = label; isintree = false ;}} class graph {private final int max_vertx = 20; // number of nodes private final int infinity = 1000000; // Private vertex vertexlist []; private int adjmat [] []; // The ing matrix private int nverts; // record the number of vertices private int NTree; // record the number of vertices in the tree private int currentvert; // The current vertex private distpar Spath []; // the Shortest Path array, which stores the distance between the Source Vertex and other vertices. Private int starttocurrent; // constructor public graph () {vertexlist = new vertex [max_vertx]; adjmat = new int [max_vertx] [max_vertx]; nverts = 0; // initialization matrix for (INT I = 0; I <max_vertx; I ++) {for (Int J = 0; j <max_vertx; j ++) {adjmat [I] [J] = infinity ;}} Spath = new distpar [max_vertx];} // Add the vertex public void addvertex (char label) {vertexlist [nverts ++] = new vertex (Label);} // Add an edge public void addedge (int sv, int DV, I NT dis) {adjmat [SV] [DV] = DIS;} // public void path () {int starttree = 0; vertexlist [0]. isintree = true; NTree = 1; for (INT I = 0; I <nverts; I ++) {int tempdis = adjmat [starttree] [I]; spath [I] = new distpar (starttree, tempdis);} while (NTree <nverts) {// (1) Find the smallest distance from the Spath array int indexmin = getmin (); int mindis = Spath [indexmin]. distance; If (mindis = infinity) {break;} else {// (2) set current vertex currentvert = indexmin; // Source Starttocurrent = Spath [indexmin]. distance;} vertexlist [currentvert]. isintree = true; NTree ++; // (3) Adjust the array adjust_spath () ;}displaypath () based on the current vertex changes; // reset NTree = 0; for (Int J = 0; j <nverts; j ++) {vertexlist [J]. isintree = false ;}}// find the Spath [I] array that is not in the tree and is the smallest vertex public int getmin () {int mindis = infinity; int indexmin = 0; for (INT I = 1; I <nverts; I ++) {If (! Vertexlist [I]. isintree & Spath [I]. distance <mindis) {mindis = Spath [I]. distance; indexmin = I;} return indexmin;} // adjust the value of the distance array from the current vertex to public void adjust_spath () {// cyclic counter, point to each vertex in turn int column = 1; while (column <nverts) {If (vertexlist [column]. isintree) {Column ++; continue;} int currenttofringe = adjmat [currentvert] [column]; int starttofring = currenttofringe + starttocurrent; int spathdis = Spath [column]. distance; If (starttofring <spathdis) {// Replace the current item so that the array always stores the Shortest Path Spath [column] from the source point to all known vertices. parvert = currentvert; Spath [column]. distance = starttofring;} column ++ ;}// the display array is the shortest distance bracket from The Source Vertex to each vertex. It is the parent node public void displaypath () {for (Int J = 0; j <nverts; j ++) {system. out. print (vertexlist [J]. label + "="); If (Spath [J]. distance = infinity) {system. out. print ("inf");} else {system. out. print (Spath [J]. distance);} Char parent = vertexlist [Spath [J]. parvert]. label; system. out. print ("(" + parent + ")") ;}} public class graph_dijkstra {public static void main (string [] ARGs) {graph G = New Graph (); g. addvertex ('A'); G. addvertex ('B'); G. addvertex ('C'); G. addvertex ('D'); G. addvertex ('E'); G. addedge (0, 1, 50); G. addedge (0, 3, 80); G. addedge (1, 2, 60); G. addedge (1, 3, 90); G. addedge (2, 4, 40); G. addedge (3, 2, 20); G. addedge (3, 4, 70); G. addedge (4, 1, 50); G. PATH ();}}

Output result: a = inf (a) B = 50 (a) C = 100 (d) d = 80 (a) E = 140 (c)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.