Skip to content

Commit

Permalink
Djikstras uses priority queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmoth committed Jun 16, 2024
1 parent 81c1e34 commit 2427e95
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions DsaDotnet/Search/Dijkstra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static partial class Search

var distances = new Dictionary<WeightedGraphNode<U>, int>();
var previous = new Dictionary<WeightedGraphNode<U>, WeightedGraphNode<U>>();
var priorityQueue = new SortedDictionary<int, WeightedGraphNode<U>>(); // Stores nodes by distance
var priorityQueue = new PriorityQueue<WeightedGraphNode<U>, int>(); // Stores nodes by distance

WeightedGraphNode<U>? endNode = null;
// Set distance to each node as the maximum
Expand All @@ -51,24 +51,23 @@ public static partial class Search

// Distance to the start node is 0.
distances[startNode] = 0;
priorityQueue[0] = startNode;
priorityQueue.Enqueue(startNode, 0);

while (priorityQueue.Count > 0)
{
var current = priorityQueue.First();
priorityQueue.Remove(current.Key);
var current = priorityQueue.Dequeue();

foreach (var (weight, neighbor) in current.Value.GetWeightedNeighbors())
foreach (var (weight, neighbor) in current.GetWeightedNeighbors())
{
var newDistance = distances[current.Value] + weight;
var newDistance = distances[current] + weight;
if (newDistance >= distances[neighbor])
{
continue;
}

distances[neighbor] = newDistance;
previous[neighbor] = current.Value;
priorityQueue[newDistance] = neighbor;
previous[neighbor] = current;
priorityQueue.Enqueue(neighbor, newDistance);
}
}

Expand Down

1 comment on commit 2427e95

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Benchmark.Net Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 2427e95 Previous: 81c1e34 Ratio
Benchmarks.Search.DijkstraBenchmark.Dijkstra(N: 100) 276973.978021978 ns (± 16206.459585937413) 82258.23655913978 ns (± 15588.27700856017) 3.37
Benchmarks.Search.DijkstraBenchmark.Dijkstra(N: 1000) 16179987.413043479 ns (± 622525.4001143273) 394207.5869565217 ns (± 57502.89067806083) 41.04

This comment was automatically generated by workflow using github-action-benchmark.

CC: @Timmoth

Please sign in to comment.