Example #1
0
        public Path depthFirstSearch(Vertex vert)
        {
            Stack<Vertex> s = new Stack<Vertex>();
            Path p = new Path();
            p.Distance = 0;
            p.StartVertex = vert;
            vert.visited = true;
            s.Push(vert);
            while (s.Count > 0)
            {
                Vertex n = (Vertex)s.Peek();
                Vertex child = getUnvisitedChildVertex(n);
                if (child != null)
                {
                    child.visited = true;
                    s.Push(child);
                    var m = (from e in Edges where e.Value.InName == n.Name && e.Value.OutName == child.Name select e).FirstOrDefault();
                    if (!m.Value.Equals(null)) { p.Distance += m.Value.Cost; }
                    p.Vertices.Add(child);
                    Console.Write(p.Distance);
                }
                else
                {
                    s.Pop();
                }
            }
            clearNodes();

            return p;
        }
Example #2
0
        private Vertex greedyNodeGrabber(Path p, Vertex vertex)
        {
            Edge grabbedNodeEdge = new Edge();

            foreach (KeyValuePair<int, Edge> kvp in Edges)
            {
                if (kvp.Value.In == vertex)
                {
                    if (kvp.Value.Cost < grabbedNodeEdge.Cost)
                    {
                        grabbedNodeEdge = kvp.Value;
                    }
                }
            }

            p.Distance += grabbedNodeEdge.Cost;

            return grabbedNodeEdge.Out;
        }