Skip to content

Commit dd768f1

Browse files
authored
Create README.md
1 parent 7f1775c commit dd768f1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
* [External Links](#links)
12+
13+
## Features
14+
15+
* Two default implementations of the priority queue data structure
16+
* Easy way to create your own implementation by implementing the `IPriorityQueue` interface
17+
18+
## Example usage
19+
20+
First define a class that implements `IPriorityElemennt` interface, which will be used in our queue.
21+
```cs
22+
class Element : IPriorityElement
23+
{
24+
public string Name { get; set; }
25+
26+
// required property
27+
public float Priority { get; set; }
28+
}
29+
```
30+
31+
Then we can create a new priority queue using one of the two default implementations
32+
```cs
33+
// Create a new instance of BinaryHeapPriorityQueue
34+
IPriorityQueue<Element> myPriorityQueue = new BinaryHeapPriorityQueue<Element>();
35+
// Insert some elements
36+
myPriorityQueue.Enqueue(new Element { Priority = 5, Name = "A" });
37+
myPriorityQueue.Enqueue(new Element { Priority = 7, Name = "B" });
38+
myPriorityQueue.Enqueue(new Element { Priority = 4, Name = "C" });
39+
// Get the top element (one with the highest priority value) and remove it
40+
myPriorityQueue.Dequeue(); // Name: "B", Priority: 7
41+
// Get the top element's value without removing it
42+
myPriorityQueue.Peek(); // Name: "A", Priority: 5;
43+
// Get the top element again, this time it will be removed
44+
myPriorityQueue.Dequeue(); // Name: "A", Priority: 5
45+
// Clear all remaining elements
46+
myPriorityQueue.Clear();
47+
myPriorityQueue.IsEmpty(); // true
48+
```
49+
50+
## Implementation comparison
51+
52+
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.
53+
54+
## Time complexity
55+
56+
|Operation|BinaryHeapPriorityQueue|MappedBinaryHeapPriorityQueue
57+
|---|---|---|
58+
|Peek|O(1)|O(1)|
59+
|Enqueue|O(log n)|O(log n)|
60+
|Dequeue|O(log n)|O(log n)|
61+
|IsEmpty|O(1)|O(1)|
62+
|Remove|O(n)|O(1)|
63+
|Contains|O(n)|O(1)|
64+
|Clear|O(n)|O(n)|
65+
66+
## Links
67+
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
68+
69+
[Priority queue on Wikipedia](https://en.wikipedia.org/wiki/Priority_queue)
70+
71+
[Binary heap on Wikipedia](https://en.wikipedia.org/wiki/Binary_heap)

0 commit comments

Comments
 (0)