//Methods whos primary purpose is to assist other methods public Edge firstBackTrackEdge(Graph g, List <Edge> path) { Edge lastMove = path[path.Count - 1]; path.Remove(lastMove); g.addBackEdgeSet(lastMove); g.addBackOtherEdges(lastMove.vertexTwo(), lastMove, coveredVertices(path)); g.addBackVertex(lastMove.vertexTwo()); Edge chosenEdge = selectShortestEdge(g, lastMove.vertexOne()); while (chosenEdge == null) { lastMove = path[path.Count - 1]; path.Remove(lastMove); g.addBackEdgeSet(lastMove); g.addBackVertex(lastMove.vertexTwo()); chosenEdge = selectShortestEdge(g, lastMove.vertexOne()); g.addBackOtherEdges(lastMove.vertexTwo(), lastMove, coveredVertices(path)); } return(chosenEdge); }
public string GeneralAlgorithm(Graph g) { DateTime start = DateTime.Now; Graph gCopy = g.Copy(); bool backTrack = false; List <Edge> path = new List <Edge>(); Edge startEdge = selectShortestEdge(gCopy, 1); justRemovedEdge = startEdge; path.Add(startEdge); gCopy.removeEdges(gCopy.listEdges(1)); gCopy.removeVertex(justRemovedEdge.vertexOne()); gCopy.removeVertex(justRemovedEdge.vertexTwo()); gCopy.removeEdge(startEdge); while (gCopy.numberOfVerticesRemaining() > 0) { Edge chosenEdge = null; if (!backTrack) { chosenEdge = selectShortestEdge(gCopy, justRemovedEdge.vertexTwo()); } else { chosenEdge = firstBackTrackEdge(gCopy, path); if (path.Count > 0) { justRemovedEdge = path[path.Count - 1]; } } if (chosenEdge != null) { path.Add(chosenEdge); gCopy.removeEdges(gCopy.listEdges(justRemovedEdge.vertexTwo())); gCopy.removeVertex(chosenEdge.vertexOne()); gCopy.removeVertex(chosenEdge.vertexTwo()); gCopy.removeEdge(chosenEdge); justRemovedEdge = chosenEdge; backTrack = false; } else if (gCopy.numberOfVerticesRemaining() > 0) { backTrack = true; } } DateTime stop = DateTime.Now; TimeSpan timeDiff = (stop - start); string outputString = String.Format("Time Taken: {0}:{1}:{2}:{3}\n", timeDiff.Days, timeDiff.Hours, timeDiff.Minutes, timeDiff.Seconds) + "Path: " + printEdges(path) + "\nDistance: " + pathDistance(path); return(outputString); }
public List <Edge> findRandomHamiltonianPath(Graph g, int startVertex) { //Make copy of the copy to pass into another run if the the current run fails Graph gCopy = g.Copy(); List <Edge> path = new List <Edge>(); Edge startEdge = selectRandomEdge(gCopy, startVertex); justRemovedEdge = startEdge; path.Add(startEdge); while (gCopy.numberOfVerticesRemaining() > 0) { Edge chosenEdge = selectRandomEdge(gCopy, justRemovedEdge.vertexTwo()); if (chosenEdge != null) { path.Add(chosenEdge); justRemovedEdge = chosenEdge; } else { //Redo if vertices remain untouched return(findRandomHamiltonianPath(g, startVertex)); } } return(path); }
public string Greedy(Graph g) { DateTime start = DateTime.Now; List <Edge> eulerCircuit = new List <Edge>(); List <Edge> finalEulerCircuit = new List <Edge>(); List <List <Edge> > iterations = new List <List <Edge> >(); bool firstVertexEntered = false; //Gets the first vertex of circuit as vertexOne. The rest should all be vertexTwo. while (g.numberofEdgesRemaining() > 0) { if (!firstVertexEntered) { //Finds first edge available from the vertices in the current eu appendEulerCircuit(g, eulerCircuit, g.findFirstRemainingVertex(finalEulerCircuit)); finalEulerCircuit.Clear(); firstVertexEntered = true; } else if (g.listEdges(justRemovedEdge.vertexTwo()).Count > 0) { appendEulerCircuit(g, eulerCircuit, justRemovedEdge.vertexTwo()); } else { appendEulerCircuit(g, eulerCircuit, justRemovedEdge.vertexTwo()); iterations.Add(new List <Edge>(eulerCircuit)); finalEulerCircuit = insertIterations(iterations); eulerCircuit.Clear(); firstVertexEntered = false; } } iterations.Add(new List <Edge>(eulerCircuit)); //eulerCircuit.Clear(); //eulerCircuit = insertIterations(iterations); finalEulerCircuit.Clear(); finalEulerCircuit = insertIterations(iterations); DateTime stop = DateTime.Now; TimeSpan timeDiff = (stop - start); string outputString = String.Format("Time Taken: {0}:{1}:{2}:{3}\n", timeDiff.Days, timeDiff.Hours, timeDiff.Minutes, timeDiff.Seconds) + "Euler Circuit: " + printEdges(finalEulerCircuit) + "\nDistance: " + pathDistance(finalEulerCircuit); return(outputString); }
public Edge selectEdge(List <Edge> edgeList, int index) { Edge e = edgeList[index]; removeEdges(edgeList); removeVertex(e.vertexOne()); removeVertex(e.vertexTwo()); return(e); }