|
| 1 | +package org.geekgao.guide; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Queue; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.Stack; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * @author geekgao |
| 16 | + * 提供静态的算法方法 |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +public class GuideAlgorithm { |
| 21 | + |
| 22 | + /** |
| 23 | + * |
| 24 | + * @param start 起始点序号 |
| 25 | + * @param end 结束点序号 |
| 26 | + * @param count 点的个数 |
| 27 | + * @param map 图 |
| 28 | + * @return |
| 29 | + */ |
| 30 | + public static Integer[] Dijkstra(int start,int end,int count,Map<Integer,Vertex> map) { |
| 31 | + if (start == end) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + class LengthAndRoad{ |
| 36 | + int length; |
| 37 | + List<Integer> road = new LinkedList<Integer>(); |
| 38 | + } |
| 39 | + LengthAndRoad[] temp= new LengthAndRoad[count + 1];//算法中临时用到的那个数组,数组下标对应节点序号 |
| 40 | + Set<Integer> alreadyFind = new HashSet<Integer>();//已经找到最短路径的那些点的集合 |
| 41 | + |
| 42 | +// =============================================== |
| 43 | +// 初始化这个数组 |
| 44 | + Vertex tmpVex = map.get(start); |
| 45 | + Set<Integer> point = tmpVex.pointNum.keySet();//这个起始点指向的那些点的序号 |
| 46 | + for (Integer i:point) { |
| 47 | + temp[i] = new LengthAndRoad(); |
| 48 | + temp[i].length = tmpVex.pointNum.get(i); |
| 49 | + temp[i].road = new LinkedList<Integer>(); |
| 50 | + temp[i].road.add(start); |
| 51 | + temp[i].road.add(i); |
| 52 | + } |
| 53 | + alreadyFind.add(start); |
| 54 | +// =============================================== |
| 55 | + int currentStart = 0;//每次循环时起始点的序号,循环进去第一步就是选择这个点(路径长度最短的那个点) |
| 56 | + int MAX = 2147483647; |
| 57 | + for (int k = 1;k <= count;k++) { |
| 58 | + long endTime = System.currentTimeMillis(); |
| 59 | + |
| 60 | + int minLength = MAX; |
| 61 | + int n = -1;//下面这个temp数组计数 |
| 62 | + for (LengthAndRoad l:temp) { |
| 63 | + n++; |
| 64 | + if (l == null || alreadyFind.contains(n)) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + if (minLength > l.length) { |
| 68 | + minLength = l.length; |
| 69 | + currentStart = n; |
| 70 | + } |
| 71 | + } |
| 72 | + if (currentStart == end) { |
| 73 | + break; |
| 74 | + } |
| 75 | + alreadyFind.add(currentStart); |
| 76 | + |
| 77 | + Vertex currentVex = map.get(currentStart);//当前起始点 |
| 78 | + List<Integer> currentStartRoad = new LinkedList<Integer>(temp[currentStart].road);//当前路径(把起始点路径复制过来) |
| 79 | + |
| 80 | + Set<Integer> currentPoint = currentVex.pointNum.keySet();//获得当前点指向哪些点 |
| 81 | + for (Integer i:currentPoint) { |
| 82 | + if (alreadyFind.contains(i)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + if ((temp[i] == null) || (temp[currentStart].length + currentVex.pointNum.get(i) < temp[i].length)) { |
| 86 | + LengthAndRoad newRoad = new LengthAndRoad(); |
| 87 | + newRoad.length = temp[currentStart].length + currentVex.pointNum.get(i); |
| 88 | + newRoad.road = new LinkedList<Integer>(currentStartRoad); |
| 89 | + newRoad.road.add(i); |
| 90 | + temp[i] = newRoad; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (currentStart != end) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + Integer[] result = new Integer[temp[currentStart].road.size()]; |
| 100 | + int n = 0; |
| 101 | + for (Integer i:temp[currentStart].road) { |
| 102 | + result[n++] = i; |
| 103 | + } |
| 104 | + return result; |
| 105 | + } |
| 106 | + |
| 107 | + public static Integer[] Bfs(int startNum, Map<Integer, Vertex> map, |
| 108 | + Map<String, String> view, Map<Integer, String> viewNumNameMap) { |
| 109 | + |
| 110 | + List<Integer> resultList = new LinkedList<Integer>(); |
| 111 | + Queue<Integer> q = new LinkedList<Integer>(); |
| 112 | + |
| 113 | + Set<Integer> alreadyFind = new HashSet<Integer>();//存储已经访问过的点 |
| 114 | + q.offer(startNum);//放入起始点 |
| 115 | + alreadyFind.add(startNum); |
| 116 | + while (!q.isEmpty()) { |
| 117 | + Integer num = q.poll(); |
| 118 | + if (viewNumNameMap.containsKey(num)) { |
| 119 | + resultList.add(num); |
| 120 | + } |
| 121 | + Set<Integer> pointNum = map.get(num).pointNum.keySet(); |
| 122 | + for (Integer i:pointNum) { |
| 123 | + if (!alreadyFind.contains(i)) { |
| 124 | + q.offer(i); |
| 125 | + alreadyFind.add(i); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Integer[] result = new Integer[resultList.size()]; |
| 131 | + for (int i = 0;i < resultList.size();i++) { |
| 132 | + result[i] = resultList.get(i); |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | + } |
| 137 | + |
| 138 | + public static Integer[] Dfs(int startNum, Map<Integer, Vertex> map, |
| 139 | + Map<String, String> view, Map<Integer, String> viewNumNameMap) { |
| 140 | + |
| 141 | + List<Integer> resultList = new LinkedList<Integer>();//算法存储最终结果的链表 |
| 142 | + Stack<Integer> s = new Stack<Integer>();//算法中用到的栈 |
| 143 | + Set<Integer> alreadyFind = new HashSet<Integer>();//存储已经访问过的点 |
| 144 | + |
| 145 | + s.push(startNum); |
| 146 | + alreadyFind.add(startNum); |
| 147 | + while (!s.isEmpty()) { |
| 148 | + int num = s.pop(); |
| 149 | + if (viewNumNameMap.containsKey(num)) { |
| 150 | + resultList.add(num); |
| 151 | + } |
| 152 | + Map<Integer,Integer> pointNum = map.get(num).pointNum; |
| 153 | + for (Iterator<Integer> it = pointNum.keySet().iterator();it.hasNext();) { |
| 154 | + int nextNum = it.next(); |
| 155 | + if (!alreadyFind.contains(nextNum)) { |
| 156 | + s.push(nextNum); |
| 157 | + alreadyFind.add(nextNum); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + Integer[] result = new Integer[resultList.size()]; |
| 162 | + for (int i = 0;i < resultList.size();i++) { |
| 163 | + result[i] = resultList.get(i); |
| 164 | + } |
| 165 | + |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments