Data Structure: single-source shortest path-Dijkstra Algorithm

Source: Internet
Author: User

Data Structure: single-source shortest path-Dijkstra Algorithm

Dijkstra Algorithm

Single-Source Shortest Path

Given the belt weight graph, the weight of each edge in the graph is non-negative, representing the distance between two vertices. Specify a vertex in the graph as the Source Vertex to find out the shortest path from the Source Vertex to other vertex and its length, that is, the single-source shortest path problem.

Dijkstra Algorithm

A common method for solving the single-source shortest path problem is the Dijkstra (dijela) algorithm. This algorithm uses a greedy policy: Find the vertex closest to the Source Vertex in the remaining vertex every time.

Algorithm IDEA

Weighted Graph G = So that S is the set of identified Shortest Path vertices, you can use a V-S to represent the set of remaining undetermined Shortest Path vertices. Assume that V0 is the source point, then the initial S = {V0 }. Use the array Distance to represent the path length from the Source Vertex V0 to the remaining vertices, and use the array pre [I] to represent the first vertex of vertex I in the shortest path sequence. Initially, pre [I] is the subscript of the source point. Repeat the following two steps:

 

Find the smallest one from the current Distance [I] and record its subscript v = I. The shortest path from the Source Vertex V0 to the vertex Vv is determined, and add Vv to S. Updates the shortest path length from the source point to the remaining vertex. Update method: the Vv vertex of the preceding step is an intermediate point. If Distance [v] + weight (v, I) It should be pointed out that the Dijkstra algorithm solves not only directed graphs, but also undirected graphs. The following shows an example of a complete directed weighted graph:

 

Instance

Directed weighted Diagram

Dijkstra algorithm solving process

INF indicates infinity.

Code

Class Definition

 

# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define MAXWEIGHT 100 # ifdef INFINITY # undef INFINITY # endif # define INFINITY 1000 class Graph {private: // Number of vertices int numV; // Number of edges int numE; // The adjacent matrix int ** matrix; public: Graph (int numV); // creates the void createGraph (int numE) Graph. // The Destructor ~ Graph (); // The dijela algorithm void Dijkstra (int); // print the adjacent matrix void printAdjacentMatrix (); // check the input bool check (int, int, int );};
    
   
  
Class implementation

 

 

// Constructor, specify the number of vertices Graph: Graph (int numV) {// checks the number of input vertices while (numV <= 0) {cout <incorrect number of vertices! Re-input; cin> numV;} this-> numV = numV; // construct the adjacent matrix and initialize matrix = new int * [numV]; int I, j; for (I = 0; I <numV; I ++) matrix [I] = new int [numV]; for (I = 0; I <numV; I ++) for (j = 0; j <numV; j ++) {if (I = j) matrix [I] [I] = 0; elsematrix [I] [j] = INFINITY ;}} void Graph: createGraph (int numE) {/* checks the number of input edges for a directed Graph of numV vertices, up to numV * (numV-1) edges */while (numE <0 | numE> numV * (numV-1) {cout <the edges are faulty! Re-input; cin> numE;} this-> numE = numE; int tail, head, weight, I; I = 0; cout <enter the start point (arc tail), End Point (ARC header), and weight of each edge <endl; while (I <numE) {cin> tail> head> weight; while (! Check (tail, head, weight) {cout <the input edge is incorrect! Enter <endl; cin> tail> head> weight;} matrix [tail] [head] = weight; I ++ ;}} Graph ::~ Graph () {int I; for (I = 0; I <numV; I ++) delete [] matrix [I]; delete [] matrix ;} /* The dijela algorithm not only obtains the shortest path length but also obtains the sequence of vertex to other vertices */void Graph: Dijkstra (int vertex) {int I; // The direct precursor int * pre = new int [numV]; for (I = 0; I <numV; I ++) of each vertex in the shortest path Sequence) pre [I] = vertex; // The path length from vertex to each vertex int * Distance = new int [numV]; // The initialization path length for (I = 0; I <numV; I ++) Distance [I] = matrix [vertex] [I]; // indicates whether the shortest path of each vertex is found. bool * find = new B Ool [numV]; memset (find, 0, numV); find [vertex] = true; int d, v, count; count = 1, v = vertex; while (count <numV) {d = INFINITY; // determine a shortest path for (I = 0; I <numV; I ++) {if (! Find [I] & Distance [I] <d) {d = Distance [I]; v = I ;}} find [v] = true; // update the precursor and shortest distance of the remaining vertex for (I = 0; I <numV; I ++) {if (! Find [I]) {d = Distance [v] + matrix [v] [I]; if (d <Distance [I]) {pre [I] = v; distance [I] = d ;}}count ++ ;}// print the Shortest Path sequence and its length
  
   
S; for (I = 0; I <numV; I ++) {if (Distance [I] = 0); else if (Distance [I] = INFINITY) cout <vertex <to vertex <I <no path! <Endl; else {cout <vertex <to vertex <I <shortest path length is <Distance [I] <, its sequence is ...; v = I; s. push (v); do {v = pre [v]; s. push (v);} while (v! = Vertex); // print the Shortest Path sequence while (! S. empty () {cout <setw (3) <s. top (); s. pop () ;}cout <endl ;}} cout <endl; delete [] find; delete [] pre; delete [] Distance ;} // print the adjacent matrix void Graph: printAdjacentMatrix () {int I, j; cout. setf (ios: left); cout <setw (7) <; for (I = 0; I <numV; I ++) cout <setw (7) <I; cout <endl; for (I = 0; I <numV; I ++) {cout <setw (7) <I; for (j = 0; j <numV; j ++) cout <setw (7) <matrix [I] [j]; cout <endl ;}} bool Graph: check (int tail, int head, int weight) {if (tail <0 | tail> = numV | head <0 | head> = numV | weight <= 0 | weight> = MAXWEIGHT) return false; return true ;}
  
Main Function

 

 

Int main () {cout <******* Dijkstra ***** by David ***** <endl; int numV, numE; cout <graph creation... <endl; cout <number of input vertices; cin> numV; Graph graph (numV); cout <number of input edges; cin> numE; graph. createGraph (numE); cout <endl <Dijkstra... <endl; for (int I = 0; I <numV; I ++) graph. dijkstra (I); system (pause); return 0 ;}
Run

 

 

 

Download complete code: Dijkstra Algorithm

 

 


If this is helpful, try again!


Column directory:

 

Data Structure and algorithm c pointer

 

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.