Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
1517c30
add merge_sort function
dvsav Oct 22, 2023
03c47a4
replace Tabs with spaces
dvsav Oct 22, 2023
0a1fbe1
merge_sort: static assert random access iterator
dvsav Oct 23, 2023
6bdde28
merge_sort: add TComparator as a template parameter to allow for sort…
dvsav Oct 23, 2023
f771c3f
merge_sort: add Doxygen comment; rename namespace sc to cs (computer …
dvsav Oct 23, 2023
c2a47b3
add quick_sort_lomuto_partition function
dvsav Oct 24, 2023
7426d0c
quick_sort.h: add function quick_sort_randomized_partition
dvsav Oct 25, 2023
ed32ab4
quick_sort.h: fix comment
dvsav Oct 25, 2023
bf49732
add Catch2 (v2, single header) as a unit test framework; add a testca…
dvsav Oct 26, 2023
b4c1424
print.h: fix a typo
dvsav Oct 26, 2023
b0c9bc8
add GNUMake/Makefile; fix compile errors as found by GCC; update .git…
dvsav Oct 27, 2023
8f1d6df
add CMakeLists.txt - a build script for CMake build system
dvsav Oct 28, 2023
3099210
add YAML file to run unit tests each time a commit is pushed to devel…
dvsav Oct 28, 2023
6d12a6c
unit_tests.yml: add test-on-windows job to GitHub Actions workflow
dvsav Oct 29, 2023
f6a424e
unit_tests.yml: fix missing -C (configuration) option for CTest
dvsav Oct 29, 2023
b2d6979
graph.h: add class Graph
dvsav Oct 31, 2023
7e12660
test.cpp: start writing graph tests
dvsav Oct 31, 2023
5bcc86d
graph.h: vertex contains list of edges; test.cpp: check edges
dvsav Nov 1, 2023
c2aa96d
graph_algorithms.h: BreadthFirstSearch in a directed graph
dvsav Nov 2, 2023
ccfe2b8
graph_algorithms.h: Breadth-first search for directed and undirected …
dvsav Nov 3, 2023
48b4f22
src/graph_algorithms.h: depth-first search
dvsav Nov 3, 2023
3096c52
graph.h: sumplify functions
dvsav Nov 4, 2023
4ccc4e0
graph.h: simplify Vertex class
dvsav Nov 5, 2023
2edbc29
graph_algorithms.h: TopologicalSort function
dvsav Nov 5, 2023
8bd1ebb
Selection, insertion and bubble sorts added
AndreyKarakozov Nov 6, 2023
289ed64
src/graph_algorithms.h: add bool visit_in_reverse_order parameter to …
dvsav Nov 6, 2023
bdefde4
get rid of TData in graph
dvsav Nov 6, 2023
8e15b9f
graph: make TLen = int by default
dvsav Nov 6, 2023
cb59c21
graph.h: add functions Graph::RemoveVertex, Graph::RemoveEdge
dvsav Nov 6, 2023
f2c8315
graph.h: Edge::Edge, Graph::AddEdge - add default value for length pa…
dvsav Nov 6, 2023
9da1739
graph_algorithms.h: add function VisitStronglyConnectedComponents_Kos…
dvsav Nov 6, 2023
70a2e95
graph_algorithms.h: fix functions TopologicalSort, VisitStronglyConne…
dvsav Nov 6, 2023
7dedc22
graph_algorithms.h: fix function DepthFirstSearch_Directed_InverseTop…
dvsav Nov 7, 2023
a82dc51
graph.h: add default value to TId template parameter
dvsav Nov 7, 2023
d611267
graph.h: add convenience functions
dvsav Nov 7, 2023
d48d30c
src/graph_algorithms.h: add function IsTopologicalOrder; test.cpp: te…
dvsav Nov 8, 2023
9118729
clean up graph unit test
dvsav Nov 8, 2023
451c05d
Typo corrected
AndreyKarakozov Nov 9, 2023
b92356f
graph.h: add input and output operators for class Graph
dvsav Nov 9, 2023
e69311f
graph.h: remove class VertexBase; make Vertex and Edge constructors p…
dvsav Nov 10, 2023
af96704
graph.h: add Doxygen comments for cs::Edge class
dvsav Nov 11, 2023
404ad6f
fix bugs in operator>>(os, graph); add a unit test for graph I/O
dvsav Nov 11, 2023
fc2ffef
Makefile: fix 'test' target; graph.h: read_adjacency_list, read_verte…
dvsav Nov 13, 2023
56f73e4
Merge pull request #1 from dvsav/simple_sorts
dvsav Nov 13, 2023
81778f3
graph.h: rename ClearAuxData to ClearDiscovered
dvsav Nov 14, 2023
beccc42
graph_algorithms.h: DijkstraShortestPath_Directed
dvsav Nov 14, 2023
b101223
graph.h: add Doxygen comments to class Vertex
dvsav Nov 15, 2023
8244f33
graph.h: read_edge function - read tail vertex and head vertex with o…
dvsav Nov 16, 2023
e8b0005
graph_algorithms.h: DijkstraShortestPath_Undirected function
dvsav Nov 16, 2023
36fb871
add unit test for DijkstraShortestPath_Directed
dvsav Nov 16, 2023
404180b
graph_algorithms.h: fix DijkstraShortestPath_Undirected; add unit tes…
dvsav Nov 17, 2023
9041ef9
graph.h: add Doxygen comments to all public functions
dvsav Nov 17, 2023
d96b867
graph.h: add Doxygen comments to class Graph
dvsav Nov 18, 2023
ce5158c
graph_algorithms.h: add Doxygen comments
dvsav Nov 18, 2023
b99208b
graph_algoritms.h: fix compile error
dvsav Nov 19, 2023
0742b22
graph_algoritms.h: DijkstraShortestPath: rename data_type to dijkstra…
dvsav Nov 19, 2023
adb1f24
distribute graph algoritms over several header files
dvsav Nov 19, 2023
b9be09d
utility.h: move DefaultComparator and ReverseComparator to global nam…
dvsav Nov 19, 2023
e2df87e
merge devel into graph; fix bubble sort (check no swaps); add comments
dvsav Nov 19, 2023
516eba4
heap.h: class Heap, MinHeap, MaxHeap
dvsav Nov 19, 2023
dccdd4f
heap.h: add heap_sort function; add Doxygen comments
dvsav Nov 20, 2023
27dfd7f
heap.h: fix compile error
dvsav Nov 20, 2023
a152657
Add complexity information to sorting algorithms; add unit test for H…
dvsav Nov 21, 2023
6e4a034
Merge pull request #3 from dvsav/graph
dvsav Nov 21, 2023
266540a
CMakeLists.txt: set working directory=/test for CTest
dvsav Nov 21, 2023
6a7ff3d
Merge pull request #4 from dvsav/heap
dvsav Nov 21, 2023
fd75cd0
binary_tree.h: class TreeNode; Traverse functions; PrintTree function
dvsav Nov 22, 2023
13b219f
binary_tree.h: add class BinarySearchTree (unbalanced); functions: fi…
dvsav Nov 23, 2023
8f9acfa
binary_tree.h: add BinarySearchTree::remove function; add unit tests …
dvsav Nov 25, 2023
3a1d365
binary_tree.h: replace 'value' with 'key'
dvsav Nov 25, 2023
e9b1f32
binary_tree.h: implement AVL tree; add unit test; TODO: cache subtree…
dvsav Nov 25, 2023
7e39050
binary_tree.h: implement AvlTree::remove function
dvsav Nov 26, 2023
8ad0a79
binary_tree.h: cache 'height' for each node to optimize performance
dvsav Nov 26, 2023
05fa28f
binary_tree.h: add 'value' to TreeNode; BinarySearchTree::operator[]
dvsav Nov 27, 2023
87a5b6e
binary_tree.h: add Doxygen comments
dvsav Nov 27, 2023
ec4398e
binary_tree.h: add Doxygen comments
dvsav Nov 27, 2023
87efa88
Merge pull request #5 from dvsav/binary_trees
dvsav Nov 27, 2023
fab7a51
btree.h: create BTree class
dvsav Nov 28, 2023
4d53d38
btree.h: create BTree class
dvsav Nov 29, 2023
7fd4b0e
btree.h: debug BTree
dvsav Nov 30, 2023
882e07b
btree.h: debug BTree::insert function
dvsav Dec 1, 2023
9388300
btree.h: cosmetic changes
dvsav Dec 1, 2023
f50e877
btree.h: BTree::find, BTree::at functions
dvsav Dec 1, 2023
9d5b61f
btree.h: implement BTree::remove(key) function; TODO: test+debug
dvsav Dec 2, 2023
4f387ef
btree.h: debug BTree of order=4
dvsav Dec 3, 2023
5297f53
btree.h: debug BTree of order=5
dvsav Dec 3, 2023
b8f0f14
btree.h: clean up code
dvsav Dec 3, 2023
b50674a
btree.h: debug, add unit tests
dvsav Dec 3, 2023
0ddc61e
binary_tree.h: static assert that TBinaryTreeNode is an instantiation…
dvsav Dec 4, 2023
6566a84
btree.h: use BinarySearch in BTreeNode::FindItem function
dvsav Dec 5, 2023
de7defc
btree.h: add Doxygen comments
dvsav Dec 5, 2023
0f4bf64
btree.h: add Doxygen comments
dvsav Dec 6, 2023
9d5f47e
btree.h: add Doxygen comments
dvsav Dec 6, 2023
fa16741
btree.h: add Doxygen comments
dvsav Dec 6, 2023
d95082f
Merge pull request #6 from dvsav/btree
dvsav Dec 6, 2023
670f965
hash_map.h: create a skeleton for class HashMap
dvsav Dec 7, 2023
04a2d57
hash_map.h: implement HashMap member functions: at, insert, find
dvsav Dec 11, 2023
dbabdf3
hash_map.h: implement class HashMap
dvsav Dec 12, 2023
cef9daa
class HashMap: add unit tests
dvsav Dec 13, 2023
fd86a77
hash_map.h: add Doxygen comments
dvsav Dec 16, 2023
6202019
Merge pull request #7 from dvsav/hash
dvsav Dec 16, 2023
8f94c6b
bloom_filter.h: class BloomFilter; unit tests
dvsav Dec 17, 2023
0669d44
bloom_filter.h: add Doxygen comments
dvsav Dec 17, 2023
8a8eb51
Merge pull request #8 from dvsav/bloom
dvsav Dec 17, 2023
ddb505e
bloom unit test fix
dvsav Dec 17, 2023
e4674b7
bloom_filter.h: fix uniform distribution for pseudorandom number gene…
dvsav Dec 23, 2023
4b042e2
Prim's algorithm for Minimum Spanning Tree (draft)
dvsav Dec 23, 2023
7a3f8ed
graph_prim.h: implement Prim's algorithm; add Doxygen comments; add u…
dvsav Dec 24, 2023
54c5e3d
graph_prim.h: add comments
dvsav Dec 26, 2023
3c28cbe
Prim: add one more check to the unit test: Edges(MST)=Vertices(graph)-1
dvsav Dec 26, 2023
bd034b6
Merge pull request #9 from dvsav/prim
dvsav Dec 26, 2023
bd58d5a
graph_kruskal.h: implement the sceleton of Kruskal's algorithm
dvsav Dec 26, 2023
630deb5
graph_kruskal.h: implement Kruskal's algorithm for Minimal Spanning T…
dvsav Dec 27, 2023
a95508d
Merge pull request #10 from dvsav/kruskal
dvsav Dec 27, 2023
8b1bad9
huffman_encoding.h: start implementing Huffman encoding
dvsav Jan 8, 2024
8a5124a
huffman_encoding.h: finish implementing Huffman encoding; add unit test
dvsav Jan 9, 2024
506f5c3
Makefile: add compiler flags to prevent file too big issue; create cm…
dvsav Jan 10, 2024
05e1d96
huffman_encoding.h: add Doxygen comments
dvsav Jan 10, 2024
634e784
Merge pull request #11 from dvsav/huffman
dvsav Jan 10, 2024
1e64c4c
Makefile: determine execution environment and add Cygwin specific part
dvsav Jan 10, 2024
81edfb0
knapsack.h: implement knapsack (backpack) problem
dvsav Jan 15, 2024
7e7e8a9
knapsack.h: Test and debug
dvsav Jan 16, 2024
0379cef
knapsack.h: add Doxygen comments
dvsav Jan 20, 2024
73df01b
Merge pull request #13 from dvsav/knapsack
dvsav Jan 20, 2024
bfda93d
create illustration for heap.insert
dvsav Mar 5, 2024
730c51e
create illustration of heap.pop
dvsav Mar 6, 2024
eba1fb0
illustrate buildHeap()
dvsav Mar 7, 2024
eceb8a2
illustrate heap; optimize buildHeap()
dvsav Mar 8, 2024
8e2b467
illustrate min heap; build heap complexity
dvsav Mar 9, 2024
2e60ca9
add ReadMe.md
snps-sdmitry May 27, 2025
7769840
ReadMe.md
snps-sdmitry May 28, 2025
2e914bf
ReadMe.md
snps-sdmitry May 28, 2025
57f7fab
readme
snps-sdmitry May 29, 2025
3a188a9
Merge pull request #14 from dvsav/readme
dvsav May 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Unit Tests

on:
push: # This workflow is triggered by pushing to a git branch specified below
branches:
- devel # Project's branch name

jobs:
test-on-ubuntu-linux:
runs-on: ubuntu-latest # Runner (execution environment)

steps:
- name: Check Out Repository
uses: actions/checkout@v2 # Pre-built GitHub Actions workflow called "actions/checkout" with version 2 (v2)

- name: Set Up GNU Make
run: |
sudo apt-get -y install make
shell: bash

- name: Build Project
run: |
make
working-directory: ${{ github.workspace }}/build/GNUMake # the directory in which the subsequent steps of the workflow will be executed; github.workspace - the root of the repository

- name: Run Unit Tests
run: |
make test
working-directory: ${{ github.workspace }}/build/GNUMake

test-on-windows:
runs-on: windows-latest # Runner (execution environment)

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set Up CMake
run: |
choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
shell: powershell

- name: Create Build Directory
run: |
mkdir win
working-directory: ${{ github.workspace }}/build/CMake

- name: Configure CMake
run: |
cmake -S . -B ./win
working-directory: ${{ github.workspace }}/build/CMake

- name: Build Project
run: |
cmake --build . --config Debug
working-directory: ${{ github.workspace }}/build/CMake/win

- name: Run Unit Tests
run: |
ctest -C Debug --output-on-failure
working-directory: ${{ github.workspace }}/build/CMake/win
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@
*.exe
*.out
*.app

# Directories
bin/
install/

# Visual Studio cache/options directory
**/.vs/
**/x64/
**/[Dd]ebug/
**/[Rr]elease/

# CMake
!build/CMake/
build/CMake/*
!/build/CMake/CMakeLists.txt
92 changes: 92 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# computer-science

This is an educational project containing implementations of some core computer science algorithms.

## Table of Contents

- [About](#about)
- [Algorithms](#algorithms)
- [Prerequisites](#prerequisites)
- [How to build](#how-to-build)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)

## About

The project is written in C++. Its goal is to showcase several things:
- Clean well-documented C++ code
- Usage of different build systems (MSBuild, CMake, GNU Make)
- Unit testing via Catch2 framework
- Automated testing via GitHub Actions

## Algorithms

The list of implemented algorithms.
- Simple sorting algorithms ([simple_sorts.h](src/simple_sorts.h))
- selection sort
- insertion sort
- bubble sort
- Merge sort ([merge_sort.h](src/merge_sort.h))
- Quick sort ([quick_sort.h](quick_sort.h))
- [Trial division](https://en.wikipedia.org/wiki/Trial_division) ([prime.h](src/prime.h))

## Prerequisites

On Linux install GNU Make and CMake build systems (example below is given for APT package manager which is used by default in Debian based Linux, e.g. Ubuntu and Linux Mint):

```bash
sudo apt-get -y install make
sudo apt-get -y install cmake
```

On Windows install CMake build system using [Chocolatey](https://chocolatey.org/) package manager:
```
choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
```
...or [download CMake installer for Windows](https://cmake.org/download/)

## How to build

Clone the repository:

```bash
git clone https://github.com/dvsav/computer-science.git
cd computer-science
```

Compile the sources using one of convenience scripts that call either GNU Make:

```bash
./make.sh test
```
If the build is successful, you'll find the binary files in `build/GNUMake/bin` directory.

...or CMake:

```bash
./cmake.sh
```
If the build is successful, you'll find a binary executable `computer-science` in `build/CMake/gnumake` directory.

## Usage

You can include the source code of this project into your own project, e.g. add this git repository as a [git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules):
```
cd your_repository
git submodule add https://github.com/dvsav/computer-science.git
```

## Contributing

Contributions are welcome! Please follow these steps:

[Fork the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo).
Create a new branch (`git checkout -b feature/YourFeature`).
Commit your changes (`git commit -m 'Add some feature'`).
Push to the branch (`git push origin feature/YourFeature`).
[Open a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request).

## License

This project is licensed under the [MIT License](LICENSE).
Empty file.
107 changes: 107 additions & 0 deletions build/CMake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)

option(VERBOSE "Show explanatory messages" ON)

function(print_message msg)
if (VERBOSE)
message("${msg}")
endif()
endfunction()

print_message("${CMAKE_GENERATOR} generator has been selected")
# determine whether we are generating a Visual Studio project
if (${CMAKE_GENERATOR} MATCHES "Visual Studio")
print_message("Visual Studio generator detected")
set (VISUAL_STUDIO TRUE)
set (NOT_VISUAL_STUDIO FALSE)
else()
set (VISUAL_STUDIO FALSE)
set (NOT_VISUAL_STUDIO TRUE)
endif()

print_message("-------------------- COMPUTER SCIENCE --------------------")

# specifies the project name (in MS Visual Studio defines the solution name)
project(computer-science
VERSION 1.0
DESCRIPTION "Computer Science (c) 2023-2025 Dmitry Savchenkov"
LANGUAGES CXX)

# the repository's root is two folders higher than this CMakeLists file
set(ROOT_DIR "${PROJECT_SOURCE_DIR}/../..")
set(SRC_DIR "${ROOT_DIR}/src")
set(TEST_DIR "${ROOT_DIR}/test")

# collect the source and header files
file(GLOB SOURCE_FILES CONFIGURE_DEPENDS
"${SRC_DIR}/*.cpp"
"${SRC_DIR}/*.h")

foreach (file ${SOURCE_FILES})
print_message("source file = ${file}")
endforeach()

# defines the executable file name (target) and its dependencies
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

if (VISUAL_STUDIO)
# set our project as the startup project in the Visual Studio solution
set_property(
DIRECTORY "${PROJECT_SOURCE_DIR}"
PROPERTY VS_STARTUP_PROJECT "${PROJECT_NAME}")

# set Visual Studio Debug working directory to the repository's root directory
set_target_properties(
${PROJECT_NAME} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${ROOT_DIR}")
else() # CYGWIN MSYS2 *NIX environment is assumed below
# Linking to libstdc++fs is required for use of the Filesystem library extensions in <experimental/filesystem>
target_link_libraries(${PROJECT_NAME} PRIVATE "stdc++fs")

# extract standard libraries' search paths from the compiler
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
COMMAND grep libraries
COMMAND cut -d= -f2
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE CXX_STD_LIB_PATHS)

# replace colons ':' with semicolons ';'
string(REPLACE ":" ";" CXX_STD_LIB_PATHS "${CXX_STD_LIB_PATHS}")
print_message("CXX_STD_LIB_PATHS = ${CXX_STD_LIB_PATHS}")

# embed library search directories into the executable
get_target_property(RPATH ${PROJECT_NAME} BUILD_RPATH)
list(APPEND RPATH ${CXX_STD_LIB_PATHS})

set_target_properties(
${PROJECT_NAME} PROPERTIES
BUILD_RPATH "${RPATH}")

set_target_properties(
${PROJECT_NAME} PROPERTIES
INSTALL_RPATH "${RPATH}")
endif()

print_message("-------------------- CTEST --------------------")

# enable testing functionality
enable_testing()

# define tests
add_test(
NAME ${PROJECT_NAME}
COMMAND $<TARGET_FILE:${PROJECT_NAME}>
WORKING_DIRECTORY ${TEST_DIR})

print_message("-------------------- INSTALLATION --------------------")

# install the executable file to ${CMAKE_INSTALL_PREFIX}
install(TARGETS ${PROJECT_NAME}
CONFIGURATIONS Release
PERMISSIONS OWNER_READ
GROUP_READ
WORLD_READ
OWNER_WRITE
GROUP_WRITE
WORLD_WRITE)
Loading