package graph.test; import graph.*; import java.util.*; /** * A random initialization for a graph. Creates a specified * number of nodes and subgraphs and attaches them together * randomly. Does not lay them out in any way. * * @author Michael Shilman (michaels@eecs.berkeley.edu) * @version $Id$ */ public class RandomInit implements Action { boolean m_recurse = false;//XXX int m_numNodes = 30; int m_numGraphs = 20; int m_numEdges = 3; static Random s_rand = new Random(System.currentTimeMillis()); /** * Create a new RandomInit actin which adds a reasonable * number of nodes, graphs, and edges. */ public RandomInit() { } /** * Create a new RandomInit actin which adds the specified * number of nodes and graphs. * * @param numNodes The number of nodes to be added * @param numGraphs The number of subgraphs to be added * @param numEdges The number of edges to be added per node/subgraph */ public RandomInit(int numNodes, int numGraphs, int m_numEdges) { m_numNodes = numNodes; m_numGraphs = numGraphs; } public void apply(Graph g) { step(g); } public void init(Graph g) { } public void step(Graph g) { Node n; for(int i = 0; i < m_numNodes; i++) { n = new Node(); n.name = new String("Node " + i); n.lbl.label = n.name; g.add(n); } Graph sub; for(int i = 0; i < m_numGraphs; i++) { sub = new Graph(); sub.name = new String("Graph " + i); sub.lbl.label = sub.name; g.add(sub); } for(Enumeration e = g.nodes.elements(); e.hasMoreElements(); ) { n = (Node)e.nextElement(); while(n.out.size() < m_numEdges) { int rand = (int)(s_rand.nextFloat() * g.nodes.size()); Node n2 = (Node)g.nodes.elementAt(rand); if(!n2.equals(n)) { try { n.attach(n2); } catch(Exception ex) { System.out.println(ex); System.exit(0); } } } } } public void finish(Graph g) { } }