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 2.0 or above
- .Net Framework 4.5 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 multi-level Red-black tree
- Multi-dimensional Kd Tree (Implementation | Tests) for range and nearest neigbour queries
- Multi-dimensional Range Tree (Implementation | Tests)
- Segment Tree (Implementation | Tests)
- Binary Indexed Tree (Fenwick tree) (Implementation | Tests)
- 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)
- 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) using Fibornacci Heap
- 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) using Fibornacci Min Heap
- Median of stream of numbers (Implementation | Tests)
- Array in zig-zag fashion (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)
- Variations (Implementation | Tests)
- Subsets (Implementation | Tests)
- kth Smallest (Implementation | Tests)
- Check Primality (Implementation | Tests)
- Generate Primes using Sieve of Eratosthenes (Implementation | Tests)
- Fast Exponentiation (Implementation | Tests)
All are top down solutions with memoization technique.
- Tower of hanoi (Implementation | Tests)
- Fibonacci number generator (Implementation | Tests)
- Optimal game strategy (Implementation | Tests)
- Optimal Binary Search Tree (Implementation | Tests)
- Max submatrix (Implementation | Tests)
- Min cost matrix path (Implementation | Tests)
- Chain matrix multiplication (Implementation | Tests)
- Max 1s Rectangle in matrix (Implementation | Tests)
- Max 1s Square in matrix (Implementation | Tests)
- Max subsquare with X sides in matrix (Implementation | Tests)
- Count bool parenthesization (Implementation | Tests)
- Count decoding (Implementation | Tests)
- Dice throw (Implementation | Tests)
- Count possible binary tree from a preorder sequence (Implementation | Tests)
- Ways to cover a distance (Implementation | Tests)
- Staircase problem in Fibornacci Series (Implementation | Tests)
- Knapsack problem (Implementation | Tests)
- Max sum subsequence (Implementation | Tests)
- Max increasing sum sequence (Implementation | Tests)
- Max profit buy/sell stocks in K transactions (Implementation | Tests)
- Box Stacking (Implementation | Tests)
- Building Bridges (Implementation | Tests)
- Burst Balloon (Implementation | Tests)
- Cutting Rod (Implementation | Tests)
- Print Max A's (Implementation | Tests)
- Weighted Job Scheduling (Implementation | Tests)
- Longest Chain (Implementation | Tests)
- Coin change problem (Implementation | Tests)
- Assembly line scheduling (Implementation | Tests)
- Min Egg drop problem (Implementation | Tests)
- Min edit distance (Implementation | Tests)
- Min array jumps (Implementation | Tests)
- Travelling Salesman Problem (Implementation | Tests)
- Longest palindrome (Implementation | Tests)
- Shortest palindrome (Implementation | Tests)
- Palindrome min cut (Min Partitioning) (Implementation | Tests)
- Min deletion to get a palindrome (Implementation | Tests)
- Balanced partition (Implementation | Tests)
- Distinct binary strings (Implementation | Tests)
- Longest common subsequence (Implementation | Tests)
- Longest increasing subsequence (Implementation | Tests)
- Longest bitonic sequence (Implementation | Tests)
- Subset sum (Implementation | Tests)
- Wild card matching (Implementation | Tests)
- String interleaving (Implementation | Tests)
- Text Justification (Implementation | Tests)
- Word Break problem (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)
- Find the element that appears once (Implementation | Tests)
- Find the two non-repeating elements in an array (Implementation | Tests)
- Find two repeating elements in an array (Implementation | Tests)
- Set bits in all numbers from 1 to n (Implementation | Tests)
- Swap without temp (Implementation | Tests)
- Swap all odd and even bits (Implementation | Tests)
- Swap bits (Implementation | Tests)
- Minimum or Maximum of two integers (Implementation | Tests)
- Add two numbers using bitwise operators (Implementation | Tests)
- Add 1 to a number (Implementation | Tests)
- A Boolean Array Puzzle (Implementation | Tests)
- Set bits in an (big) array (Implementation | Tests)
- Next higher/lower number with same number of set bits (Implementation | Tests)
- Absolute value (abs) without branching (Implementation | Tests)
- Reverse Bits of a Number (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)
- Find parity (Implementation | Tests)
- Maximum Subarray XOR (Implementation | Tests)
- Magic Number (Implementation | Tests)
- Sum of bit differences among all pairs (Implementation | Tests)
- Find Next Sparse Number (Implementation | Tests)
- Binary Subsets (Implementation | Tests)
- Calculate Logarithm (base 2 & 10) (Implementation | Tests)
- Check if word has a zero byte (Implementation | Tests)
- GCD (Implementation | Tests)
- Modulus of division by power of two (Implementation | Tests)
- Interleave (Morton Number) (Implementation | Tests)
- Toggle upper/lower case (Implementation | Tests)
- Flip a bit for longest ones sequence (Implementation | Tests)
- Count Inversions (Implementation | Tests)
- Matrix Multiplication (Implementation | Tests)