Class UniformTreeGenerator<V,​E>

  • Type Parameters:
    V - the vertices type
    E - the edges type
    All Implemented Interfaces:
    GraphGenerator<V,​E>

    public class UniformTreeGenerator<V,​E>
    extends Object
    implements GraphGenerator<V,​E>
    Generate a uniform random tree.

    The generator uses the Prufer sequence method to generate a uniform random tree. The Prufer sequence is a sequence of n-2 integers sampled uniformly in the range [0,n), where n is the number of vertices. The sequence is than converted to a tree using the algorithm described in the paper "An Optimal Algorithm for Prufer Codes" by Xiaodong Wang, Lei Wang and Yingjie Wu. The algorithm runs in linear time.

    In the following example, a tree with \(24\) vertices is generated using the vertex builder of the graph factory, while the edges are generated using a custom identifier builder. The seed of the generator is set to some fixed value to get deterministic behavior.

     
     Graph<Integer, Integer> g = new UniformTreeGenerator<>(IntGraphFactory.undirected())
     		.vertices(24)
     		.edges(IdBuilderInt.defaultBuilder())
     		.seed(0x7d0c16fa09e05751L)
     		.generate();
      

    The generator generate undirected graphs only. If zero vertices are set, an empty graph is generated.

    For deterministic behavior, set the seed of the generator using seed(long).

    Author:
    Barak Ugav
    • Constructor Detail

      • UniformTreeGenerator

        public UniformTreeGenerator()
        Create a new uniform tree generator that will use the default graph factory.

        The default graph factory does not have vertex builder, so if only the number of vertices is set using vertices(int), the vertex builder must be set explicitly using graphFactory().setVertexBuilder(...). Alternatively, the method vertices(int, IdBuilder) can be used to set the number of vertices and provide a vertex builder that will override the (maybe non existing) vertex builder of the graph factory. The vertex set can also be set explicitly using vertices(Collection). For edges, an edge builder is mandatory and it can be set using edges(IdBuilder).

      • UniformTreeGenerator

        public UniformTreeGenerator​(GraphFactory<V,​E> factory)
        Create a new uniform tree generator that will use the given graph factory.

        If the factory has a vertex builder it will be used to generate the vertices of the generated graph(s) if only the number of vertices is set using vertices(int). If the factory has an edge builder it will be used to generate the edges of the generated graph(s) if it will not be overridden by edges(IdBuilder).

        During the graph(s) generation, the method GraphFactory.setDirected(boolean) of the given factory will be called to align the created graph with the generator configuration.

        To generate int graphs, pass an instance of IntGraphFactory to this constructor.

        Parameters:
        factory - the graph factory that will be used to create the generated graph(s)
    • Method Detail

      • graphFactory

        public GraphFactory<V,​E> graphFactory()
        Get the graph factory that will be used to create the generated graph(s).

        It's possible to customize the factory before generating the graph(s), for example by using GraphFactory.addHint(GraphFactory.Hint) to optimize the generated graph(s) for a specific algorithm. If the factory has a vertex builder it will be used to generate the vertices of the generated graph(s) if only the number of vertices is set using vertices(int). If the factory has an edge builder it will be used to generate the edges of the generated graph(s) if it will not be overridden by edges(IdBuilder).

        During the graph(s) generation, the method GraphFactory.setDirected(boolean) of the given factory will be called to align the created graph with the generator configuration.

        Returns:
        the graph factory that will be used to create the generated graph(s)
      • vertices

        public UniformTreeGenerator<V,​E> vertices​(int verticesNum)
        Set the number of vertices that will be generated for each graph.

        The vertices will be generated using the vertex builder of the graph factory, see GraphFactory.setVertexBuilder(IdBuilder). The default graph factory does not have a vertex builder, so it must be set explicitly, or IntGraphFactory, which does have such builder, should be passed in the constructor. Another alternative is to use vertices(int, IdBuilder) which set the number of vertices and provide a vertex builder that will override the (maybe non existing) vertex builder of the graph factory. The generation will happen independently for each graph generated. If there is no vertex builder, an exception will be thrown during generation. This method override all previous calls to any of vertices(Collection), vertices(int) or vertices(int, IdBuilder).

        Parameters:
        verticesNum - the number of vertices that will be generated for each graph
        Returns:
        this generator
        Throws:
        IllegalArgumentException - if verticesNum is negative
      • vertices

        public UniformTreeGenerator<V,​E> vertices​(int verticesNum,
                                                        IdBuilder<V> vertexBuilder)
        Set the number of vertices that will be generated for each graph, and the vertex builder that will be used to generate them.

        The vertices will be generated using the provided vertex builder, and the vertex generator provided by the graph factory (if exists) will be ignored. The generation will happen independently for each graph generated. This method override all previous calls to any of vertices(Collection), vertices(int) or vertices(int, IdBuilder).

        Parameters:
        verticesNum - the number of vertices that will be generated for each graph
        vertexBuilder - the vertex builder, or null to use the vertex builder of the graph factory
        Returns:
        this generator
        Throws:
        IllegalArgumentException - if verticesNum is negative
      • edges

        public UniformTreeGenerator<V,​E> edges​(IdBuilder<E> edgeBuilder)
        Set the edge builder that will be used to generate edges.

        The edges will be generated using the provided edge builder, and the edge generator provided by the graph factory (if exists) will be ignored. The generation will happen independently for each graph generated. If this method is not called, or called with a null argument, the edge builder of the graph factory will be used. If the graph factory does not have an edge builder, an exception will be thrown during generation.

        Parameters:
        edgeBuilder - the edge builder, or null to use the edge builder of the graph factory
        Returns:
        this generator
      • seed

        public UniformTreeGenerator<V,​E> seed​(long seed)
        Set the seed of the random number generator used to generate the graph(s).

        By default, a random seed is used. For deterministic behavior, set the seed of the generator.

        Parameters:
        seed - the seed of the random number generator
        Returns:
        this generator
      • generateIntoBuilder

        public GraphBuilder<V,​E> generateIntoBuilder()
        Description copied from interface: GraphGenerator
        Generates a graph into a builder.

        This is the a more flexible way to generate a graph. The builder can be used to generate a mutable or immutable graph, or to add additional vertices or edges on top of the generated ones.

        Specified by:
        generateIntoBuilder in interface GraphGenerator<V,​E>
        Returns:
        a new graph builder populated by the generator with the generator parameters