CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-net-sf-jung--jung-graph-impl

Graph implementations for the JUNG project - provides concrete implementations of graph data structures for the Java Universal Network/Graph Framework

Pending
Overview
Eval results
Files

tree-structures.mddocs/

Tree Implementations

Specialized hierarchical graph structures with parent-child relationships and tree-specific operations. These implementations provide efficient tree modeling with support for single trees, forests (collections of trees), and ordered k-ary trees with indexed child access.

Capabilities

DelegateTree

Single tree implementation with a designated root vertex and hierarchical parent-child relationships.

/**
 * An implementation of Tree that delegates to a specified instance of DirectedGraph.
 * Maintains tree constraints and provides tree-specific operations.
 */
public class DelegateTree<V,E> extends GraphDecorator<V,E> implements Tree<V,E> {
    
    /**
     * Creates an instance with default DirectedSparseGraph backend
     */
    public DelegateTree();
    
    /**
     * Creates an instance with specified graph factory
     * @param graphFactory Supplier for creating the underlying directed graph
     */
    public DelegateTree(Supplier<DirectedGraph<V,E>> graphFactory);
    
    /**
     * Creates a new DelegateTree which delegates to the specified graph
     * @param graph The underlying directed graph to use
     */
    public DelegateTree(DirectedGraph<V,E> graph);
    
    /**
     * Returns a Supplier that creates instances of this tree type
     * @return Factory supplier for DelegateTree instances
     */
    public static <V,E> Supplier<Tree<V,E>> getFactory();
    
    // Tree-specific edge addition (maintains tree constraints)
    public boolean addEdge(E e, V v1, V v2, EdgeType edgeType); // v1=parent, v2=child
    public boolean addEdge(E e, V v1, V v2);
    public boolean addChild(E edge, V parent, V child, EdgeType edgeType);
    public boolean addChild(E edge, V parent, V child);
    
    // Vertex operations
    public boolean addVertex(V vertex);  // Sets root if tree is empty
    public boolean removeVertex(V vertex); // Removes vertex and all descendants
    
    // Tree navigation
    public V getRoot();
    public void setRoot(V root); // Only if root is previously unset
    public V getParent(V child);
    public Collection<V> getChildren(V parent);
    public int getChildCount(V parent);
    public Collection<E> getChildEdges(V vertex);
    public E getParentEdge(V vertex);
    
    // Tree structure queries
    public List<V> getPath(V vertex);  // Path from root to vertex
    public int getDepth(V v);          // Depth from root
    public int getHeight();            // Height of entire tree
    public boolean isRoot(V v);
    public boolean isLeaf(V v);
    public boolean isInternal(V v);    // Neither root nor leaf
    
    // Tree operations
    public boolean removeChild(V orphan); // Removes vertex and all descendants
    public Collection<Tree<V,E>> getTrees(); // Returns singleton collection
    
    // Graph interface compliance
    public int getIncidentCount(E edge); // Always returns 2
    public boolean addEdge(E edge, Collection<? extends V> vertices);
    public String toString();
}

Usage Examples:

import edu.uci.ics.jung.graph.DelegateTree;
import edu.uci.ics.jung.graph.Tree;

// Create a tree
Tree<String, String> tree = new DelegateTree<>();

// Add root vertex
tree.addVertex("root");
tree.setRoot("root");

// Add children to root
tree.addChild("edge1", "root", "child1");
tree.addChild("edge2", "root", "child2");

// Add grandchildren
tree.addChild("edge3", "child1", "grandchild1");
tree.addChild("edge4", "child1", "grandchild2");

// Tree navigation
String root = tree.getRoot();                    // "root"
Collection<String> children = tree.getChildren("root"); // ["child1", "child2"]
String parent = tree.getParent("child1");        // "root"

// Tree structure queries
int depth = tree.getDepth("grandchild1");        // 2
int height = tree.getHeight();                   // 2
boolean isLeaf = tree.isLeaf("grandchild1");     // true
boolean isInternal = tree.isInternal("child1");  // true

// Path operations
List<String> path = tree.getPath("grandchild1"); // ["root", "child1", "grandchild1"]

// Tree modification
tree.removeChild("child1"); // Removes child1 and all its descendants

DelegateForest

Forest implementation supporting multiple trees (a collection of trees) with forest-specific operations.

/**
 * An implementation of Forest that delegates to a specified DirectedGraph instance.
 * Manages multiple trees within a single forest structure.
 */
public class DelegateForest<V,E> extends GraphDecorator<V,E> implements Forest<V,E> {
    
    /**
     * Creates an instance backed by a new DirectedSparseGraph
     */
    public DelegateForest();
    
    /**
     * Creates an instance backed by the input DirectedGraph
     * @param delegate The underlying directed graph to use
     */
    public DelegateForest(DirectedGraph<V,E> delegate);
    
    // Forest-specific edge addition
    public boolean addEdge(E e, V v1, V v2, EdgeType edgeType); // v1=parent, v2=child
    public boolean addVertex(V vertex); // Adds vertex as a root
    
    // Edge and vertex removal with subtree handling
    public boolean removeEdge(E edge); // Removes edge and subtree
    public boolean removeEdge(E edge, boolean remove_subtree);
    public boolean removeVertex(V vertex); // Removes vertex and subtree
    public boolean removeVertex(V vertex, boolean remove_subtrees);
    
    // Forest navigation
    public Collection<V> getRoots(); // Roots of all trees in forest
    public Collection<Tree<V,E>> getTrees(); // All trees in the forest
    public void addTree(Tree<V,E> tree); // Add entire tree to forest
    
    // Tree operations (work on individual trees within forest)  
    public V getParent(V child);
    public Collection<V> getChildren(V v);
    public int getChildCount(V vertex);
    public Collection<E> getChildEdges(V vertex);
    public E getParentEdge(V vertex);
    
    // Tree structure queries
    public List<V> getPath(V child); // Path from root to vertex
    public V getRoot(); // Returns root if forest has single tree, null otherwise
    public void setRoot(V root); // Adds root as a root of the forest
    public boolean removeChild(V orphan);
    public int getDepth(V v);
    public int getHeight();
    public boolean isInternal(V v);
    public boolean isLeaf(V v);
    public boolean isRoot(V v);
    
    // Graph interface compliance
    public int getIncidentCount(E edge); // Always returns 2
    public boolean addEdge(E edge, Collection<? extends V> vertices);
}

Usage Examples:

import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.Forest;

// Create a forest
Forest<String, String> forest = new DelegateForest<>();

// Add multiple trees
forest.addVertex("root1");  // First tree root
forest.addVertex("root2");  // Second tree root

// Build first tree
forest.addEdge("edge1", "root1", "child1");
forest.addEdge("edge2", "root1", "child2");

// Build second tree  
forest.addEdge("edge3", "root2", "child3");
forest.addEdge("edge4", "root2", "child4");

// Forest queries
Collection<String> roots = forest.getRoots();    // ["root1", "root2"]
Collection<Tree<String,String>> trees = forest.getTrees(); // 2 trees

// Individual tree operations work within forest context
Collection<String> children1 = forest.getChildren("root1"); // ["child1", "child2"]
String parent = forest.getParent("child3");      // "root2"

// Forest modification
forest.removeVertex("root1", true); // Removes entire first tree

OrderedKAryTree

Specialized tree implementation where each vertex has at most k children, with indexed access to children by position.

/**
 * An implementation of Tree in which each vertex has ≤ k children.
 * A specific child can be retrieved directly by specifying the index 
 * at which the child is located.
 */
public class OrderedKAryTree<V,E> extends AbstractTypedGraph<V,E> implements Tree<V,E> {
    
    /**
     * Creates a new instance with the specified order (maximum number of children)
     * @param order Maximum number of children per vertex
     */
    public OrderedKAryTree(int order);
    
    /**
     * Returns a Supplier that creates instances with specified order
     * @param order Maximum children per vertex for created instances
     * @return Factory supplier for OrderedKAryTree instances
     */
    public static <V,E> Supplier<DirectedGraph<V,E>> getFactory(final int order);
    
    // Indexed child operations
    public V getChild(V vertex, int index);  // Get child at specific index
    public E getChildEdge(V vertex, int index); // Get edge to child at index
    public boolean addEdge(E e, V parent, V child, int index); // Add at specific position
    public boolean addEdge(E e, V parent, V child); // Add at next available position
    
    // Tree structure
    public int getChildCount(V vertex);
    public Collection<E> getChildEdges(V vertex);
    public Collection<V> getChildren(V vertex); // Returns ordered list
    public V getParent(V vertex);
    public E getParentEdge(V vertex);
    public V getRoot();
    public Collection<Tree<V,E>> getTrees();
    
    // Tree measurements
    public int getDepth(V vertex);
    public int getHeight(); // Returns -1 if empty
    
    // Tree predicates
    public boolean isLeaf(V vertex);
    public boolean isRoot(V vertex);
    
    // Edge operations with tree constraints
    public boolean addEdge(E e, V v1, V v2, EdgeType edge_type);
    public boolean addEdge(E edge, Collection<? extends V> vertices, EdgeType edge_type);
    public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType);
    
    // Vertex operations
    public boolean addVertex(V vertex); // Adds as root only if no root exists
    public boolean removeEdge(E edge);  // Removes edge and subtree rooted at child
    public boolean removeVertex(V vertex); // Removes vertex and all descendants
    
    // Directional graph interface (tree-specific implementations)
    public V getDest(E directed_edge);
    public Pair<V> getEndpoints(E edge);
    public Collection<E> getInEdges(V vertex);  // Parent edge or empty
    public V getOpposite(V vertex, E edge);
    public Collection<E> getOutEdges(V vertex); // Child edges
    public int getPredecessorCount(V vertex);   // 0 for root, 1 for others
    public Collection<V> getPredecessors(V vertex); // Parent or empty
    public V getSource(E directed_edge);
    public int getSuccessorCount(V vertex);     // Number of children
    public Collection<V> getSuccessors(V vertex); // Children
    
    // Graph queries
    public int inDegree(V vertex);   // 0 for root, 1 for others
    public boolean isDest(V vertex, E edge);
    public boolean isPredecessor(V v1, V v2);
    public boolean isSource(V vertex, E edge);
    public boolean isSuccessor(V v1, V v2);
    public int outDegree(V vertex);  // Number of children
    
    // Standard graph interface
    public boolean isIncident(V vertex, E edge);
    public boolean isNeighbor(V v1, V v2);
    public boolean containsEdge(E edge);
    public boolean containsVertex(V vertex);
    public E findEdge(V v1, V v2);
    public Collection<E> findEdgeSet(V v1, V v2);
    public int getEdgeCount();
    public Collection<E> getEdges();
    public int getIncidentCount(E edge); // Always returns 2
    public Collection<E> getIncidentEdges(V vertex);
    public Collection<V> getIncidentVertices(E edge);
    public int getNeighborCount(V vertex);
    public Collection<V> getNeighbors(V vertex);
    public int getVertexCount();
    public Collection<V> getVertices();
}

Usage Examples:

import edu.uci.ics.jung.graph.OrderedKAryTree;
import edu.uci.ics.jung.graph.Tree;

// Create a binary tree (k=2)
OrderedKAryTree<String, String> binaryTree = new OrderedKAryTree<>(2);

// Add root
binaryTree.addVertex("root");

// Add children at specific positions
binaryTree.addEdge("leftEdge", "root", "leftChild", 0);   // Index 0 (left)
binaryTree.addEdge("rightEdge", "root", "rightChild", 1); // Index 1 (right)

// Add children at next available position
binaryTree.addEdge("edge1", "leftChild", "grandchild1"); // Next available: index 0

// Indexed access
String leftChild = binaryTree.getChild("root", 0);       // "leftChild"
String rightChild = binaryTree.getChild("root", 1);      // "rightChild"
String noChild = binaryTree.getChild("root", 2);         // null (no child at index 2)

// Tree structure
int childCount = binaryTree.getChildCount("root");       // 2
Collection<String> children = binaryTree.getChildren("root"); // Ordered: ["leftChild", "rightChild"]

// Tree constraints enforced
// binaryTree.addEdge("edge3", "root", "thirdChild", 2); // Would fail: exceeds k=2 limit

Tree Constraints and Validation

Single Root Constraint

All tree implementations enforce single root constraints:

  • DelegateTree: Only one root vertex allowed
  • DelegateForest: Multiple roots allowed (one per tree)
  • OrderedKAryTree: Single root, additional vertices must be children

Acyclic Structure

Trees prevent cycle formation:

  • Adding edges that would create cycles is rejected
  • Parent-child relationships are strictly hierarchical
  • No vertex can be an ancestor of itself

Edge Type Constraints

Tree implementations use directed edges to represent parent-child relationships:

  • All edges are implicitly directed (parent → child)
  • EdgeType.DIRECTED is enforced for tree edges

Performance Characteristics

DelegateTree

  • Space Complexity: O(V + E) = O(V) since E = V-1 in trees
  • Tree Operations: O(1) for parent/child queries with cached depth information
  • Path Operations: O(depth) for getPath operations

DelegateForest

  • Multiple Trees: Efficient management of forest structure
  • Tree Isolation: Operations on one tree don't affect others

OrderedKAryTree

  • Indexed Access: O(1) child access by index
  • Order Constraint: Enforces k-ary constraint at insertion time
  • Memory Overhead: Additional storage for maintaining child order

Common Use Cases

DelegateTree

  • Organizational hierarchies, file system structures, decision trees
  • Taxonomy classifications, parse trees, expression trees

DelegateForest

  • Multiple classification systems, disconnected hierarchies
  • Spanning forests, collections of organizational structures

OrderedKAryTree

  • Binary trees, heaps, B-trees, game trees
  • Ordered taxonomies, indexed hierarchical data
  • Any scenario requiring positional child access

Install with Tessl CLI

npx tessl i tessl/maven-net-sf-jung--jung-graph-impl

docs

abstract-base-classes.md

directed-graphs.md

index.md

mixed-graphs.md

specialized-graphs.md

tree-structures.md

undirected-graphs.md

utilities.md

tile.json