public class Graph extends Object implements com.yworks.yfiles.algorithms.IGraphInterface
Basically, a directed graph consists of a set of objects called "nodes" (represented by instances of class Node
)
and a set of node pairs which are called "edges" (represented by instances of class Edge
).
The directed stems from the fact that all edges in the graph have direction, i.e., they have a distinct source
node and a distinct target node. Using the aforementioned pair notation, an edge would be written as
(<source node>, <target node>).
Class Graph presents a proper data type that provides support for all essential operations like element creation,
removal, access, and iteration.
Important: Class Graph is the single authority for any structural changes to the graph data type. Specifically,
this means that there is no way to create or delete a node or an edge without using an actual Graph instance.
Furthermore, this class is also responsible for providing access to its elements. This is done by means of bidirectional
cursors that present a read-only view on the node set (interface INodeCursor
) and edge set (interface
IEdgeCursor
).
Class Graph fires notification events that signal structural changes, like, e.g., creation, removal, reinsertion, or
modification of graph elements.
Classes that implement the IGraphListener
interface can be registered with this class using the com.yworks.yfiles.algorithms.Graph.addGraphListener(com.yworks.yfiles.algorithms.IGraphListener)
method in order to receive such events.
This class provides direct support for the notion of data accessors. It allows to register so-called data providers
(implementations of interface IDataProvider
) that hold arbitrary data which is associated to its nodes and/or
edges.
Also, it serves as a factory to create so-called maps (INodeMap
,
IEdgeMap
) that can be utilized to bind arbitrary data to nodes and edges.
Constructor and Description |
---|
Graph()
Instantiates an empty Graph object.
|
Graph(Graph graph)
Instantiates a new Graph object as a partial copy of the given graph.
|
Graph(Graph graph,
ICursor subNodes)
Instantiates a new Graph object as a partial copy of the given graph.
|
Modifier and Type | Method and Description |
---|---|
void |
addDataProvider(Object providerKey,
IDataProvider data)
Registers the given data provider using the given look-up key.
|
void |
changeEdge(Edge e,
Edge e1,
Edge e2,
GraphElementInsertion d1,
GraphElementInsertion d2)
Redefines an edge's end points and fires corresponding notification events to inform registered listeners.
|
void |
changeEdge(Edge e,
Node newSource,
Edge sourceReference,
GraphElementInsertion sourceD,
Node newTarget,
Edge targetReference,
GraphElementInsertion targetD)
Redefines an edge's end points and fires corresponding notification events to inform registered listeners.
|
void |
changeEdge(Edge e,
Node newSource,
Node newTarget)
Redefines an edge's end points and fires corresponding notification events to inform registered listeners.
|
void |
clear()
Removes all nodes and edges from this graph and fires corresponding notification events to inform registered listeners.
|
boolean |
contains(Edge e)
Whether or not this graph contains the given edge.
|
boolean |
contains(Node v)
Whether or not this graph contains the given node.
|
boolean |
containsEdge(Node source,
Node target)
Returns whether or not this graph contains an edge that connects the given nodes.
|
Graph |
createCopy()
Creates a copy of this graph.
|
Edge |
createEdge(Node v,
Edge e1,
Node w,
Edge e2,
GraphElementInsertion d1,
GraphElementInsertion d2)
Creates a new edge in this graph to be ordered before or after a given edge and fires a corresponding notification event
to inform registered listeners.
|
Edge |
createEdge(Node v,
Node w)
Creates a new edge in this graph and fires a corresponding notification event to inform registered listeners.
|
IEdgeMap |
createEdgeMap()
Returns a newly created edge map that is valid for the edges in this graph.
|
Graph |
createGraph()
Creates an empty base object of the same type as this graph.
|
Node |
createNode()
Creates a new node in this graph and fires a corresponding notification event to inform registered listeners.
|
INodeMap |
createNodeMap()
Returns a newly created node map that is valid for the nodes in this graph.
|
void |
disposeEdgeMap(IEdgeMap map)
Informs the graph that the given edge map is no longer needed.
|
void |
disposeNodeMap(INodeMap map)
Informs the graph that the given node map is no longer needed.
|
int |
edgeCount()
Gets the number of edges in this graph.
|
Iterable<Object> |
edgeObjects()
Returns an
Iterable that provides access to all edges residing in this graph. |
Edge |
firstEdge()
Gets the first edge in this graph.
|
Node |
firstNode()
Gets the first node in this graph.
|
protected static Edge |
firstOutEdge(Node v)
Low-level iteration support for adjacent edges.
|
IDataProvider |
getDataProvider(Object providerKey)
Returns the data provider that is registered with the graph using the given look-up key.
|
Object[] |
getDataProviderKeys()
Gets an array of all data provider look-up keys that are registered with this graph.
|
Edge[] |
getEdgeArray()
Returns an array containing all edges of this graph.
|
IEdgeCursor |
getEdgeCursor()
Provides access to the edges of the graph.
|
IEnumerable<Edge> |
getEdges()
|
Node[] |
getNodeArray()
Returns an array containing all nodes of this graph.
|
INodeCursor |
getNodeCursor()
Provides access to the nodes of the graph.
|
IEnumerable<Node> |
getNodes()
|
IEdgeMap[] |
getRegisteredEdgeMaps()
Gets all edge maps that have been created by this graph but have not yet been disposed.
|
INodeMap[] |
getRegisteredNodeMaps()
Gets all node maps that have been created by this graph but have not yet been disposed.
|
Object |
getSource(Object edge)
Returns the source node associated with the given edge.
|
Object |
getTarget(Object edge)
Returns the target node associated with the given edge.
|
protected boolean |
hasListeners()
Determines whether there are listeners registered with this instance.
|
void |
hide(Edge e)
Hides the given edge from this graph.
|
void |
hide(Node v)
Hides the given node from this graph.
|
boolean |
isEmpty()
Gets
true if this graph contains no nodes. |
Edge |
lastEdge()
Gets the last edge in this graph.
|
Node |
lastNode()
Gets the last node in this graph.
|
void |
moveToFirst(Edge e)
Moves the given edge to the first position within the sequence of edges in this graph.
|
void |
moveToFirst(Node v)
Moves the given node to the first position within the sequence of nodes in this graph.
|
void |
moveToLast(Edge e)
Moves the given edge to the last position within the sequence of edges in this graph.
|
void |
moveToLast(Node v)
Moves the given node to the last position within the sequence of nodes in this graph.
|
int |
nodeCount()
The number of nodes in this graph.
|
Iterable<Object> |
nodeObjects()
Returns an
Iterable that provides access to all nodes residing in this graph. |
void |
printNodeSlotSize()
For internal debugging purposes only.
|
void |
reInsertEdge(Edge e)
Reinserts a formerly removed edge into this graph and fires a corresponding notification event to inform registered
listeners.
|
void |
reInsertNode(Node v)
Reinserts a formerly removed node into this graph and fires a corresponding notification event to inform registered
listeners.
|
void |
removeDataProvider(Object providerKey)
Removes the data provider that is registered using the given look-up key.
|
void |
removeEdge(Edge e)
Removes the given edge from this graph and fires a corresponding notification event to inform registered listeners.
|
void |
removeNode(Node v)
Removes the given node from this graph.
|
void |
reverseEdge(Edge e)
Reverses the given edge and fires corresponding notification events to inform registered listeners.
|
void |
sortEdges(Comparator<Object> comp)
Sorts the internally held list of edges.
|
void |
sortEdges(Comparator<Object> inComp,
Comparator<Object> outComp)
Sorts incoming and outgoing edges at each node of the graph.
|
void |
sortNodes(Comparator<Object> comp)
Sorts the internally held list of nodes.
|
String |
toString()
Returns a String representation of this graph.
|
void |
unhide(Edge e)
Unhides the given edge in this graph.
|
void |
unhide(Node v)
Unhides the given node in this graph.
|
public Graph()
public Graph(Graph graph)
Only the subgraph induced by the given cursor will be copied to the new Graph instance. If no cursor is specifed, the
complete graph is copied. Values bound to the argument graph via node and edge keys are available in the new Graph
instance with the keys registered with graph
. Only references to these values are copied.
The new Graph instance also inherits all graph listeners registered with the given graph.
graph
- The graph to be (partially) copied.public Graph(Graph graph, ICursor subNodes)
Only the subgraph induced by the given cursor will be copied to the new Graph instance. If no cursor is specifed, the
complete graph is copied. Values bound to the argument graph via node and edge keys are available in the new Graph
instance with the keys registered with graph
. Only references to these values are copied.
The new Graph instance also inherits all graph listeners registered with the given graph.
graph
- The graph to be (partially) copied.subNodes
- A cursor to iterate over the nodes that actually induce the subgraph to be copied.public void addDataProvider(Object providerKey, IDataProvider data)
If there is already a data provider registered with that key, then it will be overwritten with the new one.
public void changeEdge(Edge e, Edge e1, Edge e2, GraphElementInsertion d1, GraphElementInsertion d2)
Edge e
has source node v := e1.source()
and target node w := e2.target()
.
Edge e
is inserted in such a way that an iteration over the edges at v
returns e
e1
, if d1 == AFTER
e1
, if d1 == BEFORE
,
and an iteration over the edges at w
returns e
e2
, if d2 == AFTER
e2
, if d2 == BEFORE
.e
, e1
, and e2
must belong to this graph.e
- The edge to be changed.e1
- Reference edge for insertion at a new source node.e2
- Reference edge for insertion at a new target node.d1
- One of the object insertion specifiers GraphElementInsertion.BEFORE
or GraphElementInsertion.AFTER
.d2
- One of the object insertion specifiers GraphElementInsertion.BEFORE
or GraphElementInsertion.AFTER
.public void changeEdge(Edge e, Node newSource, Edge sourceReference, GraphElementInsertion sourceD, Node newTarget, Edge targetReference, GraphElementInsertion targetD)
Edge e
has source node v := sourceReference.source()
or v := newSource
, if sourceReference == null
and target node w := targetReference.target()
or w := newTarget
, if targetReference == null
.
Edge e
is inserted in such a way that an iteration over the edges at v
returns e
sourceReference
, if sourceD == AFTER
sourceReference
, if sourceD == BEFORE
,
and an iteration over the edges at w
returns e
targetReference
, if targetD == AFTER
targetReference
, if targetD == BEFORE
.e
must belong to this graph. Also, either sourceReference
or newSource
must be non-null and
belong to this graph, and either targetReference
or newTarget
must be non-null and belong to this
graph.e
- The edge to be changed.newSource
- The new source node.sourceReference
- Reference edge for insertion at the new source node.sourceD
- One of the object insertion specifiers GraphElementInsertion.BEFORE
or GraphElementInsertion.AFTER
.newTarget
- The new target node.targetReference
- Reference edge for insertion at the new target node.targetD
- One of the object insertion specifiers GraphElementInsertion.BEFORE
or GraphElementInsertion.AFTER
.public void changeEdge(Edge e, Node newSource, Node newTarget)
The edge is appended to the lists of incoming and outgoing edges at the given source node and target node, respectively.
newSource
and newTarget
must belong to this graph.e
- The edge to be changed.newSource
- The new source node of the given edge.newTarget
- The new target node of the given edge.public void clear()
public boolean contains(Edge e)
public boolean contains(Node v)
public boolean containsEdge(Node source, Node target)
source
- The source node.target
- The target node.Node.getEdgeTo(Node)
,
Node.getEdgeFrom(Node)
,
Node.getEdge(Node)
public Graph createCopy()
Invokes Graph(Graph, ICursor)
.
public Edge createEdge(Node v, Edge e1, Node w, Edge e2, GraphElementInsertion d1, GraphElementInsertion d2)
The new edge e
has source node v
and target node w
, i.e., would be written as edge
e = (v, w)
.
Edge e
is inserted in such a way that an iteration over the edges at node v
returns e
e1
, if d1 == AFTER
e1
, if d1 == BEFORE
,
and an iteration over the edges at w
returns e
e2
, if d2 == AFTER
e2
, if d2 == BEFORE
.e1
must have source node v
and edge e2
must have target node w
.v
- The source node of the edge.e1
- An edge with source node v
.w
- The target node of the edge.e2
- An edge with target node w
.d1
- One of the object insertion specifiers GraphElementInsertion.BEFORE
or GraphElementInsertion.AFTER
.d2
- One of the object insertion specifiers GraphElementInsertion.BEFORE
or GraphElementInsertion.AFTER
.public Edge createEdge(Node v, Node w)
The new edge has source node v
and target node w
, i.e., would be written as edge e = (v, w)
.
The edge is appended to the lists of incoming and outgoing edges at the source node and target node, respectively.
v
- The source node of the edge.w
- The target node of the edge.public IEdgeMap createEdgeMap()
The implementation returned by this method can be used for any edge that is part of this Graph instance at any point of time, i.e., it is safe to modify the graph structure (add and remove nodes and edges) freely.
The implementation returned uses O(m)
memory at all times and provides true O(1)
read and write access
for each edge.
In order to release the resources held by this map, disposeEdgeMap(IEdgeMap)
has to be called.
public Graph createGraph()
Subclasses should override this method.
public Node createNode()
public INodeMap createNodeMap()
The implementation returned by this method can be used for any node that is part of this Graph instance at any point of time, i.e., it is safe to modify the graph structure (add and remove nodes and edges) freely.
The implementation returned uses O(n)
memory at all times and provides true O(1)
read and write access
for each node.
In order to release the resources held by this map, disposeNodeMap(INodeMap)
has to be called.
public void disposeEdgeMap(IEdgeMap map)
This method is used for EdgeMap implementations that have been obtained using the createEdgeMap()
factory
method.
Calling this method will destroy the edge map and associated resources can be freed. It is strongly recommended to dispose of all edge maps that are not needed anymore using this method.
public void disposeNodeMap(INodeMap map)
This method is used for NodeMap implementations that have been obtained using the createNodeMap()
factory
method.
Calling this method will destroy the node map and associated resources can be freed. It is strongly recommended to dispose of all node maps that are not needed anymore using this method.
public int edgeCount()
public Iterable<Object> edgeObjects()
Iterable
that provides access to all edges residing in this graph.edgeObjects
in interface com.yworks.yfiles.algorithms.IGraphInterface
public final Edge firstEdge()
edgeCount() > 0
public final Node firstNode()
!isEmpty()
protected static final Edge firstOutEdge(Node v)
public IDataProvider getDataProvider(Object providerKey)
The look-up domain of a returned data provider normally consists of either the nodes of the graph, or its edges, or both.
getDataProvider
in interface com.yworks.yfiles.algorithms.IGraphInterface
public Object[] getDataProviderKeys()
getDataProviderKeys
in interface com.yworks.yfiles.algorithms.IGraphInterface
public final Edge[] getEdgeArray()
public IEdgeCursor getEdgeCursor()
public final IEnumerable<Edge> getEdges()
Iterable
for
Edge
s that can be used to iterate over the edges that are contained in this instance.
This is a live enumerable and will thus reflect the current state of the graph. Note that changes to the graph structure during the traversal should be carried out with great care.
public final Node[] getNodeArray()
public INodeCursor getNodeCursor()
public final IEnumerable<Node> getNodes()
Iterable
for
Node
s that can be used to iterate over the nodes that are contained in this instance.
This is a live enumerable and will thus reflect the current state of the graph. Note that changes to the graph structure during the traversal should be carried out with great care.
public IEdgeMap[] getRegisteredEdgeMaps()
createEdgeMap()
,
disposeEdgeMap(IEdgeMap)
public INodeMap[] getRegisteredNodeMaps()
createNodeMap()
,
disposeNodeMap(INodeMap)
public Object getSource(Object edge)
getSource
in interface com.yworks.yfiles.algorithms.IGraphInterface
public Object getTarget(Object edge)
getTarget
in interface com.yworks.yfiles.algorithms.IGraphInterface
protected final boolean hasListeners()
public void hide(Edge e)
Hiding an edge means to (temporarily) remove the edge from the graph.
The only difference to a proper edge removal as performed by removeEdge(Edge)
is that no GraphEvent
will be emitted that signals the structural change (i.e. the edge's removal).
Generally, hiding should only be used in the sense of temporarily removing an object that will be reinserted shortly after.
To reinsert a hidden edge use unhide(Edge)
.
hide(Node)
,
unhide(Node)
public void hide(Node v)
Hiding a node means to (temporarily) remove the node from the graph.
The only difference to a proper node removal as performed by removeNode(Node)
is that no GraphEvent
will be emitted that signals the structural change (i.e. the node's removal).
Generally, hiding should only be used in the sense of temporarily removing an object that will be reinserted shortly after.
To reinsert a hidden node use unhide(Node)
.
hide(Edge)
,
unhide(Edge)
public final boolean isEmpty()
true
if this graph contains no nodes.true
if this graph contains no nodes, otherwise false
.public final Edge lastEdge()
edgeCount() > 0
public final Node lastNode()
!isEmpty()
public void moveToFirst(Edge e)
public void moveToFirst(Node v)
public void moveToLast(Edge e)
public void moveToLast(Node v)
public int nodeCount()
Returns the number of nodes in this graph. Same as NodeCount
.
public Iterable<Object> nodeObjects()
Iterable
that provides access to all nodes residing in this graph.nodeObjects
in interface com.yworks.yfiles.algorithms.IGraphInterface
public void printNodeSlotSize()
public void reInsertEdge(Edge e)
The reinserted edge is appended to the sequence of edges in this graph, i.e., normally, its new position does not match the position before its removal. The same holds for the edge's positions in the list of incoming and outgoing edges at its source node and target node, respectively.
Note that reinserting an edge whose source/target is not in the graph (e.g., because it's currently hidden/removed) causes an exception. Hence, in such cases, you first have to unhide/reinsert the corresponding endpoints.
e
- The edge to be reinserted.removeEdge(Edge)
public void reInsertNode(Node v)
The reinserted node is appended to the sequence of nodes in this graph, i.e., normally, its new position does not match the position before its removal.
v
- The node to be reinserted.removeNode(Node)
public void removeDataProvider(Object providerKey)
public void removeEdge(Edge e)
The edge will be deselected before it gets removed.
e
- The edge to be removed.public void removeNode(Node v)
All edges connecting to the given node are removed as well (preceding the actual node removal). Corresponding notification events are fired to inform registered listeners.
The node will be deselected before it gets removed.
v
- The node to be removed from this graph.public void reverseEdge(Edge e)
This operation exchanges source and target node of the edge.
public void sortEdges(Comparator<Object> comp)
If the given comparator is null
, then the edges will not be sorted. This list determines the order of the edges
as returned by getEdgeCursor()
.
comp
- The comparator used for the edges.public void sortEdges(Comparator<Object> inComp, Comparator<Object> outComp)
If a given comparator is null
, then the corresponding edges (i.e., incoming/outgoing) will not be sorted. This
sorts the order of the edges as returned by Node.getOutEdgeCursor(Edge)
and Node.getInEdgeCursor(Edge)
respectively.
inComp
- The comparator used for the incoming edges at each node.outComp
- The comparator used for the outgoing edges at each node.public void sortNodes(Comparator<Object> comp)
If the given comparator is null
, then the nodes will not be sorted. This list determines the order of the nodes
as returned by getNodeCursor()
.
comp
- The comparator used for the nodes.public String toString()
The result contains the String representations of all nodes followed by the String representations of all edges.
public void unhide(Edge e)
Unhiding an edge means to reinsert an edge that was formerly hidden from this graph by a call to hide(Edge)
.
The only difference to a proper edge reinsertion as performed by reInsertEdge(Edge)
is that no GraphEvent
will be emitted that signals the structural change (i.e. the edge's reinsertion).
Note that unhiding an edge whose source/target is not in the graph (e.g., because it's currently hidden/removed) causes an exception. Hence, in such cases, you first have to unhide/reinsert the corresponding endpoints.
hide(Node)
,
unhide(Node)
public void unhide(Node v)
Unhiding a node means to reinsert a node that was formerly hidden from this graph by a call to hide(Node)
.
The only difference to a proper node reinsertion as performed by reInsertNode(Node)
is that no GraphEvent
will be emitted that signals the structural change (i.e. the node's reinsertion).