Dijkstra algorithm Dijkstra

Dijkstra algorithm Dijkstra


table of Contents


Introduction and presentation

After we learn the breadth-first search (if you do not know it ... the point here) After that, we have to find a shortest path from point A to point E. Just look for the path is too much trouble, and a waste of time.

photo1-1

Of course, our path through breadth-first search, can only be said to be a shortest path as it passes the number of road is minimal. For example, like the image below ......

photo1

If we add to these roads a few things, such as what, plus some reach the end point from the start point of time, then you can find, and we just take advantage of the breadth-first search resulting path is not the most save time of. At the same time, you also can find a shorter path ......

photo2

When learning breadth-first search, we find the path that the shortest path, that is, the minimum number of segments of the road. And then we have to learn to find the shortest path to path instead of the minimum number of segments that.

  • If the path length is the same, then we 广度优先搜索and our next step is to learn 迪杰斯特拉算法, in fact, the difference is not great .... at least the result is the same.

  • The most important thing to remember is, Dijkstra algorithm applies only to directed acyclic graph !!
  • Dijkstra's algorithm does not apply to the weight of the negative side

Dijkstra (dijkstra)

We first look at a picture we want to go:

photo3

In this picture, you can put each number is thought to go this route to another city time required. Then we set out from our starting point, a time-consuming to find the shortest path ......

photo4

In fact, Dijkstra's algorithm mainly includes the following four steps, it is "graphic algorithm given"

  • Node find "cheapest" node, can be reached in the shortest possible time
  • For the neighbors of the node, check for a shorter path they go, and if so, to update its overhead
  • Repeat this process, we know the figure of each node to do so
  • Calculate the final path

After reading, it is something like this .....

For such a chart (refer to the "Introduction to Algorithms" Figure) is .....

photo5

We begin from the source point, which is our point A in the figure, then starting from point A, we look for the smallest edge, which is what we found from A-> B side of this one, then we have from point A to point update point distance B, was originally infinite, are now updated to 5, a to C simultaneously update 10.

photo6

Then, we chose a small piece side B to the city. We chose B in the city. B can be reached from the cities C, D, E city, then we attach them to the corresponding value .....

photo7

Turn this way, then we put all the points have been updated over .......

Foto8

photo9

Let us look at a dynamic picture .....

gif

Figure code runs

run


Code

Next time we learn to code the (̄ ▽ ̄) "

main.cpp

#include"Dijkstra.h"

int main()
{
    DijkstraGraph d;
    d.Init();
    d.Dijkstra();
    d.Output();
}

/*
8
0 2 8
0 3 7
0 4 1
1 0 7
2 0 1
2 1 6
2 6 2
3 1 7
3 7 9
4 2 9
5 1 8
5 7 7
6 2 8
6 4 8
6 5 3
7 5 9
-1 -1 -1
0
*/

dijkstra.h

#pragma once
#ifndef _DIJKSTRA_H_
#define _DIJKSTRA_H_
#include<iostream>
#include<cstdlib>
#include<vector>
#include<stack>
using namespace std;

struct node
{
    int to;
    int distance;
    node() {
        to = -1;
        distance = -1;
    }
};
//可以用迭代器指针去模拟
class DijkstraGraph //使用邻接表来存储
{
private:
    const int MAX = 0x3f3f3f;
    vector<vector<node>>graph;//存放图
    vector<bool>visited;
    vector<int>pre;//存放前驱
    vector<int>distance;//存放点什么到什么的距离
    int pointNumber;    
    int start;
public:
    void Init();//初始化邻接表
    void Dijkstra();//进行迪杰斯特拉算法
    void Output();//输出图示

};

#endif

dijkstra.cpp

// Dijkstra.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "Dijkstra.h"

void DijkstraGraph::Init()
{
    pointNumber = 0;
    cout << "请输入顶点个数:" << endl;
    cin >> pointNumber;
    graph.resize(pointNumber );
    visited.resize(pointNumber , false);
    pre.resize(pointNumber, -1);
    distance.resize(pointNumber , MAX);
    cout << "请输入顶点的关系,from - to - distance" << endl;
    int a, b, dis;
    
    while (cin >> a >> b >> dis) 
    {
        if (a == -1 || b == -1 || dis == -1)
            break;
        node temp;
        temp.distance = dis;
        temp.to = b;
        graph[a].push_back(temp);
    }

    cout << "存储好了邻接表" << endl;
}

void DijkstraGraph::Dijkstra()
{
    vector<node>::iterator iter1;
    int start;
    int min = MAX;
    int index = 0;
    cout << "请输入出发点" << endl;
    cin >> start;
    this->start = start;
    pre[start] = 0;
    distance[start] = 0;
    
    for (int i = 0; i < pointNumber; i++)
    {
        visited[start] = true;
        //index = 0;
        min = MAX;
        for (iter1 = graph[start].begin(); iter1 < graph[start].end(); iter1++)
        {
            if (!visited[iter1->to] && distance[start] + iter1->distance < distance[iter1->to])
            {
                pre[iter1->to] = start;//进行前驱更新
                distance[iter1->to] = iter1->distance+distance[start];//距离更新
            }
        }
        for (int j = 0; j < pointNumber; j++) //找没经历过的最小的元素值
        {
            if (!visited[j] && min > distance[j])
            {
                min = distance[j];
                start = j;
            }
        }
    }
}

void DijkstraGraph::Output()
{
    stack<int>s;
    //cout << "距离:" << endl;
    //vector<int>::iterator iter;
    //for (iter = distance.begin(); iter < distance.end(); iter++)
        //cout << *iter << " ";
    int temp;
    cout << "-----------------------" << endl;
    cout << "出发点" << this->start << endl;
    for (int i = 0; i < pointNumber; i++)
    {
        cout << "到达点 " << i << " 经过路径: ";
        temp = pre[i];
        s.push(i);
        while (temp != this->start)
        {
            s.push(temp);
            temp = pre[temp];
        }
        s.push(this->start);
        while (!s.empty())
        {
            cout << s.top() << " ";
            s.pop();
        }
        cout << "\t\t 距离是:" << distance[i] << endl;

    }
    cout << "------------------------" << endl;
    
}


Reference "algorithm illustrated", "Introduction to Algorithms", "Data Structures and Algorithms C language version"

Guess you like

Origin www.cnblogs.com/Yunrui-blogs/p/12088882.html