source: sage/graphs/planarity.pyx @ 8734:d84d24c8d5bb

Revision 8734:d84d24c8d5bb, 3.2 KB checked in by Emily Kirkman, 5 years ago (diff)

genus improvements and bm planarity

Line 
1cdef extern from "planarity/graph.h":
2    # TODO: point out to Robert how much shit I was able to erase already
3    ctypedef struct graphNode:
4        int v
5        int link[2]
6    ctypedef graphNode * graphNodeP
7   
8    ctypedef struct BM_graph:
9        graphNodeP G
10        int N
11    ctypedef BM_graph * graphP
12   
13    cdef int OK, EMBEDFLAGS_PLANAR, NONPLANAR, NOTOK
14   
15    cdef graphP gp_New()
16    cdef void gp_Free(graphP *pGraph)
17    cdef int gp_InitGraph(graphP theGraph, int N)
18    cdef int gp_AddEdge(graphP theGraph, int u, int ulink, int v, int vlink)
19    cdef int gp_Embed(graphP theGraph, int embedFlags)
20    cdef int gp_SortVertices(graphP theGraph)
21
22def is_planar(g, set_pos=True, set_emb=True, circular=False):
23    # create to and from mappings to relabel vertices to the set {0,...,n-1}
24    listto = g.vertices()
25    ffrom = {}
26    for vvv in listto:
27        ffrom[vvv] = listto.index(vvv)
28    to = {}
29    for i from 0 <= i < len(listto):
30        to[i] = listto[i]
31    g.relabel(ffrom)
32   
33    cdef graphP theGraph
34    theGraph = gp_New()
35    cdef int status
36    status = gp_InitGraph(theGraph, g.order())
37    if status != OK:
38        raise RuntimeError("status does not equal ok.")
39    for u, v, _ in g.edge_iterator():
40        gp_AddEdge(theGraph, u, 0, v, 0)
41    status = gp_Embed(theGraph, EMBEDFLAGS_PLANAR)
42    gp_SortVertices(theGraph)
43   
44    # use to and from mappings to relabel vertices back from the set {0,...,n-1}
45    g.relabel(to)
46   
47    if status == NOTOK:
48        raise RuntimeError("not ok.")
49    elif status == NONPLANAR:
50        # TODO: Kuratowski subgraph isolator
51        gp_Free(&theGraph)
52        return False
53    else:
54        if not circular:
55            if set_emb:
56                # TODO: explain to self why the following line doesn't compile
57                #cdef int i
58                emb_dict = {}
59                for i in range(theGraph.N):
60                #for i from 0 <= i < theGraph.N:
61                    linked_list = []
62                    j = theGraph.G[i].link[1]
63                    while j >= theGraph.N:
64                        linked_list.append(to[theGraph.G[j].v])
65                        j = theGraph.G[j].link[1]
66                    emb_dict[to[i]] = linked_list
67                g.__embedding__ = emb_dict
68            if set_pos:
69                schnyder(g,emb_dict)
70        else:
71            if set_emb:
72                # Take counter-clockwise embedding if circular planar test
73                # Also, pos must be set after removing extra vertex and edges
74                #
75                # TODO: explain to self why the following line doesn't compile
76                #cdef int i
77                emb_dict = {}
78                for i in range(theGraph.N):
79                #for i from 0 <= i < theGraph.N:
80                    linked_list = []
81                    j = theGraph.G[i].link[0]
82                    while j >= theGraph.N:
83                        linked_list.append(to[theGraph.G[j].v])
84                        j = theGraph.G[j].link[0]
85                    emb_dict[to[i]] = linked_list
86                g.__embedding__ = emb_dict
87        gp_Free(&theGraph)
88        return True
89
90def schnyder(g, emb_dict, circular=False):
91    # TODO
92    pass
93
94
95
Note: See TracBrowser for help on using the repository browser.