How do I find all polygons in an undirected graph?

前端 未结 2 483
栀梦
栀梦 2021-01-16 10:01

Given an undirected graph, what would be an algorithm to find all polygons within such graph? Here is an example graph with polygons in colour.

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 10:15

    Re AndreyT's suggestion to use a DCEL: the salient feature of the doubly-connected edge list representation is that, for each undirected edge, there are two list nodes, one for each direction. We refer to these nodes as darts and think of them as having a head and a tail. Given a dart (e.g., H->G, with tail H and head G), we can find the reverse dart (e.g., G->H) and the next dart with the same head in counterclockwise order (e.g., J->G). The DCEL can be constructed straightforwardly given a primitive that can be used to sort by angle (the easiest way is to sort by atan2(); the best is to find a determinant test that yields consistent results in the face of floating-point malfeasance).

    The polygons (usually called faces) can be found by finding the permutation cycles of the permutation that maps each dart to the reverse of the next dart with the same head in counterclockwise order. For example, if we start with the dart C->D, then we follow the cycle

    C->D (E->D is next) D->E (G->E is next) E->G (C->G is next) G->C (D->C is next) C->D
    

    and recover the face C-D-E-G-C. Starting with A->B, we get

    A->B B->C C->I I->H H->G G->J J->K K->L L->M M->L L->K K->A A->B,
    

    which is the face A-B-C-I-H-G-J-K-L-M-L-K-A.

    This method sort of requires a connected graph. (It will work on a disconnected graph, but it might not give the results that you want.) It also yields the infinite face, which you have indicated is undesirable. To find a dart on the infinite face (which can be used to identify it), find the vertex with the least y-coordinate, breaking ties by least x-coordinate. Then find the last dart with that head in counterclockwise order from the ray shooting straight rightward.

提交回复
热议问题