private void printPath(int[] path, int start, int end, wightedgraph G, double dis) { int v1 = 0; int v = 0; double mshy1 = 0; double mshy2 = 0; double result = 0; //prints a path, given a start and end, and an array that holds previous //nodes visited d1.path = new List <int>(); int temp = end; Stack <int> s = new Stack <int>(); while (temp != start) { s.Push(temp); temp = path[temp]; } int c = 0; int f = s.Count; while (f != 0) { int rkm = s.Pop(); f--; if (c == 0) { v = rkm; c++; } if (f == 1) { v1 = rkm; } d1.path.Add(rkm); } int c2 = G.graph[0].nieghbourr.Count(); for (int i = 0; i < c2; i++) { if (G.graph[0].nieghbourr[i].destnode == v) { mshy1 = G.graph[0].nieghbourr[i].w.distance; } } int c1 = G.graph[G.graph.Count() - 1].nieghbourr.Count(); for (int i = 0; i < c1; i++) { if (G.graph[G.graph.Count() - 1].nieghbourr[i].destnode == v1) { mshy2 = G.graph[G.graph.Count() - 1].nieghbourr[i].w.distance; } } result = mshy1 + mshy2; d1.walkingdistance = result; d1.cardistance = dis - result; }
public List <data> runqueries(string querynom, List <vertex> nlistofpoints) { List <data> qeuriesdata = new List <data>(); queries QE = new queries(); FileStream fss = new FileStream(querynom, FileMode.Open, FileAccess.Read); StreamReader srr = new StreamReader(fss); int countQ = Int32.Parse(srr.ReadLine()); using (StreamReader reader = File.OpenText(querynom)) { reader.ReadLine(); for (int i = 0; i < countQ; i++) { string text = reader.ReadLine(); string[] bitss = text.Split(' '); QE.startX = float.Parse(bitss[0]); QE.startY = float.Parse(bitss[1]); QE.endX = float.Parse(bitss[2]); QE.endY = float.Parse(bitss[3]); QE.rad = float.Parse(bitss[4]); vertex v = new vertex(); v.x = QE.startX; v.y = QE.startY; v.index = 0; nlistofpoints.Insert(0, v); vertex vv = new vertex(); vv.x = QE.endX; vv.y = QE.endY; vv.index = nlistofpoints.Count(); nlistofpoints.Add(vv); int cc = nlistofpoints.Count(); for (int j = 0; j < nlistofpoints.Count(); j++) { double result = Math.Pow(Math.Pow((nlistofpoints[j].x - QE.startX), 2) + Math.Pow((nlistofpoints[j].y - QE.startY), 2), 0.5); if (QE.rad / 1000 >= result) { weightededge n = new weightededge(0, j, (result / 5), 5, result); nieghbours ne = new nieghbours(j, n); nlistofpoints[0].nieghbourr.Add(ne); weightededge nn = new weightededge(j, 0, (result / 5), 5, result); nieghbours nee = new nieghbours(0, nn); nlistofpoints[j].nieghbourr.Add(nee); } if (j != nlistofpoints.Count() - 1) { double result1 = Math.Pow(Math.Pow((nlistofpoints[j].x - QE.endX), 2) + Math.Pow((nlistofpoints[j].y - QE.endY), 2), 0.5); if (QE.rad / 1000 >= result1) { weightededge n = new weightededge((nlistofpoints.Count() - 1), j, (result1 / 5), 5, result1); nieghbours ne = new nieghbours(j, n); nlistofpoints[(nlistofpoints.Count() - 1)].nieghbourr.Add(ne); weightededge nn = new weightededge(j, (nlistofpoints.Count() - 1), (result1 / 5), 5, result1); nieghbours nee = new nieghbours((nlistofpoints.Count() - 1), nn); nlistofpoints[j].nieghbourr.Add(nee); } } } wightedgraph g = new wightedgraph(nlistofpoints); dikstra DS = new dikstra(); Stopwatch sw = new Stopwatch(); sw.Start(); data qeurydata = DS.rundijkstra(g.graph.First().index, g.graph.Last().index, g); sw.Stop(); long lon = sw.ElapsedMilliseconds; sw.Reset(); qeurydata.elapsed = lon; qeuriesdata.Add(qeurydata); for (int m = 0; m < nlistofpoints.Count(); m++) { for (int n = 0; n < nlistofpoints[m].nieghbourr.Count(); n++) { if (nlistofpoints[m].nieghbourr[n].destnode == nlistofpoints.First().index || nlistofpoints[m].nieghbourr[n].destnode == nlistofpoints.Last().index) { nlistofpoints[m].nieghbourr.RemoveAt(n); } } } nlistofpoints.Remove(nlistofpoints.First()); nlistofpoints.Remove(nlistofpoints.Last()); } } //Console.Write(sw.ElapsedMilliseconds + " \n"); return(qeuriesdata); }
public data rundijkstra(int src, int goal, wightedgraph G) { int peforetime = DateTime.Now.Hour; double[] d = new double[G.graph.Count]; double[] dd = new double[G.graph.Count]; bool[] visited = new bool[G.graph.Count]; int[] parent = new int[G.graph.Count]; List <int> ind = new List <int>(); SortedSet <vertex> q = new SortedSet <vertex>(); for (int i = 0; i < G.graph.Count; i++) { d[i] = i == src ? 0 : int.MaxValue; dd[i] = i == src ? 0 : int.MaxValue; } q.Add(new vertex(src, 0, 0, G.graph[G.graph.IndexOf(G.graph.First())].nieghbourr)); ind.Add(0); parent[src] = src; int t = 0; while (q.Count != 0) { int u = q.Min().index; e = q.Min.exertedtime; q.Remove(q.Min()); for (int i = 0; i < lv.Count; i++) { if (e == lv[i].exertedtime && b == true) { q.Add(lv[i]); lv.RemoveAt(i); b = false; break; } } int x = 0; int z = G.graph[u].nieghbourr.Count(); for (int i = 0; i <= z - 1; i++) { int v = G.graph[u].nieghbourr[x].destnode; //index of current u double weight = G.graph[u].nieghbourr[x].w.timecost; //wieght of the next edge double distance = G.graph[u].nieghbourr[x].w.distance; if (d[v] > d[u] + weight) { if (d[v] != int.MaxValue) { q.Remove(new vertex(v, d[v], dd[v], G.graph[v].nieghbourr)); } // Updating distance of v d[v] = d[u] + weight; G.graph[v].exertedtime = d[v]; dd[v] = dd[u] + distance; parent[v] = u; if (q.Add(new vertex(v, d[v], dd[v], G.graph[v].nieghbourr)) == false) { vv = new vertex(v, d[v], dd[v], G.graph[v].nieghbourr); lv.Add(vv); b = true; } } else if (d[v] == d[u] + weight) { if (dd[v] > dd[u] + distance) { if (dd[v] != int.MaxValue) { q.Remove(new vertex(v, d[v], dd[v], G.graph[v].nieghbourr)); } // Updating distance of v d[v] = d[u] + weight; dd[v] = dd[u] + distance; parent[v] = u; if (q.Add(new vertex(v, d[v], dd[v], G.graph[v].nieghbourr)) == false) { vv = new vertex(v, d[v], dd[v], G.graph[v].nieghbourr); lv.Add(vv); b = true; } } } t++; x++; } } d1.time = d[goal] * 60; d1.totaldistance = dd[goal]; printPath(parent, 0, G.graph.Count - 1, G, dd[goal]); return(d1); }