use of com.hankcs.hanlp.seg.common.EdgeFrom in project HanLP by hankcs.
the class Dijkstra method compute.
public static List<Vertex> compute(Graph graph) {
List<Vertex> resultList = new LinkedList<Vertex>();
Vertex[] vertexes = graph.getVertexes();
List<EdgeFrom>[] edgesTo = graph.getEdgesTo();
double[] d = new double[vertexes.length];
Arrays.fill(d, Double.MAX_VALUE);
d[d.length - 1] = 0;
int[] path = new int[vertexes.length];
Arrays.fill(path, -1);
PriorityQueue<State> que = new PriorityQueue<State>();
que.add(new State(0, vertexes.length - 1));
while (!que.isEmpty()) {
State p = que.poll();
if (d[p.vertex] < p.cost)
continue;
for (EdgeFrom edgeFrom : edgesTo[p.vertex]) {
if (d[edgeFrom.from] > d[p.vertex] + edgeFrom.weight) {
d[edgeFrom.from] = d[p.vertex] + edgeFrom.weight;
que.add(new State(d[edgeFrom.from], edgeFrom.from));
path[edgeFrom.from] = p.vertex;
}
}
}
for (int t = 0; t != -1; t = path[t]) {
resultList.add(vertexes[t]);
}
return resultList;
}
use of com.hankcs.hanlp.seg.common.EdgeFrom in project HanLP by hankcs.
the class NShortPath method enQueueCurNodeEdges.
/**
* 将所有到当前结点(nCurNode)可能的边根据eWeight排序并压入队列
* @param queWork
* @param nCurNode
*/
private void enQueueCurNodeEdges(CQueue queWork, int nCurNode) {
int nPreNode;
double eWeight;
List<EdgeFrom> pEdgeToList;
queWork.clear();
pEdgeToList = graph.getEdgeListTo(nCurNode);
// Get all the edgesFrom
for (EdgeFrom e : pEdgeToList) {
nPreNode = e.from;
eWeight = e.weight;
for (int i = 0; i < N; i++) {
// 第一个结点,没有PreNode,直接加入队列
if (nPreNode == 0) {
queWork.enQueue(new QueueElement(nPreNode, i, eWeight));
break;
}
// 如果PreNode的Weight == INFINITE_VALUE,则没有必要继续下去了
if (weightArray[nPreNode - 1][i] == Double.MAX_VALUE)
break;
queWork.enQueue(new QueueElement(nPreNode, i, eWeight + weightArray[nPreNode - 1][i]));
}
}
}
Aggregations