Beispiel #1
0
def test_graph_sparse_weighted():

    edgelist = [
        (0, 1, 5),
        (0, 2, 2),
        (0, 3, 3),
        (1, 3, 1),
        (2, 3, 7.7),
        (2, 4, 3.3),
        (2, 5, 13.0),
        (0, 4, 9.999),
        (2, 6, 3.0),
    ]
    g = create_sparse_graph(edgelist, 7, directed=False)

    assert not g.type.directed
    assert g.type.weighted

    assert g.vertices == set([0, 1, 2, 3, 4, 5, 6])

    edgelist2 = []
    for e in g.edges:
        edgelist2.append(g.edge_tuple(e))
    assert edgelist2 == edgelist

    # sparse graphs cannot be modified
    with pytest.raises(ValueError):
        g.add_edge(0, 5)
Beispiel #2
0
def test_graph_sparse_lazy_incoming():

    edgelist = [(0, 1), (0, 2), (0, 3), (1, 3), (2, 3), (2, 4), (2, 5), (0, 4),
                (2, 6)]
    g = create_sparse_graph(
        edgelist,
        7,
        weighted=False,
        incoming_edges_support=IncomingEdgesSupport.LAZY_INCOMING_EDGES)

    assert g.type.directed
    assert not g.type.weighted

    assert g.vertices == set([0, 1, 2, 3, 4, 5, 6])

    assert set(g.inedges_of(3)) == {2, 3, 4}
Beispiel #3
0
def test_graph_sparse_weighted():
    edgelist = parse_edgelist_json(input1)

    assert list(edgelist) == [
        ('0', '1', 1.0),
        ('0', '2', 1.0),
        ('0', '3', 1.0),
        ('0', '4', 1.0),
        ('0', '5', 1.0),
        ('0', '6', 1.0),
        ('0', '7', 1.0),
        ('0', '8', 1.0),
        ('0', '9', 1.0),
        ('1', '2', 1.0),
        ('2', '3', 1.0),
        ('3', '4', 1.0),
        ('4', '5', 1.0),
        ('5', '6', 1.0),
        ('6', '7', 1.0),
        ('7', '8', 1.0),
        ('8', '9', 1.0),
        ('9', '1', 1.0),
    ]

    edgelist = [(int(s), int(t), w) for s, t, w in edgelist]

    g = create_sparse_graph(edgelist, 10, directed=True)

    print(g)

    assert g.type.directed
    assert g.type.weighted

    assert g.vertices == set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    edgelist2 = []
    for e in g.edges:
        edgelist2.append(g.edge_tuple(e))
    assert edgelist2 == list(edgelist)

    # sparse graphs cannot be modified
    with pytest.raises(ValueError):
        g.add_edge(0, 5)
Beispiel #4
0
def test_graph_sparse_no_vertex_count():

    edgelist = [(0, 1), (0, 2), (0, 3), (1, 3), (2, 3), (2, 4), (2, 5), (0, 4), (2, 6)]
    g = create_sparse_graph(edgelist, weighted=False)

    assert g.type.directed
    assert not g.type.weighted

    assert g.vertices == set([0, 1, 2, 3, 4, 5, 6])

    edgelist2 = []
    for e in g.edges:
        u, v, w = g.edge_tuple(e)
        edgelist2.append((u, v))
    assert edgelist2 == edgelist

    # sparse graphs cannot be modified
    with pytest.raises(ValueError):
        g.add_edge(0, 5)
Beispiel #5
0
def test_graph_sparse_no_incoming():

    edgelist = [(0, 1), (0, 2), (0, 3), (1, 3), (2, 3), (2, 4), (2, 5), (0, 4),
                (2, 6)]

    g = create_sparse_graph(
        edgelist,
        7,
        weighted=False,
        incoming_edges_support=IncomingEdgesSupport.NO_INCOMING_EDGES)

    assert g.type.directed
    assert not g.type.weighted

    assert g.vertices == set([0, 1, 2, 3, 4, 5, 6])

    # no incoming support
    with pytest.raises(ValueError):
        g.inedges_of(3)
    m = re.match(r'v([0-9]+)', id)
    vid = int(m.group(1))
    return vid


edges = [(convert_id(s), convert_id(t), w) for s, t, w in edges]

print(edges)

# %%
# Now that we have all our edges, we need to figure out the number of vertices. Sparse graphs
# contain all vertices from :math:`[0, n)` where :math:`n` is the number of vertices.
# Then we call the :py:meth:`jgrapht.create_sparse_graph()` factory.

sparse = jgrapht.create_sparse_graph(edges,
                                     num_of_vertices=5,
                                     directed=True,
                                     weighted=True)

print(sparse)

# %%
# Note that in the above call the graph must be weighted, as our edges also have a weight.
# Let us calculate a graph coloring using the greedy algorithm using saturation degree ordering.

import jgrapht.algorithms.coloring as coloring

num_colors, color_map = coloring.greedy_dsatur(sparse)

print('Total number of colors: {}'.format(num_colors))
print('Color map: {}'.format(color_map))