All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.debatty.java.graphs.Dijkstra Maven / Gradle / Ivy

Go to download

Algorithms that build k-nearest neighbors graph (k-nn graph): Brute-force, NN-Descent,...

There is a newer version: 0.41
Show newest version
/*
 * The MIT License
 *
 * Copyright 2015 Thibault Debatty.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package info.debatty.java.graphs;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

/**
 * Compute the shortest path (measured as the number of 'hops' from this source 
 * node to every other node in the graph using Dijkstra algorithm.
 * @author Thibault Debatty
 */
public class Dijkstra {

    private final Graph graph;
    private final Set settledNodes;
    private final Set unSettledNodes;
    private final Map predecessors;
    private final Map distances;

    /**
     * Compute the shortest path from source node to every other node in the 
     * graph.
     * @param graph
     * @param source 
     */
    public Dijkstra(Graph graph, Node source) {

        this.graph = graph;

        settledNodes = new HashSet();
        unSettledNodes = new HashSet();
        distances = new HashMap();
        predecessors = new HashMap();

        distances.put(source, 0);
        unSettledNodes.add(source);

        while (unSettledNodes.size() > 0) {
            Node node = getMinimum(unSettledNodes);
            settledNodes.add(node);
            unSettledNodes.remove(node);
            findMinimalDistances(node);
        }
    }

    /**
     * Return the path from the source to the selected target
     * @param target
     * @return the path from the source to the selected target
     * @throws java.lang.Exception if no path exists to this target
     */
    public LinkedList getPath(Node target) throws Exception {
        LinkedList path = new LinkedList();
        Node step = target;
        // check if a path exists
        if (predecessors.get(step) == null) {
            throw new Exception("No path found to this target");
        }
        path.add(step);
        while (predecessors.get(step) != null) {
            step = predecessors.get(step);
            path.add(step);
        }
        // Put it into the correct order
        Collections.reverse(path);
        return path;
    }

    /**
     * Return the distance (measured as the number of hops) to the most distant
     * node
     * @return the distance (measured as the number of hops) to the most distant
     * node
     */
    public int getLargestDistance() {
        int largest = 0;
        for (Integer distance : distances.values()) {
            if (distance > largest) {
                largest = distance;
            }
        }
        return largest;
    }

    private void findMinimalDistances(Node node) {
        if (! graph.containsKey(node) || graph.get(node) == null) {
            return;
        }
        
        for (Neighbor neighbor : graph.get(node)) {
            Node target = neighbor.node;

            if (getShortestDistance(target) > (getShortestDistance(node) + 1)) {
                distances.put(target, getShortestDistance(node) + 1);
                predecessors.put(target, node);
                unSettledNodes.add(target);
            }
        }
    }

    private Node getMinimum(Set nodes) {
        Node minimum = null;
        for (Node node : nodes) {
            if (minimum == null) {
                minimum = node;
            } else {
                if (getShortestDistance(node) < getShortestDistance(minimum)) {
                    minimum = node;
                }
            }
        }
        return minimum;
    }

    private int getShortestDistance(Node destination) {
        Integer d = distances.get(destination);
        if (d == null) {
            return Integer.MAX_VALUE;
        } else {
            return d;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy