X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=third-party%2Fnet.sf.jung2%2Fsrc%2Fmain%2Fjava%2Fedu%2Fuci%2Fics%2Fjung%2Falgorithms%2Fgenerators%2Frandom%2FMixedRandomGraphGenerator.java;fp=third-party%2Fnet.sf.jung2%2Fsrc%2Fmain%2Fjava%2Fedu%2Fuci%2Fics%2Fjung%2Falgorithms%2Fgenerators%2Frandom%2FMixedRandomGraphGenerator.java;h=a39a6404bd577c1295a482c49039a4790fee4d5a;hp=0000000000000000000000000000000000000000;hb=42210c03b0a4c54706320ba9f55794c0abd4d201;hpb=7576b38152b393793b1c9ec3df0ff86685f95236 diff --git a/third-party/net.sf.jung2/src/main/java/edu/uci/ics/jung/algorithms/generators/random/MixedRandomGraphGenerator.java b/third-party/net.sf.jung2/src/main/java/edu/uci/ics/jung/algorithms/generators/random/MixedRandomGraphGenerator.java new file mode 100644 index 0000000000..a39a6404bd --- /dev/null +++ b/third-party/net.sf.jung2/src/main/java/edu/uci/ics/jung/algorithms/generators/random/MixedRandomGraphGenerator.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2003, the JUNG Project and the Regents of the University of + * California All rights reserved. + * + * This software is open-source under the BSD license; see either "license.txt" + * or http://jung.sourceforge.net/license.txt for a description. + */ +/* + * Created on Jul 2, 2003 + * + */ +package edu.uci.ics.jung.algorithms.generators.random; + +import java.util.Map; +import java.util.Set; + +import org.apache.commons.collections15.Factory; + +import edu.uci.ics.jung.graph.Graph; +import edu.uci.ics.jung.graph.util.EdgeType; + +/** + * + * Generates a mixed-mode random graph based on the output of BarabasiAlbertGenerator. + * Primarily intended for providing a heterogeneous sample graph for visualization testing, etc. + * + */ +public class MixedRandomGraphGenerator { + + /** + * Equivalent to generateMixedRandomGraph(edge_weight, num_vertices, true). + */ + public static Graph generateMixedRandomGraph( + Factory> graphFactory, + Factory vertexFactory, + Factory edgeFactory, + Map edge_weight, + int num_vertices, Set seedVertices) + { + return generateMixedRandomGraph(graphFactory, vertexFactory, edgeFactory, + edge_weight, num_vertices, true, seedVertices); + } + + /** + * Returns a random mixed-mode graph. Starts with a randomly generated + * Barabasi-Albert (preferential attachment) generator + * (4 initial vertices, 3 edges added at each step, and num_vertices - 4 evolution steps). + * Then takes the resultant graph, replaces random undirected edges with directed + * edges, and assigns random weights to each edge. + */ + public static Graph generateMixedRandomGraph( + Factory> graphFactory, + Factory vertexFactory, + Factory edgeFactory, + Map edge_weights, + int num_vertices, boolean parallel, Set seedVertices) + { + int seed = (int)(Math.random() * 10000); + BarabasiAlbertGenerator bag = + new BarabasiAlbertGenerator(graphFactory, vertexFactory, edgeFactory, + 4, 3, //false, parallel, + seed, seedVertices); + bag.evolveGraph(num_vertices - 4); + Graph ug = bag.create(); + + // create a SparseMultigraph version of g + Graph g = graphFactory.create(); + //new SparseMultigraph(); + for(V v : ug.getVertices()) { + g.addVertex(v); + } + + // randomly replace some of the edges by directed edges to + // get a mixed-mode graph, add random weights + + for(E e : ug.getEdges()) { + V v1 = ug.getEndpoints(e).getFirst(); + V v2 = ug.getEndpoints(e).getSecond(); + + E me = edgeFactory.create(); + g.addEdge(me, v1, v2, Math.random() < .5 ? EdgeType.DIRECTED : EdgeType.UNDIRECTED); + edge_weights.put(me, Math.random()); + } + + return g; + } + +}