Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153002
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 405
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-23 14:38
文章分类

全部博文(47)

文章存档

2017年(7)

2016年(4)

2015年(19)

2014年(17)

我的朋友

分类: Java

2015-12-08 20:15:39

给定一个赋权图G(V,E)和一个特定顶点s作为输入,找出从s到其他每一个其他顶点的最短赋权路径。权值为非负。


点击(此处)折叠或打开

  1. /*
  2.  * Dijkstra算法实现
  3.  */
  4. import java.util.*;

  5. public class ShortestWay {
  6.     
  7.     public static ArrayList<Vertex> verList = new ArrayList<>(); //存放所有顶点
  8.     
  9.     public static void main(String[] args) {
  10.         Vertex v1 = new Vertex("V1",0);
  11.         Vertex v2 = new Vertex("V2",1);
  12.         Vertex v3 = new Vertex("V3",2);
  13.         Vertex v4 = new Vertex("V4",3);
  14.         Vertex v5 = new Vertex("V5",4);
  15.         Vertex v6 = new Vertex("V6",5);
  16.         Vertex v7 = new Vertex("V7",6);
  17.         
  18.         verList.add(v1);
  19.         verList.add(v2);
  20.         verList.add(v3);
  21.         verList.add(v4);
  22.         verList.add(v5);
  23.         verList.add(v6);
  24.         verList.add(v7);
  25.         
  26.         v1.addEdge(v2, 2);
  27.         v1.addEdge(v4, 1);
  28.         v2.addEdge(v4, 3);
  29.         v2.addEdge(v5, 10);
  30.         v3.addEdge(v1, 4);
  31.         v3.addEdge(v6, 5);
  32.         v4.addEdge(v3, 2);
  33.         v4.addEdge(v5, 2);
  34.         v4.addEdge(v6, 8);
  35.         v4.addEdge(v7, 4);
  36.         v7.addEdge(v6, 1);
  37.         
  38.         v1.setDistance(0);
  39.         
  40.         PriQueue pq = new PriQueue(verList);
  41.         
  42.         while(!pq.isEmpty()){
  43.             Vertex ele = pq.removeEle();
  44.             ArrayList<Vertex> adjList = ele.getAdjList();
  45.             for(Vertex v : adjList){
  46.                 pq.changeEle(ele, v);
  47.             }
  48.         }
  49.         System.out.print(verList);
  50.     }
  51. }

  52. /*
  53.  * 带权的边
  54.  */
  55. class WeightEdge{
  56.     Vertex start;
  57.     Vertex end;
  58.     int weight;
  59.     
  60.     public WeightEdge(Vertex s,Vertex e,int w){
  61.         this.start=s;
  62.         this.end=e;
  63.         this.weight=w;
  64.     }
  65.     
  66.     public boolean equal(Vertex s,Vertex e){
  67.         if(this.start.equals(s)&&this.end.equals(e)){
  68.             return true;
  69.         }
  70.         return false;
  71.     }
  72.     
  73.     public int getWeight(){
  74.         return weight;
  75.     }
  76.     
  77. }

  78. /*
  79.  * 顶点
  80.  */
  81. class Vertex{
  82.     public static final int LIMIT = 1000; //用以代替无穷大
  83.     public String name;
  84.     public int index;
  85.     public int distance; //距离
  86.     ArrayList<Vertex> list = new ArrayList<>(); //存放邻接点
  87.     ArrayList<WeightEdge> edges = new ArrayList<>(); //存放以此顶点为出发点的边
  88.     
  89.     public Vertex(String name,int index){
  90.         this.name=name;
  91.         this.index=index;
  92.         distance = LIMIT;
  93.     }
  94.     
  95.     //添加带权的边
  96.     public void addEdge(Vertex e,int w){
  97.         WeightEdge we = new WeightEdge(this, e, w);
  98.         edges.add(we);
  99.         addAdjList(e);
  100.     }
  101.     
  102.     //添加邻接点
  103.     public void addAdjList(Vertex b){
  104.         list.add(b);
  105.     }
  106.     
  107.     //获取对应两点的边的权
  108.     public int getEdgeWeight(Vertex s,Vertex e){
  109.         for(WeightEdge we : edges){
  110.             if(we.equal(s, e)){
  111.                 return we.getWeight();
  112.             }
  113.         }
  114.         return 0;
  115.     }
  116.     
  117.     public ArrayList<Vertex> getAdjList(){
  118.         return list;
  119.     }
  120.     
  121.     public void setDistance(int dis){
  122.         this.distance=dis;
  123.     }
  124.     
  125.     public String toString(){
  126.         return name+" "+this.getDistance();
  127.     }
  128.     
  129.     public int getIndex(){
  130.         return index;
  131.     }
  132.     
  133.     public int getDistance(){
  134.         return distance;
  135.     }
  136.     
  137.     public boolean equals(Object obj){
  138.         return this.name.equals(((Vertex)obj).name);
  139.     }
  140.     
  141. }

  142. /*
  143.  * 优先队列
  144.  */
  145. class PriQueue{
  146.     
  147.     LinkedList<Vertex> que = new LinkedList<>();
  148.     
  149.     public PriQueue(ArrayList<Vertex> list){
  150.         que.addAll(list);
  151.     }
  152.     
  153.     //获得距离最小的顶点并删除
  154.     public Vertex removeEle(){
  155.         Vertex v = findMin();
  156.         que.remove(v);
  157.         return v;
  158.     }
  159.     
  160.     //改变顶点距离
  161.     public void changeEle(Vertex v1,Vertex v2){
  162.         int weight = v1.getEdgeWeight(v1, v2);
  163.         if(v1.getDistance()+weight<v2.getDistance()){
  164.             v2.setDistance(v1.getDistance()+weight);
  165.         }
  166.     }
  167.     
  168.     public Vertex findMin(){
  169.         int a = Vertex.LIMIT;
  170.         Vertex v=null;
  171.         Iterator<Vertex> it = que.iterator();
  172.         while(it.hasNext()){
  173.             Vertex tmp = it.next();
  174.             int tmpDis = tmp.getDistance();
  175.             if(a > tmpDis){
  176.                 a = tmpDis;
  177.                 v = tmp;
  178.             }
  179.         }
  180.         return v;
  181.     }
  182.     
  183.     public boolean isEmpty(){
  184.         return que.isEmpty();
  185.     }
  186. }


阅读(1352) | 评论(0) | 转发(0) |
0

上一篇:JAVA之synchronized

下一篇:android下jni的使用

给主人留下些什么吧!~~