Skip to content

A collection of projects in C++ and Python that implement various data structures and algorithms. The projects are organized by language and topic, and include detailed explanations and examples to help you understand how they work.

License

NotificationsYou must be signed in to change notification settings

djeada/Algorithms-And-Data-Structures

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Algorithms-And-Data-Structures

GitHub starsGitHub forksGitHub license

This repository contains a collection of projects in C++ and Python that implement various data structures and algorithms. The projects are organized by language and topic, and include detailed explanations and examples to help you understand how they work.

algorithms

About

Ever since I first tackled Algorithms and Data Structures at university in 2015, I've found it super useful to regularly go back to the basics. This becomes even more important when you're trying to learn a new programming language - a strong foundation is key. To help others, I've decided to share my code and notes on the subject with everyone.

My code is written in two programming languages I really enjoy, C++ and Python. I've done my best to stick to the latest best practices. Alongside the code, you'll find the notes I made while learning. These notes give more context and could be really handy for anyone new to Algorithms and Data Structures.

Requirements

The following requirements are necessary to build and run the code in this repository:

  • For C++ projects:
    • A C++ compiler supporting C++14
    • CMake 3.15 or later
  • For Python projects:
    • Python 3.10 or later

No additional libraries or modules are required.

Running the Examples

This repository is organized into distinct algorithm implementations, each contained in its own subdirectory. These subdirectories provide the source code, unit tests, and build configuration files necessary for each algorithm. Because each algorithm forms a separate project, you should handle the build and test processes individually.

Building and Testing C++ Projects

Building and testing C++ projects involve a sequence of steps. Here's a detailed walkthrough:

  1. Navigate to the project directory: Start by moving into the directory containing the specific project you want to build and test.

  2. Create and navigate into the build directory:

mkdir -p build && cd build

This command creates a new directory named build (if it doesn't already exist) and then navigates into it. The build directory is where the output files of the build process will be stored.

  1. Generate the build files with CMake:
cmake ..

This command runs CMake to generate the build files. .. tells CMake to look for the CMakeLists.txt file in the directory above build.

  1. Build the project:
make

This command compiles the source code using the instructions specified in the CMakeLists.txt file.

  1. Run the unit tests:
ctest --verbose

The ctest --verbose command executes the unit tests and uses the verbose flag to provide a detailed output.

Testing Python Projects

To test a Python project, execute the following command in the project directory:

python -m unittest discover -v

This command uses Python's built-in unittest module to discover and run the tests. The -v (verbose) flag is used to get more detailed output from the tests.

Using the Testing Utility Script

For convenience, this repository includes a utility script named run_tests.sh. Execute the following commands from the repository's root to run tests in all subprojects:

  • To run all unit tests: ./run_tests.sh
  • To run all Python tests: ./run_tests.sh --python
  • To run all C++ tests: ./run_tests.sh --cpp
  • To read all options from terminal: ./run_tests.sh --help

Code Formatting Conventions

Consistent code formatting is essential for maintaining readability and understanding of the codebase. Therefore, we have adopted specific formatting guidelines for each programming language used in this repository.

C++ Formatting

We adhere to the Google C++ Style Guide. To automatically format the code, we use clang-format. Use the following command to format your code:

find . -regex '.*\\.(cpp|hpp|cu|c|h)' -exec clang-format -style=file -i {} \\;

This command recursively finds all files with .cpp, .hpp, .cu, .c, or .h extensions and formats them using clang-format.

CMake Formatting

CMake files should have a consistent style as well. For this, we use cmake-format. To format a CMakeLists.txt file, execute the following command:

cmake-format CMakeLists.txt -i

This command applies the cmake-format to the CMakeLists.txt file.

Python Formatting

We follow the PEP 8 - Style Guide for Python Code for Python projects and use black to automatically format the code. Use the following command to format your Python code:

black .

This command formats all Python files in the current directory and its subdirectories using black.

Notes

List of projects

Data structures

#StructureImplementation
1Linked ListPythonCpp
2VectorPythonCpp
3StackPythonCpp
4QueuePythonCpp
5HeapPythonCpp
6Binary Search TreePythonCpp
7Avl TreePythonCpp
8Red Black TreePythonCpp
9Hash TablePythonCpp

Graphs

#AlgorithmImplementation
1General graphPythonCpp
1Is Bipartite?PythonCpp
2BFSPythonCpp
3DFSPythonCpp
4DijkstraPythonCpp
5A*PythonCpp
6Bellman-FordPythonCpp
7KruskalPythonCpp
8PrimPythonCpp

Backtracking

#ProblemSolution
1PermutationsPythonCpp
2CombinationsPythonCpp
3String PatternPythonCpp
4Generating wordsPythonCpp
5K-colorable configurationsPythonCpp
6Hamiltonian pathsPythonCpp
7Knigt tourPythonCpp
8Topological orderingsPythonCpp
9Tic-tac-toe (minimax)PythonCpp

Dynamic Programming

#ProblemSolution
1FibonacciPythonCpp
2Grid travelersPythonCpp
3StairsPythonCpp
4Can sumPythonCpp
5How sumPythonCpp
6Best sumPythonCpp
7Can constructPythonCpp
8Count constructPythonCpp
9All constructPythonCpp
10CoinsPythonCpp
11Longest increasing subsequencePythonCpp
12Longest common subsequencePythonCpp
13Knuth-Morris-PrattPythonCpp
14Minimum insertions to form a palindromePythonCpp

Sorting

#AlgorithmImplementation
1Insertion sortPythonCpp
2Selection sortPythonCpp
3Heap sortPythonCpp
4Merge sortPythonCpp
5Quick sortPythonCpp

Brain Teasers

#ProblemSolution
1Minimum deletions to make valid parenthesesPythonCpp
2Is palindrome after at most one char deletion?PythonCpp
3K closest points to originPythonCpp
4Subarray sum equals KPythonCpp
5Add numbers given as stringsPythonCpp
6Dot product of two sparse vectorsPythonCpp
7Range sum of BSTPythonCpp
8Product of array except selfPythonCpp
9Convert BST to sorted doubly linked listPythonCpp
10Lowest common ancestor of a binary treePythonCpp
11LRU CachePythonCpp
12Randomize An ArrayPythonCpp
13Binary Tree Right Side ViewPythonCpp
14Design Browser HistoryPythonCpp
15Score After Flipping MatrixPythonCpp

How to Contribute

We encourage contributions that enhance the repository's value. To contribute:

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

References

Books

  • Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford
    Introduction to Algorithms, 3rd Edition (The MIT Press)
    Amazon Link

  • Halim, Steven
    Competitive Programming 3
    Amazon Link

  • Karumanchi, Narasimha
    Data Structures and Algorithms Made Easy: Data Structures and Algorithmic Puzzles
    Amazon Link

  • Kernighan, Brian; Ritchie, Dennis
    The C Programming Language
    Amazon Link

  • Skiena, Steven; Revilla, Miguel
    Programming Challenges: The Programming Contest Training Manual
    Amazon Link

  • Laaksonen, Antti
    Guide to Competitive Programming: Learning and Improving Algorithms Through Contests (Undergraduate Topics in Computer Science)
    Amazon Link

  • Nimajneb, Nite
    The Hitchhiker’s Guide to the Programming Contests

  • Prasad, L. V. Narashima; Patro, E. Kishna Rao
    Lecture Notes on Data Structures using C

Online Courses and Resources

License

This project is licensed under the MIT License - see the LICENSE file for details.

Star History

Star History Chart

About

A collection of projects in C++ and Python that implement various data structures and algorithms. The projects are organized by language and topic, and include detailed explanations and examples to help you understand how they work.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published