图单源最短路径问题(Dijkstra算法)

算法思路

1. 图用邻接表表达( http://caixindong.github.io/blog/2016/02/19/ios53/ ),每个图结点都储存一个值,记录源结点到结点的距离,一开始初始化为不可达,即距离设置为无穷大,而源结点的值为0,因为源结点到源结点的距离为0;
2. 从源结点开始,遍历从源结点出发的所有路径,某结点可达,它的值更新为源结点的值加上之间路径的值,不可达的结点保持原来的值;
3. 从那些未遍历的结点中取值最小的结点作为下一个遍历的结点;
4. 遍历从当前遍历结点出发的所有路径,某结点可达,比较当前遍历结点的值加上路径值的和与某结点的值的大小,哪个小就更新为哪个值; 5. 继续从那些未遍历的结点中取值最小的结点作为下一个遍历的结点,继续步骤4的做法,直到所有结点都被遍历。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include "graph.hpp"
#include <climits>
#include <queue>
using namespace std;
/**
 * 用于记录图结点数据
 * id   结点id
 * dist 源结点到当前结点的最短距离
 * prev 最短路径的前一个结点
 * processed 是否遍历过
 **/
struct node {
    int id;
    int dist;
    int prev;
    bool processed;
    node():dist(INT_MAX),prev(-1),processed(false){

    }
};

/**
* 用于比较距离大小的仿函数
**/
struct compare_node {
    bool operator()(node& a,node &b){
        return a.dist > b.dist;
    }
};

/**
 * Dijkstra算法
 * g 操作的图
 * source 源结点
 **/
vector<node> Dijkstra(Graph &g,int source){
    //获取图结点个数
    int node_count = (int)g.ad_vector.size();

    //将遍历每个结点的结果存放在nodes容器
    vector<node> nodes(node_count);

    for (int i=0; i< node_count; i++) {
        nodes[i].id = i;
    }

    nodes[source].dist = 0;

    //优先队列,dist小的先出队
    priority_queue<node,vector<node>,compare_node> node_pq;
    node_pq.push(nodes[source]);

    while (!node_pq.empty()) {
        node current_node = node_pq.top();
        node_pq.pop();
        if (current_node.processed) {
            continue;
        }else{
            list<edge_node> edge_nodes = g.ad_vector[current_node.id];
            for (auto pos = edge_nodes.begin(); pos!=edge_nodes.end(); pos++) {
                node& dest_node = nodes[pos->dest_id];
                if (current_node.dist+pos->weight<dest_node.dist) {
                    dest_node.dist = current_node.dist+pos->weight;
                    dest_node.prev = current_node.id;
                    node_pq.push(dest_node);
                }
            }
            current_node.processed = true;
        }
    }
    return nodes;
}

void print_path(vector<node>& nodes,int node_id){
    if (nodes[node_id].prev==-1) {
        cout<<"path: "<<node_id;
    }else{
        print_path(nodes,nodes[node_id].prev);
        cout<< "->" << node_id;
    }
}

int main(int argc, const char * argv[]) {
    Graph g(7);
    g.add_edge(0, 1, 2);
    g.add_edge(0, 3, 9);
    g.add_edge(0, 4, 6);
    g.add_edge(1, 2, 1);
    g.add_edge(1, 4, 3);
    g.add_edge(2, 4, 1);
    g.add_edge(2, 6, 6);
    g.add_edge(4, 3, 2);
    g.add_edge(4, 5, 9);
    g.add_edge(5, 6, 5);

    vector<node> result = Dijkstra(g, 0);
    for(int i = 0; i < result.size(); i++){
        cout << "Print path and dist for node:" << result[i].id << endl;
        print_path(result, result[i].id);
        cout << endl << "Dist:" << result[i].dist << endl;
    }
        return 0;
}

Comments