diff --git a/TinySTL/Detail/Graph.impl.h b/TinySTL/Detail/Graph.impl.h index 5b674cb..0703d6b 100644 --- a/TinySTL/Detail/Graph.impl.h +++ b/TinySTL/Detail/Graph.impl.h @@ -8,13 +8,14 @@ namespace TinySTL{ return node_type(index, val); } template - typename graph::node_type& + typename const graph::node_type& graph::get_node(const Index& index){ for (auto& pair : nodes_){ if (equal_func(pair.first.first, index)) return pair.first; } - return node_type(); + static node_type empty_node; + return empty_node; } template bool graph::is_contained(const Index& index){ @@ -78,33 +79,35 @@ namespace TinySTL{ return adjacent_nodes(n.first); } template - void graph::DFS(const Index& index, visiter_func_type func){ - node_type *start = &(get_node(index)); - Unordered_set, EqualFunc> visited(7); - - auto nodes = adjacent_nodes(start->first); - func(*start); - visited.insert(start->first); - for (const auto& n : nodes){ + void graph::_DFS(node_type& node, + visiter_func_type func, Unordered_set, EqualFunc>& visited){ + auto nodes = adjacent_nodes(node.first); + func(node); + visited.insert(node.first); + for (auto& n : nodes){ if (visited.count(n.first) == 0)//has not visited - DFS(n.first, func); + _DFS(n, func, visited); } } template - void graph::BFS(const Index& index, visiter_func_type func){ - node_type *start = &(get_node(index)); + void graph::DFS(const Index& index, visiter_func_type func){ + node_type start = (get_node(index)); Unordered_set, EqualFunc> visited(7); - - auto nodes = adjacent_nodes(start->first); - func(*start); - visited.insert(start->first); + _DFS(start, func, visited); + } + template + void graph::_BFS(node_type& node, + visiter_func_type func, Unordered_set, EqualFunc>& visited){ + auto nodes = adjacent_nodes(node.first); + func(node); + visited.insert(node.first); do{ nodes_set_type temp; for (auto it = nodes.begin(); it != nodes.end(); ++it){ if (visited.count(it->first) == 0){//has not visited func(*it); visited.insert(it->first); - auto s = adjacent_nodes(it->first); + auto s = adjacent_nodes(it->first); temp.insert(temp.end(), s.begin(), s.end()); } } @@ -112,6 +115,27 @@ namespace TinySTL{ } while (!nodes.empty()); } template + void graph::BFS(const Index& index, visiter_func_type func){ + node_type start = (get_node(index)); + Unordered_set, EqualFunc> visited(7); + _BFS(start, func, visited); + //auto nodes = adjacent_nodes(start->first); + //func(*start); + //visited.insert(start->first); + //do{ + // nodes_set_type temp; + // for (auto it = nodes.begin(); it != nodes.end(); ++it){ + // if (visited.count(it->first) == 0){//has not visited + // func(*it); + // visited.insert(it->first); + // auto s = adjacent_nodes(it->first); + // temp.insert(temp.end(), s.begin(), s.end()); + // } + // } + // nodes = temp; + //} while (!nodes.empty()); + } + template string graph::to_string(){ string str; std::ostringstream oss; @@ -120,7 +144,7 @@ namespace TinySTL{ oss << "[" << oit->first << "," << oit->second << "]" << ":"; auto eit = end(oit->first); for (auto iit = begin(oit->first); iit != eit; ++iit){ - oss << "[" << iit->first << ", " << iit->second << "]" << "->"; + oss << "[" << iit->first << "," << iit->second << "]" << "->"; } oss << "[nil]" << std::endl << std::setw(4) << "|" << std::endl; } @@ -231,4 +255,12 @@ namespace TinySTL{ void directed_graph::delete_node(const node_type& item){ delete_node(item.first); } + template + void directed_graph::make_edge(const Index& index1, const Index& index2){ + auto node1 = get_node(index1), node2 = get_node(index2); + for (auto it = nodes_.begin(); it != nodes_.end(); ++it){ + if (equal_func((it->first).first, index1)) + (it->second).push_front(node2); + } + } } \ No newline at end of file diff --git a/TinySTL/Graph.h b/TinySTL/Graph.h index 7a9e96e..98cc03d 100644 --- a/TinySTL/Graph.h +++ b/TinySTL/Graph.h @@ -41,6 +41,7 @@ namespace TinySTL{ virtual void add_node(const node_type& item, const nodes_set_type& nodes) = 0; //node of the index must in the graph virtual void add_node(const Index& index, const nodes_set_type& nodes) = 0; + virtual void make_edge(const Index& index1, const Index& index2) = 0; virtual void delete_node(const node_type& item) = 0; virtual void delete_node(const Index& index) = 0; @@ -49,7 +50,7 @@ namespace TinySTL{ void BFS(const Index& index, visiter_func_type func); node_type make_node(const Index& index, const Value& val); - node_type& get_node(const Index& index); + const node_type& get_node(const Index& index); bool is_contained(const Index& index); inline static nodes_set_type empty_node_set(); @@ -65,6 +66,9 @@ namespace TinySTL{ equal_func_type get_equal_func()const; string to_string(); + protected: + void _DFS(node_type& node, visiter_func_type func, Unordered_set, EqualFunc>& visited); + void _BFS(node_type& node, visiter_func_type func, Unordered_set, EqualFunc>& visited); protected: list>> nodes_; equal_func_type equal_func; @@ -138,6 +142,8 @@ namespace TinySTL{ //node n -> every node_type in the nodes set void add_node(const node_type& n, const nodes_set_type& nodes) override final; void add_node(const Index& index, const nodes_set_type& nodes) override final; + //node index1 -> node index2 + void make_edge(const Index& index1, const Index& index2) override final; void delete_node(const node_type& item) override final; void delete_node(const Index& index) override final;