Skip to content

Commit

Permalink
Modified: remove constraint for Weight
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmie committed Jul 19, 2022
1 parent c9980b6 commit 1199c46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
6 changes: 4 additions & 2 deletions Graph/Example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ int main()
{ "D", 2, "E" }, { "D", 5, "B" }, { "C", 4, "E" },
{ "B", 2, "F" }, { "F", 1, "E" }, { "C", 5, "F" },
};
graph.AddEdge("C", 3, "D");
graph.AddEdge("C", "D") = 3;
auto out_path = graph.GetOutPath("C");
out_path.clear();
graph.RemoveEdge("C", "D");
bool not_exist = graph.AddEdge("C", "D") == graph.INFINITE;
graph.GetOutPath("C", [&out_path](auto&& new_path) {out_path.push_back(new_path); });
auto in_path = graph.GetInPath("B");
in_path.clear();
graph.GetInPath("B", [&in_path](auto&& new_path) {in_path.push_back(new_path); });
auto weight = graph.GetWeight("C", "D");
auto weight = graph.AddEdge("C", "E");
not_exist = graph.AddEdge("C", "T") == graph.INFINITE;
auto result = graph.GetBestPath("A", "E");
return 0;
}
43 changes: 26 additions & 17 deletions Graph/Graph.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

template<typename Vertex, std::integral Weight = int>
template<typename Vertex, typename Weight = int, Weight Infinite = std::numeric_limits<Weight>::max()>
class Graph
{
using AdjacencyMatrix = std::unordered_map<Vertex, std::unordered_map<Vertex, Weight>>;
Expand All @@ -10,39 +10,48 @@ class Graph
using Compare = bool(*)(const Weight&, const Weight&);
std::function<std::vector<PathType>(const Vertex&, const Vertex&, Compare)> best_path;

constexpr static Weight INFINITE = std::numeric_limits<Weight>::max();

public:
constexpr static Weight INFINITE = Infinite;

Graph() = default;

Graph(auto&&... rests) { Construct(std::forward<decltype(rests)>(rests)...); }

Graph(std::initializer_list<std::tuple<Vertex, Weight, Vertex>> list)
{
for (auto&& element : list)
AddEdge(std::get<0>(element), std::get<1>(element), std::get<2>(element));
AddEdge(std::get<0>(element), std::get<2>(element)) = std::get<1>(element);
}

void AddEdge(const Vertex& begin, const Weight& weight, const Vertex& end)
Weight& AddEdge(const Vertex& begin, const Vertex& end)
{
matrix[begin][end] = weight;
if (matrix.contains(begin) && matrix[begin].contains(end))
return matrix[begin][end];
else
{
Weight& weight = matrix[begin][end];
weight = INFINITE;
return weight;
}
}

void AddEdge(Vertex&& begin, Weight&& weight, Vertex&& end)
Weight& AddEdge(Vertex&& begin, Vertex&& end)
{
matrix[std::move(begin)][std::move(end)] = std::move(weight);
if (matrix.contains(begin) && matrix[begin].contains(end))
return matrix[begin][end];
else
{
Weight& weight = matrix[std::move(begin)][std::move(end)];
weight = INFINITE;
return weight;
}
}

void RemoveEdge(const Vertex& begin, const Vertex& end)
{
matrix[begin][end] = INFINITE;
}

Weight& GetWeight(const Vertex& begin, const Vertex& end)
{
return matrix[begin][end];
}

auto GetOutPath(const Vertex& vertex)
{
std::vector<PathType> results;
Expand Down Expand Up @@ -87,13 +96,13 @@ class Graph
else
{
std::vector<std::vector<PathType>> results;
IterateGraph(results, end, {}, { begin, Weight{ 0 } }, {});
IterateGraph(results, end, {}, { begin, Weight{} }, {});
if (!results.empty())
{
std::vector<std::pair<Weight, std::size_t>> sort_weight;
for (size_t i = 0; i < results.size(); ++i)
sort_weight.emplace_back(
std::accumulate(results[i].begin(), results[i].end(), Weight{ 0 }, [](auto&& acc, auto&& pair) {return acc + pair.second; }),
std::accumulate(results[i].begin(), results[i].end(), Weight{}, [](auto&& acc, auto&& pair) {return acc + pair.second; }),
i);
std::sort(std::execution::par_unseq, sort_weight.begin(), sort_weight.end(), [comparator](auto&& first, auto&& second) {return comparator(first.first, second.first); });
auto& result = results[sort_weight[0].second];
Expand All @@ -107,13 +116,13 @@ class Graph
private:
void Construct(auto&& vertex1, auto&& weight, auto&& vertex2, auto&&... rests)
{
AddEdge(std::forward<decltype(vertex1)>(vertex1), std::forward<decltype(weight)>(weight), std::forward<decltype(vertex2)>(vertex2));
AddEdge(std::forward<decltype(vertex1)>(vertex1), std::forward<decltype(vertex2)>(vertex2)) = std::forward<decltype(weight)>(weight);
Construct(std::forward<decltype(rests)>(rests)...);
}

void Construct(auto&& vertex1, auto&& weight, auto&& vertex2)
{
AddEdge(std::forward<decltype(vertex1)>(vertex1), std::forward<decltype(weight)>(weight), std::forward<decltype(vertex2)>(vertex2));
AddEdge(std::forward<decltype(vertex1)>(vertex1), std::forward<decltype(vertex2)>(vertex2)) = std::forward<decltype(weight)>(weight);
}

void IterateGraph(std::vector<std::vector<PathType>>& results, const Vertex& target, std::vector<PathType> current_path, PathType new_path, std::unordered_set<Vertex> set)
Expand Down

0 comments on commit 1199c46

Please sign in to comment.