|
| 1 | +# PriorityQueue |
| 2 | + |
| 3 | +Priority queue implementations in C# |
| 4 | + |
| 5 | +## Table of contents |
| 6 | + |
| 7 | +* [Features](#features) |
| 8 | +* [Example usage](#example-usage) |
| 9 | +* [Implementation comparison](#implementation-comparison) |
| 10 | +* [Time complexity](#time-complexity) |
| 11 | +* [TODO](#todo) |
| 12 | +* [Sources](#sources) |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +* Two default implementations of the priority queue data structure |
| 17 | +* Easy way to create your own implementation by implementing the `IPriorityQueue` interface |
| 18 | + |
| 19 | +## Example usage |
| 20 | + |
| 21 | +First define a class that implements `IPriorityElemennt` interface, which will be used in our queue. |
| 22 | +```cs |
| 23 | +class Element : IPriorityElement |
| 24 | +{ |
| 25 | + public string Name { get; set; } |
| 26 | + |
| 27 | + // required property |
| 28 | + public float Priority { get; set; } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Then we can create a new priority queue using one of the two default implementations |
| 33 | +```cs |
| 34 | +// Create a new instance of BinaryHeapPriorityQueue |
| 35 | +IPriorityQueue<Element> myPriorityQueue = new BinaryHeapPriorityQueue<Element>(); |
| 36 | +// Insert some elements |
| 37 | +myPriorityQueue.Enqueue(new Element { Priority = 5, Name = "A" }); |
| 38 | +myPriorityQueue.Enqueue(new Element { Priority = 7, Name = "B" }); |
| 39 | +myPriorityQueue.Enqueue(new Element { Priority = 4, Name = "C" }); |
| 40 | +// Get the top element (one with the highest priority value) and remove it |
| 41 | +myPriorityQueue.Dequeue(); // Name: "B", Priority: 7 |
| 42 | +// Get the top element's value without removing it |
| 43 | +myPriorityQueue.Peek(); // Name: "A", Priority: 5; |
| 44 | +// Get the top element again, this time it will be removed |
| 45 | +myPriorityQueue.Dequeue(); // Name: "A", Priority: 5 |
| 46 | +// Clear all remaining elements |
| 47 | +myPriorityQueue.Clear(); |
| 48 | +myPriorityQueue.IsEmpty(); // true |
| 49 | +``` |
| 50 | + |
| 51 | +## Implementation comparison |
| 52 | + |
| 53 | +There are two deafult implementations: `BinaryHeapPriorityQueue` and `MappedBinaryHeapPriorityQueue`. Both use a [binary heap](https://en.wikipedia.org/wiki/Binary_heap) as their underyling data structure, but the latter also stores all elements in a dictionary for faster lookup and element removal at the expense of slight memory and computational overhead. |
| 54 | + |
| 55 | +## Time complexity |
| 56 | + |
| 57 | +|Operation|BinaryHeapPriorityQueue|MappedBinaryHeapPriorityQueue |
| 58 | +|---|---|---| |
| 59 | +|Peek|O(1)|O(1)| |
| 60 | +|Enqueue|O(log n)|O(log n)| |
| 61 | +|Dequeue|O(log n)|O(log n)| |
| 62 | +|IsEmpty|O(1)|O(1)| |
| 63 | +|Remove|O(n)|O(1)| |
| 64 | +|Contains|O(n)|O(1)| |
| 65 | +|Clear|O(n)|O(n)| |
| 66 | + |
| 67 | +## TODO |
| 68 | + |
| 69 | +* Add a fibonnaci heap based implementation |
| 70 | +* Add tests |
| 71 | + |
| 72 | +## Sources |
| 73 | +Inspired by [WilliamFiset](https://www.youtube.com/channel/UCD8yeTczadqdARzQUp29PJw)'s [playlist](https://www.youtube.com/watch?v=wptevk0bshY&list=PLDV1Zeh2NRsCLFSHm1nYb9daYf60lCcag&index=1) explaining how priority queues work, along with sample implementation code |
| 74 | + |
| 75 | +[Priority queue on Wikipedia](https://en.wikipedia.org/wiki/Priority_queue) |
| 76 | + |
| 77 | +[Binary heap on Wikipedia](https://en.wikipedia.org/wiki/Binary_heap) |
0 commit comments