Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
90 changes: 90 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ on:
branches: [master]

jobs:
cpp-static-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install cppcheck
run: sudo apt-get install -y cppcheck

- name: Run C++ static analysis
run: src/cpp/test/static_analysis.sh

cpp:
runs-on: ubuntu-latest
steps:
Expand All @@ -21,6 +32,52 @@ jobs:
- name: Run C++ BinarySearch test
run: src/cpp/test/Search/test_binary_search.sh

- name: Run C++ Knapsack test
run: src/cpp/test/DP/test_knapsack.sh

- name: Run C++ Dijkstra test
run: src/cpp/test/Graph/test_dijkstra.sh

- name: Run C++ Floyd test
run: src/cpp/test/Graph/Floyd/test_floyd.sh

- name: Run C++ Prim test
run: src/cpp/test/Graph/test_prim.sh

- name: Run C++ GCD test
run: src/cpp/test/Math/Calc/test_gcd.sh

- name: Run C++ LCM test
run: src/cpp/test/Math/Calc/test_lcm.sh

- name: Run C++ Primes test
run: src/cpp/test/Math/Calc/test_primes.sh

- name: Run C++ Comb test
run: src/cpp/test/Math/Combination/test_comb.sh

- name: Run C++ ModPow test
run: src/cpp/test/Math/ModPow/test_modpow.sh

- name: Run C++ SegmentTree test
run: src/cpp/test/Tree/SegmentTree/test_segmenttree.sh

- name: Run C++ UnionFind test
run: src/cpp/test/Tree/UnionFind/test_unionfind.sh

java-static-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'

- name: Run Java static analysis
run: src/java/test/static_analysis.sh

java:
runs-on: ubuntu-latest
steps:
Expand All @@ -39,3 +96,36 @@ jobs:

- name: Run Java BinarySearch test
run: src/java/test/Search/test_binary_search.sh

- name: Run Java Knapsack test
run: src/java/test/DP/test_knapsack.sh

- name: Run Java Dijkstra test
run: src/java/test/Graph/test_dijkstra.sh

- name: Run Java Floyd test
run: src/java/test/Graph/Floyd/test_floyd.sh

- name: Run Java Prim test
run: src/java/test/Graph/test_prim.sh

- name: Run Java GCD test
run: src/java/test/Math/Calc/test_gcd.sh

- name: Run Java LCM test
run: src/java/test/Math/Calc/test_lcm.sh

- name: Run Java Primes test
run: src/java/test/Math/Calc/test_primes.sh

- name: Run Java Comb test
run: src/java/test/Math/Combination/test_comb.sh

- name: Run Java ModPow test
run: src/java/test/Math/ModPow/test_modpow.sh

- name: Run Java SegTree test
run: src/java/test/Tree/SegmentTree/test_segtree.sh

- name: Run Java UnionFind test
run: src/java/test/Tree/UnionFind/test_unionfind.sh
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
<h3> Implementation of algorithms </h3>
<h3> Implementation of algorithms </h3>

C++ and Java implementations.

#### Algorithms

| Category | Algorithm | Description |
|----------|-----------|-------------|
| DP | Knapsack | 0-1 knapsack problem |
| Graph | Dijkstra | Shortest path (non-negative edges) |
| Graph | Floyd | All-pairs shortest path, negative cycle detection |
| Graph | Prim | Minimum spanning tree |
| Math | GCD | Greatest common divisor |
| Math | LCM | Least common multiple |
| Math | Primes | Prime factorization, Sieve of Eratosthenes, Sieve of Atkin |
| Math | Combination | nCk with modular inverse |
| Math | ModPow | Modular exponentiation by repeated squaring |
| Search | BFS | Breadth-first search |
| Search | DFS | Depth-first search |
| Search | BinarySearch | Lower bound / upper bound |
| Tree | SegmentTree | Range Minimum Query |
| Tree | UnionFind | Union-Find with path compression |

#### Test

```
# C++
src/cpp/test/<Category>/test_<name>.sh

# Java
src/java/test/<Category>/test_<name>.sh
```
26 changes: 6 additions & 20 deletions src/cpp/lib/DP/Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
int n, w;
cin >> n >> w;

vector<array<int, 2>> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i][0] >> arr[i][1];
}

// 0-1 Knapsack problem
// arr[i] = {value, weight}
// dp[i][j] = max value using first i items with capacity j
vector<vector<int>> knapsack(const vector<array<int, 2>>& arr, int w) {
int n = arr.size();
vector<vector<int>> dp(n + 1, vector<int>(w + 1, 0));

for (int i = 1; i <= n; i++) {
Expand All @@ -21,15 +17,5 @@ int main() {
}
}

cout << "DP table:" << endl;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < w; j++) {
cout << dp[i][j] << " ";
}
cout << dp[i][w] << endl;
}
cout << "ans:" << endl;
cout << dp[n][w];

return 0;
return dp;
}
32 changes: 7 additions & 25 deletions src/cpp/lib/Graph/Dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//コストが負の辺がない場合(最短から求めるため負の辺を後から取り込むことができない)
// For graphs with non-negative edge costs
#include <bits/stdc++.h>
using namespace std;

Expand All @@ -9,28 +9,14 @@ struct Node {
}
};

int main() {
int n;
cin >> n;

vector<vector<Node>> adj(n);
vector<int> dijkstra(int start, const vector<vector<Node>>& adj) {
int n = adj.size();
vector<int> d(n, INT_MAX);
vector<bool> visit(n, false);

for (int i = 0; i < n; i++) {
int v, k;
cin >> v >> k;
for (int j = 0; j < k; j++) {
int u, c;
cin >> u >> c;
adj[v].push_back({u, c});
}
}

// dijkstra
d[0] = 0;
d[start] = 0;
priority_queue<Node, vector<Node>, greater<Node>> pq;
pq.push({0, 0});
pq.push({start, 0});

while (!pq.empty()) {
Node nd = pq.top();
Expand All @@ -39,7 +25,7 @@ int main() {
visit[v] = true;
if (d[v] < nd.cost) continue;

for (auto& node : adj[v]) {
for (const auto& node : adj[v]) {
int t = node.node;
if (visit[t]) continue;
if (d[t] > d[v] + node.cost) {
Expand All @@ -49,9 +35,5 @@ int main() {
}
}

for (int i = 0; i < n; i++) {
cout << d[i] << " ";
}

return 0;
return d;
}
37 changes: 7 additions & 30 deletions src/cpp/lib/Graph/Floyd/Floyd.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
int n, m;
cin >> n >> m;

vector<vector<int>> edge(n, vector<int>(n, INT_MAX));
for (int i = 0; i < n; i++) edge[i][i] = 0;

for (int i = 0; i < m; i++) {
int v, u, d;
cin >> v >> u >> d;
edge[v][u] = d;
}

// floyd 計算量はノード数Vとした時、O(|V|^3)。
for (int k = 0; k < n; k++) { // ノードiからjへ移動する時の経由地をkとする。
// Floyd-Warshall. O(|V|^3).
// Returns true if a negative cycle is detected
bool floyd(vector<vector<int>>& edge) {
int n = edge.size();
for (int k = 0; k < n; k++) { // k is the intermediate node from i to j
for (int i = 0; i < n; i++) {
if (edge[i][k] == INT_MAX) continue;
for (int j = 0; j < n; j++) {
Expand All @@ -25,21 +15,8 @@ int main() {
}
}

bool negative = false;
for (int i = 0; i < n; i++) {
if (edge[i][i] < 0) negative = true; // 負の閉路があれば対自ノードへのコストは負となる。
}

if (negative) {
cout << "negative cycle" << endl;
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
cout << edge[i][j] << " ";
}
cout << edge[i][n - 1] << endl;
}
if (edge[i][i] < 0) return true; // negative cycle: self-loop cost becomes negative
}

return 0;
return false;
}
25 changes: 6 additions & 19 deletions src/cpp/lib/Graph/Prim.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

vector<vector<int>> edge(n, vector<int>(n));
vector<int> d(n, INT_MAX); // 最小全域木を構成する時の始点からの距離
vector<int> p(n, -1); // 最小全域木を構成する時の親ノード
// Minimum spanning tree weight using Prim's algorithm
int prim(const vector<vector<int>>& edge) {
int n = edge.size();
vector<int> d(n, INT_MAX); // distance from start when constructing MST
vector<int> p(n, -1); // parent node when constructing MST
vector<bool> visited(n, false);

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int t;
cin >> t;
edge[i][j] = (t == -1) ? INT_MAX : t;
}
}

// prim
d[0] = 0;
while (true) {
int minV = INT_MAX;
Expand Down Expand Up @@ -47,7 +36,5 @@ int main() {
sum += edge[i][p[i]];
}
}
cout << sum << endl;

return 0;
return sum;
}
8 changes: 8 additions & 0 deletions src/cpp/lib/Math/Calc/GCD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <bits/stdc++.h>
using namespace std;

long long gcd(long long c, long long d) {
if (c < d) return gcd(d, c);
if (d == 0) return c;
return gcd(d, c % d);
}
6 changes: 3 additions & 3 deletions src/cpp/lib/Math/Calc/Primes.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

// 素因数分解
// Prime factorization
// key: prime, value: count
map<int, int> primes(int n) {
vector<int> list;
Expand Down Expand Up @@ -30,7 +30,7 @@ map<int, int> primes(int n) {
return mp;
}

// n以下の素数列挙
// List primes up to n
// O(NloglogN)
vector<int> eratosthenes(int n) {
vector<bool> flag(n + 1, false);
Expand All @@ -51,7 +51,7 @@ vector<int> eratosthenes(int n) {
return list;
}

// n以下の素数列挙
// List primes up to n
vector<int> atkin(int n) {
vector<bool> flag(n + 1, false);
int sq = (int)sqrt(n);
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/lib/Math/Combination/Comb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Comb {
return b % 2 == 0 ? x * x % MOD : x * (x * a % MOD) % MOD;
}

// 逆元。x^(-1)x^(p-2) (MOD p) xとpは互いに素。
// Modular inverse. x^(-1)x^(p-2) (MOD p), x and p are coprime.
long long inv(long long n) {
return modpow(n, MOD - 2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/lib/Math/ModPow/ModPow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using namespace std;

long long MOD;

// 繰り返し二乗法によるべき乗計算。O(logN)
// Modular exponentiation by repeated squaring. O(logN).
long long power(long long x, long long n) {
long long sum = 1;
while (n > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/lib/Search/BFS/BFS.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

void bfs(int s, vector<vector<int>>& adj, vector<int>& d) {
void bfs(int s, const vector<vector<int>>& adj, vector<int>& d) {
fill(d.begin(), d.end(), INT_MAX);
d[s] = 0;
deque<int> q;
Expand Down
Loading