Interface Algorithm
- Author:
- Andrey Malykhanov, Vitaliy Chernenko, Andrey Korotin
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumEnumeration used to differentiate arcs when traversing a graphstatic interfaceFunctional interface for a function answering the question "What must be the context of the node being added considering we know the added arc, the added node itself, and the context of the already traversed node adjacent to this arc?"static interfaceFunctional interface for a predicate answering the question "Must we end the traversal after we added the specific arc, node and calculated the specific node context?"static interfaceFunctional interface for a predicate answering the question "Can we traverse the arc and the node adjacent to this arc?" -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic <A,B> GraphPath <A, B> calculateShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Calculates shortest path between the two specified nodes having data about nodes of the minimal spanning tree and arcs of the minimal spanning tree, i.e.getClosestNodes(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator.getClosestNodes(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, int maxNodesCount) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator.getClosestNodes(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator.getClosestNodes(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, int maxNodesCount) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator.static <A,B> GraphPath <A, B> getFirstCycle(Graph<A, B> graph) Returns first found cycle in the specified graph.static <A,B> GraphPath <A, B> Returns the first found, not necessarily the shortest one, path between the specified source and destination nodes in the given graph.getFragments(Graph<A, B> graph) Returns all fragments (i.e.getInsignificantNodesAndArcs(Graph<A, B> graph, Set<Graph<A, B>.Node> significantNodes) Returns the pair containing sets of insignificant nodes and arcs of the specified graph against the specified set of significant nodes.getInsignificantNodesAndArcs(Graph<A, B> graph, Set<Graph<A, B>.Node> significantNodes, Set<Graph<A, B>.Arc> barrierArcs) Returns the pair containing sets of insignificant nodes and arcs of the specified graph against the specified set of significant nodes.getMinPathWithContext(Graph<A, B> graph, Graph<A, B>.Node sourceNode, BiPredicate<Graph<A, B>.Node, T> searchSuccessPredicate, T sourceNodeContext, BiFunction<T, Graph<A, B>.Arc, T> contextProvider, Comparator<T> contextComparator) Returns a minimal-context path in the graph using a context-aware variant of Dijkstra’s algorithm.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns shortest path between the two specified graph nodes.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, double range) Returns shortest path between the two specified graph nodes if the total weight of such path does not exceed the specified range.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Returns shortest path between the two specified graph nodes.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, double range) Returns shortest path between the two specified graph nodes if the total weight of such path does not exceed the specified range.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, double range) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator if the total weight of such path does not exceed the specified range.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, double range) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator, if the total weight of such path does not exceed the specified range.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, Set<Graph<A, B>.Arc> barrierArcs, double range) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator.static <A,B> GraphPath <A, B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, List<Predicate<Graph<A, B>.Node>> destNodeIndicators, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns shortest path from the specified graph node through nodes specified by indicators.getTreeNodesByLevels(Graph<A, B> graph) Groups the nodes of the specified oriented tree by levels.topologicalSort(Graph<A, B> graph, Graph<A, B>.Node rootNode) Performs topological sorting of the graph with respect to the specified root node and returns a map containing mapping from graph nodes to integer numbers.topologicalSort(Graph<A, B> graph, List<Graph<A, B>.Node> rootNodes) Performs topological sorting of the graph with respect to the specified root nodes and returns a map containing mapping from graph nodes to integer numbers.static <A,B, C> C traverse(Graph<A, B>.Node sourceNode, C sourceContext, Algorithm.GraphTraversalPredicate<A, B> traversedArcPredicate, Algorithm.GraphTraversalContextCalculator<A, B, C> recalculateNodeContext, Algorithm.GraphTraversalEndPredicate<A, B, C> endTraversalPredicate) Traverses the graph starting from the specified sourceNode by adding the arcs and nodes allowed by traversedArcPredicate until all reachable nodes are traversed or the specified endTraversalPredicate evaluated totrue.
-
Method Details
-
getFirstPath
static <A,B> GraphPath<A,B> getFirstPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode) Returns the first found, not necessarily the shortest one, path between the specified source and destination nodes in the given graph.If the source and destination nodes are the same, a single node path is returned. If no path exists, the method returns
null.- Parameters:
graph- The graph containing the nodessourceNode- The starting node of the pathdestNode- The target node of the path- Returns:
- the first found path from
sourceNodetodestNode, ornullif no path exists
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns shortest path between the two specified graph nodes. If such path does not exist, returnsnullIf source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNode- Destination node of the path being searchedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodes- Returns:
- Shortest path between the two specified graph nodes, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, double range) Returns shortest path between the two specified graph nodes if the total weight of such path does not exceed the specified range. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNode- Destination node of the path being searchedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesrange- Total weight that the returned path must not exceed- Returns:
- Shortest path between the two specified graph nodes not exceeding the
specified weight, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Returns shortest path between the two specified graph nodes. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNode- Destination node of the path being searchedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Map containing weights of nodes of the minimal spanning tree built while finding the shortest path. This collection will be filled inside the methodoutArcsVisited- Set containing arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path. This collection will be filled inside the method- Returns:
- Shortest path between the two specified graph nodes, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, double range) Returns shortest path between the two specified graph nodes if the total weight of such path does not exceed the specified range. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNode- Destination node of the path being searchedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Map containing weights of nodes of the minimal spanning tree built while finding the shortest path. This collection will be filled inside the methodoutArcsVisited- Set containing arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path. This collection will be filled inside the methodrange- Total weight that the returned path must not exceed- Returns:
- Shortest path between the two specified graph nodes, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition of destination nodearcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodes- Returns:
- Shortest path between the two specified graph nodes, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, double range) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator if the total weight of such path does not exceed the specified range. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition of destination nodearcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesrange- Total weight that the returned path must not exceed- Returns:
- Shortest path between the two specified graph nodes, not exceeding
the specified range, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition of destination nodearcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Map containing weights of nodes of the minimal spanning tree built while finding the shortest path. This collection will be filled inside the methodoutArcsVisited- Set containing arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path. This collection will be filled inside the method- Returns:
- Shortest path between the two specified graph nodes, or
nullif path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, double range) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator, if the total weight of such path does not exceed the specified range. If such path does not exist, returnsnull. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition of destination nodearcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Map containing weights of nodes of the minimal spanning tree built while finding the shortest path. This collection will be filled inside the methodoutArcsVisited- Set containing arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path. This collection will be filled inside the methodrange- Total weight that the returned path must not exceed- Returns:
- Shortest path between the two specified graph nodes, not exceeding
the specified range, or
nullif such path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, Set<Graph<A, B>.Arc> barrierArcs, double range) Returns shortest path from the specified graph node to the closest graph node satisfying the condition determined by the specified indicator. If such path does not exist, returns null. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition of destination nodearcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Map containing weights of nodes of the minimal spanning tree built while finding the shortest path. This collection will be filled inside the methodoutArcsVisited- Set containing arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path. This collection will be filled inside the methodbarrierArcs- arcs that should not be traversedrange- Total weight that the returned path must not exceed- Returns:
- Shortest path between the two specified graph nodes, or null if path does not exist
-
getMinPathWithContext
static <A,B, Pair<GraphPath<A,T> B>, getMinPathWithContextT> (Graph<A, B> graph, Graph<A, B>.Node sourceNode, BiPredicate<Graph<A, B>.Node, T> searchSuccessPredicate, T sourceNodeContext, BiFunction<T, Graph<A, B>.Arc, T> contextProvider, Comparator<T> contextComparator) Returns a minimal-context path in the graph using a context-aware variant of Dijkstra’s algorithm.Each node holds a context value of type
T, updated bycontextProviderwhen traversing arcs. The search expands arcs in order of minimal context according tocontextComparator, and stops whensearchSuccessPredicateevaluates totrue.- Type Parameters:
T- search context type (e.g., cost, distance, or state)- Parameters:
graph- the graph to searchsourceNode- the starting nodesearchSuccessPredicate- predicate determining when the search stopssourceNodeContext- initial context at the source nodecontextProvider- function computing the next context from a source context and an arc that is added to the spanning treecontextComparator- comparator defining ordering of context values (lower means better)- Returns:
- a
Paircontaining the resultingGraphPathand final context, ornullif no path was found
-
calculateShortestPath
static <A,B> GraphPath<A,B> calculateShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, Graph<A, B>.Node destNode, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Calculates shortest path between the two specified nodes having data about nodes of the minimal spanning tree and arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being calculateddestNode- Destination node of the path being calculatedoutNodeWeights- Map containing weights of nodes of the minimal spanning tree built while finding the shortest pathoutArcsVisited- Set containing arcs of the minimal spanning tree, i.e. arcs visited while finding the shortest path- Returns:
- Shortest path between the two specified graph nodes, or empty path if such path does not exist
-
getClosestNodes
static <A,B> List<Pair<Graph<A,B>.Node, getClosestNodesDouble>> (Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator. Allows to set the search range that will limit the search to the specified distance. List is sorted by distance from the source node. If there are several nodes at the same distance, their order is not guaranteed. If there are no nodes satisfying the specified indicator, empty list will be returned.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node from which the search startsdestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition for nodes being searchedrange- Range that limits the search. The search is stopped if distance from the source node is more than range. If no limitation by distance is required, Double.POSITIVE_INFINITY should be specifiedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodes- Returns:
- Shortest path between the two specified graph nodes, or null if path does not exist
-
getClosestNodes
static <A,B> List<Pair<Graph<A,B>.Node, getClosestNodesDouble>> (Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, int maxNodesCount) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator. Allows to set the search range that will limit the search to the specified distance. List is sorted by distance from the source node. If there are several nodes at the same distance, their order is not guaranteed. If there are no nodes satisfying the specified indicator, empty list will be returned.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node from which the search startsdestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition for nodes being searchedrange- Range that limits the search. The search is stopped if distance from the source node is more than range. If no limitation by distance is required, Double.POSITIVE_INFINITY should be specifiedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesmaxNodesCount- Maximum number of nodes to find. The search will stop after this amount of nodes is found- Returns:
- Shortest path between the two specified graph nodes, or null if path does not exist
-
getClosestNodes
static <A,B> List<Pair<Graph<A,B>.Node, getClosestNodesDouble>> (Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator. Allows to set the search range that will limit the search to the specified distance. List is sorted by distance from the source node. If there are several nodes at the same distance, their order is not guaranteed. If there are no nodes satisfying the specified indicator, empty list will be returned.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node from which the search startsdestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition for nodes being searchedrange- Range that limits the search. The search is stopped if distance from the source node is more than range. If no limitation by distance is required, Double.POSITIVE_INFINITY should be specifiedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Mutable map of weights of nodes that are already known. Will be filled as a result of calling this method.outArcsVisited- Initially empty mutable set of arcs that will be visited during the search of the closest nodes. Will be filled as a result of calling this method.- Returns:
- Shortest path between the two specified graph nodes, or null if path does not exist
-
getClosestNodes
static <A,B> List<Pair<Graph<A,B>.Node, getClosestNodesDouble>> (Graph<A, B> graph, Graph<A, B>.Node sourceNode, Predicate<Graph<A, B>.Node> destNodeIndicator, double range, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter, Map<Graph<A, B>.Node, Double> outNodeWeights, Set<Graph<A, B>.Arc> outArcsVisited, int maxNodesCount) Returns list of pairs of nodes and distances to them from the specified source node satisfying the condition determined by the specified indicator. Allows to set the search range that will limit the search to the specified distance. List is sorted by distance from the source node. If there are several nodes at the same distance, their order is not guaranteed. If there are no nodes satisfying the specified indicator, empty list will be returned.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node from which the search startsdestNodeIndicator- Class containing callback which will be called to check if node satisfies the condition for nodes being searchedrange- Range that limits the search. The search is stopped if distance from the source node is more than range. If no limitation by distance is required, Double.POSITIVE_INFINITY should be specifiedarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodesoutNodeWeights- Mutable map of weights of nodes that are already known. Will be filled as a result of calling this method.outArcsVisited- Initially empty mutable set of arcs that will be visited during the search of the closest nodes. Will be filled as a result of calling this method.maxNodesCount- maximum number of nodes. The search will stop after this number of nodes is found. To specify unlimited number of nodes passInteger.MAX_VALUEto this parameter- Returns:
- Shortest path between the two specified graph nodes, or null if path does not exist
-
getShortestPath
static <A,B> GraphPath<A,B> getShortestPath(Graph<A, B> graph, Graph<A, B>.Node sourceNode, List<Predicate<Graph<A, B>.Node>> destNodeIndicators, Function<Graph<A, B>.Arc, Double> arcWeighter, Function<Graph<A, B>.Node, Double> nodeWeighter) Returns shortest path from the specified graph node through nodes specified by indicators. An indicator is a class that specifies condition according to which a node is chosen. If such path does not exist, returns null. If source node is equal to destination node, returns empty path. If there are more than 1 shortest paths, the method will always return the same path but there is no guarantee which path will be chosen.- Parameters:
graph- The graph containing source and destination nodessourceNode- Source node of the path being searcheddestNodeIndicators- Class containing list of callbacks determining the sequence of nodesarcWeighter- Class containing callback which will be called to calculate weight of graph arcsnodeWeighter- Class containing callback which will be called to calculate weight of graph nodes- Returns:
- Shortest path through nodes specified by indicators, or null if path does not exist
-
getTreeNodesByLevels
Groups the nodes of the specified oriented tree by levels. A level is a group of nodes having the same distance from a root node. A distance in this case is defined as number of arcs that need to be traversed to reach the given node from a root one. There can be several root nodes in the specified graph. If the specified graph is not an oriented tree, aIllegalArgumentExceptionwill be thrown- Parameters:
graph- that must be an oriented tree- Returns:
- List of lists of nodes grouped by levels
-
getInsignificantNodesAndArcs
static <A,B> Pair<Set<Graph<A,B>.Node>, getInsignificantNodesAndArcsSet<Graph<A, B>.Arc>> (Graph<A, B> graph, Set<Graph<A, B>.Node> significantNodes) Returns the pair containing sets of insignificant nodes and arcs of the specified graph against the specified set of significant nodes. A node or arc is considered insignificant if it cannot be a part of any minimal path between any two significant nodes. The necessary assumption for this function is non-negative weights of all nodes and arcs which is the case in majority of real life cases.- Parameters:
graph- The specified graphsignificantNodes- Set of significant node in the specified graph- Returns:
- Pair containing sets of insignificant nodes (first member of the pair) and arcs (second member of the pair). The returned pair and sets inside the pair are always non-null. Sets of insignificant arcs and nodes can be empty
-
getInsignificantNodesAndArcs
static <A,B> Pair<Set<Graph<A,B>.Node>, getInsignificantNodesAndArcsSet<Graph<A, B>.Arc>> (Graph<A, B> graph, Set<Graph<A, B>.Node> significantNodes, Set<Graph<A, B>.Arc> barrierArcs) Returns the pair containing sets of insignificant nodes and arcs of the specified graph against the specified set of significant nodes. A node or arc is considered insignificant if it cannot be a part of any minimal path between any two significant nodes. The necessary assumption for this function is non-negative weights of all nodes and arcs which is the case in majority of real life cases.- Parameters:
graph- The specified graphsignificantNodes- Set of significant node in the specified graphbarrierArcs- arcs that should not be traversed- Returns:
- Pair containing sets of insignificant nodes (first member of the pair) and arcs (second member of the pair). The returned pair and sets inside the pair are always non-null. Sets of insignificant arcs and nodes can be empty
-
getBarrierArcs
-
getFirstCycle
Returns first found cycle in the specified graph. If there are no cycles in the graph, returns null. The function can return any cycle if there are several cycles in the graph, but for the same graph every time the same cycle will be returned.- Parameters:
graph- The specified graph- Returns:
- The first found cycle in the graph, or null if there are no cycles in the graph
-
topologicalSort
static <A,B> Map<Graph<A,B>.Node, topologicalSortInteger> (Graph<A, B> graph, Graph<A, B>.Node rootNode) Performs topological sorting of the graph with respect to the specified root node and returns a map containing mapping from graph nodes to integer numbers. Each number in this mapping is equal to minimum number of arcs to reach the given node from the root node.- Parameters:
graph- The specified graphrootNode- The specified root node- Returns:
- Map containing mapping from graph nodes to integer numbers. Each number in this mapping is equal to minimum number of arcs to reach the given node from the root node. If a node cannot be reached from the source node, the mapping will contain -1 for this node.
-
topologicalSort
static <A,B> Map<Graph<A,B>.Node, topologicalSortInteger> (Graph<A, B> graph, List<Graph<A, B>.Node> rootNodes) Performs topological sorting of the graph with respect to the specified root nodes and returns a map containing mapping from graph nodes to integer numbers. Each number in this mapping is equal to minimum number of arcs to reach the given node from the closest one of the root nodes.- Parameters:
graph- The specified graphrootNodes- The specified root nodes- Returns:
- Map containing mapping from graph nodes to integer numbers. Each number in this mapping is equal to minimum number of arcs to reach the given node from the closest one of the root nodes. If a node cannot be reached from the source node, the mapping will contain -1 for this node.
-
getFragments
Returns all fragments (i.e. connected components) of the specified graph. A fragment is a fully isolated subgraph that can consist even of a single node.If the specified graph is a connected graph, returns a list containing only the specified graph.
- Parameters:
graph- specified graph- Returns:
- list containing all fragments (i.e. connected components) of the specified graph
-
traverse
static <A,B, C traverseC> (Graph<A, B>.Node sourceNode, C sourceContext, Algorithm.GraphTraversalPredicate<A, B> traversedArcPredicate, Algorithm.GraphTraversalContextCalculator<A, B, C> recalculateNodeContext, Algorithm.GraphTraversalEndPredicate<A, B, C> endTraversalPredicate) Traverses the graph starting from the specified sourceNode by adding the arcs and nodes allowed by traversedArcPredicate until all reachable nodes are traversed or the specified endTraversalPredicate evaluated totrue.A context of type C is calculated for every node being traversed. For the source node, the context is equal to the specified sourceContext. For every next node being traversed, its context is calculated by the specified recalculateNodeContext function. The result of the traversal is the last added context.
Note that this function does not consider weights and thus does not build minimum spanning tree while traversal
- Type Parameters:
A- type of graph node valuesB- type of graph arc valuesC- type of context, that is calculated for each node being traversed and returned as result of traversal- Parameters:
sourceNode- source node of the traversalsourceContext- context value of the specified source nodetraversedArcPredicate- predicate answering the question "Can we traverse the arc and the node adjacent to this arc?".Example of implementation:
(addedArc, addedNode, addedArcDirection) -> truerecalculateNodeContext- predicate answering the question "What must be the context of the node being added considering we know the added arc, the added node itself, and the context of the already traversed node adjacent to this arc?"Example of implementation for context of type
Double:(addedArc, addedNode, addedArcDirection, prevNodeContext) -> prevNodeContext + addedArc.getValue().getLength()endTraversalPredicate- predicate answering the question "Must we end the traversal after we added the specific arc, node and calculated the specific node context?"Example of implementation for context of type
Double:(addedArc, addedNode, addedNodeContext) -> addedNodeContext >= 100- Returns:
- result of the traversal
-