package prefuse.data; import java.util.Iterator; import prefuse.data.column.Column; import prefuse.data.event.ColumnListener; import prefuse.data.event.EventConstants; import prefuse.data.event.GraphListener; import prefuse.data.event.TableListener; import prefuse.data.expression.Predicate; import prefuse.data.tuple.CompositeTupleSet; import prefuse.data.tuple.TableEdge; import prefuse.data.tuple.TableNode; import prefuse.data.tuple.TupleManager; import prefuse.data.tuple.TupleSet; import prefuse.data.util.Index; import prefuse.data.util.NeighborIterator; import prefuse.util.PrefuseConfig; import prefuse.util.collections.CompositeIntIterator; import prefuse.util.collections.CompositeIterator; import prefuse.util.collections.CopyOnWriteArrayList; import prefuse.util.collections.IntArrayIterator; import prefuse.util.collections.IntIterator; /** *

A Graph models a network of nodes connected by a collection of edges. * Both nodes and edges can have any number of associated data fields. * Additionally, edges are either directed or undirected, indicating a * possible directionality of the connection. Each edge has both a source * node and a target node, for a directed edge this indicates the * directionality, for an undirected edge this is just an artifact * of the order in which the nodes were specified when added to the graph. *

* *

Both nodes and edges are represented by backing data {@link Table} * instances storing the data attributes. For edges, this table must * also contain a source node field and a target node field. The default * column name for these fields are {@link #DEFAULT_SOURCE_KEY} and * {@link #DEFAULT_TARGET_KEY}, but these can be configured by the graph * constructor. These columns should be integer valued, and contain * either the row number for a corresponding node in the node table, * or another unique identifier for a node. In this second case, the * unique identifier must be included as a data field in the node * table. This name of this column can be configured using the appropriate * graph constructor. The default column name for this field is * {@link #DEFAULT_NODE_KEY}, which by default evaluates to null, * indicating that no special node key should be used, just the direct * node table row numbers. Though the source, target, and node key * values completely specify the graph linkage structure, to make * graph operations more efficient an additional table is maintained * internally by the Graph class, storing node indegree and outdegree * counts and adjacency lists for the inlinks and outlinks for all nodes.

* *

Graph nodes and edges can be accessed by application code by either * using the row numbers of the node and edge tables, which provide unique ids * for each, or using the {@link prefuse.data.Node} and * {@link prefuse.data.Edge} classes -- * {@link prefuse.data.Tuple} instances that provide * object-oriented access to both node or edge data values and the * backing graph structure. Node and Edge tuples are maintained by * special TupleManager instances contained within this Graph. By default, they * are not accessible from the backing node or edge tables directly. The reason * for this is that these tables might be used in multiple graphs * simultaneously. For example, a node data table could be used in a number of * different graphs, exploring different possible linkages between those node. * In short, use this Graph instance to request iterators over Node or * Edge tuples, not the backing data tables.

* *

All Graph instances also support an internally generated * spanning tree, provided by the {@link #getSpanningTree()} or * {@link #getSpanningTree(Node)} methods. This is particularly * useful in visualization contexts that use an underlying tree structure * to compute a graph layout.

* * @author jeffrey heer */ public class Graph extends CompositeTupleSet { /** Indicates incoming edges (inlinks) */ public static final int INEDGES = 0; /** Indicates outgoing edges (outlinks) */ public static final int OUTEDGES = 1; /** Indicates all edges, regardless of direction */ public static final int UNDIRECTED = 2; /** Default data field used to uniquely identify a node */ public static final String DEFAULT_NODE_KEY = PrefuseConfig.get("data.graph.nodeKey"); /** Default data field used to denote the source node in an edge table */ public static final String DEFAULT_SOURCE_KEY = PrefuseConfig.get("data.graph.sourceKey"); /** Default data field used to denote the target node in an edge table */ public static final String DEFAULT_TARGET_KEY = PrefuseConfig.get("data.graph.targetKey"); /** Data group name to identify the nodes of this graph */ public static final String NODES = PrefuseConfig.get("data.graph.nodeGroup"); /** Data group name to identify the edges of this graph */ public static final String EDGES = PrefuseConfig.get("data.graph.edgeGroup"); // -- auxiliary data structures ----- /** Table containing the adjacency lists for the graph */ protected Table m_links; /** TupleManager for managing Node tuple instances */ protected TupleManager m_nodeTuples; /** TupleManager for managing Edge tuple instances */ protected TupleManager m_edgeTuples; /** Indicates if this graph is directed or undirected */ protected boolean m_directed = false; /** The spanning tree over this graph */ protected SpanningTree m_spanning = null; /** The node key field (for the Node table) */ protected String m_nkey; /** The source node key field (for the Edge table) */ protected String m_skey; /** The target node key field (for the Edge table) */ protected String m_tkey; /** Reference to an index over the node key field */ protected Index m_nidx; /** Update listener */ private Listener m_listener; /** Listener list */ private CopyOnWriteArrayList m_listeners = new CopyOnWriteArrayList(); // ------------------------------------------------------------------------ // Constructors /** * Creates a new, empty undirected Graph. */ public Graph() { this(false); } /** * Creates a new, empty Graph. * @param directed true for directed edges, false for undirected */ public Graph(boolean directed) { this(new Table(), directed); } /** * Create a new Graph using the provided table of node data and * an empty set of edges. * @param nodes the backing table to use for node data. * Node instances of this graph will get their data from this table. * @param directed true for directed edges, false for undirected */ public Graph(Table nodes, boolean directed) { this(nodes, directed, DEFAULT_NODE_KEY, DEFAULT_SOURCE_KEY, DEFAULT_TARGET_KEY); } /** * Create a new Graph using the provided table of node data and * an empty set of edges. * @param nodes the backing table to use for node data. * Node instances of this graph will get their data from this table. * @param directed true for directed edges, false for undirected * @param nodeKey data field used to uniquely identify a node. If this * field is null, the node table row numbers will be used * @param sourceKey data field used to denote the source node in an edge * table * @param targetKey data field used to denote the target node in an edge * table */ public Graph(Table nodes, boolean directed, String nodeKey, String sourceKey, String targetKey) { Table edges = new Table(); edges.addColumn(sourceKey, int.class, new Integer(-1)); edges.addColumn(targetKey, int.class, new Integer(-1)); init(nodes, edges, directed, nodeKey, sourceKey, targetKey); } /** * Create a new Graph, using node table row numbers to uniquely * identify nodes in the edge table's source and target fields. * @param nodes the backing table to use for node data. * Node instances of this graph will get their data from this table. * @param edges the backing table to use for edge data. * Edge instances of this graph will get their data from this table. * @param directed true for directed edges, false for undirected */ public Graph(Table nodes, Table edges, boolean directed) { this(nodes, edges, directed, DEFAULT_NODE_KEY, DEFAULT_SOURCE_KEY, DEFAULT_TARGET_KEY); } /** * Create a new Graph, using node table row numbers to uniquely * identify nodes in the edge table's source and target fields. * @param nodes the backing table to use for node data. * Node instances of this graph will get their data from this table. * @param edges the backing table to use for edge data. * Edge instances of this graph will get their data from this table. * @param directed true for directed edges, false for undirected * @param sourceKey data field used to denote the source node in an edge * table * @param targetKey data field used to denote the target node in an edge * table */ public Graph(Table nodes, Table edges, boolean directed, String sourceKey, String targetKey) { init(nodes, edges, directed, DEFAULT_NODE_KEY, sourceKey, targetKey); } /** * Create a new Graph. * @param nodes the backing table to use for node data. * Node instances of this graph will get their data from this table. * @param edges the backing table to use for edge data. * Edge instances of this graph will get their data from this table. * @param directed true for directed edges, false for undirected * @param nodeKey data field used to uniquely identify a node. If this * field is null, the node table row numbers will be used * @param sourceKey data field used to denote the source node in an edge * table * @param targetKey data field used to denote the target node in an edge * table */ public Graph(Table nodes, Table edges, boolean directed, String nodeKey, String sourceKey, String targetKey) { init(nodes, edges, directed, nodeKey, sourceKey, targetKey); } // ------------------------------------------------------------------------ // Initialization /** * Initialize this Graph instance. * @param nodes the node table * @param edges the edge table * @param directed the edge directionality * @param nodeKey data field used to uniquely identify a node * @param sourceKey data field used to denote the source node in an edge * table * @param targetKey data field used to denote the target node in an edge * table */ protected void init(Table nodes, Table edges, boolean directed, String nodeKey, String sourceKey, String targetKey) { // sanity check if ( (nodeKey!=null && nodes.getColumnType(nodeKey) != int.class) || edges.getColumnType(sourceKey) != int.class || edges.getColumnType(targetKey) != int.class ) { throw new IllegalArgumentException( "Incompatible column types for graph keys"); } removeAllSets(); super.addSet(EDGES, edges); super.addSet(NODES, nodes); m_directed = directed; // INVARIANT: these three should all reference the same type // currently limited to int m_nkey = nodeKey; m_skey = sourceKey; m_tkey = targetKey; // set up indices if ( nodeKey != null ) { nodes.index(nodeKey); m_nidx = nodes.getIndex(nodeKey); } // set up tuple manager if ( m_nodeTuples == null ) m_nodeTuples = new TupleManager(nodes, this, TableNode.class); m_edgeTuples = new TupleManager(edges, this, TableEdge.class); // set up node attribute optimization initLinkTable(); // set up listening if ( m_listener == null ) m_listener = new Listener(); nodes.addTableListener(m_listener); edges.addTableListener(m_listener); m_listener.setEdgeTable(edges); } /** * Set the tuple managers used to manage the Node and Edge tuples of this * Graph. * @param ntm the TupleManager to use for nodes * @param etm the TupleManager to use for edges */ public void setTupleManagers(TupleManager ntm, TupleManager etm) { if ( !Node.class.isAssignableFrom(ntm.getTupleType()) ) throw new IllegalArgumentException("The provided node " + "TupleManager must generate tuples that implement " + "the Node interface."); if ( !Edge.class.isAssignableFrom(etm.getTupleType()) ) throw new IllegalArgumentException("The provided edge " + "TupleManager must generate tuples that implement " + "the Edge interface."); m_nodeTuples = ntm; m_edgeTuples = etm; } /** * Dispose of this graph. Unregisters this graph as a listener to its * included tables. */ public void dispose() { getNodeTable().removeTableListener(m_listener); getEdgeTable().removeTableListener(m_listener); } /** * Updates this graph to use a different edge structure for the same nodes. * All other settings will remain the same (e.g., directionality, keys) * @param edges the new edge table. */ public void setEdgeTable(Table edges) { Table oldEdges = getEdgeTable(); oldEdges.removeTableListener(m_listener); m_edgeTuples.invalidateAll(); m_links.clear(); init(getNodeTable(), edges, m_directed, m_nkey, m_skey, m_tkey); } // ------------------------------------------------------------------------ // Data Access Optimization /** * Initialize the link table, which holds adjacency lists for this graph. */ protected void initLinkTable() { // set up cache of node data m_links = createLinkTable(); IntIterator edges = getEdgeTable().rows(); while ( edges.hasNext() ) { updateDegrees(edges.nextInt(), 1); } } /** * Instantiate and return the link table. * @return the created link table */ protected Table createLinkTable() { return LINKS_SCHEMA.instantiate(getNodeCount()); } /** * Internal method for updating the linkage of this graph. * @param e the edge id for the updated link * @param incr the increment value, 1 for an added link, * -1 for a removed link */ protected void updateDegrees(int e, int incr) { if ( !getEdgeTable().isValidRow(e) ) return; int s = getSourceNode(e); int t = getTargetNode(e); if ( s < 0 || t < 0 ) return; updateDegrees(e, s, t, incr); if ( incr < 0 ) m_edgeTuples.invalidate(e); } /** * Internal method for updating the linkage of this graph. * @param e the edge id for the updated link * @param s the source node id for the updated link * @param t the target node id for the updated link * @param incr the increment value, 1 for an added link, * -1 for a removed link */ protected void updateDegrees(int e, int s, int t, int incr) { int od = m_links.getInt(s, OUTDEGREE); int id = m_links.getInt(t, INDEGREE); // update adjacency lists if ( incr > 0 ) { // add links addLink(OUTLINKS, od, s, e); addLink(INLINKS, id, t, e); } else if ( incr < 0 ) { // remove links remLink(OUTLINKS, od, s, e); remLink(INLINKS, id, t, e); } // update degree counts m_links.setInt(s, OUTDEGREE, od+incr); m_links.setInt(t, INDEGREE, id+incr); // link structure changed, invalidate spanning tree m_spanning = null; } /** * Internal method for adding a link to an adjacency list * @param field which adjacency list (inlinks or outlinks) to use * @param len the length of the adjacency list * @param n the node id of the adjacency list to use * @param e the edge to add to the list */ protected void addLink(String field, int len, int n, int e) { int[] array = (int[])m_links.get(n, field); if ( array == null ) { array = new int[] {e}; m_links.set(n, field, array); return; } else if ( len == array.length ) { int[] narray = new int[Math.max(3*array.length/2, len+1)]; System.arraycopy(array, 0, narray, 0, array.length); array = narray; m_links.set(n, field, array); } array[len] = e; } /** * Internal method for removing a link from an adjacency list * @param field which adjacency list (inlinks or outlinks) to use * @param len the length of the adjacency list * @param n the node id of the adjacency list to use * @param e the edge to remove from the list * @return true if the link was removed successfully, false otherwise */ protected boolean remLink(String field, int len, int n, int e) { int[] array = (int[])m_links.get(n, field); for ( int i=0; i 0 ) { int[] links = (int[])m_links.get(node, INLINKS); for ( int i=id; --i>=0; ) removeEdge(links[i]); } int od = getOutDegree(node); if ( od > 0 ) { int[] links = (int[])m_links.get(node, OUTLINKS); for ( int i=od; --i>=0; ) removeEdge(links[i]); } } return nodeTable.removeRow(node); } /** * Remove a node from the graph, also removing all incident edges. * @param n the Node to remove from the graph * @return true if the node was successfully removed, false if the * node was not found in this graph */ public boolean removeNode(Node n) { nodeCheck(n, true); return removeNode(n.getRow()); } /** * Remove an edge from the graph. * @param edge the edge id (edge table row number) of the edge to remove * @return true if the edge was successfully removed, false if the * edge was not found or was not valid */ public boolean removeEdge(int edge) { return getEdgeTable().removeRow(edge); } /** * Remove an edge from the graph. * @param e the Edge to remove from the graph * @return true if the edge was successfully removed, false if the * edge was not found in this graph */ public boolean removeEdge(Edge e) { edgeCheck(e, true); return removeEdge(e.getRow()); } /** * Internal method for clearing the edge table, removing all edges. */ protected void clearEdges() { getEdgeTable().clear(); // TODO this can be removed after removeEdge fix is implemented // for ( IntIterator rows = m_links.rows(); rows.hasNext(); ) { // int row = rows.nextInt(); // m_links.setInt(row, OUTDEGREE, 0); // m_links.setInt(row, INDEGREE, 0); // } } // ------------------------------------------------------------------------ // Node Accessor Methods /** * Internal method for checking the validity of a node. * @param n the Node to check for validity * @param throwException true if this method should throw an Exception * when an invalid node is encountered * @return true is the node is valid, false if invalid */ protected boolean nodeCheck(Node n, boolean throwException) { if ( !n.isValid() ) { if ( throwException ) { throw new IllegalArgumentException( "Node must be valid."); } return false; } Graph ng = n.getGraph(); if ( ng != this && ng.m_spanning != this ) { if ( throwException ) { throw new IllegalArgumentException( "Node must be part of this Graph."); } return false; } return true; } /** * Get the collection of nodes as a TupleSet. Returns the same result as * {@link CompositeTupleSet#getSet(String)} using * {@link #NODES} as the parameter. * @return the nodes of this graph as a TupleSet instance */ public TupleSet getNodes() { return getSet(NODES); } /** * Get the backing node table. * @return the table of node values */ public Table getNodeTable() { return (Table)getSet(NODES); } /** * Get the number of nodes in this graph. * @return the number of nodes */ public int getNodeCount() { return getNodeTable().getRowCount(); } /** * Get the Node tuple instance corresponding to a node id. * @param n a node id (node table row number) * @return the Node instance corresponding to the node id */ public Node getNode(int n) { if ( n < 0 ) { System.err.println("catch"); } return (Node)m_nodeTuples.getTuple(n); } /** * Get the Node tuple corresponding to the input node key field value. * The node key field is used to find the node id (node table row number), * which is then used to retrieve the Node tuple. * @param key a node key field value * @return the requested Node instance */ public Node getNodeFromKey(int key) { int n = getNodeIndex(key); return (n<0 ? null : getNode(n) ); } /** * Get the in-degree of a node, the number of edges for which the node * is the target. * @param node the node id (node table row number) * @return the in-degree of the node */ public int getInDegree(int node) { return m_links.getInt(node, INDEGREE); } /** * Get the in-degree of a node, the number of edges for which the node * is the target. * @param n the Node instance * @return the in-degree of the node */ public int getInDegree(Node n) { nodeCheck(n, true); return getInDegree(n.getRow()); } /** * Get the out-degree of a node, the number of edges for which the node * is the source. * @param node the node id (node table row number) * @return the out-degree of the node */ public int getOutDegree(int node) { return m_links.getInt(node, OUTDEGREE); } /** * Get the out-degree of a node, the number of edges for which the node * is the source. * @param n the Node instance * @return the out-degree of the node */ public int getOutDegree(Node n) { nodeCheck(n, true); return getOutDegree(n.getRow()); } /** * Get the degree of a node, the number of edges for which a node * is either the source or the target. * @param node the node id (node table row number) * @return the total degree of the node */ public int getDegree(int node) { return getInDegree(node) + getOutDegree(node); } /** * Get the degree of a node, the number of edges for which a node * is either the source or the target. * @param n the Node instance * @return the total degree of the node */ public int getDegree(Node n) { nodeCheck(n, true); return getDegree(n.getRow()); } // ------------------------------------------------------------------------ // Edge Accessor Methods /** * Indicates if the edges of this graph are directed or undirected. * @return true if directed edges, false if undirected edges */ public boolean isDirected() { return m_directed; } /** * Internal method for checking the validity of an edge. * @param e the Edge to check for validity * @param throwException true if this method should throw an Exception * when an invalid node is encountered * @return true is the edge is valid, false if invalid */ protected boolean edgeCheck(Edge e, boolean throwException) { if ( !e.isValid() ) { if ( throwException ) { throw new IllegalArgumentException( "Edge must be valid."); } return false; } if ( e.getGraph() != this ) { if ( throwException ) { throw new IllegalArgumentException( "Edge must be part of this Graph."); } return false; } return true; } /** * Get the collection of edges as a TupleSet. Returns the same result as * {@link CompositeTupleSet#getSet(String)} using * {@link #EDGES} as the parameter. * @return the edges of this graph as a TupleSet instance */ public TupleSet getEdges() { return getSet(EDGES); } /** * Get the backing edge table. * @return the table of edge values */ public Table getEdgeTable() { return (Table)getSet(EDGES); } /** * Get the number of edges in this graph. * @return the number of edges */ public int getEdgeCount() { return getEdgeTable().getRowCount(); } /** * Get the Edge tuple instance corresponding to an edge id. * @param e an edge id (edge table row number) * @return the Node instance corresponding to the node id */ public Edge getEdge(int e) { return ( e < 0 ? null : (Edge)m_edgeTuples.getTuple(e) ); } /** * Returns an edge from the source node to the target node. This * method returns the first such node found; in the case of multiple * edges there may be more. */ public int getEdge(int source, int target) { int outd = getOutDegree(source); if ( outd > 0 ) { int[] edges = (int[])m_links.get(source, OUTLINKS); for ( int i=0; i -1 && ((isSrc && t > -1) || (!isSrc && s > -1)) ) updateDegrees(e, isSrc?p:s, isSrc?t:p, -1); if ( s > -1 && t > -1 ) updateDegrees(e, s, t, 1); } else { throw new IllegalStateException(); } } public void columnChanged(Column src, int type, int start, int end) { // should never be called throw new IllegalStateException(); } public void columnChanged(Column src, int idx, long prev) { // should never be called throw new IllegalStateException(); } public void columnChanged(Column src, int idx, float prev) { // should never be called throw new IllegalStateException(); } public void columnChanged(Column src, int idx, double prev) { // should never be called throw new IllegalStateException(); } public void columnChanged(Column src, int idx, boolean prev) { // should never be called throw new IllegalStateException(); } public void columnChanged(Column src, int idx, Object prev) { // should never be called throw new IllegalStateException(); } } // end of inner class Listener // ------------------------------------------------------------------------ // Graph Linkage Schema /** In-degree data field for the links table */ protected static final String INDEGREE = "_indegree"; /** Out-degree data field for the links table */ protected static final String OUTDEGREE = "_outdegree"; /** In-links adjacency list data field for the links table */ protected static final String INLINKS = "_inlinks"; /** Out-links adjacency list data field for the links table */ protected static final String OUTLINKS = "_outlinks"; /** Schema used for the internal graph linkage table */ protected static final Schema LINKS_SCHEMA = new Schema(); static { Integer defaultValue = new Integer(0); LINKS_SCHEMA.addColumn(INDEGREE, int.class, defaultValue); LINKS_SCHEMA.addColumn(OUTDEGREE, int.class, defaultValue); LINKS_SCHEMA.addColumn(INLINKS, int[].class); LINKS_SCHEMA.addColumn(OUTLINKS, int[].class); } } // end of class Graph