Graph implementations for the JUNG project - provides concrete implementations of graph data structures for the Java Universal Network/Graph Framework
—
Foundation classes that provide common graph functionality and establish consistent interfaces across all JUNG graph implementations. These abstract classes handle shared operations like edge validation, vertex relationships, and common graph queries.
Base implementation of the Graph interface providing common functionality for all graph types.
/**
* Abstract implementation of the Graph interface designed to simplify
* implementation of new graph classes.
*/
public abstract class AbstractGraph<V,E> implements Graph<V,E>, Serializable {
// Edge addition methods
public boolean addEdge(E edge, Collection<? extends V> vertices);
public boolean addEdge(E edge, Collection<? extends V> vertices, EdgeType edgeType);
public boolean addEdge(E e, V v1, V v2);
public boolean addEdge(E e, V v1, V v2, EdgeType edge_type);
public boolean addEdge(E edge, Pair<? extends V> endpoints);
public abstract boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType);
// Vertex degree and relationship queries
public int inDegree(V vertex);
public int outDegree(V vertex);
public boolean isPredecessor(V v1, V v2);
public boolean isSuccessor(V v1, V v2);
public int getPredecessorCount(V vertex);
public int getSuccessorCount(V vertex);
public boolean isNeighbor(V v1, V v2);
public boolean isIncident(V vertex, E edge);
public int getNeighborCount(V vertex);
public int degree(V vertex);
// Edge queries and navigation
public int getIncidentCount(E edge);
public V getOpposite(V vertex, E edge);
public E findEdge(V v1, V v2);
public Collection<E> findEdgeSet(V v1, V v2);
public Collection<V> getIncidentVertices(E edge);
// Utility methods
public String toString();
// Protected helper methods
protected Pair<V> getValidatedEndpoints(E edge, Pair<? extends V> endpoints);
}Usage Examples:
// AbstractGraph is extended by concrete implementations
public class CustomGraph<V,E> extends AbstractGraph<V,E> {
@Override
public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) {
// Implementation specific logic
return true;
}
// Implement other required abstract methods
}
// Common operations available on all graph implementations
Graph<String, String> graph = new DirectedSparseGraph<>();
graph.addVertex("A");
graph.addVertex("B");
graph.addEdge("edge1", "A", "B");
// These methods work on any AbstractGraph subclass
int degree = graph.degree("A"); // Get vertex degree
boolean connected = graph.isNeighbor("A", "B"); // Check if vertices are neighbors
String opposite = graph.getOpposite("A", "edge1"); // Get opposite vertex of edgeAbstract class for graphs where all edges have the same EdgeType, simplifying implementation of homogeneous graph types.
/**
* An abstract class for graphs whose edges all have the same EdgeType.
* Intended to simplify the implementation of such graph classes.
*/
public abstract class AbstractTypedGraph<V,E> extends AbstractGraph<V,E> {
/**
* Creates an instance with the specified edge type
* @param edge_type The edge type for all edges in this graph
*/
public AbstractTypedGraph(EdgeType edge_type);
/**
* Returns this graph's edge type
* @return The default edge type for this graph
*/
public EdgeType getDefaultEdgeType();
/**
* Returns this graph's edge type, or null if e is not in this graph
* @param e The edge to check
* @return Edge type or null if edge not found
*/
public EdgeType getEdgeType(E e);
/**
* Returns the edge set for this graph if edgeType matches default type
* @param edge_type The edge type to filter by
* @return Collection of edges of specified type
*/
public Collection<E> getEdges(EdgeType edge_type);
/**
* Returns the edge count for this graph if edge_type matches default type
* @param edge_type The edge type to count
* @return Number of edges of specified type
*/
public int getEdgeCount(EdgeType edge_type);
// Protected fields and methods
protected final EdgeType edge_type;
protected boolean hasEqualEdgeType(EdgeType edge_type);
protected void validateEdgeType(EdgeType edge_type);
}Usage Examples:
// AbstractTypedGraph ensures all edges have the same type
DirectedSparseGraph<String, String> directedGraph = new DirectedSparseGraph<>();
// All edges in this graph will be DIRECTED
UndirectedSparseGraph<String, String> undirectedGraph = new UndirectedSparseGraph<>();
// All edges in this graph will be UNDIRECTED
// Query edge type information
EdgeType type = directedGraph.getDefaultEdgeType(); // Returns EdgeType.DIRECTED
Collection<String> directedEdges = directedGraph.getEdges(EdgeType.DIRECTED);
int directedCount = directedGraph.getEdgeCount(EdgeType.DIRECTED);
// This would return empty/0 since graph only has directed edges
Collection<String> undirectedEdges = directedGraph.getEdges(EdgeType.UNDIRECTED);AbstractGraph uses the template method pattern where concrete subclasses implement the abstract addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) method while inheriting common behavior for other edge addition variants.
AbstractTypedGraph enforces type safety by validating that all operations maintain the graph's declared edge type, preventing mixed directed/undirected edges in homogeneous graph implementations.
Both abstract classes delegate complex operations to simpler primitive operations, allowing subclasses to implement minimal functionality while inheriting comprehensive graph behavior.
Install with Tessl CLI
npx tessl i tessl/maven-net-sf-jung--jung-graph-impl