graph_undirected: adapt bidi graph to undirected

Introduces an adaptor (like the BGL's reverse_graph) that presents an
undirected view of a bidirectional graph.

Initially used in ng_calc_components.
This commit is contained in:
Justin Viiret
2017-12-13 10:15:21 +11:00
committed by Chang, Harry
parent 16076ed4a3
commit c7c90c7ab7
5 changed files with 750 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -53,11 +53,11 @@
#include "ng_depth.h"
#include "ng_holder.h"
#include "ng_prune.h"
#include "ng_undirected.h"
#include "ng_util.h"
#include "grey.h"
#include "ue2common.h"
#include "util/graph_range.h"
#include "util/graph_undirected.h"
#include "util/make_unique.h"
#include <map>
@@ -310,28 +310,19 @@ void splitIntoComponents(unique_ptr<NGHolder> g,
return;
}
unordered_map<NFAVertex, NFAUndirectedVertex> old2new;
auto ug = createUnGraph(*g, true, true, old2new);
auto ug = make_undirected_graph(*g);
// Construct reverse mapping.
unordered_map<NFAUndirectedVertex, NFAVertex> new2old;
for (const auto &m : old2new) {
new2old.emplace(m.second, m.first);
}
// Filter specials and shell vertices from undirected graph.
unordered_set<NFAVertex> bad_vertices(
{g->start, g->startDs, g->accept, g->acceptEod});
bad_vertices.insert(head_shell.begin(), head_shell.end());
bad_vertices.insert(tail_shell.begin(), tail_shell.end());
// Filter shell vertices from undirected graph.
unordered_set<NFAUndirectedVertex> shell_undir_vertices;
for (auto v : head_shell) {
shell_undir_vertices.insert(old2new.at(v));
}
for (auto v : tail_shell) {
shell_undir_vertices.insert(old2new.at(v));
}
auto filtered_ug = boost::make_filtered_graph(
ug, boost::keep_all(), make_bad_vertex_filter(&shell_undir_vertices));
ug, boost::keep_all(), make_bad_vertex_filter(&bad_vertices));
// Actually run the connected components algorithm.
map<NFAUndirectedVertex, u32> split_components;
map<NFAVertex, u32> split_components;
const u32 num = connected_components(
filtered_ug, boost::make_assoc_property_map(split_components));
@@ -348,10 +339,8 @@ void splitIntoComponents(unique_ptr<NGHolder> g,
// Collect vertex lists per component.
for (const auto &m : split_components) {
NFAUndirectedVertex uv = m.first;
NFAVertex v = m.first;
u32 c = m.second;
assert(contains(new2old, uv));
NFAVertex v = new2old.at(uv);
verts[c].push_back(v);
DEBUG_PRINTF("vertex %zu is in comp %u\n", (*g)[v].index, c);
}