public void TestKruskal() { float expected = 10; Graph graph = new Graph(); classicAlgorithms ca = new classicAlgorithms(); graph.addNode("_0"); graph.addNode("_1"); graph.addNode("_2"); graph.addNode("_3"); graph.addNode("_4"); graph.addEdge(3, "_0", "_4", "_1"); graph.addEdge(21, "_0", "_3", "_2"); graph.addEdge(3, "_1", "_2", "_3"); graph.addEdge(31, "_1", "_4", "_4"); graph.addEdge(2, "_2", "_4", "_5"); graph.addEdge(2, "_3", "_4", "_6"); var result = ca.kruskal(ref graph); Assert.AreEqual(expected, result.getGraphWeight()); }
//calculate the fitness public void calcFitness() { fitness = 0; bool actualtree = true; Graph temp = null; foreach (Edge e in chromosome) { //add to temporary graph to check if edges already exist if (temp == null) { temp = new Graph(); if (!temp.hasVertex(e.node1)) { temp.addNode(e.node1); } if (!temp.hasVertex(e.node2)) { temp.addNode(e.node2); } } if (!temp.hasVertex(e.node1)) { temp.addNode(e.node1); } if (!temp.hasVertex(e.node2)) { temp.addNode(e.node2); } //add edge to temp graph for comparisons if (temp.hasEdge(e)) { fitness += MaxFitness; actualtree = false; } if (!temp.hasEdge(e)) { temp.addEdge(e.weight, e.node1, e.node2, e.data); } //add the weight to fitness fitness += e.weight; } if (temp.GetEdges().Count != genes.Count - 1) { fitness += MaxFitness * 2; actualtree = false; } var ca = new classicAlgorithms(); Vertex startV = null; //chooese a start vertex that has more than 1 neighbour foreach (Vertex v in temp.GetVertices()) { if (v.neighbours.Count > 1) { startV = v; } } if (startV == null) { startV = temp.getRandomVertex(); } if (ca.checkCycle(temp, startV) && startV != null) { fitness += MaxFitness; actualtree = false; } if (actualtree == true) { fitness = temp.getGraphWeight(); } }