RandomGraphGenerator.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package algorithms;
  2. import model.LayeredGraphNode;
  3. import model.LayeredNode;
  4. /**
  5. * creates random {@link LayeredGraphNode}s for testing purposes
  6. * @author kolja
  7. *
  8. */
  9. public class RandomGraphGenerator {
  10. private double pSubgraph;
  11. private double pEdge;
  12. private int minLayer;
  13. private int maxLayer;
  14. private int minNodePerLayer;
  15. private int maxNodePerLayer;
  16. private int maxDepth;
  17. /**
  18. * sets the parameters for the random graph
  19. * @param pSubgraph the probability that a node contains a graph
  20. * @param pEdge the probability that there is an edge between two nodes in subsequent layers
  21. * @param minLayer the minimum number of layers in each graph
  22. * @param maxLayer the maximum number of layers in each graph
  23. * @param minNodePerLayer the minimum number of layers in each layer
  24. * @param maxNodePerLayer the maximum number of layers in each layer
  25. * @param maxDepth the maximum hierarchy depth
  26. */
  27. public RandomGraphGenerator( double pSubgraph, double pEdge, int minLayer, int maxLayer, int minNodePerLayer, int maxNodePerLayer, int maxDepth )
  28. {
  29. this.pSubgraph = pSubgraph;
  30. this.pEdge = pEdge;
  31. this.minLayer = minLayer;
  32. this.maxLayer = maxLayer;
  33. this.minNodePerLayer = minNodePerLayer;
  34. this.maxNodePerLayer = maxNodePerLayer;
  35. this.maxDepth = maxDepth;
  36. }
  37. /**
  38. * creates a random {@link LayeredGraphNode} using the parameters given to the constructor of the {@link RandomGraphGenerator}
  39. * @param parent the parent node of the nodes in the graph
  40. * @param depth the current depth of the graph (used to check if the maximum depth is reached)
  41. * @return the generated {@link LayeredGraphNode}
  42. */
  43. public LayeredGraphNode createRandomNode( LayeredGraphNode parent, int depth )
  44. {
  45. LayeredGraphNode node = new LayeredNode( null, null );
  46. if( parent != null )
  47. node = parent.createNode( null );
  48. if( ( Math.random() <= pSubgraph && depth < maxDepth ) || depth == 0 )
  49. {
  50. int index = 0;
  51. int layer = (int)( Math.random() * ( maxLayer - minLayer ) ) + minLayer;
  52. for( int i = 0; i < layer; i++ )
  53. {
  54. int knoten = (int)( Math.random() * ( maxNodePerLayer - minNodePerLayer ) ) + minNodePerLayer;
  55. for( int j = 0; j < knoten; j++ )
  56. {
  57. LayeredGraphNode n = createRandomNode( node, depth + 1 );
  58. n.setLayer( i );
  59. if( i > 0 )
  60. {
  61. for( LayeredGraphNode n2 : node.getContainedLayers().get( i - 1 ) )
  62. {
  63. if( Math.random() <= pEdge )
  64. node.createSimpleEdge( null, n2, n );
  65. }
  66. }
  67. n.setName( "" + (index+1) );
  68. index++;
  69. }
  70. }
  71. }
  72. return node;
  73. }
  74. }