Package org.graph4j

Class GraphBuilder

java.lang.Object
org.graph4j.GraphBuilder

public class GraphBuilder extends Object
Support class for creating a graph of any type.
Author:
Cristian Frăsinaru
  • Field Details

    • vertices

      protected int[] vertices
    • dynamicVertices

      protected final List<Integer> dynamicVertices
    • vertexLabelMap

      protected final Map<Object,Integer> vertexLabelMap
    • vertexWeightMap

      protected final Map<Integer,Double> vertexWeightMap
    • maxVertices

      protected Integer maxVertices
    • numEdges

      protected Long numEdges
    • avgDegree

      protected Integer avgDegree
    • density

      protected Double density
    • directed

      protected boolean directed
    • allowingSelfLoops

      protected boolean allowingSelfLoops
    • allowingMultiEdges

      protected boolean allowingMultiEdges
    • vertexDataSize

      protected int vertexDataSize
    • edgeDataSize

      protected int edgeDataSize
    • name

      protected String name
    • edges

      protected final List<Edge> edges
    • paths

      protected final List<int[]> paths
    • cycles

      protected final List<int[]> cycles
    • cliques

      protected final List<int[]> cliques
  • Constructor Details

    • GraphBuilder

      protected GraphBuilder()
  • Method Details

    • empty

      public static GraphBuilder empty()
      Creates an empty graph (with no vertices).
      Returns:
      a new a GraphBuilder.
    • numVertices

      public static GraphBuilder numVertices(int numVertices)
      Creates a graph having as vertices the numbers from 0 to numVertices - 1.
      Parameters:
      numVertices - the actual number of vertices in the graph.
      Returns:
      a new a GraphBuilder.
    • vertexRange

      public static GraphBuilder vertexRange(int firstVertex, int lastVertex)
      Creates a new graph having as vertices the numbers in the specified range.
      Parameters:
      firstVertex - The number of the first vertex.
      lastVertex - The number of the last vertex.
      Returns:
      a new a GraphBuilder.
    • vertices

      public static GraphBuilder vertices(int... vertices)
      Creates a new graph having the specified vertex numbers.
      Parameters:
      vertices - the vertices of the graph.
      Returns:
      a new a GraphBuilder.
    • labeledVertices

      public static <V> GraphBuilder labeledVertices(Collection vertexObjects)
      Creates a new graph having the specified vertex labels. The vertex numbers will be assigned in the order of the collection, starting with 0.
      Type Parameters:
      V - the type of vertex labels.
      Parameters:
      vertexObjects - a collection of objects, representing the labeled vertices of the graph.
      Returns:
      a new a GraphBuilder.
    • labeledVertices

      public static <V> GraphBuilder labeledVertices(V... vertexObjects)
      Creates a new graph having the specified vertex labels. The vertex numbers will be the indices in the array of labels.
      Type Parameters:
      V - the type of vertex labels.
      Parameters:
      vertexObjects - a list of objects, representing the labeled vertices of the graph.
      Returns:
      a new a GraphBuilder.
    • verticesFrom

      public static GraphBuilder verticesFrom(Graph graph)
      Creates a new graph having the same vertex numbers as the specified graph.
      Parameters:
      graph - a graph of any type.
      Returns:
      a new a GraphBuilder.
    • edges

      public static GraphBuilder edges(String edges)
      Creates a new graph based on the string representation of its edge set, for example: "1-2, 2-3, 3-1", "a-b, b-c, c-d", etc. "0-1,0-2-0-3" may be written as "0-1,2,3". Useful for creating small graphs for testing purposes.
      Parameters:
      edges - a text representation of the edges of the graph.
      Returns:
      a new a GraphBuilder.
    • named

      public GraphBuilder named(String name)
      Sets the name of the graph.
      Parameters:
      name - the name to be set for the graph.
      Returns:
      a reference to this object.
    • estimatedDensity

      public GraphBuilder estimatedDensity(double density)
      Sets an estimation of the graph density, in order to optimize memory allocation.
      Parameters:
      density - the estimated density of the graph.
      Returns:
      a reference to this object.
    • estimatedAvgDegree

      public GraphBuilder estimatedAvgDegree(int avgDegree)
      Sets an estimation of the graph average vertex degree, in order to optimize memory allocation.
      Parameters:
      avgDegree - the estimated average degree of the vertices.
      Returns:
      a reference to this object.
    • estimatedNumEdges

      public GraphBuilder estimatedNumEdges(long numEdges)
      Sets an estimation of the number of edges in the graph, in order to optimize memory allocation.
      Parameters:
      numEdges - the estimated maximum number of edges.
      Returns:
      a reference to this object.
    • estimatedNumVertices

      public GraphBuilder estimatedNumVertices(int numVertices)
      Sets an estimation of the number of vertices in the graph, in order to optimize memory allocation. No vertex will be added to the graph as a result of this invocation.
      Parameters:
      numVertices - the estimated maximum number of vertices.
      Returns:
      a reference to this object.
    • vertexDataSize

      public GraphBuilder vertexDataSize(int vertexDataSize)
    • edgeDataSize

      public GraphBuilder edgeDataSize(int edgeDataSize)
    • addEdge

      public GraphBuilder addEdge(Edge e)
      Adds an edge to the graph.
      Parameters:
      e - an object containing the endpoints of the edge, eventually its weight and label.
      Returns:
      a reference to this object.
    • addPath

      public GraphBuilder addPath(int... path)
      Adds a path to the graph, specified by its vertex numbers.
      Parameters:
      path - an array of vertex numbers.
      Returns:
      a reference to this object.
    • addCycle

      public GraphBuilder addCycle(int... cycle)
      Adds a cycle to the graph, specified by its vertex numbers.
      Parameters:
      cycle - an array of vertex numbers.
      Returns:
      a reference to this object.
    • addClique

      public GraphBuilder addClique(int... clique)
      Adds a clique to the graph, specified by its vertex numbers.
      Parameters:
      clique - an array of vertex numbers.
      Returns:
      a reference to this object.
    • adjList

      public GraphBuilder adjList(int[][] a)
      Builds the graph from the adjacency list. The number of rows is the number of vertices, for the row with the index i, a[i] represents the neighbors (successors) of the vertex with the index i in the graph.
      Parameters:
      a - the adjacency list.
      Returns:
      a reference to this object.
    • addEdges

      public GraphBuilder addEdges(String edges)
      Adds to the graph a set of edges represented as a string, for example: "1-2, 2-3, 3-1", "a-b, b-c, c-d", etc. "0-1,0-2-0-3" may be written as "0-1,2,3".
      Parameters:
      edges - a text representation of the edges to be added.
      Returns:
      a reference to this object.
    • addEdge

      public <V> GraphBuilder addEdge(V vLabel, V uLabel)
      Adds an edge to the graph, specified using its vertex labels.
      Type Parameters:
      V - the type of vertex labels.
      Parameters:
      vLabel - a labeled vertex.
      uLabel - a labeled vertex.
      Returns:
      a reference to this object.
    • addEdge

      public GraphBuilder addEdge(int v, int u)
      Adds an edge to the graph, specified using its vertex numbers.
      Parameters:
      v - a vertex number.
      u - a vertex number.
      Returns:
      a reference to this object.
    • buildGraph

      public Graph buildGraph()
      Builds an undirected graph, without multiple edges or self loops.
      Returns:
      a simple undirected graph.
    • buildDigraph

      public Digraph buildDigraph()
      Builds a directed graph, without multiple edges or self loops.
      Returns:
      a simple directed graph.
    • buildMultigraph

      public Multigraph buildMultigraph()
      Builds an undirected graph, without self loops, allowing multiple edges between two vertices.
      Returns:
      an undirected multigraph.
    • buildDirectedMultigraph

      public DirectedMultigraph buildDirectedMultigraph()
      Builds a directed graph, without self loops, allowing multiple edges between two vertices.
      Returns:
      a directed multigraph.
    • buildPseudograph

      public Pseudograph buildPseudograph()
      Builds an undirected graph, allowing both multiple edges between two vertices and self loops.
      Returns:
      an undirected pseudograph.
    • buildDirectedPseudograph

      public DirectedPseudograph buildDirectedPseudograph()
      Builds a directed graph, allowing both multiple edges between two vertices and self loops.
      Returns:
      a directed pseudograph.
    • getVertex

      protected int getVertex(int v)
    • getVertex

      protected int getVertex(String vstr)
    • findVertexWithLabel

      protected int findVertexWithLabel(String strLabel)
    • nextVertexNumber

      protected int nextVertexNumber()
    • validate

      protected void validate()
    • avgDegree

      protected int avgDegree()