diff --git a/src/nfagraph/ng_undirected.h b/src/nfagraph/ng_undirected.h index b9fbd58d..1e27ad79 100644 --- a/src/nfagraph/ng_undirected.h +++ b/src/nfagraph/ng_undirected.h @@ -52,11 +52,11 @@ namespace ue2 { * of parallel edges. The only vertex property constructed is \a * vertex_index_t. */ -using NFAUndirectedGraph = - boost::adjacency_list>; +using NFAUndirectedGraph = boost::adjacency_list< + boost::listS, // out edges + boost::listS, // vertices + boost::undirectedS, // graph is undirected + boost::property>; // vertex properties using NFAUndirectedVertex = NFAUndirectedGraph::vertex_descriptor; @@ -95,6 +95,14 @@ NFAUndirectedGraph createUnGraph(const Graph &g, boost::put(boost::vertex_index, ug, nuv, idx++); } + // Track seen edges so that we don't insert parallel edges. + using Vertex = typename Graph::vertex_descriptor; + unordered_set> seen; + seen.reserve(num_edges(g)); + auto make_ordered_edge = [](Vertex a, Vertex b) { + return std::make_pair(std::min(a, b), std::max(a, b)); + }; + for (const auto &e : ue2::edges_range(g)) { auto u = source(e, g); auto v = target(e, g); @@ -109,6 +117,10 @@ NFAUndirectedGraph createUnGraph(const Graph &g, continue; } + if (!seen.emplace(make_ordered_edge(u, v)).second) { + continue; // skip parallel edge. + } + NFAUndirectedVertex new_u = old2new.at(u); NFAUndirectedVertex new_v = old2new.at(v);