Exemplo n.º 1
0
    def test_make_graph(self):
        expected = Graph(directed=False)

        # Top-Down
        expected.add_edge((0, 0), (1, 0))
        expected.add_edge((1, 0), (2, 0))
        expected.add_edge((0, 1), (1, 1))
        expected.add_edge((1, 1), (2, 1))

        # Left-Right
        expected.add_edge((0, 0), (0, 1))
        expected.add_edge((1, 0), (1, 1))
        expected.add_edge((1, 1), (1, 2))
        expected.add_edge((2, 0), (2, 1))

        # Diagonal
        expected.add_edge((0, 0), (1, 1))
        expected.add_edge((0, 1), (1, 2))
        expected.add_edge((2, 0), (1, 1))
        expected.add_edge((2, 1), (1, 2))

        actual = self.simple_game.make_graph()

        self.assertSetEqual(set(expected.vertices()), set(actual.vertices()))
        self.assertSetEqual(set(frozenset(edge) for edge in expected.edges()),
                            set(frozenset(edge) for edge in actual.edges()))
Exemplo n.º 2
0
    def create_graph(self):
        graph = Graph(directed=False, multiedged=False)

        for i, level in enumerate(self._field):
            for j, cell, in enumerate(level):
                start = i, j
                ends = [i for i in self.get_environment(i, j)]

                for valid_end in (end for end in ends if self.is_valid(*end)):
                    graph.add_edge(start, valid_end)

        return graph
Exemplo n.º 3
0
    def setUp(self):
        self.test_solver = solver.Solver()
        self.field1 = TriangleField([[1], [1, 0, 2], [3, 0, 3, 0, 2]])
        self.field2 = TriangleField([[1], [1, 0, 2], [1, 0, 3, 0, 2],
                                     [3, 0, 0, 0, 4, 0, 4]])
        self.field3 = TriangleField([[1], [1, 0, 2]])

        self.graph = Graph(directed=False)
        self.graph.add_edge('a', 'b')
        self.graph.add_edge('a', 'c')
        self.graph.add_edge('c', 'b')
        self.graph.add_edge('b', 'd')
Exemplo n.º 4
0
    def make_graph(self):
        """
        Строит граф шестиугольного поля и возвращает его.
        """
        graph = Graph(directed=False, multiedged=False)

        middle = len(self.field) // 2
        for i, level in enumerate(self.field):
            for j, cell, in enumerate(level):
                start = i, j
                ends = {(i + 1, j), (i, j + 1),
                        (i + 1, j + 1) if i < middle else (i + 1, j - 1)}

                for valid_end in (end for end in ends if self.is_valid(*end)):
                    graph.add_edge(start, valid_end)

        return graph
Exemplo n.º 5
0
    def setUp(self):
        self.instance_one_solution = HexLink([
            [1, 0],  # 1 0
            [0, 2, 1],  # 0 2 1
            [0, 2]  # 0 2
        ])
        self.instance_many_solutions = HexLink([
            [1, 2],  # 1 2
            [0, 0, 0],  # 0 0 0
            [1, 2],  # 1 2
        ])
        self.instance_no_solutions = HexLink([
            [1, 2],  # 1 2
            [0, 0, 0],  # 0 0 0
            [2, 1]  # 2 1
        ])

        self.graph = Graph(directed=False)
        self.graph.add_edge("p", "q")  # e_1
        self.graph.add_edge("p", "r")  # e_2
        self.graph.add_edge("r", "q")  # e_3
        self.graph.add_edge("q", "s")  # e_4
Exemplo n.º 6
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True)
ok(g)
ok(g.is_directed())
ok(not g.is_undirected())

g.add_vertices(1, 2, 3, 4)
ok(g.has_vertex(1))
ok(g.has_vertex(2))
ok(g.has_vertex(3))
ok(g.has_vertex(4))
ok(not g.has_vertex(5))
ok(not g.has_vertex(0))

g.add_edge(1, 2)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 4)
ok(g.has_edge(1, 2))
ok(g.has_edge(2, 3))
ok(not g.has_edge(2, 1))
ok(not g.has_edge(1, 3))
ok(not g.has_edge(4, 1))

eq(len(g.vertices()), 4)
eq(len(g.successors(1)), 1)
eq(len(g.predecessors(1)), 0)
Exemplo n.º 7
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False, multiedged=True)
ok(g)
ok(not g.directed())
ok(g.multiedged())

g = Graph(directed=True, multiedged=True)
ok(g)
ok(g.directed())
ok(g.multiedged())
Exemplo n.º 8
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True)
g.add_vertices(1, 2, 3)
g.add_edge(1, 2)
g.add_edge(1, 2)
g.add_edge(2, 3)

eq(len(g.neighbors(1)), 1)
eq(len(g.neighbors(2)), 2)
eq(len(g.neighbors(3)), 1)

g.delete_vertex(2)

eq(len(g.neighbors(1)), 0)
eq(len(g.neighbors(3)), 0)

g = Graph(directed=False)
g.add_vertices(1, 2, 3)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.delete_vertex(1)
eq(len(g.vertices()), 2)
eq(len(g.neighbors(2)), 1)
eq(len(g.neighbors(3)), 1)
Exemplo n.º 9
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False)
ok(g)
ok(not g.is_directed())
ok(g.is_undirected())

g.add_vertices(1, 2, 3, 4)
ok(g.has_vertex(1))
ok(g.has_vertex(2))
ok(g.has_vertex(3))
ok(g.has_vertex(4))
ok(not g.has_vertex(5))
ok(not g.has_vertex(0))

g.add_edge(1, 2)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 4)
ok(g.has_edge(1, 2))
ok(g.has_edge(2, 3))
ok(g.has_edge(2, 1))
ok(not g.has_edge(1, 3))
ok(not g.has_edge(4, 1))

eq(len(g.vertices()), 4)
eq(len(g.neighbors(1)), 1)
eq(len(g.neighbors(2)), 2)
Exemplo n.º 10
0
#!/usr/bin/env python3
from graph_tools import Graph
from collections import deque
import pprint

H, W = map(int, input().split())
S = []
for i in range(H):
    l = str(input())
    S.append(list(l))
# print(H, W)
# print(S)

connections = []

g = Graph(connections)
dist = [-1 for i in range(H * W)]
dq = deque()

# init
dq.append(0)
dist[0] = 0

while dq != []:
    v = dq.pop()
    for v_adj in g.adjacency_list():
        # undiscovered vertex
        if (dist[v_adj] == -1):
            dist[v_adj] = dist[v] + 1
            dq.append(v_adj)
Exemplo n.º 11
0
import shutil

from enum import Enum
from dataclasses import dataclass
from typing import Sequence

from graph_tools import Graph
from kubernetes import client
from kubernetes.client import V1IPBlock

map_ip_2_svc = {}
map_name_2_pod = {}
map_name_2_rs = {}
map_name_2_deployments = {}

g = Graph()
logger = logging.getLogger('traffic_analyser')

EDGE_ATTRIBUTE_PORT_ID = 0
EDGE_ATTRIBUTE_PORT_NAME = 'ports'
VERTEX_ATTRIBUTE_POD = 'pod'

include_ingress_in_policy = True
include_egress_in_policy = True
create_dns_policy = True


def process_file(file):
    base_folder = file[:file.rindex('/')]
    with open(file, "r") as a_file:
        capture_metadata = json.loads(a_file.read())