public static void TestPriorityQueue() { Random r = new Random(); int size = 7; int range = 100; PriorityQueue q = new PriorityQueue(size); Console.WriteLine("-----Test INSERT-----"); for (int i = 0; i < size; i++) { double val = r.NextDouble() * range; if (!q.Insert(i, val)) { Console.WriteLine("ERROR on index " + i + " with value " + val.ToString("#.##")); return; } Console.WriteLine("inserted item " + i + " with value " + val.ToString("#.##")); Console.WriteLine("queue:\n" + q.ToString()); } Console.WriteLine(); Console.WriteLine("-----Test REDUCE_VAL-----"); for (int i = 0; i < size; i++) { double val = r.NextDouble() * range; if (q.ReduceVal(i, val)) { Console.WriteLine("update item " + i + " with value " + val.ToString("#.##")); Console.WriteLine("queue:\n" + q.ToString()); } else { Console.WriteLine("ERROR updating index " + i + " with value " + val.ToString("#.##")); } } Console.WriteLine(); Console.WriteLine("-----Test GET_VAL-----"); for (int i = 0; i < size; i++) { Console.WriteLine("Node " + i + " has a value of " + q.GetVal(i).ToString("#.##")); } Console.WriteLine(); Console.WriteLine("-----Test POP_MIN-----"); for (int i = 0; i < size; i++) { int id = q.PopMin(); Console.WriteLine("popped " + id + " off the queue"); Console.WriteLine("queue:\n" + q.ToString()); } Console.WriteLine("ALL DONE"); }
public double AllPathDijkstra(Stopwatch s) { int n_nodes = points.Count(); PriorityQueue q = new PriorityQueue(n_nodes); double[] src_dist = new double[n_nodes]; int[] prev = new int[n_nodes]; int i; s.Start(); for (i = 0; i < n_nodes; i++) { src_dist[i] = double.MaxValue; prev[i] = -1; q.Insert(i, src_dist[i]); } src_dist[src] = 0; q.ReduceVal(src, src_dist[src]); while (!q.IsEmpty()) { int id = q.PopMin(); HashSet <int> adj_nodes = adjacencyList[id]; for (i = 0; i < adj_nodes.Count(); i++) { int temp_id = adj_nodes.ElementAt(i); double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]); //Console.WriteLine("Distance from " + id + " to " + temp_id + " is " + temp_dist); if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance { src_dist[temp_id] = temp_dist; prev[temp_id] = id; if (!q.ReduceVal(temp_id, temp_dist)) { Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id); return(1); } //Console.WriteLine("queue:\n" + q.ToString()); } } } s.Stop(); return(src_dist[dst]); }
public double AllPathDijkstra(Stopwatch s) { int n_nodes = points.Count(); PriorityQueue q = new PriorityQueue(n_nodes); double[] src_dist = new double[n_nodes]; int[] prev = new int[n_nodes]; int i; s.Start(); for (i = 0; i < n_nodes; i++) { src_dist[i] = double.MaxValue; prev[i] = -1; q.Insert(i, src_dist[i]); } src_dist[src] = 0; q.ReduceVal(src, src_dist[src]); while (!q.IsEmpty()) { int id = q.PopMin(); HashSet<int> adj_nodes = adjacencyList[id]; for (i = 0; i < adj_nodes.Count(); i++) { int temp_id = adj_nodes.ElementAt(i); double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]); //Console.WriteLine("Distance from " + id + " to " + temp_id + " is " + temp_dist); if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance { src_dist[temp_id] = temp_dist; prev[temp_id] = id; if (!q.ReduceVal(temp_id, temp_dist)) { Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id); return 1; } //Console.WriteLine("queue:\n" + q.ToString()); } } } s.Stop(); return src_dist[dst]; }
public double OnePathDijkstra(Stopwatch s) { int n_nodes = points.Count(); PriorityQueue q = new PriorityQueue(n_nodes); double[] src_dist = new double[n_nodes]; int[] prev = new int[n_nodes]; int i; s.Start(); for (i = 0; i < n_nodes; i++) { src_dist[i] = double.MaxValue; prev[i] = -1; } src_dist[src] = 0; prev[src] = -1; q.Insert(src, src_dist[src]); while (!q.IsEmpty()) { int id = q.PopMin(); if (id == dst) { break; } HashSet <int> adj_nodes = adjacencyList[id]; for (i = 0; i < adj_nodes.Count(); i++) { int temp_id = adj_nodes.ElementAt(i); double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]); if (src_dist[temp_id] == double.MaxValue) // If the node has NOT been visited { src_dist[temp_id] = temp_dist; prev[temp_id] = id; if (!q.Insert(temp_id, src_dist[temp_id])) { Console.WriteLine("ERROR inserting id " + temp_id + " connected to id " + id); return(1); } } else if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance { src_dist[temp_id] = temp_dist; prev[temp_id] = id; if (!q.ReduceVal(temp_id, temp_dist)) { Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id); return(1); } } } } s.Stop(); if (src_dist[dst] != double.MaxValue) { Pen pen = new Pen(Color.Black); Font font = new Font("Arial", 8); SolidBrush brush = new SolidBrush(Color.Black); int temp = dst; while (prev[temp] != -1) { PointF p1 = points[temp]; PointF p2 = points[prev[temp]]; graphics.DrawLine(pen, p1, p2); graphics.DrawString(GetDist(p1, p2).ToString("#.##"), font, brush, GetMidPoint(p1, p2)); temp = prev[temp]; } pictureBox.Refresh(); } else { Console.WriteLine("Destination is unreachable"); } return(src_dist[dst]); }
public double OnePathDijkstra(Stopwatch s) { int n_nodes = points.Count(); PriorityQueue q = new PriorityQueue(n_nodes); double[] src_dist = new double[n_nodes]; int[] prev = new int[n_nodes]; int i; s.Start(); for (i = 0; i < n_nodes; i++) { src_dist[i] = double.MaxValue; prev[i] = -1; } src_dist[src] = 0; prev[src] = -1; q.Insert(src, src_dist[src]); while (!q.IsEmpty()) { int id = q.PopMin(); if (id == dst) { break; } HashSet<int> adj_nodes = adjacencyList[id]; for (i = 0; i < adj_nodes.Count(); i++) { int temp_id = adj_nodes.ElementAt(i); double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]); if (src_dist[temp_id] == double.MaxValue) // If the node has NOT been visited { src_dist[temp_id] = temp_dist; prev[temp_id] = id; if (!q.Insert(temp_id, src_dist[temp_id])) { Console.WriteLine("ERROR inserting id " + temp_id + " connected to id " + id); return 1; } } else if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance { src_dist[temp_id] = temp_dist; prev[temp_id] = id; if (!q.ReduceVal(temp_id, temp_dist)) { Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id); return 1; } } } } s.Stop(); if (src_dist[dst] != double.MaxValue) { Pen pen = new Pen(Color.Black); Font font = new Font("Arial", 8); SolidBrush brush = new SolidBrush(Color.Black); int temp = dst; while (prev[temp] != -1) { PointF p1 = points[temp]; PointF p2 = points[prev[temp]]; graphics.DrawLine(pen, p1, p2); graphics.DrawString(GetDist(p1, p2).ToString("#.##"), font, brush, GetMidPoint(p1, p2)); temp = prev[temp]; } pictureBox.Refresh(); } else { Console.WriteLine("Destination is unreachable"); } return src_dist[dst]; }