zoukankan      html  css  js  c++  java
  • 数据结构10——最短路径

    最短路径:带权图中从一个结点到另外一个结点可能会有多个路径,但是将带权路径长度值最小的一条路径成为最短路径。

    1:(Dijkastra)迪克斯特拉算法:

    迪克斯特拉算法用于最短路径的方法和prim用于最小生成树的方法类似都是按照路径权值由小到大的顺序来产生最短路径。

    主要思想:设置两个集合的结点S和T,集合S用于存放已经找到最短路径的结点,

                      集合T中存放当前未找到的最短路径。

     

    与前面的prim算法类似都是通过路径逐渐递增的方式来获得。

    迪克斯特拉算法的具体实现:

     1 package com.hone.graph.test;
     2 
     3 import com.hone.graph.AdjMWGraph;
     4 
     5 /*
     6  * 定义dijkstra算法用来表示最短路径,其中g表示带权值的图形,v0表示初试下标的结点
     7  * distance[]数组用于储存最短路径,path路径用于输出前一个结点的下标
     8  */
     9 public class Dijkstra {
    10     static final int maxWeight=9999;                //设定一个初始值表示无穷大
    11     public static void dijkstra(AdjMWGraph g,int v0,
    12             int [] distance ,int[] path){
    13         
    14         int n=g.getNumOfVertices();                    //获得结点的总个数
    15         int[] s=new int [n];                        //定义一个数组用来存储各个结点的标记,0表示没有存储,1表示已经存储
    16         
    17         int minDistance;
    18         int u=0;                                    //定义一个用于交换下标的常量
    19         
    20         //初始化各个距离,并且将初始点v0添加到最小路径集合s中去
    21         for (int i = 0; i < n; i++) {
    22             distance[i]= g.getWeight(v0, i);            //获得v0和i结点构成的边之间的权重
    23             s[i]=0;                                        //同时标记i没有被访问
    24             if(i!=v0&&distance[i]<maxWeight){            //如果i结点不是v0结点,并且v0和i之间的权重是最小路径,则遍历出v0结点                
    25                 path[i]=v0;
    26             }else{
    27                 path[i]=-1;                                
    28             }
    29         }
    30         
    31         s[v0]=1;                                    //对s[v0]进行赋值,1表示v0结点已经储存到对相应的集合s中
    32         
    33         /*
    34          * 在从没有找到最小路径的结点集合中选出最短路径的结点u
    35          */
    36         for( int i=1; i<n; i++){
    37             minDistance=maxWeight;
    38             for (int j = 0; j < n; j++) {                    
    39                 if (s[j]==0&& distance[j]<minDistance) {            //如果j结点没有被访问过
    40                     u=j;                                            //将j赋值给u表示u为最短路径
    41                     minDistance=distance[j];                        //并且此刻的distance[j]就为最短路径
    42                 }
    43             }
    44             
    45             //如果不存在路径的时候算法结束
    46             if (minDistance==maxWeight) {
    47                 return;
    48             }
    49             s[u]=1;                                        //用于标记u结点已经加入到最短路径集合中去
    50             
    51             //修改从v0到其他节点的最短路径,并且存入到其他路径中去
    52             for (int j = 0; j < n; j++) {
    53                 if (s[j]==0&&g.getWeight(u, j)<maxWeight&&distance[u]+g.getWeight(u, j)<distance[j]) {
    54                     distance[j]=distance[u]+g.getWeight(u, j);
    55                     path[j]=u;
    56                 }
    57             }
    58         }
    59     }
    60 }

    迪克斯特拉算法的测试类:

     1 package com.hone.graph.test;
     2 
     3 import com.hone.graph.AdjMWGraph;
     4 
     5 public class TestDijkstra {
     6     
     7     static final int maxVertices=100;
     8     
     9     public static void createGraph(AdjMWGraph g,Object[] v
    10             ,int n,RowColWeight[] rc,int e)throws Exception{
    11         
    12         //在图中插入结点
    13         for (int i = 0; i < n; i++) {
    14             g.insetVertex(v[i]);
    15         }
    16         //在图中插入边
    17         for (int i = 0; i < e; i++) {
    18             g.insertEdge(rc[i].row, rc[i].col, rc[i].weight);
    19         }
    20     }
    21     
    22     public static void main(String[] args){
    23         AdjMWGraph g=new AdjMWGraph(maxVertices);
    24         Character[] a={new Character('A'),new Character('B'),
    25                 new Character('C'),new Character('D'),
    26                 new Character('E'),new Character('F')};
    27     
    28         RowColWeight[] rowColWeights={new RowColWeight(0, 2, 5),
    29                 new RowColWeight(0, 3, 30),new RowColWeight(1, 0, 2),
    30                 new RowColWeight(1, 4, 8),new RowColWeight(2, 1, 15),
    31                 new RowColWeight(2, 5, 7),new RowColWeight(4, 3, 4),
    32                 new RowColWeight(5, 3, 10),new RowColWeight(5, 4, 18)
    33         };
    34         
    35         int n=6,e=9;
    36         
    37         try {
    38             createGraph(g, a, n, rowColWeights, e);
    39             
    40             int[] distance=new int[n];
    41             int[] path=new int[n];
    42             
    43             Dijkstra.dijkstra(g, 0, distance, path);
    44             
    45             System.out.println("从顶点A到其他个顶点的最短距离为:");
    46             for (int i = 1; i < n; i++) {
    47                 System.out.println("到顶点:" + g.getValue(i)+ "的最短距离为:"+ distance[i]);
    48             }
    49             
    50             System.out.println("从顶点A到其他个顶点的前一项为:");
    51             for (int i = 0; i < n; i++) {
    52                 if (path[i]!=-1) {
    53                     System.out.println("到顶点:" + g.getValue(i)+ "的前一项为:"+ g.getValue(path[i]));
    54                 }
    55             }
    56         } catch (Exception e1) {
    57             e1.printStackTrace();
    58         }
    59     }    
    60 }
  • 相关阅读:
    Option使用和实现内幕源码揭秘之Scala学习笔记-22
    模式匹配高级实战:嵌套的Case class之Scala学习笔记-21
    Case class和Case object代码实战解析之Scala学习笔记-20
    Scala提取器Extractor实战详解之Scala学习笔记-19
    Type、Array、List、Tuple模式匹配实战解析之Scala学习笔记-18
    Scala中模式匹配入门实战详解之Scala学习笔记-17
    FragmentTabHost
    android项目中使用开源数据库litepal
    android Studio项目运行时报错“Could not identify launch activity: Default Activity not found”
    DrawerLayout学习,抽屉效果
  • 原文地址:https://www.cnblogs.com/xiaxj/p/6674348.html
Copyright © 2011-2022 走看看