Skip to content

Commit 5bc884a

Browse files
committed
Add Dijkstra algorithm
1 parent 962fb7e commit 5bc884a

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
# Build
35+
build
36+
37+
# VSCOde
38+
.vscode

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
project(algorithms_in_cpp)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
include(FetchContent)
8+
FetchContent_Declare(
9+
googletest
10+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
11+
)
12+
# For Windows: Prevent overriding the parent project's compiler/linker settings
13+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
14+
FetchContent_MakeAvailable(googletest)
15+
16+
enable_testing()
17+
18+
add_executable(${PROJECT_NAME} main.cpp)
19+
20+
target_link_libraries(${PROJECT_NAME} GTest::gtest_main)
21+
22+
file(GLOB SUBDIRS ${PROJECT_SOURCE_DIR}/algorithms/*)
23+
foreach(dir ${SUBDIRS})
24+
add_subdirectory(${dir})
25+
endforeach()
26+
27+
include(GoogleTest)
28+
gtest_discover_tests(${PROJECT_NAME})

algorithms/dijkstra/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target_sources(${PROJECT_NAME} PUBLIC test_dijkstra.cpp dijkstra.cpp)

algorithms/dijkstra/dijkstra.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "dijkstra.hpp"
2+
3+
std::vector<int64_t> dijkstra(
4+
const std::vector<std::vector<std::pair<int64_t, int64_t>>> graph,
5+
const int64_t source_node
6+
)
7+
{
8+
size_t n = graph.size();
9+
std::vector<int64_t> distance(n, -1);
10+
std::vector<bool> relaxed(n, false);
11+
12+
distance[source_node] = 0;
13+
for (size_t i = 0; i < n; ++i)
14+
{
15+
int64_t u = -1;
16+
for (int64_t j = 0; j < n; ++j)
17+
{
18+
if (
19+
relaxed[j] == false && (
20+
u == -1 || (
21+
distance[j] != -1 &&
22+
distance[j] < distance[u]
23+
)
24+
)
25+
)
26+
{
27+
u = j;
28+
}
29+
}
30+
if (u == -1)
31+
{
32+
break;
33+
}
34+
35+
for (const auto &[v, w] : graph[u])
36+
{
37+
if (
38+
distance[v] == -1 ||
39+
distance[v] > distance[u] + w
40+
)
41+
{
42+
distance[v] = distance[u] + w;
43+
}
44+
}
45+
46+
relaxed[u] = true;
47+
}
48+
49+
return distance;
50+
}

algorithms/dijkstra/dijkstra.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <vector>
2+
#include <cstdint>
3+
4+
std::vector<int64_t> dijkstra(
5+
const std::vector<std::vector<std::pair<int64_t, int64_t>>> graph,
6+
const int64_t source_node
7+
);

algorithms/dijkstra/test_dijkstra.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <gtest/gtest.h>
2+
#include <iostream>
3+
4+
#include "dijkstra.hpp"
5+
6+
TEST(Dijkstra, Test0)
7+
{
8+
std::vector<std::vector<std::pair<int64_t, int64_t>>> graph = {
9+
{{1, 1}, {2, 1}},
10+
{{0, 1}, {2, 1}},
11+
{{0, 1}, {1, 1}},
12+
};
13+
14+
int64_t source_node = 0;
15+
16+
std::vector<int64_t> distance = dijkstra(graph, source_node);
17+
18+
std::vector<int64_t> expected_distance = {0, 1, 1};
19+
20+
ASSERT_EQ(distance, expected_distance);
21+
}
22+
23+
TEST(Dijkstra, Test1)
24+
{
25+
std::vector<std::vector<std::pair<int64_t, int64_t>>> graph = {
26+
{{1, 1}},
27+
{{2, 1}},
28+
{{3, 1}},
29+
{},
30+
};
31+
32+
int64_t source_node = 0;
33+
34+
std::vector<int64_t> distance = dijkstra(graph, source_node);
35+
36+
std::vector<int64_t> expected_distance = {0, 1, 2, 3};
37+
38+
ASSERT_EQ(distance, expected_distance);
39+
}
40+
41+
TEST(Dijkstra, Test2)
42+
{
43+
std::vector<std::vector<std::pair<int64_t, int64_t>>> graph = {
44+
{{1, 1}, {2, 1}},
45+
{{3, 1}},
46+
{{3, 1}},
47+
{},
48+
};
49+
50+
int64_t source_node = 0;
51+
52+
std::vector<int64_t> distance = dijkstra(graph, source_node);
53+
54+
std::vector<int64_t> expected_distance = {0, 1, 1, 2};
55+
56+
ASSERT_EQ(distance, expected_distance);
57+
}

main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <gtest/gtest.h>
2+
3+
int main(int argc, char **argv)
4+
{
5+
testing::InitGoogleTest(&argc, argv);
6+
return RUN_ALL_TESTS();
7+
}

0 commit comments

Comments
 (0)