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; }
public Edge(Vertex inPath, Vertex outPath, int cost) { In = inPath; Out = outPath; Cost = cost; Id = EdgeCount++; }
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; }
Vertex getUnvisitedChildVertex(Vertex n) { foreach (Edge e in n.Edges) { if (!e.Out.visited && !e.visited) { e.visited = true; Paths.Add(depthFirstSearch(e.Out)); return e.Out; } } return null; }