码迷,mamicode.com
首页 > 编程语言 > 详细

迪杰斯特拉算法

时间:2015-11-11 16:19:41      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

最近一直带两个考研学生的《数据结构》,正好讲到图这一章,顺便实现了一下迪杰斯特拉算法。

  1 #include"stdafx.h"
  2 #include <iostream>
  3 
  4 using namespace std;
  5 const int MAXSIZE = 10;
  6 const int INF = 99999;//当作最大值
  7 typedef struct VertexNode{
  8     int Index;//点的编号默认为从1开始
  9     char info;
 10 };
 11 
 12 typedef struct MGraph{
 13     int edges[MAXSIZE][MAXSIZE];
 14     VertexNode nodes[MAXSIZE];
 15     int n, e;
 16 };
 17 //构建一个用邻接矩阵存储的图
 18 void CreateMGraph(MGraph& g,int n,int e);
 19 //迪杰斯特拉算法求图的最短路径
 20 void DijkStra(MGraph& g, int v, int dist[], int path[]);
 21 //输出该最短路径
 22 void PrintRoad(int path[],int v);
 23 
 24 void main(void)
 25 {
 26     MGraph g;
 27     int dist[MAXSIZE];
 28     int path[MAXSIZE];
 29     CreateMGraph(g, 7, 12);
 30     DijkStra(g, 1, dist, path);
 31     PrintRoad(path, 7);
 32 }
 33 
 34 void CreateMGraph(MGraph& g, int n,int e)
 35 {
 36     g.n = n;
 37     g.e = e;
 38     int vertex1, vertex2;
 39     int value;
 40     for (int i = 1; i <= n; ++i)
 41     {
 42         g.nodes[i].Index = i;
 43     }
 44 
 45     for (int i = 1; i <= g.n; ++i)
 46     for (int j = 1; j <= g.n; ++j)
 47     {
 48         g.edges[i][j] = INF;
 49     }
 50     
 51     for (int j = 1; j <= e; j++)
 52     {
 53         cout << "请输入边的两个点,中间以空格隔开\n";
 54         cin >> vertex1 >> vertex2;
 55         cout << "请输入该边的权值\n";
 56         cin >> value;
 57         g.edges[vertex1][vertex2] = value;
 58     }
 59 }
 60 
 61 void DijkStra(MGraph& g, int v,int dist[], int path[])
 62 {
 63     int i, j, min, u;
 64     int visited[MAXSIZE];
 65     for (i = 1; i <= g.n; ++i)
 66     {
 67         if (g.edges[v][i] < INF)
 68         {
 69             dist[i] = g.edges[v][i];
 70             path[i] = v;
 71         }
 72         else
 73         {
 74             dist[i] = -1;
 75             path[i] = -1;
 76         }
 77 
 78         visited[i] = 0;
 79     }
 80     visited[v] = 1;
 81     for (i = 1; i <= g.n; ++i)
 82     {
 83         min = INF;
 84         for (j = 1; j <= g.n;++j)
 85         if (visited[j] == 0 && dist[j] < min)
 86         {
 87             min = dist[j];
 88             u = j;
 89         }
 90         visited[u] = 1;
 91         for (j = 1; j <= g.n; ++j)
 92         {
 93             if (visited[j] == 0 && dist[j] > g.edges[u][j] + dist[u])
 94             {
 95                 dist[j] = g.edges[u][j] + dist[u];
 96                 path[j] = u;
 97             }
 98         }
 99     }
100 }
101 
102 void PrintRoad(int path[],int v)
103 {
104     int stack[MAXSIZE], top = -1;
105     while (path[v]!= -1)
106     {
107         stack[++top] = v;
108         v = path[v];
109     }
110     stack[++top] = v;
111     while (top != -1)
112     {
113         cout << stack[top--] << " ";
114     }
115     cout << endl;
116 }

 

迪杰斯特拉算法

标签:

原文地址:http://www.cnblogs.com/tgycoder/p/4956175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!