Package com.mxgraph.analysis
Class mxGraphAnalysis
java.lang.Object
com.mxgraph.analysis.mxGraphAnalysis
A singleton class that provides algorithms for graphs. Assume these
variables for the following examples:
mxICostFunction cf = mxDistanceCostFunction();
Object[] v = graph.getChildVertices(graph.getDefaultParent());
Object[] e = graph.getChildEdges(graph.getDefaultParent());
mxGraphAnalysis mga = mxGraphAnalysis.getInstance();
Shortest Path (Dijkstra)
For example, to find the shortest path between the first and the second selected cell in a graph use the following code:Object[] path = mga.getShortestPath(graph, from, to, cf, v.length, true);
Minimum Spanning Tree
This algorithm finds the set of edges with the minimal length that connect all vertices. This algorithm can be used as follows:Prim
mga.getMinimumSpanningTree(graph, v, cf, true))
Kruskal
mga.getMinimumSpanningTree(graph, v, e, cf))
Connection Components
The union find may be used as follows to determine whether two cells are connected:boolean connected = uf.differ(vertex1, vertex2)
.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static mxGraphAnalysis
Holds the shared instance of this class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected mxFibonacciHeap
Hook for subclassers to provide a custom fibonacci heap.protected mxUnionFind
createUnionFind
(Object[] v) Hook for subclassers to provide a custom union find structure.getConnectionComponents
(mxGraph graph, Object[] v, Object[] e) Returns a union find structure representing the connection components of G=(E,V).static mxGraphAnalysis
Object[]
getMinimumSpanningTree
(mxGraph graph, Object[] v, mxICostFunction cf, boolean directed) Returns the minimum spanning tree (MST) for the graph defined by G=(E,V).Object[]
getMinimumSpanningTree
(mxGraph graph, Object[] v, Object[] e, mxICostFunction cf) Returns the minimum spanning tree (MST) for the graph defined by G=(E,V).Object[]
getShortestPath
(mxGraph graph, Object from, Object to, mxICostFunction cf, int steps, boolean directed) Returns the shortest path between two cells or their descendants represented as an array of edges in order of traversal.static void
setInstance
(mxGraphAnalysis instance) Sets the shared instance of this class.sort
(mxCellState[] states, mxICostFunction cf) Returns a sorted set forcells
with respect tocf
.double
sum
(mxCellState[] states, mxICostFunction cf) Returns the sum of all cost forcells
with respect tocf
.
-
Field Details
-
instance
Holds the shared instance of this class.
-
-
Constructor Details
-
mxGraphAnalysis
protected mxGraphAnalysis()
-
-
Method Details
-
getInstance
- Returns:
- Returns the sharedInstance.
-
setInstance
Sets the shared instance of this class.- Parameters:
instance
- The instance to set.
-
getShortestPath
public Object[] getShortestPath(mxGraph graph, Object from, Object to, mxICostFunction cf, int steps, boolean directed) Returns the shortest path between two cells or their descendants represented as an array of edges in order of traversal.
This implementation is based on the Dijkstra algorithm.- Parameters:
graph
- The object that defines the graph structurefrom
- The source cell.to
- The target cell (aka sink).cf
- The cost function that defines the edge length.steps
- The maximum number of edges to traverse.directed
- If edge directions should be taken into account.- Returns:
- Returns the shortest path as an alternating array of vertices
and edges, starting with
from
and ending withto
. - See Also:
-
getMinimumSpanningTree
public Object[] getMinimumSpanningTree(mxGraph graph, Object[] v, mxICostFunction cf, boolean directed) Returns the minimum spanning tree (MST) for the graph defined by G=(E,V). The MST is defined as the set of all vertices with minimal lengths that forms no cycles in G.
This implementation is based on the algorihm by Prim-Jarnik. It uses O(|E|+|V|log|V|) time when used with a Fibonacci heap and a graph whith a double linked-list datastructure, as is the case with the default implementation.- Parameters:
graph
- the object that describes the graphv
- the vertices of the graphcf
- the cost function that defines the edge length- Returns:
- Returns the MST as an array of edges
- See Also:
-
getMinimumSpanningTree
Returns the minimum spanning tree (MST) for the graph defined by G=(E,V). The MST is defined as the set of all vertices with minimal lenths that forms no cycles in G.
This implementation is based on the algorihm by Kruskal. It uses O(|E|log|E|)=O(|E|log|V|) time for sorting the edges, O(|V|) create sets, O(|E|) find and O(|V|) union calls on the union find structure, thus yielding no more than O(|E|log|V|) steps. For a faster implementatin- Parameters:
graph
- The object that contains the graph.v
- The vertices of the graph.e
- The edges of the graph.cf
- The cost function that defines the edge length.- Returns:
- Returns the MST as an array of edges.
- See Also:
-
getConnectionComponents
Returns a union find structure representing the connection components of G=(E,V).- Parameters:
graph
- The object that contains the graph.v
- The vertices of the graph.e
- The edges of the graph.- Returns:
- Returns the connection components in G=(E,V)
- See Also:
-
sort
Returns a sorted set forcells
with respect tocf
.- Parameters:
states
- the cell states to sortcf
- the cost function that defines the order- Returns:
- Returns an ordered set of
cells
wrt.cf
-
sum
Returns the sum of all cost forcells
with respect tocf
.- Parameters:
states
- the cell states to use for the sumcf
- the cost function that defines the costs- Returns:
- Returns the sum of all cell cost
-
createUnionFind
Hook for subclassers to provide a custom union find structure.- Parameters:
v
- the array of all elements- Returns:
- Returns a union find structure for
v
-
createPriorityQueue
Hook for subclassers to provide a custom fibonacci heap.
-