Skip to content

Commit

Permalink
feat: 链式图支持拓扑排序
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Oct 13, 2023
1 parent b7ce43a commit 193a518
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/01graph_topo/include/graph_topo/graph_topo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "builder.hpp"
#include "inplace_modifier.h"
#include "polymorph_graph.h"
#include "polymorph_graph.hpp"
#include "searcher.h"

#endif// GRAPH_TOPO_GRAPH_TOPO_H
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <sstream>
#include <unordered_map>
#include <unordered_set>

namespace refactor::graph_topo {
using namespace common;
Expand Down Expand Up @@ -40,6 +41,7 @@ namespace refactor::graph_topo {
NodeRc pushNode(TN, std::vector<EdgeRc>);
void eraseNode(size_t);
void eraseNode(NodeRc);
void sort();
};

template<class TN, class TE>
Expand All @@ -57,6 +59,8 @@ namespace refactor::graph_topo {
TN const &info() const;
std::vector<EdgeRc> const &inputs() const;
std::vector<EdgeRc> const &outputs() const;
std::unordered_set<NodeRc> const &predecessors() const;
std::unordered_set<NodeRc> const &successors() const;
void connect(size_t i, EdgeRc);
void disconnect(size_t i);
};
Expand Down Expand Up @@ -124,6 +128,9 @@ namespace refactor::graph_topo {

LINKED_GRAPH_FN setInputs(std::vector<EdgeRc> inputs)->void {
_inputs = std::move(inputs);
for (auto &e : _inputs) {
e->_source = nullptr;
}
}

LINKED_GRAPH_FN setOutputs(std::vector<EdgeRc> outputs)->void {
Expand Down Expand Up @@ -169,6 +176,23 @@ namespace refactor::graph_topo {
}
}

LINKED_GRAPH_FN sort()->void {
std::vector<NodeRc> ans;
ans.reserve(_nodes.size());
std::unordered_set<void *> known;
while (known.size() < _nodes.size()) {
for (auto &n : _nodes) {
if (!n) { continue; }
if (std::all_of(n->_inputs.begin(), n->_inputs.end(),
[&known](auto const &e) { return !e || !e->_source || known.find(e->_source.get()) != known.end(); })) {
known.insert(n.get());
ans.push_back(std::move(n));
}
}
}
_nodes = std::move(ans);
}

LINKED_GRAPH_FN Node::share(TN info, std::vector<EdgeRc> outputs)
->NodeRc {
auto ans = std::shared_ptr<Node>(new Node(std::move(info), std::move(outputs)));
Expand All @@ -190,6 +214,26 @@ namespace refactor::graph_topo {
return _outputs;
}

LINKED_GRAPH_FN Node::predecessors() const->std::unordered_set<NodeRc> const & {
std::unordered_set<NodeRc> ans;
for (auto const &e : _inputs) {
if (e->_source) {
ans.insert(e->_source);
}
}
return ans;
}

LINKED_GRAPH_FN Node::successors() const->std::unordered_set<NodeRc> const & {
std::unordered_set<NodeRc> ans;
for (auto const &e : _outputs) {
for (auto const &[n, _] : e->_targets) {
ans.insert(n);
}
}
return ans;
}

LINKED_GRAPH_FN Node::connect(size_t i, EdgeRc input)->void {
if (i < _inputs.size()) {
disconnect(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define GRAPH_TOPO_POLYMORPH_GRAPH_H

#include "container.h"
#include "linked_graph.h"
#include "linked_graph.hpp"
#include <variant>

namespace refactor::graph_topo {
Expand Down
5 changes: 4 additions & 1 deletion src/01graph_topo/test/test_linked.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "graph_topo/linked_graph.h"
#include "graph_topo/linked_graph.hpp"
#include "topo.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -29,4 +29,7 @@ TEST(GraphTopo, LinkedGraph) {

g_.eraseNode(n2);
fmt::println("{}", g_.toString());

g_.sort();
fmt::println("{}", g_.toString());
}

0 comments on commit 193a518

Please sign in to comment.