Skip to content

Commit a506a36

Browse files
committed
Merge branch 'develop'
2 parents 7291ace + 40143ad commit a506a36

File tree

124 files changed

+650
-590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+650
-590
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ MutableValueGraph<City, Distance> roads = ValueGraphBuilder.directed()
227227

228228
**Graph algorithms**
229229

230+
> NOTE: In the following, the C++ implementations are currently only available on MSVC.
231+
230232
- A\* search algorithm, CCSP#2.2.5: A single-pair shortest path algorithm. This is a variant of Dijkstra's algorithm using heuristics to try to speed up the search.
231233
- Bellman-Ford algorithm, CLRS#24.1: [c++](cpp-algorithm/src/cpp-algorithm/src/graph/bellman_ford.h), [java#1](java-algorithm/src/main/java/com/example/algorithm/graph/BellmanFord1.java), [java#2](java-algorithm/src/main/java/com/example/algorithm/graph/BellmanFord2.java) | A single source the shortest path algorithm that can handle negative edge weights. It finds the shortest path from a source vertex to all other vertices in a weighted graph.
232234
@@ -387,6 +389,8 @@ algorithm Prim(G, root):
387389

388390
**Examples**
389391

392+
> NOTE: In the following, the C++ implementations are currently only available on MSVC.
393+
390394
- Maze problem: [java](java-algorithm/src/main/java/com/example/algorithm/graph/MazeProblem.java) | A maze problem is that find a path from the start to the goal. The maze is represented by a graph. The start and the goal are represented by vertices. The path is represented by a sequence of vertices.
391395
- Minimum spanning tree (Kruskal, Prim, Boruvka), CLRS#23, CCSP#4.4.2: [python(test)](python-algorithm/algorithm/graph/test/test_minimum_spanning_tree.py) | Find the minimum spanning tree of a graph. cf. Kruskal(CLRS#23.2, CLRS#21.1), Prim(CLRS#23.2)
392396

cpp-algorithm/src/array/advancing_through.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
auto AdvancingThrough::CanReachEnd(const std::vector<int>& max_advance_steps) -> bool
44
{
5-
auto furthest_reach_so_far = 0;
6-
const auto last_index = static_cast<int>(max_advance_steps.size()) - 1;
5+
int reach_so_far = 0; // furthest reach so far
6+
const int last_index = static_cast<int>(max_advance_steps.size()) - 1;
77

8-
for (int i = 0; i <= furthest_reach_so_far && furthest_reach_so_far < last_index; ++i)
8+
for (int i = 0; i <= reach_so_far && reach_so_far < last_index; ++i)
99
{
10-
furthest_reach_so_far = std::max(furthest_reach_so_far, max_advance_steps[i] + i);
10+
reach_so_far = std::max(reach_so_far, max_advance_steps[i] + i);
1111
}
1212

13-
return furthest_reach_so_far >= last_index;
13+
return reach_so_far >= last_index;
1414
}

cpp-algorithm/src/array/arbitrary_precision_integer.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@ auto ArbitraryPrecision::PlusOne(std::vector<int> number_array) -> std::vector<i
2020
return number_array;
2121
}
2222

23-
auto ArbitraryPrecision::StringAddition(const std::string& number_string1, const std::string& number_string2) -> std::vector<int>
23+
auto ArbitraryPrecision::StringAddition(const std::string& number_string1,
24+
const std::string& number_string2)
25+
-> std::vector<int>
2426
{
25-
const auto size1 = static_cast<int>(number_string1.size());
26-
const auto size2 = static_cast<int>(number_string2.size());
27+
const int size1 = static_cast<int>(number_string1.size());
28+
const int size2 = static_cast<int>(number_string2.size());
2729

28-
const auto larger = size1 >= size2 ? size1 : size2;
30+
const int larger = size1 >= size2 ? size1 : size2;
2931
auto sum = std::vector<int>(larger);
3032

3133
for (int i = size1 - 1; i >= 0; --i)
3234
{
3335
sum[i] += number_string1.at(i) == '1' ? 1 : 0;
3436
}
3537

36-
auto carry = 0;
38+
int carry = 0;
3739
for (int i = size2 - 1; i >= 0; --i)
3840
{
3941
sum[i] += carry;
@@ -58,9 +60,11 @@ auto ArbitraryPrecision::StringAddition(const std::string& number_string1, const
5860
return sum;
5961
}
6062

61-
auto ArbitraryPrecision::Multiply(std::vector<int>& number_array1, std::vector<int>& number_array2) -> std::vector<int>
63+
auto ArbitraryPrecision::Multiply(std::vector<int>& number_array1,
64+
std::vector<int>& number_array2)
65+
-> std::vector<int>
6266
{
63-
const auto sign = ((number_array1.front() < 0) ^ (number_array2.front() < 0)) ? -1 : 1;
67+
const int sign = ((number_array1.front() < 0) ^ (number_array2.front() < 0)) ? -1 : 1;
6468
number_array1.front() = std::abs(number_array1.front());
6569
number_array2.front() = std::abs(number_array2.front());
6670

@@ -75,7 +79,8 @@ auto ArbitraryPrecision::Multiply(std::vector<int>& number_array1, std::vector<i
7579
}
7680
}
7781

78-
result = {std::ranges::find_if_not(begin(result), end(result), [](const int i) { return i == 0; }), end(result)};
82+
result = {std::ranges::find_if_not(begin(result), end(result), [](const int i) { return i == 0; }),
83+
end(result)};
7984

8085
if (std::empty(result))
8186
{

cpp-algorithm/src/array/benchmark/arbitrary_precision_integer_benchmark.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ BENCHMARK(BM_PlusOne);
1515

1616
static void BM_StringAddition(benchmark::State& state)
1717
{
18-
const auto number1 = std::string{"101"};
19-
const auto number2 = std::string{"101"};
18+
const std::string number1 = "101";
19+
const std::string number2 = "101";
2020
for (auto _ : state)
2121
{
2222
ArbitraryPrecision::StringAddition(number1, number2);

cpp-algorithm/src/array/benchmark/delete_element_benchmark.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BENCHMARK(BM_DeleteDuplicateElements);
2727
static void BM_DeleteSpecificElements(benchmark::State& state)
2828
{
2929
auto numbers = std::vector<int>{2, 3, 5, 5, 7, 11, 11, 11, 13};
30-
constexpr auto element = 11;
30+
constexpr int element = 11;
3131
for (auto _ : state)
3232
{
3333
DeleteElement::DeleteSpecificElements(numbers, element);

cpp-algorithm/src/array/benchmark/random_data_sampling_benchmark.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
static void BM_OfflineRandomSampling(benchmark::State& state)
66
{
7-
constexpr auto k = 3;
7+
constexpr int k = 3;
88
auto arr = std::vector<int>{3, 7, 5, 11};
99
for (auto _ : state)
1010
{
@@ -16,7 +16,7 @@ BENCHMARK(BM_OfflineRandomSampling);
1616

1717
static void BM_ComputeRandomPermutation(benchmark::State& state)
1818
{
19-
constexpr auto k = 3;
19+
constexpr int k = 3;
2020
for (auto _ : state)
2121
{
2222
RandomDataSampling::ComputeRandomPermutation(k);

cpp-algorithm/src/array/delete_element.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ auto DeleteElement::DeleteDuplicates(std::vector<int>& numbers) -> std::vector<i
99
return {};
1010
}
1111

12-
auto write_index = 1;
12+
int write_index = 1;
1313
for (int i = 1; i < static_cast<int>(numbers.size()); ++i)
1414
{
1515
if (numbers[write_index - 1] != numbers[i])

cpp-algorithm/src/array/dutch_national_flag.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ auto DutchFlag::DutchFlagPartition2(const int pivot_index, std::vector<Color>& a
3434
{
3535
const auto pivot = arr[pivot_index];
3636

37-
auto smaller = 0;
37+
int smaller = 0;
3838
for (int i = 0; i < static_cast<Color>(arr.size()); ++i)
3939
{
4040
if (arr[i] < pivot)
@@ -43,7 +43,7 @@ auto DutchFlag::DutchFlagPartition2(const int pivot_index, std::vector<Color>& a
4343
}
4444
}
4545

46-
auto larger = static_cast<Color>(arr.size()) - 1;
46+
int larger = static_cast<Color>(arr.size()) - 1;
4747
for (int i = static_cast<Color>(arr.size()) - 1; i >= 0; --i)
4848
{
4949
if (arr[i] > pivot)
@@ -59,8 +59,8 @@ auto DutchFlag::DutchFlagPartition3(const int pivot_index, std::vector<Color>& a
5959
{
6060
const auto pivot = arr[pivot_index];
6161

62-
auto smaller = 0;
63-
auto equal = 0;
62+
int smaller = 0;
63+
int equal = 0;
6464
int larger = static_cast<Color>(arr.size());
6565

6666
while (equal < larger)

cpp-algorithm/src/array/enumerate_prime_number.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <deque>
44

5-
auto EnumeratePrime::GeneratePrimes(int n) -> std::vector<int>
5+
auto EnumeratePrime::GeneratePrimes(const int n) -> std::vector<int>
66
{
77
std::vector<int> primes;
88
std::deque<bool> is_prime(n + 1, true);

cpp-algorithm/src/array/enumerate_prime_number.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
namespace EnumeratePrime
77
{
88
/**
9-
* brief Enumerate prime numbers in the range.
10-
* param n upper bound
11-
* return prime numbers
9+
* \brief Enumerate prime numbers in the range.
10+
* \param n upper bound
11+
* \return prime numbers
1212
*/
1313
auto GeneratePrimes(int n) -> std::vector<int>;
1414
}

cpp-algorithm/src/array/order_element.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
void OrderElement::EvenOdd(std::vector<int>& arr)
44
{
5-
auto next_even = 0;
6-
auto next_odd = static_cast<int>(arr.size()) - 1;
5+
int next_even = 0;
6+
int next_odd = static_cast<int>(arr.size()) - 1;
77

88
while (next_even < next_odd)
99
{

cpp-algorithm/src/array/random_data_sampling.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ namespace RandomDataSampling
2222
* \return result array
2323
*/
2424
auto OnlineRandomSampling(const std::vector<int>::const_iterator& begin,
25-
const std::vector<int>::const_iterator& end, int k) -> std::vector<int>;
25+
const std::vector<int>::const_iterator& end, int k)
26+
-> std::vector<int>;
2627

2728
/**
2829
* \brief Compute permutation of the array generated by random sampling.

cpp-algorithm/src/array/replace_element.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "replace_element.h"
22

3-
auto ReplaceElement::ReplaceAndRemoveString1(std::vector<std::string>& arr, const std::string& replace_str,
4-
const std::string& remove_str) -> std::vector<std::string>
3+
auto ReplaceElement::ReplaceAndRemoveString1(std::vector<std::string>& arr,
4+
const std::string& replace_str,
5+
const std::string& remove_str)
6+
-> std::vector<std::string>
57
{
6-
auto write_index = 0;
7-
auto a_count = 0;
8+
int write_index = 0;
9+
int a_count = 0;
810
for (int i = 0; i < static_cast<int>(arr.size()); ++i)
911
{
1012
if (arr[i] != remove_str)
@@ -17,9 +19,9 @@ auto ReplaceElement::ReplaceAndRemoveString1(std::vector<std::string>& arr, cons
1719
}
1820
}
1921

20-
auto current_index = write_index - 1;
22+
int current_index = write_index - 1;
2123
write_index = write_index + a_count - 1;
22-
auto final_size = write_index + 1;
24+
const int final_size = write_index + 1;
2325
while (current_index >= 0)
2426
{
2527
if (arr[current_index] == replace_str)
@@ -37,8 +39,10 @@ auto ReplaceElement::ReplaceAndRemoveString1(std::vector<std::string>& arr, cons
3739
return std::vector<std::string>{arr.begin(), arr.begin() + final_size};
3840
}
3941

40-
auto ReplaceElement::ReplaceAndRemoveString2(std::vector<std::string>& arr, const std::string& replace_str,
41-
const std::string& remove_str) -> std::vector<std::string>
42+
auto ReplaceElement::ReplaceAndRemoveString2(std::vector<std::string>& arr,
43+
const std::string& replace_str,
44+
const std::string& remove_str)
45+
-> std::vector<std::string>
4246
{
4347
std::erase(arr, remove_str);
4448
for (int i = 0; i < static_cast<int>(arr.size()); ++i)

cpp-algorithm/src/array/replace_element.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ namespace ReplaceElement
1313
* \param remove_str remove string
1414
* \return result array
1515
*/
16-
auto ReplaceAndRemoveString1(std::vector<std::string>& arr, const std::string& replace_str,
17-
const std::string& remove_str) -> std::vector<std::string>;
16+
auto ReplaceAndRemoveString1(std::vector<std::string>& arr,
17+
const std::string& replace_str,
18+
const std::string& remove_str)
19+
-> std::vector<std::string>;
1820

1921
/**
2022
* \brief Replace element and remove element in the array.
@@ -24,8 +26,10 @@ namespace ReplaceElement
2426
* \param remove_str remove string
2527
* \return result array
2628
*/
27-
auto ReplaceAndRemoveString2(std::vector<std::string>& arr, const std::string& replace_str,
28-
const std::string& remove_str) -> std::vector<std::string>;
29+
auto ReplaceAndRemoveString2(std::vector<std::string>& arr,
30+
const std::string& replace_str,
31+
const std::string& remove_str)
32+
-> std::vector<std::string>;
2933

3034
/**
3135
* \brief Telex encoding for punctuation marks.

cpp-algorithm/src/array/stock_trading.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
auto StockTrading::BuyAndSellStockOnceBruteForce(const std::vector<int>& prices) -> double
66
{
7-
auto max_profit = 0.0;
7+
double max_profit = 0.0;
88

99
for (int i = 0; i < static_cast<int>(prices.size()); ++i)
1010
{
1111
for (int j = i + 1; j < static_cast<int>(prices.size()); ++j)
1212
{
13-
if (const auto profit = prices[j] - prices[i]; profit > max_profit)
13+
if (const int profit = prices[j] - prices[i]; profit > max_profit)
1414
{
1515
max_profit = profit;
1616
}
@@ -22,8 +22,8 @@ auto StockTrading::BuyAndSellStockOnceBruteForce(const std::vector<int>& prices)
2222

2323
auto StockTrading::BuyAndSellStockOnce(const std::vector<int>& prices) -> double
2424
{
25-
auto min_price_so_far = std::numeric_limits<double>::infinity();
26-
auto max_profit = 0.0;
25+
double min_price_so_far = std::numeric_limits<double>::infinity();
26+
double max_profit = 0.0;
2727

2828
for (double price : prices)
2929
{
@@ -39,7 +39,7 @@ auto StockTrading::BuyAndSellStockTwice(const std::vector<double>& prices) -> do
3939
{
4040
double max_total_profit = 0;
4141
std::vector<double> first_buy_sell_profits(prices.size(), 0);
42-
auto min_price_so_far = std::numeric_limits<double>::infinity();
42+
double min_price_so_far = std::numeric_limits<double>::infinity();
4343

4444
for (int i = 0; i < static_cast<int>(prices.size()); ++i)
4545
{
@@ -48,7 +48,7 @@ auto StockTrading::BuyAndSellStockTwice(const std::vector<double>& prices) -> do
4848
first_buy_sell_profits[i] = max_total_profit;
4949
}
5050

51-
auto max_price_so_far = std::numeric_limits<double>::min();
51+
double max_price_so_far = std::numeric_limits<double>::min();
5252
for (int i = static_cast<int>(prices.size()) - 1; i > 0; --i)
5353
{
5454
max_price_so_far = std::max(max_price_so_far, prices[i]);

cpp-algorithm/src/array/test/arbitrary_precision_integer_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ GTEST_TEST(ArbitraryPrecision, PlusOne)
1313

1414
GTEST_TEST(ArbitraryPrecision, StringAddition)
1515
{
16-
const auto number1 = std::string{"101"};
17-
const auto number2 = std::string{"101"};
16+
const std::string number1 = "101";
17+
const std::string number2 = "101";
1818
const auto expected = std::vector<int>{1, 0, 1, 0};
1919
auto result = ArbitraryPrecision::StringAddition(number1, number2);
2020
std::ranges::reverse(result.begin(), result.end());

cpp-algorithm/src/array/test/delete_element_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ GTEST_TEST(DeleteElement, DeleteDuplicateElements)
2121
GTEST_TEST(DeleteElement, DeleteSpecificElements)
2222
{
2323
auto numbers = std::vector<int>{2, 3, 5, 5, 7, 11, 11, 11, 13};
24-
constexpr auto element = 11;
24+
constexpr int element = 11;
2525
const auto expected = std::vector<int>{2, 3, 5, 5, 7, 13};
2626
const auto result = DeleteElement::DeleteSpecificElements(numbers, element);
2727
EXPECT_EQ(expected, result);

cpp-algorithm/src/array/test/random_data_sampling_test.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
GTEST_TEST(RandomDataSampling, OfflineRandomSampling)
77
{
8-
constexpr auto k = 3;
8+
constexpr int k = 3;
99
auto arr = std::vector<int>{3, 7, 5, 11};
1010
const auto result = RandomDataSampling::OfflineRandomSampling(k, arr);
1111

1212
std::stringstream stream;
13-
for (const auto& item : result)
13+
for (const int& item : result)
1414
{
1515
stream << item << " ";
1616
}
@@ -21,12 +21,12 @@ GTEST_TEST(RandomDataSampling, OfflineRandomSampling)
2121

2222
GTEST_TEST(RandomDataSampling, ComputeRandomPermutation)
2323
{
24-
constexpr auto k = 3;
24+
constexpr int k = 3;
2525
auto arr = std::vector<int>{3, 7, 5, 11};
2626
const auto result = RandomDataSampling::ComputeRandomPermutation(k);
2727

2828
std::stringstream stream;
29-
for (const auto& item : result)
29+
for (const int& item : result)
3030
{
3131
stream << item << " ";
3232
}

0 commit comments

Comments
 (0)