public Route GetShortestPath(Vertex from, Vertex to) { ResetExploration(); var queue = new PriorityQueue<double, Route>(); queue.Enqueue(0, new Route { @from }); while (!queue.IsEmpty) { var route = queue.Dequeue(); if (route.Last().IsExplored) continue; if (route.Last().Equals(to)) return route; route.Last().IsExplored = true; foreach (Edge n in route.Last().AdjacentEdges) { var newRoute = route.Clone(); newRoute.Add(n.Tail); queue.Enqueue(newRoute.Cost, newRoute); } } return null; }
public Algorithm(List<List<int>> input) { // Create cities cities = new Vertex[input[0][0]]; for (int i = 0; i < input[0][0]; i++) { cities[i] = new Vertex(); } // Create ways ways = new List<Edge>(); // and other stuff linked = new List<int>(); done = new List<int>(); routes = new List<List<int>>(); for (int i = 1; i < input.Count; i++) { Edge way = new Edge(); way.weight = input[i][0]; way.u = input[i][1]; way.v = input[i][2]; ways.Add(way); } }
/* * printing result to file * city: Vertex.id, length: Vertex.d, route: Vertex.id > ... > Vertex.id */ public void printResults(string file, Vertex[] cities, List<List<int>> routes) { FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); for (int i = 0; i < cities.Count(); i++) { string route = ""; foreach (int cid in routes[i]) { route += " > "; route += cid; } sw.WriteLine("city: " + cities[i].id + ", length: " + cities[i].d + ", route: " + cities[i].id + route); } sw.Close(); fs.Close(); }
public Edge(Vertex head, Vertex tail, int cost) { Head = head; Tail = tail; Cost = cost; }
public Edges(Vertex node1, Vertex node2, int cost) { this.node1 = node1; this.node2 = node2; this.cost = cost; }
public void AddEdge(Vertex node1, Vertex node2,int cost) { //Adds edges to nodes Edges connect = new Edges(node1, node2, cost); Edges connect2 = new Edges(node2, node1, cost); edges.Add(connect); edges.Add(connect2); }
public void Add(string x) { //Adds nodes to graph Vertex node = new Vertex(x); vertex.Add(node); }