A--dijkstra algorithm for the shortest path algorithm of the graph with the right direction

Source: Internet
Author: User

Dijkstra algorithm

1. Definition Overview

The Dijkstra (Dijkstra) algorithm is a typical single-source shortest path algorithm that calculates the shortest path of a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point. The Dijkstra algorithm is a representative shortest path algorithm,

In many professional courses as the basic content of the detailed introduction, such as data structure, graph theory, operations research and so on. Note that the algorithm requires no negative weight edges in the graph.

Problem Description: In the g= graph (v,e), assume that each edge e[i] length is w[i], find the shortest path from the vertex V0 to the remaining points. (Single source Shortest path)

2. Algorithm description

First, the path of the starting point to all points is set to infinity (the start-to-start cost is set to 0), the minimum cost (which must be the starting point) from the starting point to the point in an unmarked point without being marked, and the cost of setting the starting point to this point is v.dist, If the point v to the point adjacent to it w satisfies V.dist + <v,w>.weight < W.dist, where <v,w>.weight represents the weight from V to W, then w.dist = V.dist + <v,w>. Weight, and put the node v mark (do not mark W), repeat the above steps until all points are marked, the i.dist of all points is the minimum cost to start at this point.

3 Code implementation

According to the above thought, the implementation code can be, the graph is represented by adjacency table.

#include <iostream>using namespace std; Related definitions of #define INF 65535#define notaverter-1/////////////////////adjacency table//////////////////////typedef struct Edgenode *  position;typedef struct led_table* table;    struct Edgenode//Benzi node {int Adjvex;     The adjacency point field, which stores the subscript int weight of the vertex; The weighted value of the corresponding edge position next; Chain field, pointing to the next adjacency point};                struct led_table//adjacency table structure {int data;       The size of the adjacency table position *firstedge;  Side table header pointer, can be understood as an array}; adjacency table correlation function definition///////////////table creat_lable (int maxelements)//maxelements parameter is the number of nodes you want to create { Table table1 = static_cast<table> (malloc (struct led_table)); table1->data = maxelements;if (table1 = = NULL) {cout << "out of Space!!!";} Table1->firstedge = static_cast<position*> (malloc (sizeof (position) * (Table1->data))); if (Table1->firstedge = = NULL) {cout << "out of Space!!!";} Assigns a value to each table header, starting with 0 for (int i = 0; I <= table1->data-1; ++i) {Table1->firstedge [i] = Static_cast<position> (malloc (position)); Request a node if (Table1->firstedge [i] = = NULL) {cout << "out of Space!!!";}   Table1->firstedge [I]->adjvex = 0;   Table header This parameter is stored in degrees Table1->firstedge [i]->weight = 0; This parameter has no meaning at this time Table1->firstedge [i]->next = NULL;} return table1;} void Insert (Table table1, int v, int w, int Weig)//indicates that an edge exists for <v,w&gt, and the weight is weig{position P = Static_cast<positi   On> (malloc (position)); Request a node if (p = = NULL) {cout << "out of Space!!!";}    P->adjvex = W; P->weight = Weig;p->next = Table1->firstedge [V]->next;table1->firstedge [V]->next = P;} Single source The definition of/////////////////////////typedef struct unweight_path *unweight_node;    struct Unweight_path//{bool know;    int dist;     The adjacency point field, which stores the subscript int path corresponding to the vertex; Weighted value of the corresponding edge};unweight_node unweight_init (Table table1, int start)//node initialization {unweight_node node = static_cast<unweight _node> (malloc (sizeof (UnweiGht_path) * (Table1->data))); if (Node = = NULL) {cout << "out of Space!!!";} for (int i = 0; I! = table1->data; ++i) {Node[i].know = false; Node[i].dist = INF; Node[i].path = Notaverter;} Node[start].dist = 0;return Node;} The single source is entitled to the shortest path algorithm Dijkstra/////////////////////////////void Dijkstra_algorithm (Table table1,  Unweight_node node) {int v;//used two times for loop, with a higher time complexity for (int j = 0; J! = table1->data; ++j) {int zh = inf;for (int i = 0; I! = table1->data; ++i)//Find the path with the smallest and no marked dot {if (!  Node[i].know && zh > Node[i].dist)//If this point is unknown and the distance is smaller than {zh = node[i].dist;   v = i;    }}//at this time V is the smallest point of the distance Node[v].know = true; Mark this point position p = Table1->firstedge [V]->next;while (P! = NULL)//distance from this node +1{if (! Node[p->adjvex].know && node[v].dist + p->weight < node[p->adjvex].dist) {Node[p->adjvex].dist = Node[v].dist + p->weight; Node[p->adjvex].path = V;} p = P->next;}} }/////////////////////print the actual right shortest path////////Using a recursive algorithm, push the void Printpath (int V, unweight_node node) {if (Node[v].path! = notaverter) {  Printpath (Node[v].path, Node); cout << ",";} cout << "V" << v;    }int Main () {Table table_1 = creat_lable (7);  Create an adjacency table of size 7//insert (table_1, 0, 1, 2) for the adjacency table by graph, insert (table_1, 0, 2, 4), insert (table_1, 0, 3, 1);  Insert (Table_1, 1, 3, 3); Insert (Table_1, 1, 4, 10);  Insert (Table_1, 2, 5, 5);  Insert (Table_1, 3, 2, 2), insert (Table_1, 3, 5, 8), insert (Table_1, 3, 6, 4);  Insert (Table_1, 4, 3, 2); Insert (Table_1, 4, 6, 6);    Insert (Table_1, 6, 5, 1);   Unweight_node node_1 = unweight_init (table_1, 0);   Initialize node Dijkstra_algorithm (table_1, node_1);  for (int i = 0; I! = table_1->data; ++i) {cout << node_1[i].dist << ' \ t ';  } cout << Endl;   Printpath (6, node_1);   while (1); return 0;}

For speaking

With V0 as the starting point, the shortest path to each point is:

The following v0->v3->v6 represents the point through which the shortest path v0 to V6.

The Dijkstra algorithm is a typical greedy algorithm and does not guarantee a 100% correct rate.

can also refer to http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html this blog idea, essentially similar.

It's late at night .....

A--dijkstra algorithm for the shortest path algorithm of the graph with the right direction

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.