Search in sources :

Example 1 with State

use of com.hankcs.hanlp.seg.Dijkstra.Path.State 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;
}
Also used : Vertex(com.hankcs.hanlp.seg.common.Vertex) PriorityQueue(java.util.PriorityQueue) LinkedList(java.util.LinkedList) EdgeFrom(com.hankcs.hanlp.seg.common.EdgeFrom) State(com.hankcs.hanlp.seg.Dijkstra.Path.State) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with State

use of com.hankcs.hanlp.seg.Dijkstra.Path.State in project HanLP by hankcs.

the class DijkstraSegment method dijkstra.

/**
     * dijkstra最短路径
     * @param graph
     * @return
     */
private static List<Vertex> dijkstra(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;
}
Also used : State(com.hankcs.hanlp.seg.Dijkstra.Path.State)

Aggregations

State (com.hankcs.hanlp.seg.Dijkstra.Path.State)2 EdgeFrom (com.hankcs.hanlp.seg.common.EdgeFrom)1 Vertex (com.hankcs.hanlp.seg.common.Vertex)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 PriorityQueue (java.util.PriorityQueue)1