Ejemplo n.º 1
0
    def test_forests(self):
        try:
            #universe = tl.grid(8, 8, 0.37)
            universe = [(1, 2), (1, 10), (2, 3), (2, 11), (3, 12), (4, 5), (5, 6), (5, 14), (6, 15), (7, 8), (8, 9), (8, 17), (9, 18), (10, 11), (11, 12), (11, 20), (13, 22), (14, 15), (14, 23), (16, 25), (17, 26), (18, 27), (19, 20), (19, 28), (20, 21), (21, 22), (21, 30), (22, 23), (22, 31), (23, 24), (24, 25), (25, 26), (26, 27), (26, 35), (27, 36), (28, 29), (28, 37), (29, 30), (29, 38), (30, 31), (31, 40), (32, 33), (32, 41), (33, 42), (34, 35), (37, 38), (37, 46), (38, 39), (40, 41), (41, 42), (41, 50), (42, 51), (43, 52), (44, 45), (45, 54), (46, 55), (47, 48), (47, 56), (48, 49), (49, 58), (50, 59), (51, 52), (51, 60), (52, 53), (53, 54), (53, 62), (55, 56), (56, 65), (57, 58), (57, 66), (59, 60), (59, 68), (61, 62), (61, 70), (62, 63), (64, 65), (64, 73), (65, 74), (66, 75), (67, 76), (68, 69), (69, 70), (69, 78), (70, 71), (70, 79), (71, 80), (72, 81), (74, 75), (75, 76), (76, 77), (80, 81)]
            GraphSet.set_universe(universe)

            generators = [1, 9, 73, 81]
            forests = GraphSet.forests(roots=generators, is_spanning=True)
            self.assertEqual(len(forests), 54060425088)

            too_large_trees = GraphSet()
            for substation in generators:
                too_large_trees |= GraphSet.trees(root=substation).larger(23)
            safe_forests = forests.excluding(too_large_trees)
            self.assertEqual(len(safe_forests), 294859080)

            closed_switches = (forests - safe_forests).choice()
            scores = {}
            for switch in universe:
                scores[switch] = 1 if switch in closed_switches else -1

            failures = safe_forests.blocking().minimal()
            self.assertEqual(len(failures), 1936)
            failure = failures.choice()
            for line in failure:
                safe_forests = safe_forests.excluding(line)
            self.assertEqual(len(safe_forests), 0)
        except ImportError:
            pass
Ejemplo n.º 2
0
    def test_forests(self):
        try:
            universe = tl.grid(8, 8, 0.37)
            GraphSet.set_universe(universe)

            generators = [1, 9, 73, 81]
            forests = GraphSet.forests(roots=generators, is_spanning=True)
            self.assertEqual(len(forests), 54060425088)

            too_large_trees = GraphSet()
            for substation in generators:
                too_large_trees |= GraphSet.trees(root=substation).larger(23)
            safe_forests = forests.excluding(too_large_trees)
            self.assertEqual(len(safe_forests), 294859080)

            closed_switches = (forests - safe_forests).choice()
            scores = {}
            for switch in universe:
                scores[switch] = 1 if switch in closed_switches else -1

            failures = safe_forests.blocking().minimal()
            self.assertEqual(len(failures), 1936)
            failure = failures.choice()
            for line in failure:
                safe_forests = safe_forests.excluding(line)
            self.assertEqual(len(safe_forests), 0)
        except ImportError:
            pass
Ejemplo n.º 3
0
    def test_forests(self):
        try:
            universe = tl.grid(8, 8, 0.37)
            GraphSet.set_universe(universe)

            generators = [1, 9, 73, 81]
            forests = GraphSet.forests(roots=generators, is_spanning=True)
            self.assertEqual(len(forests), 54060425088)

            too_large_trees = GraphSet()
            for substation in generators:
                too_large_trees |= GraphSet.trees(root=substation).larger(23)
            safe_forests = forests.excluding(too_large_trees)
            self.assertEqual(len(safe_forests), 294859080)

            closed_switches = (forests - safe_forests).choice()
            scores = {}
            for switch in universe:
                scores[switch] = 1 if switch in closed_switches else -1

            failures = safe_forests.blocking().minimal()
            self.assertEqual(len(failures), 1936)
            failure = failures.choice()
            for line in failure:
                safe_forests = safe_forests.excluding(line)
            self.assertEqual(len(safe_forests), 0)
        except ImportError:
            pass
Ejemplo n.º 4
0
    def test_forests(self):
        try:
            #universe = tl.grid(8, 8, 0.37)
            universe = [(1, 2), (1, 10), (2, 3), (2, 11), (3, 12), (4, 5),
                        (5, 6), (5, 14), (6, 15), (7, 8), (8, 9), (8, 17),
                        (9, 18), (10, 11), (11, 12), (11, 20), (13, 22),
                        (14, 15), (14, 23), (16, 25), (17, 26), (18, 27),
                        (19, 20), (19, 28), (20, 21), (21, 22), (21, 30),
                        (22, 23), (22, 31), (23, 24), (24, 25), (25, 26),
                        (26, 27), (26, 35), (27, 36), (28, 29), (28, 37),
                        (29, 30), (29, 38), (30, 31), (31, 40), (32, 33),
                        (32, 41), (33, 42), (34, 35), (37, 38), (37, 46),
                        (38, 39), (40, 41), (41, 42), (41, 50), (42, 51),
                        (43, 52), (44, 45), (45, 54), (46, 55), (47, 48),
                        (47, 56), (48, 49), (49, 58), (50, 59), (51, 52),
                        (51, 60), (52, 53), (53, 54), (53, 62), (55, 56),
                        (56, 65), (57, 58), (57, 66), (59, 60), (59, 68),
                        (61, 62), (61, 70), (62, 63), (64, 65), (64, 73),
                        (65, 74), (66, 75), (67, 76), (68, 69), (69, 70),
                        (69, 78), (70, 71), (70, 79), (71, 80), (72, 81),
                        (74, 75), (75, 76), (76, 77), (80, 81)]
            GraphSet.set_universe(universe)

            generators = [1, 9, 73, 81]
            forests = GraphSet.forests(roots=generators, is_spanning=True)
            self.assertEqual(len(forests), 54060425088)

            too_large_trees = GraphSet()
            for substation in generators:
                too_large_trees |= GraphSet.trees(root=substation).larger(23)
            safe_forests = forests.excluding(too_large_trees)
            self.assertEqual(len(safe_forests), 294859080)

            closed_switches = (forests - safe_forests).choice()
            scores = {}
            for switch in universe:
                scores[switch] = 1 if switch in closed_switches else -1

            failures = safe_forests.blocking().minimal()
            self.assertEqual(len(failures), 1936)
            failure = failures.choice()
            for line in failure:
                safe_forests = safe_forests.excluding(line)
            self.assertEqual(len(safe_forests), 0)
        except ImportError:
            pass
Ejemplo n.º 5
0
    def test_graphs(self):
        GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
                               (5, 6)])

        # any subgraph
        gs = GraphSet.graphs()
        self.assertTrue(isinstance(gs, GraphSet))
        self.assertEqual(len(gs), 2**7)
        self.assertTrue([(1, 2)] in gs)

        # subgraphs separating [1, 5] and [2]
        gs = GraphSet.graphs(vertex_groups=[[1, 5], [2]])
        self.assertEqual(len(gs), 6)
        self.assertTrue([(1, 4), (4, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (4, 5)] not in gs)

        # matching
        dc = {}
        for v in range(1, 7):
            dc[v] = range(0, 2)
        gs = GraphSet.graphs(degree_constraints=dc)
        self.assertEqual(len(gs), 22)
        self.assertTrue([(1, 2), (3, 6)] in gs)
        self.assertTrue([(1, 2), (2, 3), (3, 6)] not in gs)
        for g in gs:
            self.assertTrue(len(g) < 4)

        # subgraphs with 1 or 2 edges
        gs = GraphSet.graphs(num_edges=range(1, 3))
        self.assertEqual(len(gs), 28)
        for g in gs:
            self.assertTrue(1 <= len(g) and len(g) < 3)

        # single connected component and vertex islands
        gs = GraphSet.graphs(vertex_groups=[[]])
        self.assertEqual(len(gs), 80)
        self.assertTrue([(1, 2), (2, 3)] in gs)
        self.assertTrue([(1, 2), (2, 3), (4, 5)] not in gs)

        # any forest
        gs = GraphSet.graphs(no_loop=True)
        self.assertEqual(len(gs), 112)
        self.assertTrue([(1, 2), (1, 4), (2, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)
        for g in gs:
            self.assertTrue(len(g) < 6)

        # constrained by GraphSet
        gs = GraphSet.graphs(no_loop=True)
        gs = gs.graphs(vertex_groups=[[]])
        self.assertEqual(len(gs), 66)
        self.assertTrue([(1, 2), (1, 4), (2, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)

        # single connected components across 1, 3, and 5
        gs = GraphSet.connected_components([1, 3, 5])
        self.assertEqual(len(gs), 35)
        self.assertTrue([(1, 2), (2, 3), (2, 5)] in gs)
        self.assertTrue([(1, 2), (2, 3), (5, 6)] not in gs)

        GraphSet.set_universe([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4),
                               (2, 5), (3, 4), (3, 5), (4, 5)])

        # cliques with 4 vertices
        gs = GraphSet.cliques(4)
        self.assertEqual(len(gs), 5)
        self.assertTrue([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] in gs)
        self.assertTrue([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 5)] not in gs)

        GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
                               (5, 6)])

        # trees rooted at 1
        gs = GraphSet.trees(1)
        self.assertEqual(len(gs), 45)
        self.assertTrue([] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)

        # spanning trees
        gs = GraphSet.trees(is_spanning=True)
        self.assertEqual(len(gs), 15)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5), (4, 5)] not in gs)
        for g in gs:
            self.assertEqual(len(g), 5)

        # forests rooted at 1 and 3
        gs = GraphSet.forests([1, 3])
        self.assertEqual(len(gs), 54)
        self.assertTrue([] in gs)
        self.assertTrue([(1, 2), (2, 3)] not in gs)

        # spanning forests rooted at 1 and 3
        gs = GraphSet.forests([1, 3], is_spanning=True)
        self.assertEqual(len(gs), 20)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (3, 6)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5)] not in gs)
        for g in gs:
            self.assertEqual(len(g), 4)

        # cycles
        gs = GraphSet.cycles()
        self.assertEqual(len(gs), 3)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] in gs)
        self.assertTrue([] not in gs)

        # hamilton cycles
        gs = GraphSet.cycles(is_hamilton=True)
        self.assertEqual(len(gs), 1)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (3, 6), (4, 5), (5, 6)] in gs)

        # paths between 1 and 6
        gs = GraphSet.paths(1, 6)
        self.assertEqual(len(gs), 4)
        self.assertTrue([(1, 2), (2, 3), (3, 6)] in gs)
        self.assertTrue([(1, 2), (2, 3), (5, 6)] not in gs)

        # hamilton paths between 1 and 6
        gs = GraphSet.paths(1, 6, is_hamilton=True)
        self.assertEqual(len(gs), 1)
        self.assertTrue([(1, 4), (2, 3), (2, 5), (3, 6), (4, 5)] in gs)

        # called as instance methods
        gs = GraphSet.graphs(no_loop=True)
        _ = gs.connected_components([1, 3, 5])
        _ = gs.cliques(4)
        _ = gs.trees(1)
        _ = gs.forests([1, 3])
        _ = gs.cycles()
        _ = gs.paths(1, 6)

        # exceptions
        self.assertRaises(KeyError, GraphSet.graphs, vertex_groups=[[7]])
        self.assertRaises(KeyError, GraphSet.graphs, degree_constraints={7: 1})
Ejemplo n.º 6
0
    def test_graphs(self):
        GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
                               (5, 6)])

        # any subgraph
        gs = GraphSet.graphs()
        self.assertTrue(isinstance(gs, GraphSet))
        self.assertEqual(len(gs), 2**7)
        self.assertTrue([(1, 2)] in gs)

        # subgraphs separating [1, 5] and [2]
        gs = GraphSet.graphs(vertex_groups=[[1, 5], [2]])
        self.assertEqual(len(gs), 6)
        self.assertTrue([(1, 4), (4, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (4, 5)] not in gs)

        # matching
        dc = {}
        for v in range(1, 7):
            dc[v] = range(0, 2)
        gs = GraphSet.graphs(degree_constraints=dc)
        self.assertEqual(len(gs), 22)
        self.assertTrue([(1, 2), (3, 6)] in gs)
        self.assertTrue([(1, 2), (2, 3), (3, 6)] not in gs)
        for g in gs:
            self.assertTrue(len(g) < 4)

        # subgraphs with 1 or 2 edges
        gs = GraphSet.graphs(num_edges=range(1, 3))
        self.assertEqual(len(gs), 28)
        for g in gs:
            self.assertTrue(1 <= len(g) and len(g) < 3)

        # single connected component and vertex islands
        gs = GraphSet.graphs(vertex_groups=[[]])
        self.assertEqual(len(gs), 80)
        self.assertTrue([(1, 2), (2, 3)] in gs)
        self.assertTrue([(1, 2), (2, 3), (4, 5)] not in gs)

        # any forest
        gs = GraphSet.graphs(no_loop=True)
        self.assertEqual(len(gs), 112)
        self.assertTrue([(1, 2), (1, 4), (2, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)
        for g in gs:
            self.assertTrue(len(g) < 6)

        # constrained by GraphSet
        gs = GraphSet.graphs(no_loop=True)
        gs = gs.graphs(vertex_groups=[[]])
        self.assertEqual(len(gs), 66)
        self.assertTrue([(1, 2), (1, 4), (2, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)

        # single connected components across 1, 3, and 5
        gs = GraphSet.connected_components([1, 3, 5])
        self.assertEqual(len(gs), 35)
        self.assertTrue([(1, 2), (2, 3), (2, 5)] in gs)
        self.assertTrue([(1, 2), (2, 3), (5, 6)] not in gs)

        GraphSet.set_universe([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4),
                               (2, 5), (3, 4), (3, 5), (4, 5)])

        # cliques with 4 vertices
        gs = GraphSet.cliques(4)
        self.assertEqual(len(gs), 5)
        self.assertTrue([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] in gs)
        self.assertTrue([(1, 2), (1, 3), (1, 4), (2, 3), (2,
                                                          4), (3,
                                                               5)] not in gs)

        GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
                               (5, 6)])

        # trees rooted at 1
        gs = GraphSet.trees(1)
        self.assertEqual(len(gs), 45)
        self.assertTrue([] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)

        # spanning trees
        gs = GraphSet.trees(is_spanning=True)
        self.assertEqual(len(gs), 15)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5), (4, 5)] not in gs)
        for g in gs:
            self.assertEqual(len(g), 5)

        # forests rooted at 1 and 3
        gs = GraphSet.forests([1, 3])
        self.assertEqual(len(gs), 54)
        self.assertTrue([] in gs)
        self.assertTrue([(1, 2), (2, 3)] not in gs)

        # spanning forests rooted at 1 and 3
        gs = GraphSet.forests([1, 3], is_spanning=True)
        self.assertEqual(len(gs), 20)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (3, 6)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5)] not in gs)
        for g in gs:
            self.assertEqual(len(g), 4)

        # cycles
        gs = GraphSet.cycles()
        self.assertEqual(len(gs), 3)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] in gs)
        self.assertTrue([] not in gs)

        # hamilton cycles
        gs = GraphSet.cycles(is_hamilton=True)
        self.assertEqual(len(gs), 1)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (3, 6), (4, 5), (5, 6)] in gs)

        # paths between 1 and 6
        gs = GraphSet.paths(1, 6)
        self.assertEqual(len(gs), 4)
        self.assertTrue([(1, 2), (2, 3), (3, 6)] in gs)
        self.assertTrue([(1, 2), (2, 3), (5, 6)] not in gs)

        # hamilton paths between 1 and 6
        gs = GraphSet.paths(1, 6, is_hamilton=True)
        self.assertEqual(len(gs), 1)
        self.assertTrue([(1, 4), (2, 3), (2, 5), (3, 6), (4, 5)] in gs)

        # called as instance methods
        gs = GraphSet.graphs(no_loop=True)
        _ = gs.connected_components([1, 3, 5])
        _ = gs.cliques(4)
        _ = gs.trees(1)
        _ = gs.forests([1, 3])
        _ = gs.cycles()
        _ = gs.paths(1, 6)

        # exceptions
        self.assertRaises(KeyError, GraphSet.graphs, vertex_groups=[[7]])
        self.assertRaises(KeyError, GraphSet.graphs, degree_constraints={7: 1})
Ejemplo n.º 7
0
# イテレーションを導入することで同値なグラフを代表元のみにする.
from graphillion import GraphSet
import graphillion.tutorial as tl
import networkx as nx
from tree_univ import MakeUniverse, DrawGraph, OperationtoTrees

n = 8
[wtd_tree, wtd] = MakeUniverse().tu_wtd(n)

# universe内の頂点8の木がなすGraphsetを作る
# is_spanning はTrue にするとグラフセット全体ですべての頂点を通るようにしてくれる.
GraphSet.set_universe(wtd_tree)
tree = GraphSet.trees(root=0, is_spanning=False)
gs = tree.len(n - 1)
'''
# 適当に制約を付けてchoice() で一つ選ぶ
gs1 = gs.including((1, 9))
g = gs1.choice()

draw = DrawGraph()
draw.graph(g, wtd, n=8)
'''
'''
for k in range(8-1):
    [edge_k, wtd_k] = MakeUniverse().te_wtd(8, k)

    print(len(edge_k))
    draw = DrawGraph()
    draw.graph(edge_k, wtd_k, n=8)
'''
Ejemplo n.º 8
0
    pos[int(row[1])] = (x_pos, y_pos)

#路線を設定
for row in fp2:
    edge_list.append([int(row[0]), int(row[1]), float(row[2])])
    G.add_edge(int(row[0]), int(row[1]))
    edges = int(row[0]), int(row[1]), float(row[2])
    universe.append(edges)

print(len(universe))
#グラフ作成
start_station = 1122115  #スタート駅コード
end_station = ''
degree_constraints = {start_station: 1}
GraphSet.set_universe(universe)
tree = GraphSet.trees(root=start_station)
for edge in tree._weights:
    for row in edge_list:
        if row[0] in (edge[0], edge[1]) and row[1] in (edge[0], edge[1]):
            edges = int(edge[0]), int(edge[1]), float(row[2])
            tree_uni.append(edges)
for v in tree._vertices:
    if v == start_station:
        dc.setdefault(v, 1)
        continue
    dc.setdefault(v, zero_or_two)

#Graphillionで全探索・経路数表示
path_list = search_all_station_graphillion(start_station, tree, tree_uni, dc)
print(len(path_list))
"""
Ejemplo n.º 9
0
from graphillion import GraphSet
import graphillion.tutorial as tl

universe = tl.grid(2, 2)
GraphSet.set_universe(universe)
lines = GraphSet({'include': [(8, 9), (5, 8), (4, 5)], 'exclude': [(6, 9)]})
trees = GraphSet.trees(is_spanning=True)
common = trees & lines

for path in common:
    tl.draw(path)