Beispiel #1
0
def test_graph():
    graph = Graph()
    assert graph
    assert graph.nodes == set()

    foo = Node()
    bar = Node()

    graph = Graph(foo)
    assert graph.nodes == {foo}

    graph = Graph(foo, bar)
    assert graph.nodes == {foo, bar}
Beispiel #2
0
def welsh_powell(graph: Graph, color_generator=iterate_colors()):
    sorted_nodes = sorted(graph.nodes, key=attrgetter("degree"), reverse=True)

    for node in sorted_nodes:
        if node.color:
            continue

        color = next(color_generator)
        node.color = color

        non_adjacent_nodes = {
            node
            for node in graph.nodes_not_connected_to(node) if not node.color
        }

        non_adjacent_nodes_sorted = sorted(non_adjacent_nodes,
                                           key=attrgetter("degree"),
                                           reverse=True)

        nodes_not_to_color = set()
        for non_adjacent_node in non_adjacent_nodes_sorted:
            if non_adjacent_node in nodes_not_to_color:
                continue
            nodes_not_to_color.update(non_adjacent_node.connections)

        nodes_to_color = non_adjacent_nodes - nodes_not_to_color

        for node_to_color in nodes_to_color:
            node_to_color.color = color
Beispiel #3
0
def test_graph_chromatic_number():
    foo = Node(color=Color.RED)
    bar = Node(color=Color.BLUE)
    bar_again = Node(color=Color.BLUE)

    graph = Graph(foo, bar, bar_again)
    assert graph.chromatic_number == 2
Beispiel #4
0
def test_graph_node_connect_hook():
    foo = Node(1)
    bar = Node(2)

    graph = Graph(foo, bar)

    assert graph.connections == {
        1: set(),
        2: set(),
    }

    # connection to a node updates graph connection state
    foo.connect(bar)
    assert foo.connections == {bar}
    assert bar.connections == {foo}
    assert graph.connections == {
        1: {bar},
        2: {foo},
    }

    foo.disconnect(bar)
    assert foo.connections == set()
    assert bar.connections == set()
    assert graph.connections == {
        1: set(),
        2: set(),
    }
Beispiel #5
0
def graph_from_custom_file(path, node_callback, connection_factory):
    """ Iterate the file line by line and uses callback to instantiate node instances """

    nodes = set()
    with open(path, "r") as nodes_file:
        for line in nodes_file:
            node = node_callback(line)
            nodes.add(node)

    return Graph(*nodes, connection_factory=connection_factory)
Beispiel #6
0
def test_graph_connections():
    red_one = Node(color=Color.RED)
    red_two = Node(color=Color.RED)
    blue_one = Node(color=Color.BLUE)
    blue_two = Node(color=Color.BLUE)

    nodes = {red_one, red_two, blue_one, blue_two}

    def factory_method(node):
        return node.color

    Graph(*nodes, connection_factory=factory_method)

    assert red_one.connections == {red_two}
    assert red_two.connections == {red_one}
    assert blue_one.connections == {blue_two}
    assert blue_two.connections == {blue_one}
Beispiel #7
0
def graph_from_description(description):
    nodes = {}

    def __get_node(node_id):
        node = nodes.get(node_id)

        if not node:
            node = Node(node_id=int(node_id))
            nodes[node_id] = node

        return node

    for node_id, connection_ids in description:
        node = __get_node(node_id)

        for connection_id in connection_ids.split(";"):
            connected_node = __get_node(connection_id)

            node.connect(connected_node)

    return Graph(*set(nodes.values()))
Beispiel #8
0
def test_graph_multiple_connections():
    content_one = {"number": 1}
    content_two = {"number": 2}

    red_one = Node(color=Color.RED, content=content_one)
    red_two = Node(color=Color.RED, content=content_two)

    blue_one = Node(color=Color.BLUE, content=content_one)
    blue_two = Node(color=Color.BLUE, content=content_two)

    nodes = {red_one, red_two, blue_one, blue_two}

    def factory_method(node):
        return node.color, node.content["number"]

    Graph(*nodes, connection_factory=factory_method)

    assert red_one.connections == {red_two, blue_one}
    assert red_two.connections == {red_one, blue_two}
    assert blue_one.connections == {blue_two, red_one}
    assert blue_two.connections == {blue_one, red_two}
Beispiel #9
0
def test_graph_connection_rule():
    content_one = {"number": 1}
    content_two = {"number": 2}

    red_one = Node(color=Color.RED, content=content_one)
    red_two = Node(color=Color.RED, content=content_one)
    red_three = Node(color=Color.RED, content=content_two)

    blue_one = Node(color=Color.BLUE, content=content_one)
    blue_two = Node(color=Color.BLUE, content=content_two)

    nodes = {red_one, red_two, red_three, blue_one, blue_two}

    def factory_method(node):
        # Conect if the nodes have the same color and number
        return ConnectionRule(node.content["number"], node.color)

    Graph(*nodes, connection_factory=factory_method)

    assert red_one.connections == {red_two}
    assert red_two.connections == {red_one}
    assert red_three.connections == set()
    assert blue_one.connections == set()
    assert blue_two.connections == set()