Shortest path algorithm in graph (Dijkstra algorithm)

Source: Internet
Author: User

1.Dijkstra

1) Applicable conditions & scope:

A) Single source shortest path (from source point s to all other vertex V);

b) The direction graph & the non-direction graph (u,v), (V,u) is the same as the direction graph of the edge set E)

c) All edge rights non-negative (I,J) ∈e have wij≥0);

2) Algorithm Description:

The most common problem in weighted graphs is to find the shortest path problem between two points.

The most famous algorithm for solving the shortest path problem is the Djikstra algorithm. The implementation of this algorithm is based on the adjacency matrix notation of graphs, which not only can find the shortest path of any two points, but also can find the shortest path of a specified point to all other vertices.

The basic idea of this algorithm is:

1> selects the specified vertex, listing the weights of the vertices to other vertices, not adjacent to the Infinity

2> the minimum value from the above weights, the minimum is the shortest path from the starting point to the corresponding vertex, and the corresponding vertex is marked

3> the direct distance from the starting point to the other unlabeled vertices and the start point to the previous marker vertex plus the marker vertex to the other vertex distance, and if the latter is small, the corresponding weight is updated.

4> ext. 2

The program code is as follows:

#include <iostream>

#include <iomanip>

using namespace Std;

#include "Graph.h"

void Main ()

{

int i, n;

cout << "Enter the number of vertices you have entered for the weighted graph:";

CIN >> N;

Adjmatrix G;

Initmatrix (g);

Creatematrix (g);

cout << "The adjacency matrix of the weighted graph you entered is represented as:" << Endl;

Printmatrix (g, N);

int * d = new int [n];

Edgenode * * Path = new Edgenode * [n];

cout << "Please enter the source point you want to enter:";

Cin >> I;

Dijkstra (g, D, Path, I, n);

Printpath (d, Path, I, n);

}

graph.h**********************

#define MAXVERNUM 20

#define MAXVALUE 10000

typedef int ADJMATRIX[MAXVERNUM][MAXVERNUM]; Type definitions for adjacency matrices

typedef struct NODE

{

int Adjvex;

struct Node * NEXT;

}edgenode; Pointer array path[] base type definition

A weighted graph of the initialized adjacency matrix representation

void Initmatrix (Adjmatrix G)

{

int I, J;

for (i = 0; i < maxvernum; i++)

for (j = 0; J < Maxvernum; J + +)

G[I][J] = MaxValue;

}

To establish an adjacency matrix representation of the right to the graph (that is, by each edge of the input graph to establish the adjacency matrix of the graph)

void Creatematrix (Adjmatrix G)

{

int I, j, X;

cout << "Please enter vertices and corresponding weights:" << Endl;

Cin >> I >> j >> x;

while (i! =-1)

{

G[I][J] = x;

Cin >> I >> j >> x;

}

}

The weighted graph represented by the output adjacency matrix (that is, each edge of the output graph)

void Printmatrix (Adjmatrix G, int n)

{

int I, J;

for (i = 0; i < n; i++)

{

for (j = 0; J < N; j + +)

{

if (g[i][j] = = MaxValue)

cout << setiosflags (ios::left) << SETW (5) << "INF";

Else

cout << setiosflags (ios::left) << SETW (5) << g[i][j];

}

cout << Endl;

}

}

void Path (Edgenode * path[], int m, int j)

{

Edgenode * p, * q, *s;

p = path[j];

while (P! = NULL)

{

PATH[J] = p->next;

Delete p;

p = path[j];

}

p = path[m];

while (P! = NULL)

{

Q = new Edgenode;

Q->adjvex = p->adjvex;

if (path[j] = = NULL)

PATH[J] = q;

Else

S->next = q;

s = q;

p = p->next;

}

Q = new Edgenode;

Q->adjvex = j;

Q->next = NULL;

S->next = q;

}

Dijkstral algorithm for finding shortest path

void Dijkstra (Adjmatrix GA, int dist[], edgenode *path[], int i, int n)

{

Int J, K, W, M;

BOOL * s = new Bool[n];

for (j = 0; J < N; j + +)

{

if (j = = i)

S[J] = true;

Else

S[J] = false;

DIST[J] = Ga[i][j];

if (Dist[j] < MaxValue && J! = i)

{

Edgenode * p1 = new Edgenode;

Edgenode * P2 = new Edgenode;

P1->adjvex = i;

P2->adjvex = j;

P2->next = NULL;

P1->next = p2;

PATH[J] = p1;

}

Else

PATH[J] = NULL;

}

for (k = 1; k <= n-2; k++)

{

W = MaxValue;

m = i;

for (j = 0; J < N; j + +)

if (s[j] = = False && Dist[j] < W)

{

W = dist[j];

m = j;

}

if (m! = i)

S[m] = true;

Else

Break

for (j = 0; J < N; j + +)

if (s[j] = = False && Dist[m] + ga[m][j] < Dist[j])

{

DIST[J] = Dist[m]+ga[m][j];

Path (Path, M, j);

}

}

delete []s;

}

function to output the shortest path and length from the source point to each vertex

void Printpath (int dist[], Edgenode * path[], int i, int n)

{

Int J;

for (j = 0; J < N; j + +)

{

if (i! = j)

{

cout << "Vertex v" << i << "to Vertex v" << J << "Shortest path length is"

<< Dist[j] << ", Shortest path:";

Edgenode * p = path[j];

while (P! = NULL)

{

cout << SETW (4) << p->adjvex;

p = p->next;

}

cout << Endl;

}

}

}

Program Run Result:

Enter the number of vertices you have entered for the weighted graph: 6

Please enter a vertex and a corresponding weight value:

0 1 10

0 2 12

1 3 16

1 4 25

2 0 4

2 1 3

2 3 12

2 5 8

3 4 7

5 3 2

5 4 10

-1 2 3

The adjacency matrix of the weighted graph you have entered is represented as:

INF. inf INF

INF inf INF

4 3 INF 8 inf

INF inf INF 7 inf

INF INF inf INF

INF INF 2 inf

Please enter the source point you want to enter: 0

The shortest path to vertex v0 to vertex v1 is 10 and the shortest path is: 0 1

The shortest path to vertex v0 to vertex v2 is 12 and the shortest path is: 0 2

The shortest path to vertex v0 to vertex v3 is 22 and the shortest path is: 0 2 5 3

The shortest path to vertex v0 to vertex v4 is 29 and the shortest path is: 0 2 5 3 4

The shortest path to vertex v0 to vertex V5 is 20 and the shortest path is: 0 2 5

Press any key to continue

Transferred from: http://blog.sina.com.cn/s/blog_51b6521b0100k96c.html

Shortest path algorithm in graph (Dijkstra algorithm) (RPM)

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.