Explore the path JGraphT based implementation

Explore the path JGraphT based implementation

Proposed memory-based business, there are between two points to explore and undirected path between a minimal diagrams of multi-point demand, following record using the implementation process JGraphT.

GraphT is a free Java class libraries, objects and provide mathematical graph theory algorithms, this covers only part of the path to explore.

Figure example Introduction

The following sources of information graph-structures

Available Graphs Overview

Graphs Side direction Loopback Vertices of multilateral Weighted
SimpleGraph undirected no no no
Multigraph undirected no yes no
Pseudograph undirected yes yes no
DefaultUndirectedGraph undirected yes no no
SimpleWeightedGraph undirected no no yes
WeightedMultigraph undirected no yes yes
WeightedPseudograph undirected yes yes yes
DefaultUndirectedWeightedGraph undirected yes no yes
SimpleDirectedGraph directed no no no
DirectedMultigraph directed no yes no
DirectedPseudograph directed yes yes no
DefaultDirectedGraph directed yes no no
SimpleDirectedWeightedGraph directed no no yes
DirectedWeightedMultigraph directed no yes yes
DirectedWeightedPseudograph directed yes yes yes
DefaultDirectedWeightedGraph directed yes no yes

Structural properties

Undirected edges (undirected edges): a pair of vertices connected to only one edge without applying direction.

Directed edge (directed edges): edges have start and end points.

Loopback (self-loops): whether to allow the connection to the vertex edge itself.

Multilateral same direction (multiple edges): if there are a plurality of edges between the same pair of vertices (two edges directed graph, between the same pair of vertices, the opposite direction is not counted as multilateral).

Weighted (weighted): floating point whether the edge value weight (for class diagram, usually DefaultWeightedEdge as edge type),
unweighted were considered to FIG uniform edge weights 1.0, so that they can be used in the algorithm, such as looking the shortest path.

Business class

Abstract class vertex

public class Node{

    //顶点id
    private String id;
    
    ...
}

Abstract class edge

public class Link{
    
    //边缘id
    private String id;
    
    //起始点id
    private String source;
    
    //终止点id
    private String target;
    
    ...
}

FIG abstract data type

public class GraphDescription{
    
    //顶点集合
    private List<Node> nodes;
    
    //边缘集合
    private List<Link> links;
    
    ...
}

There are two paths to explore

The business use UID (String) class as a vertex, edge configured to use the default class edge DefaultEdge.

Graph<String, DefaultEdge> directedGraph = new DirectedMultigraph<>(DefaultEdge.class);

graphDescription.getNodes().forEach(node -> directedGraph.addVertex(node.getId()));

graphDescription.getLinks().forEach(link -> directedGraph.addEdge(link.getSource(), link.getTarget()));

Explore the shortest path, to find out the length of the shortest path, the shortest path length is less than the time limit, according to the shortest hop count to identify all non-loopback path

DijkstraShortestPath<String, DefaultEdge> dijkstraAlg = new DijkstraShortestPath<>(directedGraph);

GraphPath<String, DefaultEdge> shorest = dijkstraAlg.getPath(start, end);

if (shorest != null && shorest.getLength() <= hopsLimit) {

 AllDirectedPaths allPaths = new AllDirectedPaths(directedGraph);

 fullRes = allPaths.getAllPaths(start, end, true, shorest.getLength());

}

Explore the full path, directly explore the results by limiting the number of hops

AllDirectedPaths allPaths = new AllDirectedPaths(directedGraph);

fullRes = allPaths.getAllPaths(start, end, true, hopsLimit);

No two paths to explore

We support structure and undirected multilateral Multigraph

Graph<String, DefaultEdge> multiGraph = new Multigraph<>(DefaultEdge.class);

graphDescription.getNodes().forEach(node -> graph.addVertex(node.getId()));

graphDescription.getLinks().forEach(link -> graph.addEdge(link.getSource(), link.getTarget()));

There is no search for the path similar to the steps to verify that seek the shortest path hops, limit integer result is set to the maximum value of k

BidirectionalDijkstraShortestPath<String, DefaultEdge> dijkstraAlg = new BidirectionalDijkstraShortestPath<>(multiGraph);

GraphPath<String, DefaultEdge> shorest = dijkstraAlg.getPath(start, end);

if (shorest != null && shorest.getLength() <= hopsLimit) {

    KShortestSimplePaths simplePaths = new KShortestSimplePaths(multiGraph, shorest.getLength());
    
    fullRes = simplePaths.getPaths(start, end, Integer.MAX_VALUE);
}

Explore the full path, directly explore the results by limiting the number of hops

KShortestSimplePaths simplePaths = new KShortestSimplePaths(graph, hopsLimit);

fullRes = simplePaths.getPaths(start, end, Integer.MAX_VALUE);

Multi-point to explore the best kid map

Set based multigraph formed, a plurality of points, with itself as the Cartesian product, after addition of fullRes twenty-two seek the shortest path and de-emphasis, is the set of edges form a minimal FIG.

Guess you like

Origin www.cnblogs.com/miracle9527/p/12182235.html