Skip to content

Commit

Permalink
added eulerian path and cycle check
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jan 24, 2024
1 parent d5cf57a commit cabd97a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ template <typename T> class graph {
*/
bool connected();

/*
*eulerian function.
*Returns 0 if a graph is not eulerian.
*Returns 1 if a graph is semi-eulerian.
*Returns 2 if a graph is eulerian.
*/
int eulerian();

/*
* visualize function.
* Returns .dot file that can be previewed in vscode with graphviz.
Expand Down Expand Up @@ -435,6 +443,24 @@ template <typename T> bool graph<T>::connected() {
return true;
}

template <typename T> int graph<T>::eulerian() {
if (this->connected() == false) {
return false;
}

int64_t odd = 0;
for (auto &el : adj) {
if (adj[el.first].size() & 1) {
odd++;
}
}

if (odd > 2) {
return false;
}
return (odd) ? 1 : 2;
}

template <typename T> void graph<T>::visualize() {
std::string s;
if (__type == "directed") {
Expand Down Expand Up @@ -643,6 +669,14 @@ template <typename T> class weighted_graph {
*/
bool connected();

/*
*eulerian function.
*Returns 0 if a graph is not eulerian.
*Returns 1 if a graph is semi-eulerian.
*Returns 2 if a graph is eulerian.
*/
int eulerian();

/*
* visualize function.
* Returns .dot file that can be previewed in vscode with graphviz.
Expand Down Expand Up @@ -1009,6 +1043,25 @@ template <typename T> bool weighted_graph<T>::connected() {
return true;
}

template <typename T> int weighted_graph<T>::eulerian() {
if (this->connected() == false) {
return false;
}

int odd = 0;
for (auto &el : adj) {
if (adj[el.first].size() & 1) {
odd++;
}
}

if (odd > 2) {
return 0;
}

return (odd) ? 1 : 2;
}

template <typename T> void weighted_graph<T>::visualize() {
std::string s;
if (__type == "directed") {
Expand Down
29 changes: 29 additions & 0 deletions tests/graph/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,33 @@ TEST_CASE("testing connectivity") {
g2.add_edge(0, 1);
g2.add_edge(5, 6);
REQUIRE(g2.connected() == false);
}

TEST_CASE("testing eulerian check") {
graph<int> g("undirected");
g.add_edge(1, 3);
g.add_edge(1, 0);
g.add_edge(1, 2);
g.add_edge(2, 0);
g.add_edge(0, 3);
g.add_edge(3, 4);

REQUIRE(g.eulerian() == 0);

graph<int> g2("undirected");
g2.add_edge(1, 2);
g2.add_edge(2, 0);
g2.add_edge(1, 0);
g2.add_edge(0, 3);
g2.add_edge(3, 4);
REQUIRE(g2.eulerian() == 1);

graph<int> g3("undirected");
g3.add_edge(1, 0);
g3.add_edge(1, 2);
g3.add_edge(2, 0);
g3.add_edge(0, 3);
g3.add_edge(0, 4);
g3.add_edge(3, 4);
REQUIRE(g3.eulerian() == 2);
}
29 changes: 29 additions & 0 deletions tests/graph/weighted_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,33 @@ TEST_CASE("testing connectivity") {
g2.add_edge(0, 1, 100);
g2.add_edge(5, 6, 9999);
REQUIRE(g2.connected() == false);
}

TEST_CASE("testing eulerian check") {
weighted_graph<int> g("undirected");
g.add_edge(1, 3, 5);
g.add_edge(1, 0, 10);
g.add_edge(1, 2, 4);
g.add_edge(2, 0, 3);
g.add_edge(0, 3, 14);
g.add_edge(3, 4, 13);

REQUIRE(g.eulerian() == 0);

weighted_graph<int> g2("undirected");
g2.add_edge(1, 2, 11);
g2.add_edge(2, 0, 10);
g2.add_edge(1, 0, 8);
g2.add_edge(0, 3, 4);
g2.add_edge(3, 4, 3);
REQUIRE(g2.eulerian() == 1);

weighted_graph<int> g3("undirected");
g3.add_edge(1, 0, 10);
g3.add_edge(1, 2, 12);
g3.add_edge(2, 0, 13);
g3.add_edge(0, 3, 15);
g3.add_edge(0, 4, 13);
g3.add_edge(3, 4, 8);
REQUIRE(g3.eulerian() == 2);
}

0 comments on commit cabd97a

Please sign in to comment.