Skip to content

landerrosette/algs4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

This repository contains C++ implementations of the algorithms and clients in the textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

For the original Java source code, visit the official repository.

Algorithms

Fundamentals

Sorting

Symbol Tables

Graphs

Strings

Clients

Fundamentals

Sorting

  • Sorts (Selection, Insertion, Shell, Merge, MergeBU, Quick, Quick3way, Heap): Sorting.cpp.in
  • Heap priority queue (MaxPQ): MaxPQ.cpp

Symbol Tables

  • Symbol table tests (TestSequentialSearchST, TestBinarySearchST, TestBST, TestRedBlackBST, TestSeparateChainingHashST, TestLinearProbingHashST): TestST.cpp.in

Graphs

  • Depth-first search (DepthFirstPaths) | Breadth-first search (BreadthFirstPaths): Paths.cpp.in
  • Connected components (CC, KosarajuSCC): CC.cpp.in
  • Reachability (DirectedDFS): DirectedDFS.cpp
  • Topological order (Topological): Topological.cpp
  • Minimum spanning tree (PrimMST, KruskalMST): MST.cpp.in
  • Shortest paths (DijkstraSP, AcyclicSP, BellmanFordSP): SP.cpp.in

Strings

  • String sorts (LSD, MSD, Quick3string): Sorting.cpp.in
  • Trie symbol table tests (TestTrieST) | TST symbol table tests (TestTST): TestST.cpp.in
  • Substring search (KMP, BoyerMoore, RabinKarp): SubstrSearch.cpp.in
  • Regular expression pattern matching (GREP): GREP.cpp
  • Huffman compression/expansion (Huffman) | LZW compression/expansion (LZW): Compress.cpp.in

Usage

Build the project

This project uses CMake as the build system. Ensure you have CMake 3.21+ and a C++20 compliant compiler.

cmake -B build
cmake --build build

Run the clients

By default, all clients are built and the executables can be found in the build directory. Refer to the comments in the source files listed in Clients for instructions on how to run each client. Go to the book's website for test data.

Use as a header-only library

Option A: CMake add_subdirectory

Add to your CMakeLists.txt:

add_subdirectory(/path/to/algs4)
target_link_libraries(your_target PRIVATE algs4)

Option B: Simple include

If you are not using CMake, simply ensure the include/ directory is in your compiler's include path and include the headers you need. For example:

#include <iostream>
#include <string>

#include "algs4/BST.hpp"

int main() {
    algs4::BST<std::string, int> st;
    
    st.put("A", 1);
    st.put("B", 2);
    
    if (const auto &val = st.get("A"))
        std::cout << "Key A has value: " << *val << std::endl;
    
    return 0;
}

About

Algorithms, 4th edition textbook code in C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published