-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PriorityQueue.Remove (#93994)
* Implement PriorityQueue.Remove * Update src/libraries/System.Collections/src/System/Collections/Generic/PriorityQueue.cs * Update src/libraries/System.Collections/src/System/Collections/Generic/PriorityQueue.cs Co-authored-by: Dan Moseley <[email protected]> * Update src/libraries/System.Collections/src/System/Collections/Generic/PriorityQueue.cs * Update src/libraries/System.Collections/src/System/Collections/Generic/PriorityQueue.cs * Update src/libraries/System.Collections/src/System/Collections/Generic/PriorityQueue.cs Co-authored-by: Stephen Toub <[email protected]> * Address feedback. * Address feedback * Add a Dijkstra smoke test. * Alias distance type --------- Co-authored-by: Dan Moseley <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
- Loading branch information
1 parent
44a5abd
commit c1f4341
Showing
6 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/libraries/System.Collections/tests/Generic/PriorityQueue/PriorityQueue.Tests.Dijkstra.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
using NodeId = int; | ||
using Distance = int; | ||
|
||
namespace System.Collections.Tests | ||
{ | ||
public partial class PriorityQueue_NonGeneric_Tests | ||
{ | ||
public record struct Graph(Edge[][] nodes); | ||
public record struct Edge(NodeId neighbor, Distance weight); | ||
|
||
[Fact] | ||
public static void PriorityQueue_DijkstraSmokeTest() | ||
{ | ||
var graph = new Graph([ | ||
[new Edge(1, 7), new Edge(2, 9), new Edge(5, 14)], | ||
[new Edge(0, 7), new Edge(2, 10), new Edge(3, 15)], | ||
[new Edge(0, 9), new Edge(1, 10), new Edge(3, 11), new Edge(5, 2)], | ||
[new Edge(1, 15), new Edge(2, 11), new Edge(4, 6)], | ||
[new Edge(3, 6), new Edge(5, 9)], | ||
[new Edge(0, 14), new Edge(2, 2), new Edge(4, 9)], | ||
]); | ||
|
||
NodeId startNode = 0; | ||
|
||
(NodeId node, Distance distance)[] expectedDistances = | ||
[ | ||
(0, 0), | ||
(1, 7), | ||
(2, 9), | ||
(3, 20), | ||
(4, 20), | ||
(5, 11), | ||
]; | ||
|
||
(NodeId node, Distance distance)[] actualDistances = RunDijkstra(graph, startNode); | ||
|
||
Assert.Equal(expectedDistances, actualDistances); | ||
} | ||
|
||
public static (NodeId node, Distance distance)[] RunDijkstra(Graph graph, NodeId startNode) | ||
{ | ||
Distance[] distances = Enumerable.Repeat(int.MaxValue, graph.nodes.Length).ToArray(); | ||
var queue = new PriorityQueue<NodeId, Distance>(); | ||
|
||
distances[startNode] = 0; | ||
queue.Enqueue(startNode, 0); | ||
|
||
do | ||
{ | ||
NodeId nodeId = queue.Dequeue(); | ||
Distance nodeDistance = distances[nodeId]; | ||
|
||
foreach (Edge edge in graph.nodes[nodeId]) | ||
{ | ||
Distance distance = distances[edge.neighbor]; | ||
Distance newDistance = nodeDistance + edge.weight; | ||
if (newDistance < distance) | ||
{ | ||
distances[edge.neighbor] = newDistance; | ||
// Simulate priority update by attempting to remove the entry | ||
// before re-inserting it with the new distance. | ||
queue.Remove(edge.neighbor, out _, out _); | ||
queue.Enqueue(edge.neighbor, newDistance); | ||
} | ||
} | ||
} | ||
while (queue.Count > 0); | ||
|
||
return distances.Select((distance, nodeId) => (nodeId, distance)).ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters