/* * Intcatch ASL giugno 2017 Background Subtraction Library multi-thread * Copyright 2017 Domenico Bloisi, Leonardo Dalla Riva, Carlo Bottaro. * * This file is part of Intcatch ASL giugno 2017 and it is distributed under the terms of the * GNU Lesser General Public License (Lesser GPL) * * * * Intcatch ASL giugno 2017 is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Intcatch ASL giugno 2017 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Intcatch ASL giugno 2017. If not, see . * * This file contains the C++ OpenCV based implementation for * Intcatch ASL giugno 2017 algorithm described in * * Domenico D. Bloisi, Carlo Bottaro and Leonardo Dalla Riva * "Intcatch ASL giugno 2017" * Pattern Recognition Letters * * Please, cite the above paper if you use Intcatch ASL giugno 2017. * * Intcatch ASL giugno 2017 has been written by Domenico D. Bloisi, Carlo Bottaro and Leonardo Dalla Riva * * Please, report suggestions/comments/bugs to * leo00.dallariva@gmail.com * bottarocarloo@gmail.com * */ package intcatch; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; public class DijkstraAlgorithm { private final List nodes; private final List edges; private Set settledNodes; private Set unSettledNodes; private Map predecessors; private Map distance; public DijkstraAlgorithm(Graph graph) { // create a copy of the array so that we can operate on this array this.nodes = new ArrayList(graph.getNodes()); this.edges = new ArrayList(graph.getEdges()); } public void execute(Node source) { settledNodes = new HashSet(); unSettledNodes = new HashSet(); distance = new HashMap(); predecessors = new HashMap(); distance.put(source, 0.); unSettledNodes.add(source); while (unSettledNodes.size() > 0) { Node node = getMinimum(unSettledNodes); settledNodes.add(node); unSettledNodes.remove(node); findMinimalDistances(node); } } private void findMinimalDistances(Node node) { List adjacentNodes = getNeighbors(node); for (Node target : adjacentNodes) { if (getShortestDistance(target) > getShortestDistance(node) + getDistance(node, target)) { distance.put(target, getShortestDistance(node) + getDistance(node, target)); predecessors.put(target, node); unSettledNodes.add(target); } } } private double getDistance(Node node, Node target) { for (Edge edge : edges) { if (edge.getSource().equals(node) && edge.getDestination().equals(target)) { return edge.getWeight(); } } throw new RuntimeException("Should not happen"); } private List getNeighbors(Node node) { List neighbors = new ArrayList(); for (Edge edge : edges) { if (edge.getSource().equals(node) && !isSettled(edge.getDestination())) { neighbors.add(edge.getDestination()); } } return neighbors; } private Node getMinimum(Set vertexes) { Node minimum = null; for (Node vertex : vertexes) { if (minimum == null) { minimum = vertex; } else { if (getShortestDistance(vertex) < getShortestDistance(minimum)) { minimum = vertex; } } } return minimum; } private boolean isSettled(Node vertex) { return settledNodes.contains(vertex); } private double getShortestDistance(Node destination) { Double d = distance.get(destination); if (d == null) { return Double.MAX_VALUE; } else { return d; } } /* * This method returns the path from the source to the selected target and * NULL if no path exists */ public LinkedList getPath(Node target) { LinkedList path = new LinkedList(); Node step = target; // check if a path exists if (predecessors.get(step) == null) { return null; } 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; } }