完成BFS
This commit is contained in:
@@ -101,12 +101,33 @@ namespace TinySTL{
|
||||
auto nodes = adjacent_nodes(start->first);
|
||||
func(*start);
|
||||
visited.insert(start->first);
|
||||
for (auto& n : nodes){
|
||||
for (const auto& n : nodes){
|
||||
if (visited.count(n.first) == 0)//has not visited
|
||||
DFS(n.first, func);
|
||||
}
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
void graph<Index, Value, EqualFunc>::BFS(const Index& index, visiter_func_type func){
|
||||
node *start = &(get_node(index));
|
||||
Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7);
|
||||
|
||||
auto nodes = adjacent_nodes(start->first);
|
||||
func(*start);
|
||||
visited.insert(start->first);
|
||||
do{
|
||||
node_sets 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<class Index, class Value, class EqualFunc>
|
||||
string graph<Index, Value, EqualFunc>::to_string(){
|
||||
string str;
|
||||
std::ostringstream oss;
|
||||
|
||||
@@ -40,12 +40,14 @@ namespace TinySTL{
|
||||
|
||||
//node can be not in the graph
|
||||
virtual void add_node(const node& item, const node_sets& nodes) = 0;
|
||||
//node of the index must in the graph
|
||||
virtual void add_node(const Index& index, const node_sets& nodes) = 0;
|
||||
//virtual void delte_node(const node& item) = 0;
|
||||
|
||||
//node of the index must in the graph
|
||||
virtual void add_node(const Index& index, const node_sets& nodes) = 0;
|
||||
//virtual void delte_node(const Index& index) = 0;
|
||||
|
||||
void DFS(const Index& index, visiter_func_type func);
|
||||
//virtual void BFS(visiter_func_type func) = 0;
|
||||
void BFS(const Index& index, visiter_func_type func);
|
||||
|
||||
node& new_node(const Index& index, const Value& val);
|
||||
void del_node(node *p);
|
||||
|
||||
Reference in New Issue
Block a user