-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce747d5
commit 1d13b2d
Showing
12 changed files
with
201 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#ifdef __cplusplus | ||
#include <iostream> | ||
#endif | ||
|
||
#ifndef GCD_H | ||
#define GCD_H | ||
|
||
namespace gcd { | ||
|
||
/* | ||
*binary_gcd function. | ||
*@param a: first element. | ||
*@param b: second element. | ||
*Returns the gcd(a,b). | ||
*/ | ||
int64_t binary_gcd(const int64_t a, const int64_t b) { | ||
if (a == b) { | ||
return a; | ||
} | ||
if (a == 0) { | ||
return b; | ||
} | ||
if (b == 0) { | ||
return a; | ||
} | ||
|
||
if (~a & 1) { | ||
if (~b & 1) { | ||
return binary_gcd(a >> 1, b >> 1) << 1; | ||
} else { | ||
return binary_gcd(a >> 1, b); | ||
} | ||
} | ||
if (~b & 1) { | ||
return binary_gcd(a, b >> 1); | ||
} | ||
|
||
if (a > b) { | ||
return binary_gcd((a - b) >> 1, b); | ||
} | ||
return binary_gcd((b - a) >> 1, a); | ||
} | ||
|
||
/* | ||
*euclidean_gcd function. | ||
*@param a: first element. | ||
*@param b: second element. | ||
*Returns the gcd(a,b). | ||
*/ | ||
int64_t euclidean_gcd(int64_t a, int64_t b) { | ||
while (a > 0 && b > 0) { | ||
if (a > b) { | ||
a = a - b; | ||
} else { | ||
b = b - a; | ||
} | ||
} | ||
return (a + b); | ||
} | ||
|
||
}; // namespace gcd | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,86 @@ | ||
#ifndef DISJOINT_SET_H | ||
#define DISJOINT_SET_H | ||
|
||
#ifdef __cplusplus | ||
#include <vector> | ||
#endif | ||
|
||
class dsu { | ||
private: | ||
std::vector<uint64_t> p; ///< keeps track of the parent of ith element | ||
std::vector<uint64_t> depth; ///< tracks the depth(rank) of i in the tree | ||
std::vector<uint64_t> setSize; ///< size of each chunk(set) | ||
std::vector<uint64_t> maxElement; ///< maximum of each set to which i belongs to | ||
std::vector<uint64_t> minElement; ///< minimum of each set to which i belongs to | ||
public: | ||
|
||
explicit dsu(uint64_t n) { | ||
p.assign(n, 0); | ||
for (uint64_t i = 0; i < n; i++) { | ||
p[i] = i; | ||
} | ||
depth.assign(n, 0); | ||
maxElement.assign(n, 0); | ||
minElement.assign(n, 0); | ||
for (uint64_t i = 0; i < n; i++) { | ||
depth[i] = 0; | ||
maxElement[i] = i; | ||
minElement[i] = i; | ||
} | ||
setSize.assign(n, 0); | ||
for (uint64_t i = 0; i < n; i++) { | ||
setSize[i] = 1; | ||
} | ||
private: | ||
std::vector<uint64_t> p; ///< keeps track of the parent of ith element | ||
std::vector<uint64_t> depth; ///< tracks the depth(rank) of i in the tree | ||
std::vector<uint64_t> setSize; ///< size of each chunk(set) | ||
std::vector<uint64_t> | ||
maxElement; ///< maximum of each set to which i belongs to | ||
std::vector<uint64_t> | ||
minElement; ///< minimum of each set to which i belongs to | ||
public: | ||
explicit dsu(uint64_t n) { | ||
p.assign(n, 0); | ||
for (uint64_t i = 0; i < n; i++) { | ||
p[i] = i; | ||
} | ||
depth.assign(n, 0); | ||
maxElement.assign(n, 0); | ||
minElement.assign(n, 0); | ||
for (uint64_t i = 0; i < n; i++) { | ||
depth[i] = 0; | ||
maxElement[i] = i; | ||
minElement[i] = i; | ||
} | ||
|
||
uint64_t findSet(uint64_t i) { | ||
if (p[i] == i) { | ||
return i; | ||
} | ||
return (p[i] = findSet(p[i])); | ||
setSize.assign(n, 0); | ||
for (uint64_t i = 0; i < n; i++) { | ||
setSize[i] = 1; | ||
} | ||
void UnionSet(uint64_t i, uint64_t j) { | ||
if (isSame(i, j)) { | ||
return; | ||
} | ||
} | ||
|
||
uint64_t x = findSet(i); | ||
uint64_t y = findSet(j); | ||
uint64_t findSet(uint64_t i) { | ||
if (p[i] == i) { | ||
return i; | ||
} | ||
return (p[i] = findSet(p[i])); | ||
} | ||
void UnionSet(uint64_t i, uint64_t j) { | ||
if (isSame(i, j)) { | ||
return; | ||
} | ||
|
||
if (depth[x] > depth[y]) { | ||
std::swap(x, y); | ||
} | ||
p[x] = y; | ||
uint64_t x = findSet(i); | ||
uint64_t y = findSet(j); | ||
|
||
if (depth[x] == depth[y]) { | ||
depth[y]++; | ||
} | ||
setSize[y] += setSize[x]; | ||
maxElement[y] = std::max(maxElement[x], maxElement[y]); | ||
minElement[y] = std::min(minElement[x], minElement[y]); | ||
if (depth[x] > depth[y]) { | ||
std::swap(x, y); | ||
} | ||
|
||
bool isSame(uint64_t i, uint64_t j) { | ||
if (findSet(i) == findSet(j)) { | ||
return true; | ||
} | ||
return false; | ||
p[x] = y; | ||
|
||
if (depth[x] == depth[y]) { | ||
depth[y]++; | ||
} | ||
|
||
std::vector<uint64_t> get(uint64_t i) { | ||
std::vector<uint64_t> ans; | ||
ans.push_back(get_min(i)); | ||
ans.push_back(get_max(i)); | ||
ans.push_back(size(i)); | ||
return ans; | ||
setSize[y] += setSize[x]; | ||
maxElement[y] = std::max(maxElement[x], maxElement[y]); | ||
minElement[y] = std::min(minElement[x], minElement[y]); | ||
} | ||
|
||
bool isSame(uint64_t i, uint64_t j) { | ||
if (findSet(i) == findSet(j)) { | ||
return true; | ||
} | ||
|
||
uint64_t size(uint64_t i) { return setSize[findSet(i)]; } | ||
|
||
uint64_t get_max(uint64_t i) { return maxElement[findSet(i)]; } | ||
|
||
uint64_t get_min(uint64_t i) { return minElement[findSet(i)]; } | ||
return false; | ||
} | ||
|
||
std::vector<uint64_t> get(uint64_t i) { | ||
std::vector<uint64_t> ans; | ||
ans.push_back(get_min(i)); | ||
ans.push_back(get_max(i)); | ||
ans.push_back(size(i)); | ||
return ans; | ||
} | ||
|
||
uint64_t size(uint64_t i) { return setSize[findSet(i)]; } | ||
|
||
uint64_t get_max(uint64_t i) { return maxElement[findSet(i)]; } | ||
|
||
uint64_t get_min(uint64_t i) { return minElement[findSet(i)]; } | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#define CATCH_CONFIG_MAIN | ||
#include "../../src/algorithms/number_theory/gcd.h" | ||
#include "../catch2/catch.hpp" | ||
#include <string> | ||
|
||
using namespace gcd; | ||
|
||
TEST_CASE("testing binary gcd") { | ||
int64_t a = 100, b = 4; | ||
REQUIRE(binary_gcd(a, b) == 4); | ||
REQUIRE(binary_gcd(b, a) == 4); | ||
REQUIRE(euclidean_gcd(a, b) == 4); | ||
a = 100; | ||
b = 400; | ||
REQUIRE(binary_gcd(a, b) == 100); | ||
REQUIRE(euclidean_gcd(a, b) == 100); | ||
a = 90; | ||
b = 85430; | ||
REQUIRE(binary_gcd(a, b) == 10); | ||
REQUIRE(euclidean_gcd(a, b) == 10); | ||
} |