Commit cd3f7aa9 authored by Stian Jakobsen's avatar Stian Jakobsen
Browse files

Cleaned up a bit, added readme, test that accidentally got removed and other minor things.

parent cd3cb4cd
2
3 3
S 1 1 1
1 2 -1 -1
2 3 -1 -1
3 4 -1 -1
4 T -1 -1
\ No newline at end of file
......@@ -24,9 +24,10 @@ namespace dte3603::predef::testing::week2_zombie::fixtures
void TearDown() final { gold.release(); }
};
using EmptyGraphF = GoldGraphTestF<gold::EmptyGraph>;
using FirstExampleGraphF = GoldGraphTestF<gold::FirstExampleGraph>;
using MiTExampleGraphF = GoldGraphTestF<gold::MiTGraphExample>;
using EmptyGraphF = GoldGraphTestF<gold::EmptyGraph>;
using FirstExampleGraphF = GoldGraphTestF<gold::FirstExampleGraph>;
using MiTExampleGraphF = GoldGraphTestF<gold::MiTGraphExample>;
using NegativeExampleGraphF = GoldGraphTestF<gold::NegativeCycleExampleGraph>;
// // Special purpose fixtures
// using SingleNodeDAGF = GoldGraphTestF<gold::SingleNodeDAG>;
......
......@@ -210,8 +210,11 @@ namespace dte3603::predef::testing::week2_zombie
VD& Source() { return source; }
VD& Sink() { return sink; }
VDVecVector bellmanFordGold() const { return {}; }
std::vector<std::pair<std::vector<VD>, double>> cycles;
std::vector<std::pair<std::vector<VD>, double>> bellmanFordGold()
{
return cycles;
}
};
// MiT Graph example from MiT lecture
......@@ -285,7 +288,82 @@ namespace dte3603::predef::testing::week2_zombie
VDVecVector bellmanFordGold() const { return {}; }
};
class NegativeCycleExampleGraph
: public detail::GoldGraphTemplate<detail::types::BidirectionalGraph> {
// Getting our current path and gonig for the flow_graphs folder that will
// hopefully be present *may* require platform-specific code for paths,
// since Windows can be funky.
std::string graph_src_path = std::filesystem::current_path().append(
"flow_graphs/negative_cycle_ex_graph.txt");
// Using ifstream rather than fstream since we only need to read the file
std::ifstream input_reader;
// number of floors and nodes as read from the file "header"
int n_floors;
std::vector<int> n_nodes;
// map of vertices in graph as read from file
std::unordered_map<std::string, VD> v_map;
// Vertex descriptors for the source and sink to be read from file
VD source, sink;
public:
NegativeCycleExampleGraph()
{
input_reader.open(graph_src_path);
input_reader >> n_floors;
// May need to implement some safety checks at some point
int v;
for (auto i = 0; i < n_floors; i++) {
input_reader >> v;
}
// Finding and adding all edges from file
while (input_reader.peek() != EOF) {
std::string v1, v2;
double cost, flow_cap;
input_reader >> v1 >> v2 >> flow_cap >> cost;
insertInMap(v1, "S", source);
insertInMap(v2, "T", sink);
boost::add_edge(
v_map.at(v1), v_map.at(v2),
EP{.flow = 0., .cost = cost, .flow_capacity = flow_cap}, m_graph);
}
}
// Would ideally be a universal helper method for all test classes.
// Could not work around an issue to solve this, ideally done in the
// future.
void insertInMap(std::string& v, std::string target, VD& v_target)
{
if (!v_map.contains(v)) {
if (v == target) {
v_target = boost::add_vertex(VP{.name = v}, m_graph);
v_map.insert(std::make_pair(v, v_target));
}
else {
v_map.insert(
std::make_pair(v, boost::add_vertex(VP{.name = v}, m_graph)));
}
}
}
~NegativeCycleExampleGraph() override {}
VD& Source() { return source; }
VD& Sink() { return sink; }
std::vector<std::pair<std::vector<VD>, double>> cycles;
std::vector<std::pair<std::vector<VD>, double>> bellmanFordGold()
{
return cycles;
}
};
} // namespace gold
} // namespace dte3603::predef::testing::week2_zombie
......
Project containing implementation/solution for week 1 and week 2 tasks in DTE-3603.
All tasks are available in the master branch and have been confirmed to compile with Clang 12.0.1 and GCC 11.1.0.
......@@ -16,8 +16,8 @@ namespace dte3603::week2_zombie::algorithms
// finding negative cycles in graphs (cycles where the edges add up to a
// negative value) reachable from the source
template <typename Graph_T>
// TODO: Return type might need to change, currently returning vector of a
// pair consisting of a vector with VDs, and a double
// Heavily inspired by Joachim Kristensen's Bellman-Ford implementation,
// credit goes to him for the main logic flow.
std::vector<
std::pair<std::vector<typename Graph_T::vertex_descriptor>, double>>
bellman_ford([[maybe_unused]] Graph_T& graph,
......
#ifndef DTE3603_WEEK2_ZOMBIE_MIN_COST_H
#define DTE3603_WEEK2_ZOMBIE_MIN_COST_H
namespace dte3603::week2_zombie::algorithms
{
}
#endif // DTE3603_WEEK2_ZOMBIE_MIN_COST_H
\ No newline at end of file
......@@ -4,7 +4,6 @@
// libs
#include <week2_zombie/bellman_ford.h>
#include <week2_zombie/goldberg_tarjan.h>
#include <week2_zombie/min_cost.h>
// gtest
#include <gtest/gtest.h>
......@@ -22,6 +21,13 @@ namespace alg = dte3603::week2_zombie::algorithms;
// algorithms
TEST_F(FirstExampleGraphF, bellmanFord)
{
auto paths = alg::bellman_ford(gold->graph(), gold->Source(), gold->Sink());
auto gold_paths = gold->bellmanFordGold();
EXPECT_EQ(paths, gold_paths);
}
TEST_F(NegativeExampleGraphF, bellmanFord)
{
auto paths = alg::bellman_ford(gold->graph(), gold->Source(), gold->Sink());
auto gold_paths = gold->bellmanFordGold();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment