### Question:
BarabasiAlbertGraphGenerator implements GraphGenerator<V, E, V> { @Override public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { Set<V> oldNodes = new HashSet<>(target.vertexSet()); Set<V> newNodes = new HashSet<>(); new CompleteGraphGenerator<V, E>(m0).generateGraph(target, vertexFactory, resultMap); target.vertexSet().stream().filter(v -> !oldNodes.contains(v)).forEach(newNodes::add); List<V> nodes = new ArrayList<>(n * m); nodes.addAll(newNodes); for (int i = 0; i < m0 - 2; i++) { nodes.addAll(newNodes); } for (int i = m0; i < n; i++) { V v = vertexFactory.createVertex(); if (!target.addVertex(v)) { throw new IllegalArgumentException("Invalid vertex factory"); } List<V> newEndpoints = new ArrayList<>(); int added = 0; while (added < m) { V u = nodes.get(rng.nextInt(nodes.size())); if (!target.containsEdge(v, u)) { target.addEdge(v, u); added++; newEndpoints.add(v); if (i > 1) { newEndpoints.add(u); } } } nodes.addAll(newEndpoints); } } BarabasiAlbertGraphGenerator(int m0, int m, int n);  BarabasiAlbertGraphGenerator(int m0, int m, int n, long seed);  BarabasiAlbertGraphGenerator(int m0, int m, int n, Random rng); @Override void generateGraph(
        Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap);  }### Answer:
@Test public void testUndirected() { final long seed = 5; GraphGenerator<Integer, DefaultEdge, Integer> gen = new BarabasiAlbertGraphGenerator<>(3, 2, 10, seed); Graph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class); gen.generateGraph(g, new IntegerVertexFactory(), null); assertEquals(10, g.vertexSet().size()); }
@Test public void testUndirectedWithOneInitialNode() { final long seed = 7; GraphGenerator<Integer, DefaultEdge, Integer> gen = new BarabasiAlbertGraphGenerator<>(1, 1, 20, seed); Graph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class); gen.generateGraph(g, new IntegerVertexFactory(), null); assertEquals(20, g.vertexSet().size()); }
@Test public void testDirected() { final long seed = 5; GraphGenerator<Integer, DefaultEdge, Integer> gen = new BarabasiAlbertGraphGenerator<>(3, 2, 10, seed); Graph<Integer, DefaultEdge> g = new SimpleDirectedGraph<>(DefaultEdge.class); gen.generateGraph(g, new IntegerVertexFactory(), null); assertEquals(10, g.vertexSet().size()); }
@Test public void testDirectedWithOneInitialNode() { final long seed = 13; GraphGenerator<Integer, DefaultEdge, Integer> gen = new BarabasiAlbertGraphGenerator<>(1, 1, 20, seed); Graph<Integer, DefaultEdge> g = new SimpleDirectedGraph<>(DefaultEdge.class); gen.generateGraph(g, new IntegerVertexFactory(), null); assertEquals(20, g.vertexSet().size()); }
@Test public void testUndirectedWithGraphWhichAlreadyHasSomeVertices() { final long seed = 5; GraphGenerator<Integer, DefaultEdge, Integer> gen = new BarabasiAlbertGraphGenerator<>(3, 2, 10, seed); Graph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class); g.addVertex(1000); gen.generateGraph(g, new IntegerVertexFactory(), null); assertEquals(11, g.vertexSet().size()); }