publicstatic UndirectedGraph createAtomicGraph(int graphSize) { UndirectedGraph returnGraph = new SimpleGraph(DefaultEdge.class); String[] v = new String[graphSize+1]; int vertexDegree, targetVertex; ArrayList vertex = new ArrayList(); /* * Here we are populating the graph we will be returning with the string * names of each vertex, this will need to be changed when we go on to * make it a graph of vertex Class Verteices instead of strings */ for (int i = 0; i < v.length; i++) { v[i]= "v"+i; vertex.add(new Vertex(v[i])); returnGraph.addVertex(vertex.get(i)); }// end for(populate the return graph if(graphSize == 1) return returnGraph; /* * Next we will create the graph given the graphSize graphSize will be * given by user, minus one so we off set the difference in where we start to count */ for (int currentVertex = 0; currentVertex <= graphSize; currentVertex++) { /* * first we must determine the degree of the current vertex why * minus two you ask? Well, we are asserting we do not want a self * loop therefore it can point to any vertex in the graph * "graphSize" minus it's self therefore graphSize minus 1 */ vertexDegree = randInt(1, graphSize-1); /* * Now that we have determined the degree we want to have on the * current vertex we must now find other vertices for our vertex to * "point to". * * we want to make sure we don't get into a infinite loop, if you * look at the old code it appears that we can get into this kind of * situation when we are insisting on giving a node a high degree * when it may already have a lot of vertex pointing at it */ if(returnGraph.degreeOf(vertex.get(currentVertex)) >= vertexDegree){ /* * The degree of the current vertex is greater than (or '=') the degree * we want to assign to it, therefore we will leave it alone and * move onto the next vertex. */ } /* * The degree of the current vertex is less than the degree we wish to assign it * therefore we can add the difference between the two vertexes to the current one */ elseif (returnGraph.degreeOf(vertex.get(currentVertex)) < vertexDegree) { /* * NOTE: observe that we run this for loop for the difference between * the degree we wish to have and the degree that current one has */ for(int j = 0; j <= Math.abs((returnGraph.degreeOf(vertex.get(currentVertex))-vertexDegree));j++){ targetVertex = randInt(0,graphSize - 1); /* * We generate a random number to have the current vertex connect to, * we decrement by one for the fact we do not * want self loops, note this is of the possible vertex, we still need * to check and make sure it doesn't violate 1) no self loops and 2) * no more than one edge between two given nodes */ /* * If we generate the same vertex as the current vertex or the the edge already exist * we must generate a new target vertex, then check again that is is valid */ while(targetVertex == currentVertex || returnGraph.containsEdge(vertex.get(currentVertex), vertex.get(targetVertex)) || returnGraph.containsEdge(vertex.get(targetVertex), vertex.get(currentVertex))){ targetVertex = randInt(0,graphSize - 1); }//end while loop /* * We have no confiremd that the edge we wish to create.. * 1) is not a self loop * 2) does not already exist * 3) does not give too high of a vertex degree to our current vertex * Therefore we can add the vertex to the graph */ returnGraph.addEdge(vertex.get(currentVertex),vertex.get(targetVertex)); }//end for loop }//end else if current degree less than degree we desire }// end for loop outer, the current vertex /* * Now we cycle the graph once more to make sure there are no nodes with no edges */ for(int i = 0; i <= graphSize; i ++){ if(returnGraph.degreeOf(vertex.get(i)) == 0){ returnGraph.removeVertex(vertex.get(i)); }//end if }// end for loop, make sure there's not any degree of zero's return returnGraph; }//end create an atomic graph