(HW)Dijkstra算法(Java)

  1 import java.util.Comparator;
  2 import java.util.HashMap;
  3 import java.util.LinkedList;
  4 import java.util.List;
  5 import java.util.Map;
  6 import java.util.PriorityQueue;
  7 import java.util.Stack;
  8 
  9 public class test
 10 {
 11     public static void main(String[] args)
 12     {
 13         Graph g = new Graph();
 14         Dijkstra(g, g.s);
 15 //        print_Path(g, g.s, g.t);
 16         Stack<Vertex> stack1 = new Stack<>();
 17         Vertex a1 = g.x;
 18         while(a1 != null)
 19         {
 20             stack1.push(a1);
 21             a1 = a1.predecessor;
 22         }
 23         System.out.println("s 至 x 的最短路径为");
 24         while(!stack1.isEmpty())
 25             System.out.print(stack1.pop().name + " ");
 26         System.out.print("\n" + "路径长度为 " + "\n" + g.x.d);
 27     }
 28     
 29     public static void Dijkstra(Graph g, Vertex s)  //时间复杂度: O(elogv)
 30     {
 31         PriorityQueue<Vertex> queue = new PriorityQueue<>(new Comparator<Vertex>()
 32         {
 33             public int compare(Vertex o1, Vertex o2)
 34             {
 35                 return o1.d - o2.d;
 36             }
 37         });
 38         s.d = 0;
 39         Vertex u;
 40         queue.add(s);
 41         queue.add(g.t);
 42         queue.add(g.x);
 43         queue.add(g.y);
 44         queue.add(g.z);
 45         while(!queue.isEmpty())
 46         {
 47             u = queue.poll();
 48             u.isvisited = true;
 49             for(Edge e : g.adjacencyMap.get(u))  //O(elogv)
 50                 if(e.dst.isvisited == false)
 51                 {
 52                     relax(e.dst, u, e);
 53                     queue.remove(e.dst);  //最小堆化 O(logv)
 54                     queue.add(e.dst);
 55                 }
 56         }
 57     }
 58     
 59     public static void relax(Vertex i, Vertex j, Edge e)
 60     {
 61         if(i.d > j.d + e.weight)
 62         {
 63             i.d = j.d + e.weight;
 64             i.predecessor = j;
 65         }
 66     }
 67     
 68 //    递归打印最短路径
 69 //    public static void print_Path(Graph g, Vertex src, Vertex dst)
 70 //    {
 71 //        if(dst == src)
 72 //        {
 73 //            System.out.print(src.name + " ");
 74 //            return;
 75 //        }
 76 //        
 77 //        print_Path(g, src, dst.predecessor);
 78 //        System.out.print(dst.name + " ");
 79 //    }
 80 }
 81 
 82 class Vertex
 83 {
 84     String name;
 85     int d;
 86     Vertex predecessor;
 87     boolean isvisited;
 88     
 89     public Vertex(String name)
 90     {
 91         this.name = name;
 92         this.d = Integer.MAX_VALUE;
 93         this.predecessor = null;
 94         this.isvisited = false;
 95     }
 96 }
 97 
 98 class Edge
 99 {
100     Vertex src;  //起点
101     Vertex dst;  //终点
102     int weight;  //权重
103     
104     public Edge(Vertex src, Vertex dst, int weight)
105     {
106         this.src = src;
107         this.dst = dst;
108         this.weight = weight;
109     }
110 }
111 
112 class Graph
113 {
114     Vertex s, t, y, x, z;
115     Map<Vertex, List<Edge>> adjacencyMap;
116     
117     public Graph()
118     {
119         this.adjacencyMap = new HashMap<>();
120         s = new Vertex("s");
121         t = new Vertex("t");
122         y = new Vertex("y");
123         x = new Vertex("x");
124         z = new Vertex("z");
125         
126         //s的邻接边
127         List<Edge> sN = new LinkedList<>();
128         Edge st = new Edge(s, t, 10);
129         Edge sy = new Edge(s, y, 5);
130         sN.add(st);
131         sN.add(sy);
132         adjacencyMap.put(s, sN);
133         
134         //t的邻接边
135         List<Edge> tN = new LinkedList<>();
136         Edge ty = new Edge(t, y, 2);
137         Edge tx = new Edge(t, x, 1);
138         tN.add(ty);
139         tN.add(tx);
140         adjacencyMap.put(t, tN);
141         
142         //y的邻接边
143         List<Edge> yN = new LinkedList<>();
144         Edge yt = new Edge(y, t, 3);
145         Edge yx = new Edge(y, x, 9);
146         Edge yz = new Edge(y, z, 2);
147         yN.add(yt);
148         yN.add(yx);
149         yN.add(yz);
150         adjacencyMap.put(y, yN);
151         
152         //x的邻接边
153         List<Edge> xN = new LinkedList<>();
154         Edge xz = new Edge(x, z, 4);
155         xN.add(xz);
156         adjacencyMap.put(x, xN);
157         
158         //z的邻接边
159         List<Edge> zN = new LinkedList<>();
160         Edge zs = new Edge(z, s, 7);
161         Edge zx = new Edge(z, x, 6);
162         zN.add(zs);
163         zN.add(zx);
164         adjacencyMap.put(z, zN);
165     }
166 }

 

转载于:https://www.cnblogs.com/Huayra/p/10886894.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值