Beispiel #1
0
    def cluster_in_box(self, m, pt=None):
        r"""
        Return the cluster (as a list) in the primal box [-m,m]^d
        containing the point pt.

        INPUT:

        - ``m`` - integer
        - ``pt`` - tuple, point in Z^d. If None, pt=zero is considered.

        EXAMPLES::

            sage: from slabbe import BondPercolationSample
            sage: S = BondPercolationSample(0.3,2)
            sage: S.cluster_in_box(2)         # random
            [(-2, -2), (-2, -1), (-1, -2), (-1, -1), (-1, 0), (0, 0)]
        """
        G = Graph()
        G.add_edges(self.edges_in_box(m))
        if pt is None:
            pt = self.zero()
        if pt in G:
            return G.connected_component_containing_vertex(pt)
        else:
            return []
Beispiel #2
0
    def cluster_in_box(self, m, pt=None):
        r"""
        Return the cluster (as a list) in the primal box [-m,m]^d
        containing the point pt.

        INPUT:

        - ``m`` - integer
        - ``pt`` - tuple, point in Z^d. If None, pt=zero is considered.

        EXAMPLES::

            sage: from slabbe import BondPercolationSample
            sage: S = BondPercolationSample(0.3,2)
            sage: S.cluster_in_box(2)         # random
            [(-2, -2), (-2, -1), (-1, -2), (-1, -1), (-1, 0), (0, 0)]
        """
        G = Graph()
        G.add_edges(self.edges_in_box(m))
        if pt is None:
            pt = self.zero()
        if pt in G:
            return G.connected_component_containing_vertex(pt)
        else:
            return []
Beispiel #3
0
def AfricaMap(continental=False, year=2018):
    """
    Return African states as a graph of common border.

    "African state" here is defined as an independent
    state having the capital city in Africa. The graph
    has an edge between those countries that have common
    *land* border.

    INPUT:

    - ``continental``, a Boolean -- if set, only return states in
      the continental Africa
    - ``year`` -- reserved for future use

    EXAMPLES::

        sage: Africa = graphs.AfricaMap(); Africa
        Africa Map: Graph on 54 vertices
        sage: sorted(Africa.neighbors('Libya'))
        ['Algeria', 'Chad', 'Egypt', 'Niger', 'Sudan', 'Tunisia']

        sage: cont_Africa = graphs.AfricaMap(continental=True)
        sage: cont_Africa.order()
        48
        sage: 'Madagaskar' in cont_Africa
        False
    """
    if year != 2018:
        raise ValueError("currently only year 2018 is implemented")

    common_border = {
        'Algeria':
        ['Libya', 'Mali', 'Mauritania', 'Morocco', 'Niger', 'Tunisia'],
        'Angola': ['Namibia', 'Zambia'],
        'Benin': ['Burkina Faso', 'Niger', 'Nigeria', 'Togo'],
        'Botswana': ['Namibia', 'South Africa', 'Zimbabwe'],
        'Burkina Faso': ['Ghana', 'Ivory Coast', 'Mali', 'Niger', 'Togo'],
        'Cameroon':
        ['Central Africa', 'Chad', 'Equatorial Guinea', 'Gabon', 'Nigeria'],
        'Central Africa': ['Chad', 'South Sudan', 'Sudan'],
        'Chad': ['Libya', 'Niger', 'Nigeria', 'Sudan'],
        'Republic of the Congo': [
            'Gabon', 'Cameroon', 'Central Africa', 'Angola',
            'Democratic Republic of the Congo'
        ],
        'Democratic Republic of the Congo': [
            'Zambia', 'South Sudan', 'Tanzania', 'Burundi', 'Rwanda', 'Uganda',
            'Central Africa', 'Angola'
        ],
        'Djibouti': ['Eritrea', 'Ethiopia', 'Somalia'],
        'Ethiopia': ['Eritrea', 'Kenya', 'Somalia', 'South Sudan', 'Sudan'],
        'Gabon': ['Equatorial Guinea'],
        'Ghana': ['Ivory Coast', 'Togo'],
        'Guinea': ['Guinea-Bissau', 'Ivory Coast', 'Liberia', 'Sierra Leone'],
        'Kenya': ['Somalia', 'South Sudan', 'Tanzania', 'Uganda'],
        'Liberia': ['Ivory Coast', 'Sierra Leone'],
        'Libya': ['Egypt', 'Niger', 'Sudan', 'Tunisia'],
        'Mali': ['Guinea', 'Ivory Coast', 'Mauritania', 'Niger', 'Senegal'],
        'Mozambique': ['Malawi', 'South Africa', 'Swaziland', 'Zimbabwe'],
        'Niger': ['Nigeria'],
        'Rwanda': ['Burundi', 'Tanzania', 'Uganda'],
        'Senegal': ['Guinea', 'Guinea-Bissau', 'Mauritania', 'Gambia'],
        'South Africa': ['Lesotho', 'Namibia', 'Swaziland', 'Zimbabwe'],
        'South Sudan': ['Uganda', 'Sudan', 'Democratic Republic of the Congo'],
        'Sudan': ['Egypt', 'Eritrea'],
        'Tanzania': ['Burundi', 'Malawi', 'Mozambique', 'Uganda', 'Zambia'],
        'Zambia': ['Malawi', 'Mozambique', 'Namibia', 'Zimbabwe']
    }

    no_land_border = [
        'Cape Verde', 'Seychelles', 'Mauritius',
        'S\xc3\xa3o Tom\xc3\xa9 and Pr\xc3\xadncipe', 'Madagascar', 'Comoros'
    ]

    G = Graph(common_border, format='dict_of_lists')

    if continental:
        G = G.subgraph(
            G.connected_component_containing_vertex('Central Africa'))
        G.name(new="Continental Africa Map")
    else:
        G.add_vertices(no_land_border)
        G.name(new="Africa Map")

    return G
Beispiel #4
0
def EuropeMap(continental=False, year=2018):
    """
    Return European states as a graph of common border.

    "European state" here is defined as an independent
    state having the capital city in Europe. The graph
    has an edge between those countries that have common
    *land* border.

    INPUT:

    - ``continental``, a Boolean -- if set, only return states in
      the continental Europe
    - ``year`` -- reserved for future use

    EXAMPLES::

        sage: Europe = graphs.EuropeMap(); Europe
        Europe Map: Graph on 44 vertices
        sage: Europe.neighbors('Ireland')
        ['United Kingdom']

        sage: cont_Europe = graphs.EuropeMap(continental=True)
        sage: cont_Europe.order()
        40
        sage: 'Iceland' in cont_Europe
        False
    """
    if year != 2018:
        raise ValueError("currently only year 2018 is implemented")

    common_border = {
        'Poland': [
            'Slovakia', 'Czech Republic', 'Lithuania', 'Russia', 'Ukraine',
            'Germany'
        ],
        'Germany': [
            'Czech Republic', 'Netherlands', 'Switzerland', 'Luxembourg',
            'Denmark'
        ],
        'Croatia': [
            'Bosnia and Herzegovina', 'Serbia', 'Hungary', 'Montenegro',
            'Slovenia'
        ],
        'Austria': [
            'Czech Republic', 'Germany', 'Switzerland', 'Slovenia',
            'Liechtenstein'
        ],
        'France':
        ['Germany', 'Italy', 'Switzerland', 'Monaco', 'Luxembourg', 'Andorra'],
        'Hungary':
        ['Slovakia', 'Serbia', 'Romania', 'Ukraine', 'Slovenia', 'Austria'],
        'Italy':
        ['Switzerland', 'Vatican City', 'San Marino', 'Slovenia', 'Austria'],
        'Belarus': ['Poland', 'Latvia', 'Lithuania', 'Russia', 'Ukraine'],
        'Montenegro': ['Bosnia and Herzegovina', 'Serbia', 'Albania'],
        'Belgium': ['Germany', 'Netherlands', 'Luxembourg', 'France'],
        'Russia': ['Finland', 'Lithuania', 'Estonia', 'Ukraine'],
        'Romania': ['Serbia', 'Moldova', 'Bulgaria', 'Ukraine'],
        'Latvia': ['Lithuania', 'Russia', 'Estonia'],
        'Slovakia': ['Czech Republic', 'Ukraine', 'Austria'],
        'Switzerland': ['Liechtenstein'],
        'Spain': ['Portugal', 'Andorra', 'France'],
        'Norway': ['Finland', 'Sweden', 'Russia'],
        'Ireland': ['United Kingdom'],
        'Serbia': ['Bosnia and Herzegovina', 'Bulgaria'],
        'Greece': ['Macedonia', 'Bulgaria', 'Albania'],
        'Ukraine': ['Moldova'],
        'Macedonia': ['Serbia', 'Bulgaria', 'Albania'],
        'Sweden': ['Finland']
    }
    no_land_border = ['Iceland', 'Malta']

    G = Graph(common_border, format='dict_of_lists')

    if continental:
        G = G.subgraph(G.connected_component_containing_vertex('Austria'))
        G.name(new="Continental Europe Map")
    else:
        G.add_vertices(no_land_border)
        G.name(new="Europe Map")

    return G
Beispiel #5
0
def AfricaMap(continental=False, year=2018):
    """
    Return African states as a graph of common border.

    "African state" here is defined as an independent
    state having the capital city in Africa. The graph
    has an edge between those countries that have common
    *land* border.

    INPUT:

    - ``continental``, a Boolean -- if set, only return states in
      the continental Africa
    - ``year`` -- reserved for future use

    EXAMPLES::

        sage: Africa = graphs.AfricaMap(); Africa
        Africa Map: Graph on 54 vertices
        sage: sorted(Africa.neighbors('Libya'))
        ['Algeria', 'Chad', 'Egypt', 'Niger', 'Sudan', 'Tunisia']

        sage: cont_Africa = graphs.AfricaMap(continental=True)
        sage: cont_Africa.order()
        48
        sage: 'Madagaskar' in cont_Africa
        False

    TESTS::

        sage: Africa.plot()
        Graphics object consisting of 159 graphics primitives
    """
    if year != 2018:
        raise ValueError("currently only year 2018 is implemented")

    common_border = {
     'Algeria': ['Libya', 'Mali', 'Mauritania', 'Morocco', 'Niger', 'Tunisia'],
     'Angola': ['Namibia', 'Zambia'],
     'Benin': ['Burkina Faso', 'Niger', 'Nigeria', 'Togo'],
     'Botswana': ['Namibia', 'South Africa', 'Zimbabwe'],
     'Burkina Faso': ['Ghana', 'Ivory Coast', 'Mali', 'Niger', 'Togo'],
     'Cameroon': ['Central Africa', 'Chad', 'Equatorial Guinea', 'Gabon', 'Nigeria'],
     'Central Africa': ['Chad', 'South Sudan', 'Sudan'],
     'Chad': ['Libya', 'Niger', 'Nigeria', 'Sudan'],
     'Republic of the Congo': ['Gabon', 'Cameroon', 'Central Africa', 'Angola',
                               'Democratic Republic of the Congo'],
     'Democratic Republic of the Congo': ['Zambia', 'South Sudan', 'Tanzania', 'Burundi',
                                          'Rwanda', 'Uganda', 'Central Africa', 'Angola'],
     'Djibouti': ['Eritrea', 'Ethiopia', 'Somalia'],
     'Ethiopia': ['Eritrea', 'Kenya', 'Somalia', 'South Sudan', 'Sudan'],
     'Gabon': ['Equatorial Guinea'],
     'Ghana': ['Ivory Coast', 'Togo'],
     'Guinea': ['Guinea-Bissau', 'Ivory Coast', 'Liberia', 'Sierra Leone'],
     'Kenya': ['Somalia', 'South Sudan', 'Tanzania', 'Uganda'],
     'Liberia': ['Ivory Coast', 'Sierra Leone'],
     'Libya': ['Egypt', 'Niger', 'Sudan', 'Tunisia'],
     'Mali': ['Guinea', 'Ivory Coast', 'Mauritania', 'Niger', 'Senegal'],
     'Mozambique': ['Malawi', 'South Africa', 'Swaziland', 'Zimbabwe'],
     'Niger': ['Nigeria'],
     'Rwanda': ['Burundi', 'Tanzania', 'Uganda'],
     'Senegal': ['Guinea', 'Guinea-Bissau', 'Mauritania', 'Gambia'],
     'South Africa': ['Lesotho', 'Namibia', 'Swaziland', 'Zimbabwe'],
     'South Sudan': ['Uganda', 'Sudan', 'Democratic Republic of the Congo'],
     'Sudan': ['Egypt', 'Eritrea'],
     'Tanzania': ['Burundi', 'Malawi', 'Mozambique', 'Uganda', 'Zambia'],
     'Zambia': ['Malawi', 'Mozambique', 'Namibia', 'Zimbabwe']
     }

    no_land_border = ['Cape Verde', 'Seychelles', 'Mauritius', u'São Tomé and Príncipe', 'Madagascar', 'Comoros']

    G = Graph(common_border, format='dict_of_lists')

    if continental:
        G = G.subgraph(G.connected_component_containing_vertex('Central Africa'))
        G.name(new="Continental Africa Map")
    else:
        G.add_vertices(no_land_border)
        G.name(new="Africa Map")

    return G
Beispiel #6
0
def EuropeMap(continental=False, year=2018):
    """
    Return European states as a graph of common border.

    "European state" here is defined as an independent
    state having the capital city in Europe. The graph
    has an edge between those countries that have common
    *land* border.

    INPUT:

    - ``continental``, a Boolean -- if set, only return states in
      the continental Europe
    - ``year`` -- reserved for future use

    EXAMPLES::

        sage: Europe = graphs.EuropeMap(); Europe
        Europe Map: Graph on 44 vertices
        sage: Europe.neighbors('Ireland')
        ['United Kingdom']

        sage: cont_Europe = graphs.EuropeMap(continental=True)
        sage: cont_Europe.order()
        40
        sage: 'Iceland' in cont_Europe
        False
    """
    if year != 2018:
        raise ValueError("currently only year 2018 is implemented")

    common_border = {
     'Poland': ['Slovakia', 'Czech Republic', 'Lithuania', 'Russia', 'Ukraine', 'Germany'],
     'Germany': ['Czech Republic', 'Netherlands', 'Switzerland', 'Luxembourg', 'Denmark'],
     'Croatia': ['Bosnia and Herzegovina', 'Serbia', 'Hungary', 'Montenegro', 'Slovenia'],
     'Austria': ['Czech Republic', 'Germany', 'Switzerland', 'Slovenia', 'Liechtenstein'],
     'France': ['Germany', 'Italy', 'Switzerland', 'Monaco', 'Luxembourg', 'Andorra'],
     'Hungary': ['Slovakia', 'Serbia', 'Romania', 'Ukraine', 'Slovenia', 'Austria'],
     'Italy': ['Switzerland', 'Vatican City', 'San Marino', 'Slovenia', 'Austria'],
     'Belarus': ['Poland', 'Latvia', 'Lithuania', 'Russia', 'Ukraine'],
     'Montenegro': ['Bosnia and Herzegovina', 'Serbia', 'Albania'],
     'Belgium': ['Germany', 'Netherlands', 'Luxembourg', 'France'],
     'Russia': ['Finland', 'Lithuania', 'Estonia', 'Ukraine'],
     'Romania': ['Serbia', 'Moldova', 'Bulgaria', 'Ukraine'],
     'Latvia': ['Lithuania', 'Russia', 'Estonia'],
     'Slovakia': ['Czech Republic', 'Ukraine', 'Austria'], 'Switzerland': ['Liechtenstein'],
     'Spain': ['Portugal', 'Andorra', 'France'], 'Norway': ['Finland', 'Sweden', 'Russia'],
     'Ireland': ['United Kingdom'], 'Serbia': ['Bosnia and Herzegovina', 'Bulgaria'],
     'Greece': ['Macedonia', 'Bulgaria', 'Albania'], 'Ukraine': ['Moldova'],
     'Macedonia': ['Serbia', 'Bulgaria', 'Albania'], 'Sweden': ['Finland']
    }
    no_land_border = ['Iceland', 'Malta']

    G = Graph(common_border, format='dict_of_lists')

    if continental:
        G = G.subgraph(G.connected_component_containing_vertex('Austria'))
        G.name(new="Continental Europe Map")
    else:
        G.add_vertices(no_land_border)
        G.name(new="Europe Map")

    return G
def geometric_basis(G, E, p):
    r"""
    Return a geometric basis, based on a vertex.

    INPUT:

    - ``G`` -- the graph of the bounded regions of a Voronoi Diagram

    - ``E`` -- the subgraph of ``G`` formed by the edges that touch an unbounded
    region

    - ``p`` -- a vertex of ``E``

    OUTPUT: A geometric basis. It is formed by a list of sequences of paths.
    Each path is a list of vertices, that form a closed path in `G`, based at
    `p`, that goes to a region, surrounds it, and comes back by the same path it
    came. The concatenation of all these paths is equivalent to `E`.

    EXAMPLES::

        sage: from sage.schemes.curves.zariski_vankampen import geometric_basis
        sage: points = [(-3,0),(3,0),(0,3),(0,-3)]+ [(0,0),(0,-1),(0,1),(1,0),(-1,0)]
        sage: V = VoronoiDiagram(points)
        sage: G = Graph()
        sage: for reg  in V.regions().values():
        ....:     G = G.union(reg.vertex_graph())
        ....:
        sage: E = Graph()
        sage: for reg  in V.regions().values():
        ....:     if reg.rays() or reg.lines():
        ....:         E  = E.union(reg.vertex_graph())
        ....:
        sage: p = E.vertices()[0]
        sage: geometric_basis(G, E, p)
        [[A vertex at (-2, -2),
          A vertex at (2, -2),
          A vertex at (2, 2),
          A vertex at (1/2, 1/2),
          A vertex at (1/2, -1/2),
          A vertex at (2, -2),
          A vertex at (-2, -2)],
         [A vertex at (-2, -2),
          A vertex at (2, -2),
          A vertex at (1/2, -1/2),
          A vertex at (1/2, 1/2),
          A vertex at (-1/2, 1/2),
          A vertex at (-1/2, -1/2),
          A vertex at (1/2, -1/2),
          A vertex at (2, -2),
          A vertex at (-2, -2)],
         [A vertex at (-2, -2),
          A vertex at (2, -2),
          A vertex at (1/2, -1/2),
          A vertex at (-1/2, -1/2),
          A vertex at (-2, -2)],
         [A vertex at (-2, -2),
          A vertex at (-1/2, -1/2),
          A vertex at (-1/2, 1/2),
          A vertex at (1/2, 1/2),
          A vertex at (2, 2),
          A vertex at (-2, 2),
          A vertex at (-1/2, 1/2),
          A vertex at (-1/2, -1/2),
          A vertex at (-2, -2)],
         [A vertex at (-2, -2),
          A vertex at (-1/2, -1/2),
          A vertex at (-1/2, 1/2),
          A vertex at (-2, 2),
          A vertex at (-2, -2)]]

    """
    EC = [v[0] for v in orient_circuit(E.eulerian_circuit())]
    i = EC.index(p)
    EC = EC[
        i:] + EC[:i +
                 1]  # A counterclockwise eulerian circuit on the boundary, based at p
    if len(G.edges()) == len(E.edges()):
        if E.is_cycle():
            return [EC]
    I = Graph()
    for e in G.edges():
        if not E.has_edge(e):
            I.add_edge(e)  # interior graph
    # treat the case where I is empty
    if not I.vertices():
        for v in E.vertices():
            if len(E.neighbors(v)) > 2:
                I.add_vertex(v)

    for i in range(len(EC)):  # q and r are the points we will cut through

        if EC[i] in I.vertices():
            q = EC[i]
            connecting_path = EC[:i]
            break
        elif EC[-i] in I.vertices():
            q = EC[-i]
            connecting_path = list(reversed(EC[-i:]))
            break
    distancequotients = [
        (E.distance(q, v)**2 / I.distance(q, v), v) for v in E.vertices()
        if v in I.connected_component_containing_vertex(q) and not v == q
    ]
    r = max(distancequotients)[1]
    cutpath = I.shortest_path(q, r)
    Gcut = copy(G)
    Ecut = copy(E)
    Ecut.delete_vertices([q, r])
    Gcut.delete_vertices(cutpath)
    # I think this cannot happen, but just in case, we check it to raise
    # an error instead of giving a wrong answer
    if Gcut.connected_components_number() != 2:
        raise ValueError("unable to compute a correct path")
    G1, G2 = Gcut.connected_components_subgraphs()

    for v in cutpath:
        neighs = G.neighbors(v)
        for n in neighs:
            if n in G1.vertices() + cutpath:
                G1.add_edge(v, n, None)
            if n in G2.vertices() + cutpath:
                G2.add_edge(v, n, None)

    if EC[EC.index(q) + 1] in G2.vertices():
        G1, G2 = G2, G1

    E1, E2 = Ecut.connected_components_subgraphs()
    if EC[EC.index(q) + 1] in E2.vertices():
        E1, E2 = E2, E1

    for i in range(len(cutpath) - 1):
        E1.add_edge(cutpath[i], cutpath[i + 1], None)
        E2.add_edge(cutpath[i], cutpath[i + 1], None)

    for v in [q, r]:
        for n in E.neighbors(v):
            if n in E1.vertices():
                E1.add_edge(v, n, None)
            if n in E2.vertices():
                E2.add_edge(v, n, None)

    gb1 = geometric_basis(G1, E1, q)
    gb2 = geometric_basis(G2, E2, q)

    resul = [
        connecting_path + path + list(reversed(connecting_path))
        for path in gb1 + gb2
    ]
    for r in resul:
        i = 0
        while i < len(r) - 2:
            if r[i] == r[i + 2]:
                r.pop(i)
                r.pop(i)
                if i > 0:
                    i -= 1
            else:
                i += 1
    return resul