//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);
        }
Ejemplo n.º 2
0
        public Edge selectEdge(List <Edge> edgeList, int index)
        {
            Edge e = edgeList[index];

            removeEdges(edgeList);
            removeVertex(e.vertexOne());
            removeVertex(e.vertexTwo());
            return(e);
        }
        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);
        }