Hello Shangdi,
the normal planarity test already runs in linear time, so it should usually suffice. In fact, that’s roughly how e.g. MaximalPlanarSubgraphSimple
works–it always tests whether adding an edge would make it non-planar. Maybe that class already does everything you want? If not, here's the general idea of how to go about it:
edge e = G.addEdge(v, w);
if (!isPlanar(G)) {
G.delEdge(e);
}
The VariableEmbeddingInserter
gives you a crossing minimum planarization of G+e (with G planar) such that all crossings lie on the edge e. So you could just check afterwards whether there are any crossings on e; if not, G+e is planar.
However, this method also needs linear time because it builds up an SPQR-tree of the whole graph. In fact, in practice this construction might take more time than the normal planarity test.
We also have VariableEmbeddingInserterDyn
which uses a dynamic SPQR-tree but that only helps when inserting multiple edges at once.
VariableEmbeddingInserter inserter;
Graph G;
...
for (...) {
edge e = G.newEdge(v,w);
PlanRepLight pr{G};
Array<edge> insertEdges({e});
inserter.call(pr, insertEdges);
if (pr.chain(e).size() > 1) {
// e is split up into more than one edge segment (-> has crossing)
G.delEdge(e);
}
}
Best regards,
Dagobert
Hello Shangdi,
1). Yes, you can use the same PlanRepLight
in every loop with:
prl.removeEdgePath(origEdge);
or, if you use a CombinatorialEmbedding
in some way:
prl.removeEdgePathEmbedded(myCombinatorialEmbedding, origEdge);
I think you are right and there is a way to do the planarity check using a (dynamic) SPQR-tree, but I don’t think we have this exact functionality in the OGDF (maybe the VariableEmbeddingInserter
does this implicitly).
2). Yes, looks right. This is the way to do it (since v->index()
should generally not be used in user code).
3). Oh, I forgot a stupid quirk of PlanRep(Light):
PlanRepLight prl {pr};
prl.initCC(0); // As long as your graph is connected, this should work.
Best regards,
Dagobert
Hello Shangdi,
you can do a for-loop up to prl.numberOfCCs() - 1
, see https://ogdf.github.io/doc/ogdf/classogdf_1_1_plan_rep_light.html
Best regards,
Dagobert