gboduljak / clrs-implementations

This is a repository of pseudocode implementations discussed in the main text of a classic - πŸ“–Introduction to Algorithms, known as CLRS. All implementations are written in C. The repository is a result of self study of algorithms discussed in CLRS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ‘¨β€πŸ’» CLRS Implementations

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.
huffman codes
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.

Table of Contents

  1. Foundations
  2. Data Structures
  3. Dynamic programming
  4. Greedy
  5. Graph algorithms
  6. My favorites

Foundations (Chapters 2, 3, 4) πŸ”—

  • InsertionSort
  • MergeSort
  • Maximum Subarray (Divide and Conquer, BruteForce)

Sorting and Order Statistics (Chapters 6, 7, 8, 9)

Data Structures (Chapters 10, 11, 12, 13)

  • 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

Advanced Design and Analysis Techniques (Chapters 15, 16)

Dynamic Programming (Chapter 15)

  • 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)

Greedy (Chapter 16)

  • 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 πŸ”—

Graph Algorithms (Chapters 22, 23, 24, 25)

Elementary (Chapter 22)

  • BFS πŸ”—
    • performs unweighted shortest path computation
  • DFS πŸ”—
    • performs vertex timestamping and edge classification
  • Topological Sorting
    • classical dfs-based implementation πŸ”—
    • Kahn's algorithm implementation πŸ”—
    • both implementations come with a simple program in the same directory which performs topological sort of a textbook 'clothing' directed acyclic graph
  • Strongly connected components of a directed graph
    • Kosaraju's 'two pass' algorithm πŸ”—
      • implemented with the aid of dfs-based topological sort, also implemented above
  • Biconnected components of an undirected graph
    • Hopcroft-Tarjan's algorithm for detecting articulation points and bridges πŸ”—
      • Detects bridges
      • Detects articulation points
  • An Euler tour of a directed graph
    • presented as the problem in End of Chapter 22 problems
    • implemented Hierholzer's algorithm πŸ”—

Minimum Spanning Tree (Chapter 23)

  • Prim's algorithm πŸ”—
    • computes the cost and the tree edges set
  • Kruskal's algorithm πŸ”—
    • computes the cost and the tree edges set

Single Source Shortest Paths (Chapter 24)

  • 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

All Pairs Shortest Paths (Chapter 25)

  • '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

Favorites

huffman codes
Huffman codes πŸ”— - a beautiful greedy 'construction' algorithm

printing-neatly
printing neatly πŸ”— - a neat, non obvious application of dynamic programming

optimal-bst
constructing an optimal binary search tree πŸ”—- a nice application of DP on trees

euler-tour
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.

interval-graph-coloring
scheduling lectures with interval graph coloring πŸ”— - a nice, real world application of greedy activity scheduling. Presented as one of exercises.

topsort
topological sorting of a 'clothing' graph πŸ”— - a nice 'real-world' example of topological sorting graph algorithm

About

This is a repository of pseudocode implementations discussed in the main text of a classic - πŸ“–Introduction to Algorithms, known as CLRS. All implementations are written in C. The repository is a result of self study of algorithms discussed in CLRS.

License:The Unlicense


Languages

Language:C 95.3%Language:C++ 2.9%Language:Shell 1.7%