This is a repository of pseudocode implementations discussed in the main text of a classic - π Introduction to Algorithms, widely known as CLRS. There are some great solution manuals for more theoretical problems, such as https://walkccc.github.io/CLRS/, so I have decided to focus only on the main text pseudocode and personally more interesing exercises. All implementations are written in C and should be cross-platform.
The folder structure of this repository resembles the book structure. Usually, implementations come with small auxiliary programs and simple test inputs.
For example, along with an implementation of Huffman coding algorihm there is a small program to print the generated prefix code.
For some "heavier" implementations, depending on some data structures or algorithms, there is also a small bash script in the implementation folder to aid compilation.
Also, although the pseudocode in the book may not have the best variable naming, I have decided to respect it for the sake of consistency. However, I did not respect pseudocode conventions when it comes to implementations of algorithms presented as exercise problems or as end of chapter problems.
Foundations (Chapters 2, 3, 4) π
- InsertionSort
- MergeSort
- Maximum Subarray (Divide and Conquer, BruteForce)
- Max Heap π
- HeapSort π
- Max Priority Queue π
- Min Priority Queue π
- Implemented as Max Priority Queue with inverted keys
- QuickSort (non-randomised) π
- QuickSort (randomised) π
- CountingSort π
- RadixSort π
- BucketSort π
- RandomisedSelect aka QuickSelect π
- Stack
- Queue
- Linked-List (doubly-linked, circular) π
- Hash Table
- Binary Search Tree π
- All discussed operations
- Red Black Tree π
- Insertion
- Deletion
- AVL Tree π
- AVL Sort
- All discussed BST operations, along with AVL Insert and Delete
- Disjoint Set Union π
- implemented path compression and union by rank heuristics
- rod cutting π
- matrix chain multiplication π
- longest common subsequence π
- optimal binary search tree π
- longest path in a dag π
- longest palindromic subsequence π
- implemented as longest-common-subsequence(input, reversed(input))
- printing neatly π
- also known as a variant of the word wrap problem
- simplified-edit-distance π
- coin changing π
- classical dynamic programming problem, presented as exercise 16.1.d)
- activity scheduling π
- interval graph coloring(scheduling) - exercise 16.1.4
- We are given n activities to be held in some lecture halls. For each activity we know its starting time, finishing time and id. Any activity can take place in any lecture hall. Design a fast greedy algorithm to determine the minimal number of lecture halls required to ensure all activities can happen. Algorithm should also determine activity schedule taking place in each lecture hall used.
- The algorithm implemented π here has O(nlogn) time complexity. All data structures used (min priority queue, doubly linked list) are implemented in the corresponding folders in this repository.
- Huffman codes π
- A small program to print a generated prefix code π
- BFS π
- performs unweighted shortest path computation
- DFS π
- performs vertex timestamping and edge classification
- Topological Sorting
- Strongly connected components of a directed graph
- Kosaraju's 'two pass' algorithm π
- implemented with the aid of dfs-based topological sort, also implemented above
- Kosaraju's 'two pass' algorithm π
- Biconnected components of an undirected graph
- Hopcroft-Tarjan's algorithm for detecting articulation points and bridges π
- Detects bridges
- Detects articulation points
- Hopcroft-Tarjan's algorithm for detecting articulation points and bridges π
- An Euler tour of a directed graph
- presented as the problem in End of Chapter 22 problems
- implemented Hierholzer's algorithm π
- Prim's algorithm π
- computes the cost and the tree edges set
- Kruskal's algorithm π
- computes the cost and the tree edges set
- The Bellman-Ford algorithm π
- detects existence of a negative-weight cycle reachable from source vertex
- in case there is no negative-weight cycle reachable from source vertex, the algorithm returns the shortest path from s to every vertex v in the input graph as well as the shortest path tree rooted at s
- Shortest Path in a directed acyclic graph using Topological Sort + Relax π
- the algorithm returns the shortest path from s to every vertex v in the input graph as well as the shortest path tree rooted at s
- Dijkstra's algorithm π
- computes the shortest path weights and shortest path tree from a source vertex s
- implemented with a Min Priority Queue
-
'Slow all pairs shortest paths' π
- This algorithm is based on dynamic programming on edges and has worst case complexity O(|V|^4)
- The algorithm is developed 'from scratch' in the textbook Chapter 25.1
- An algebraically optimised version of the same algorithm is also implemented π, having worst case complexity O(|V|^3 log(|V|))
- Implemented both shortest path cost computation and path reconstruction for every vertex pair (non-optimised version only)
-
Floyd-Warshall algorithm π
- Implemented both shortest path cost computation and path reconstruction for every vertex pair
-
Transitive closure algorithm π
- Adapted Floyd-Warshall's procedure
Huffman codes π - a beautiful greedy 'construction' algorithm
printing neatly π - a neat, non obvious application of dynamic programming
constructing an optimal binary search tree π- a nice application of DP on trees
constructing an Euler tour with Hierholzer's algorithm
π - a nice 'construction' graph algorithm, challenging to implement in C. Presented as one of end of chapter problems.
scheduling lectures with interval graph coloring π - a nice, real world application of greedy activity scheduling. Presented as one of exercises.
topological sorting of a 'clothing' graph π - a nice 'real-world' example of topological sorting graph algorithm