Dijkstra算法图文详解

Dijkstra算法详解
本文深入讲解Dijkstra算法的原理及其实现,通过实例演示如何利用贪心策略寻找单源最短路径,适用于加权图的最短路径问题解决。

博主自己写的java实现代码:

public class dijkstra {


    public static void main(String[] args) {
        int[][] map = new int[6][6];
        initmap(map);
        for(int i=0;i<6;i++)
        {
            for (int j = 0; j < 6; j++) {
                System.out.print(String.format("%3d", map[i][j]));
            }
            System.out.println("");
        }


        //长度序列
        int[] len = new int[6];
        //是否计算过队列,贪心,1为确认过
        int[] bool = new int[6];

        // 初始化len数组,起点到自身距离为0,到其他点设为极大值
        for (int i = 0; i < len.length; i++) {
            len[i] = Integer.MAX_VALUE;
        }
        len[0] = 0;

        for (int i = 0; i < bool.length; i++) {

            //记录每行的最小值坐标
            int minRow = -1;
            int min = Integer.MAX_VALUE;

            // 找到距离起点最近的未确定节点
            //找此行,哪一列最小
            for (int j = 0; j < map[i].length; j++) {
                if (bool[j] == 0  && len[j] < min) {
                    minRow = j;
                    min = len[j];
                }
            }

            if (-1 == minRow) {
                break;
            }
            bool[minRow] = 1;
            //minRow 转移到第几行计算总距离
            for (int k = 0; k < map[minRow].length; k++) {
                if (map[minRow][k] > 0 && bool[k] == 0 && len[minRow] + map[minRow][k] < len[k]) {
                    len[k] = len[minRow] + map[minRow][k];
                }
            }

            for (int num : len) {
                System.out.print(num + " ");
            }
            System.out.println("");
        }

        for(int i=0;i<6;i++)
        {
            System.out.print(String.format("%20d", bool[i]));
        }
        System.out.println();
        for(int i=0;i<6;i++)
        {
            System.out.print(String.format("%20d", len[i]));
        }


    }

    private static void initmap(int[][] map) {
        map[0][1]=2;map[0][2]=3;map[0][3]=6;
        map[1][0]=2;map[1][4]=4;map[1][5]=6;
        map[2][0]=3;map[2][3]=2;
        map[3][0]=6;map[3][2]=2;map[3][4]=1;map[3][5]=3;
        map[4][1]=4;map[4][3]=1;
        map[5][1]=6;map[5][3]=3;
    }
}

//结果:0 2 3 5 6 8

博主自己找的java实现代码:

import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class dijkstra {
    static class node
    {
        int x; //节点编号
        int lenth;//长度
        public node(int x,int lenth) {
            this.x=x;
            this.lenth=lenth;
        }
    }

    public static void main(String[] args) {

        int[][] map = new int[6][6];//记录权值,顺便记录链接情况,可以考虑附加邻接表
        initmap(map);//初始化
        boolean bool[]=new boolean[6];//判断是否已经确定
        int len[]=new int[6];//长度
        for(int i=0;i<6;i++)
        {
            len[i]=Integer.MAX_VALUE;
        }
        Queue<node>q1=new PriorityQueue<node>(com);
        len[0]=0;//从0这个点开始
        q1.add(new node(0, 0));
        int count=0;//计算执行了几次dijkstra
        while (!q1.isEmpty()) {
            node t1=q1.poll();
            int index=t1.x;//节点编号
            int length=t1.lenth;//节点当前点距离
            bool[index]=true;//抛出的点确定
            count++;//其实执行了6次就可以确定就不需要继续执行了  这句可有可无,有了减少计算次数
            for(int i=0;i<map[index].length;i++)
            {
                if(map[index][i]>0&&!bool[i])
                {
                    node node=new node(i, length+map[index][i]);
                    if(len[i]>node.lenth)//需要更新节点的时候更新节点并加入队列
                    {
                        len[i]=node.lenth;
                        q1.add(node);
                    }
                }
            }
        }
        for(int i=0;i<6;i++)
        {
            System.out.println(len[i]);
        }
    }
    static Comparator<node>com=new Comparator<node>() {

        public int compare(node o1, node o2) {
            return o1.lenth-o2.lenth;
        }
    };

    private static void initmap(int[][] map) {
        map[0][1]=2;map[0][2]=3;map[0][3]=6;
        map[1][0]=2;map[1][4]=4;map[1][5]=6;
        map[2][0]=3;map[2][3]=2;
        map[3][0]=6;map[3][2]=2;map[3][4]=1;map[3][5]=3;
        map[4][1]=4;map[4][3]=1;
        map[5][1]=6;map[5][3]=3;
    }
}

//结果:0 2 3 5 6 8

下文内容转载自:https://blog.csdn.net/lbperfect123/article/details/84281300

Dijkstra算法

 

Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不会更近,如果更近了就更新距离,这样把所有的点找遍之后就存下了起点到其他所有点的最短距离。

 

问题引入:

指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”。例如求下图中的1号顶点到2、3、4、5、6号顶点的最短路径。

 

 

 

 

 

 下面我们来模拟一下:

 这就是Dijkstra算法的基本思路:

接下来是代码:

已经把几个过程都封装成了基本模块:


 
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. #define Inf 0x3f3f3f3f
  6. using namespace std;
  7. int map[ 1005][ 1005];
  8. int vis[ 1005],dis[ 1005];
  9. int n,m; //n个点,m条边
  10. void Init ()
  11. {
  12. memset( map,Inf, sizeof( map));
  13. for( int i= 1;i<=n;i++)
  14. {
  15. map[i][i]= 0;
  16. }
  17. }
  18. void Getmap()
  19. {
  20. int u,v,w;
  21. for( int t= 1;t<=m;t++)
  22. {
  23. scanf( "%d%d%d",&u,&v,&w);
  24. if( map[u][v]>w)
  25. {
  26. map[u][v]=w;
  27. map[v][u]=w;
  28. }
  29. }
  30. }
  31. void Dijkstra(int u)
  32. {
  33. memset(vis, 0, sizeof(vis));
  34. for( int t= 1;t<=n;t++)
  35. {
  36. dis[t]= map[u][t];
  37. }
  38. vis[u]= 1;
  39. for( int t= 1;t<n;t++)
  40. {
  41. int minn=Inf,temp;
  42. for( int i= 1;i<=n;i++)
  43. {
  44. if(!vis[i]&&dis[i]<minn)
  45. {
  46. minn=dis[i];
  47. temp=i;
  48. }
  49. }
  50. vis[temp]= 1;
  51. for( int i= 1;i<=n;i++)
  52. {
  53. if( map[temp][i]+dis[temp]<dis[i])
  54. {
  55. dis[i]= map[temp][i]+dis[temp];
  56. }
  57. }
  58. }
  59. }
  60. int main()
  61. {
  62. scanf( "%d%d",&m,&n);
  63. Init();
  64. Getmap();
  65. Dijkstra(n);
  66. printf( "%d\n",dis[ 1]);
  67. return 0;
  68. }

 

 

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">51</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/lbperfect123">
                    <img src="https://profile.csdnimg.cn/6/D/A/3_lbperfect123" class="avatar_pic" username="lbperfect123">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/lbperfect123" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">lbperfect123</a></span>
                                            </div>
                    <div class="text"><span>发布了327 篇原创文章</span> · <span>获赞 354</span> · <span>访问量 10万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=lbperfect123" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值