Various important computer science algorithms generically implemented in C#
Install by nuget
For beta releases on beta branch
Install-Package Advanced.Algorithms -Pre
Not a stable release yet.
Supports
- .Net Standard 1.0 or above
- .Net Framework 4.0 or above
- Array List (dynamic array) (Implementation | Tests)
- Skip List (Implementation | Tests)
- HashSet (using Separate Chaining optionally Open Address Linear Probing) (Implementation | Tests)
- Tree HashSet (Implementation | Tests)
- Dictionary (using Separate Chaining optionally Open Address Linear Probing) (Implementation | Tests)
- Tree Dictionary (Implementation | Tests)
- Stack (using Dynamic Array and optionally using Singly Linked List) (Implementation | Tests)
- Queue (using Dynamic Array and optionally using Doubly Linked List) (Implementation | Tests)
- Min Priority Queue (Implementation | Tests)
- Max Priority Queue (Implementation | Tests)
- Singly linked list (Implementation | Tests)
- Doubly linked list (Implementation | Tests)
- Circular linked list (Implementation | Tests)
- Binary Min Heap (Implementation | Tests)
- d-ary Min Heap (Implementation | Tests)
- Binomial Min Heap (Implementation | Tests)
- Fibornacci Min Heap (Implementation | Tests)
- Pairing Min Heap (Implementation | Tests)
- Binary Max Heap (Implementation | Tests)
- d-ary Max Heap (Implementation | Tests)
- Binomial Max Heap (Implementation | Tests)
- Fibornacci Max Heap (Implementation | Tests)
- Pairing Max Heap (Implementation | Tests)
Note: It is observed that among the implementations here in practice, with the exclusion of DecrementKey/IncrementKey operation regular Binary Heap & d-ary Heap outperforms other in theory superiors. Likely because it don't have pointer juggling overhead and hey arrays are faster!
- Tree (Implementation | Tests)
- Binary Tree (Implementation | Tests)
- Binary Search Tree (Implementation | Tests)
- AVL Tree (Implementation | Tests)
- Red Black Tree (Implementation | Tests)
- Splay Tree (Implementation | Tests)
- Treap Tree (Implementation | Tests)
- B Tree (Implementation | Tests)
- B+ Tree (Implementation | Tests)
- Multi-dimensional Interval Tree (Implementation | Tests) using nested Red-black tree
- Multi-dimensional Kd Tree (Implementation | Tests) for range and nearest neigbour queries
- Multi-dimensional Range Tree (Implementation | Tests) using nested Red-black tree
- Segment Tree (Implementation | Tests)
- Binary Indexed Tree (Fenwick tree) (Implementation | Tests)
TODO: Support multi-dimentional segment tree & binary indexed tree. Implement QuadTree, Octree & R-tree
- Prefix Tree (Trie) (Implementation | Tests)
- Suffix Tree (Implementation | Tests)
- Ternary Search Tree (Implementation | Tests)
- Expression Tree (Implementation | Tests)
- Disjoint Set (Implementation | Tests)
- Sparse Set (Implementation | Tests)
- Bloom Filter (Implementation | Tests)
- Graph (Implementation | Tests)
- Weighted Graph (Implementation | Tests)
- DiGraph (Implementation | Tests)
- Weighted DiGraph (Implementation | Tests)
- Graph (Implementation | Tests)
- Weighted Graph (Implementation | Tests)
- DiGraph (Implementation | Tests)
- Weighted DiGraph (Implementation | Tests)
- Tarjan's Articulation Points Finder (Implementation | Tests)
- Tarjan's Bridge Finder (Implementation | Tests)
- Kosaraju's Strongly Connected Component Finder (Implementation | Tests)
- Tarjan's Strongly Connected Component Finder (Implementation | Tests)
- Tarjan's BiConnected Graph Tester (Implementation | Tests)
- M-Coloring (Implementation | Tests)
- Min Vertex Cover (Implementation | Tests)
- Ford-Fulkerson algorithm (Implementation | Tests)
- Edmonds Karp's improvement (Implementation | Tests) on Ford-Fulkerson algorithm
- Push Relabel algorithm (Implementation | Tests)
- Bellman-Ford algorithm (Implementation | Tests)
- Dijikstra's algorithm (Implementation | Tests) using Fibornacci Heap.
- Floyd-Warshall algorithm (Implementation | Tests)
- Johnson's algorithm (Implementation | Tests)
- Travelling Salesman Problem (Implementation | Tests)
- Max Bipartite Matching (Implementation | Tests) using Edmonds Karp's improved Ford Fulkerson Max Flow algorithm
- Max Bipartite Matching (Implementation | Tests) using Hopcroft Karp algorithm
- Minimum Cut (Implementation | Tests) using Edmonds Karp's improved Ford Fulkerson Max Flow algorithm
- Depth First (Implementation | Tests)
- Breadth First (Implementation | Tests)
- Bi-Directional (Implementation | Tests)
- Depth First Method (Implementation | Tests)
- Kahn's algorithm (Implementation | Tests)
- Kruskal's algorithm (Implementation | Tests) using Merge Sort and Disjoint Set
- Prim's algorithm (Implementation | Tests)
- Manacher's algorithm for linear time longest Palindrome (Implementation | Tests)
- Rabin-Karp string search (Implementation | Tests)
- Knuth–Morris–Pratt (KMP) string search (Implementation | Tests)
- Z algorithm for string search (Implementation | Tests)
- Huffman Coding (Implementation | Tests)
- Median of stream of numbers (Implementation | Tests)
- Binary Search (Implementation | Tests)
- Search on almost sorted array (Implementation | Tests)
- Sort an almost sorted array (Implementation | Tests)
- Bubble Sort (Implementation | Tests)
- Insertion Sort (Implementation | Tests)
- Selection Sort (Implementation | Tests)
- Shell Sort (Implementation | Tests)
- Tree Sort (Implementation | Tests)
- Quick Sort (Implementation | Tests)
- Heap Sort (Implementation | Tests)
- Merge Sort (Implementation | Tests)
- Bucket Sort (Implementation | Tests)
- Radix Sort (Implementation | Tests)
- Counting Sort (Implementation | Tests)
Note: On a decent desktop, in given implementations here for +ive random input integers, the clear winner is counting sort (~0.1 seconds to sort 1 million integers) followed by Radix Sort (~0.4 seconds). Merge Sort, Heap Sort, Quick Sort & Bucket Sort are all winners for +ive & -ive random integer inputs. Tree sort has pointer juggling overhead on backing Red-Black Tree, so performs 8 times slower than Merge Sort in practice. Bubble Sort, Insertion Sort, Selection Sort & Shell Sort are among the worst for random input as observed from results.
- Permutations (Implementation | Tests)
- Combinations (Implementation | Tests)
- Subsets (Implementation | Tests)
- Circular Queue (Ring Buffer) (Implementation | Tests)
- Consistant Hash (Implementation | Tests)
- LRU Cache (Implementation | Tests)
- kth Smallest (Implementation | Tests)
- Check Primality (Implementation | Tests)
- Generate Primes using Sieve of Eratosthenes (Implementation | Tests)
- Fast Exponentiation (Implementation | Tests)
- Convex hull using Gift wrapping algorithm (Implementation | Tests)
- Line intersection (Implementation | Tests)
- Closest point pair (Implementation | Tests)
- Check if given point inside polygon (Implementation | Tests)
- Rectangle intersection (Implementation | Tests)
- Point Rotation (Implementation | Tests)
- Some common bit hacks (Implementation | Tests)
- Int to Binary string (Implementation | Tests)
- Base conversion (Implementation | Tests)
- Next Power of 2 (Implementation | Tests)
- Check if a Number is Multiple of 3 (Implementation | Tests)
- Check if a number is multiple of 9 (Implementation | Tests)
- Calculate Logarithm (base 2 & 10) (Implementation | Tests)
- GCD (Implementation | Tests)
- Modulus of division by power of two (Implementation | Tests)
- Toggle upper/lower case (Implementation | Tests)
- Matrix Multiplication (Implementation | Tests)