我正在尝试为平面图生成构建一些算法。我已经阅读了很多关于这个主题的材料,这里有一些结果:
public Graph graph;
...
public Level(int numPoints, int numEdges, int levelID, int timeLim)
{
this.graph = new Graph(numPoints, numEdges);
this.graph.GeneratePlanarGraph();
...
}
在这里,我写了一些构建平面图的函数......
public void GeneratePlanarGraph() {
if(this.edges_num < this.vertices_num) {
System.out.println("erroe: number of edges mast be\n "+
"bigger than number of vertices\n");
return;
}
int edges = 0;
// add vertices to graph
for(int i = 0 ; i < this.vertices_num ; i++)
this.AddVertex(i+1);
// connect all vertices to circular list
// ( from each vertex 2 connections )
for(int i = 1 ; i <= this.vertices_num ; i++) {
if(i < this.vertices_num) this.AddEdge(i, i+1);
else this.AddEdge(i, 1);
edges++;
}
//connect other edges if its exist
int step = 2;
while(edges < this.edges_num) {
for(int i = 1 ; i <= this.vertices_num ; i++) {
if(i + step <= this.vertices_num) {
this.AddEdge(i, i + step);
edges++;
if(edges == this.edges_num) break;
}
else {
this.AddEdge(i, (i + step) - this.vertices_num);
edges++;
break;
}
}
step++;
}
}
当我启动我的程序时,它会以这种方式生成许多平面图:
for(int i = 0 ; i < 100 ; i++)
{
levels[i] = new Level(numPoints, numEdges, levelID, timeLim);
}
所有这些代码都有效,但我发现在测试图形(级别)24、36、...时不是平面的。
我知道欧拉公式,所以我尝试通过欧拉公式生成我的顶点/边,但正如我所见,它并没有真正起作用。