Example #1
0
 def test_is_regular_should_return_true_for_regular(self):
     """Should return true when each node has the
     same number of edges"""
     vertices = [Vertex('a'), Vertex('b'), Vertex('9')]
     g = Graph(vertices, [])
     g.add_regular_edges()
     self.assertTrue(g.is_regular())
Example #2
0
def test_order(g):
    gr = Graph()

    assert gr.order == 0

    for i in range(MAX):
        gr.insert(i)
        assert gr.order == i + 1
Example #3
0
 def test_choose_method(self):
     """The object should choose the righ method
     for populating edges based on graph rules"""
     g = Graph()
     self.assertFalse(g.choose_method(1, 1))
     self.assertEquals(g.choose_method(1, 2), g.add_all_edges)
     with self.assertRaises(Exception):
         g.choose_method(2, 2)
Example #4
0
def test_set_weight_raises(g):
    g = Graph(range(5))

    with pytest.raises(KeyError):
        g.weight[1, 2] = 5

    with pytest.raises(KeyError):
        g.weight[42, 2] = 5
Example #5
0
def test_coloring_sparse():
    assert len(coloring(Graph())) == 0

    for i in range(1, 21):
        g = Graph(range(i))

        n_colors = len({c for _, c in coloring(g).items()})

        assert n_colors == 1
Example #6
0
 def test_regular_edges_works_with_three_vertices(self):
     """to build .add_regular_edges,
     we'll need a way tp count a Vertex's 
     number of edges"""
     vertices = [Vertex('a'), Vertex('b'), Vertex('c')]
     g = Graph(vertices, [])
     g.add_regular_edges()
     self.assertTrue(len(g.edges()) == 3)
     self.assertTrue(g.is_regular())
Example #7
0
    def test_regular_edges_fail_when_edges_exist(self):
        """should raise exception when edge exists"""
        vertices = [Vertex('a'), Vertex('b')]
        e = Edge(*vertices)
        g = Graph(vertices)
        g.add_edge(e)

        with self.assertRaises(Exception):
            g.add_regular_edges()
Example #8
0
def test_hamiltonian_cycle():
    g = Graph(
        range(11),
        zip(range(10), range(1, 10)),
    )

    for v in range(10):

        g.link(v, 10, 2)

    hc = hamiltonian_cycle(g, 0)

    assert hc == list(range(11))
Example #9
0
 def test_regular_edges_works_with_six_vertices(self):
     """to build .add_regular_edges,
     we'll need a way tp count a Vertex's 
     number of edges"""
     import string
     six_letters = [c for c in string.ascii_lowercase][:6]
     vertices = [Vertex(l) for l in six_letters]
     g = Graph(vertices, [])
     g.add_regular_edges()
     #pp = pprint.PrettyPrinter()
     #pp.pprint(g)
     self.assertTrue(len(g.edges()) == 6)
     self.assertTrue(g.is_regular())
Example #10
0
    def test_out_vertices(self):
        """Need this for the random graph"""
        x = Vertex('x')
        y = Vertex('y')
        z = Vertex('z')
        a = Vertex('a')
        edge_xy = Edge(x, y) 
        edge_ay = Edge(a, y) 
        g = Graph([x, y, z, a])
        g.add_edge(edge_ay)
        g.add_edge(edge_xy)

        self.assertTrue(len(g.out_vertices(y)) == 2)
        self.assertTrue(x in g.out_vertices(y))
        self.assertTrue(a in g.out_vertices(y))
Example #11
0
def test_tree_false(g):
    assert not is_tree(Graph())

    for i in range(MAX):
        g.link(i, (i + 1) % MAX)

    assert not is_tree(g)
Example #12
0
def g1():
    return Graph(range(1, 5), {
        (1, 2, 24),
        (1, 4, 20),
        (3, 1, 3),
        (4, 3, 12),
    })
Example #13
0
def test_color_dot(g):
    g = Graph(range(10), zip(range(9), range(1, 10)))

    def edge_color(v1, v2, w):
        return 'red' if (v1 + v2) > 5 else None

    assert to_dot(g, edge_color=edge_color) == g_dot_color
Example #14
0
def test_hamiltonian_cycle_raises():
    g = Graph(
        range(3),
        ((0, 1), (1, 2)),
    )

    with pytest.raises(HamiltonianCycleNotFound):
        hamiltonian_cycle(g, 1)
Example #15
0
def test_cycle_false(g):
    assert not has_cycle(Graph())

    vertices = list(g.vertices)

    for v1, v2 in zip(vertices, vertices[1:]):
        g.link(v1, v2, (v1 + v2) % 5)

    assert not has_cycle(g)
Example #16
0
def test_dfs():
    g = Graph(range(10), zip(range(9), range(1, 10)))

    for i in range(10):
        r = dfs(g, 0, lambda v: v == i)

        assert r == i

    assert dfs(g, 0, lambda v: False) is None
Example #17
0
def test_init():
    vertices = {1, 2, 3, 4}
    edges = {(1, 2), (2, 3, 10), (3, 4)}

    expected_edges = {(1, 2, 1), (2, 3, 10), (3, 4, 1)}

    g = Graph(set(vertices), set(edges))

    assert g.vertices == vertices
    assert g.edges == expected_edges
Example #18
0
    def setUp(self):
        logging.basicConfig(
            #filename ="/tmp/python.log",
            format="%(levelname)-10s %(asctime)s %(filename)s %(lineno)d %(message)s",
            level=logging.DEBUG
        )
        self.log = logging.getLogger(sys.argv[0])

        self.v = Vertex('v')
        self.w = Vertex('w')
        self.e = Edge(self.v, self.w)

        self.g = Graph([self.v, self.w], [self.e])
Example #19
0
def test_connected(g):
    assert is_connected(Graph())

    assert not is_connected(g)

    for i in range(0, MAX, 2):
        g.link(i, i + 1)

    assert not is_connected(g)

    for i in range(1, MAX, 2):
        g.link(i, (i + 1) % MAX)

    assert is_connected(g)
Example #20
0
 def test_is_connected_sees_non_connected_graph(self):
     a, b, c, d  = Vertex('a'), Vertex('b'), Vertex('c'), Vertex('d')
     e = Vertex('e')
     g = Graph([a, b, c, d, e]) 
     g.add_edge(Edge(a, b))
     g.add_edge(Edge(b, c))
     g.add_edge(Edge(c, d))
     g.add_edge(Edge(d, a))
     self.assertFalse(g.is_connected())
Example #21
0
def test_kruskal_raises():
    g = Graph(range(10))

    for v in g.vertices:
        g.link(0, v, 5)

    for v1 in g.vertices:
        for v2 in g.vertices:
            if v1 != v2 and not g.has_edge(v1, v2):
                g.link(v1, v2, 10)

    with pytest.raises(ValueError):
        kruskal(g)
Example #22
0
 def test_is_regular_should_return_false_for_nonregular(self):
     """Should return false when each node has the
     same number of edges"""
     vertices = Vertex('a'), Vertex('c')
     g = Graph(vertices, [])
     b = Vertex('b')
     g.add_vertex(b)
     e = Edge(b, vertices[1])
     g.add_edge(e)
     self.assertFalse(g.is_regular())
Example #23
0
 def test_is_connected_sees_connected_graph(self):
     """a graph is connected if there is a path
     from every node to every other node"""
     a, b, c, d  = Vertex('a'), Vertex('b'), Vertex('c'), Vertex('d')
     g = Graph([a, b, c, d]) 
     g.add_edge(Edge(a, b))
     g.add_edge(Edge(b, c))
     g.add_edge(Edge(c, d))
     g.add_edge(Edge(d, a))
     self.assertTrue(g.is_connected())
Example #24
0
    def test_add_all_edges(self):
        """Add edges foe each node"""
        empty_g = Graph()

        letters = ['l', 'm', 'o']
        for l in letters:
            empty_g.add_vertex(Vertex(l))

        empty_g.add_all_edges()
        self.assertTrue(len(empty_g.edges()) == 3)
Example #25
0
def test_regular(g):
    assert is_regular(Graph())

    assert is_regular(g)

    g.link(0, 1)

    assert not is_regular(g)

    for i in range(1, MAX):
        g.link(i, (i + 1) % MAX)

    assert is_regular(g)

    g.link(1, 3)

    assert not is_regular(g)
Example #26
0
def g2():
    return Graph(
        range(9), {
            (5, 6, 2),
            (1, 2, 8),
            (6, 8, 6),
            (1, 7, 11),
            (2, 5, 4),
            (7, 8, 7),
            (0, 7, 8),
            (6, 7, 1),
            (3, 5, 14),
            (2, 3, 7),
            (3, 4, 9),
            (2, 8, 2),
            (0, 1, 4),
            (4, 5, 10),
        })
Example #27
0
def test_str(g):
    g = Graph()

    assert str(g) == 'Graph(set(), set())'

    g.insert(0)
    g.insert(1)

    assert str(g) == 'Graph({0, 1}, set())'

    g.link(0, 1, 5)

    assert str(g) == 'Graph({0, 1}, {(0, 1, 5)})'
Example #28
0
def test_prim():
    g = Graph(range(10))

    for v in g.vertices:
        if v != 0:
            g.link(0, v, 5)

    for v1 in g.vertices:
        for v2 in g.vertices:
            if v1 != v2 and not g.has_edge(v1, v2):
                g.link(v1, v2, 10)

    tree = prim(g)

    assert tree.vertices == set(range(10))

    assert tree.edges == {(0, i, 5) for i in range(1, 10)}
Example #29
0
def test_fringe_raises():
    g = Graph()

    with pytest.raises(ValueError):
        fringe(g, [0])
Example #30
0
def g():
    return Graph(range(MAX))
Example #31
0
from itertools import product

import pytest

from context import (Graph, biclique, binary_tree, complete, crown, is_tree,
                     lattice)

lattice_test1 = Graph(
    {
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24
    }, {(0, 1), (2, 7), (17, 18), (5, 6), (4, 9), (6, 7), (8, 9), (10, 15),
        (3, 4), (7, 8), (15, 16), (15, 20), (13, 14), (21, 22), (2, 3), (0, 5),
        (17, 22), (8, 13), (10, 11), (22, 23), (3, 8), (11, 12), (23, 24),
        (6, 11), (14, 19), (12, 17), (1, 2), (16, 17), (18, 23), (5, 10),
        (19, 24), (12, 13), (20, 21), (1, 6), (16, 21), (18, 19), (11, 16),
        (9, 14), (7, 12), (13, 18)})

lattice_test2 = Graph(
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
    {(11, 13), (0, 1), (1, 3), (6, 7), (8, 9), (0, 2), (2, 4), (9, 11), (2, 3),
     (14, 15), (8, 10), (10, 12), (10, 11), (4, 5), (16, 17), (5, 7), (17, 19),
     (4, 6), (6, 8), (14, 16), (12, 13), (16, 18), (18, 19), (3, 5), (7, 9),
     (15, 17), (13, 15), (12, 14)})

lattice_test3 = Graph({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
                      {(0, 1), (2, 7), (5, 6), (4, 9), (6, 7), (8, 9), (3, 4),
                       (7, 8), (13, 14), (2, 3), (0, 5), (8, 13), (10, 11),
                       (3, 8), (11, 12), (6, 11), (1, 2), (5, 10), (12, 13),
                       (1, 6), (9, 14), (7, 12)})
Example #32
0
def test_fringe():
    g = Graph(range(10))

    g.link(0, 1)
    g.link(0, 2)
    g.link(0, 3)
    g.link(0, 4)

    g.link(1, 5)
    g.link(1, 6)
    g.link(1, 7)
    g.link(1, 8)

    g.link(2, 9)

    assert fringe(g, [0]) == set(range(1, 5))

    assert fringe(g, [1]) == {0} | set(range(5, 9))

    assert fringe(g, [2]) == {0, 9}

    assert fringe(g, [0, 1]) == set(range(2, 9))

    assert fringe(g, [0, 1, 2]) == g.vertices - {0, 1, 2}
Example #33
0
def test_weighted_dot(g):
    g = Graph(range(10), zip(range(9), range(1, 10), [5] * 9))

    assert to_dot(g) == g_dot_weighted
Example #34
0
def g():
    return Graph(range(10), zip(range(9), range(1, 10)))
Example #35
0
class TestGraph(unittest.TestCase):
    """
    testing graph class from Think Complexity
    """
    def setUp(self):
        logging.basicConfig(
            #filename ="/tmp/python.log",
            format="%(levelname)-10s %(asctime)s %(filename)s %(lineno)d %(message)s",
            level=logging.DEBUG
        )
        self.log = logging.getLogger(sys.argv[0])

        self.v = Vertex('v')
        self.w = Vertex('w')
        self.e = Edge(self.v, self.w)

        self.g = Graph([self.v, self.w], [self.e])
        
    def test_get_edge_returns_edge(self):
        q = Vertex('q')
        e2 = Edge(self.v, q)
        self.g.add_vertex(q)
        self.g.add_edge(e2)

        edge = self.g.get_edge(self.v, self.w)
        self.assertTrue(edge == self.e)

    #this one was giving me trouble
    def test_edges_returns_edges(self):
        expected = [self.e]
        self.assertEqual(self.g.edges(), expected)

    def test_vertices_should_return_vertices(self):
        self.assertTrue(self.v in self.g.vertices() \
                and self.w in self.g.vertices() \
                and len(self.g.vertices()) == 2)

    def test_get_edge_returns_none_for_missing_edge(self):
        """look for an edge that isn't there"""
        x = Vertex('x')
        edge = self.g.get_edge(x, self.w)
        self.assertTrue(edge == None)

    def test_remove_edge_removes_edge(self):
        """delete an edge"""
        self.g.remove_edge(self.e)
        self.assertTrue(self.g.get_edge(self.v, self.w) == None)

    def test_equality_of_edges(self):
        e1 = Edge(self.v, self.w) 
        e2 = Edge(self.w, self.v) 
        self.assertEquals(e1, e2)

    def test_out_edges(self):
        """Test that you get edges attached 
        to a vertex"""
        q = Vertex('q')
        e2 = Edge(self.v, q)
        self.g.add_vertex(q)
        self.g.add_edge(e2)

        self.assertTrue(self.e in self.g.out_edges(self.v), [self.e, e2])
        self.assertTrue(e2 in self.g.out_edges(self.v), [self.e, e2])
        self.assertTrue(len(self.g.out_edges(self.v)) == 2)
    
    def test_out_vertices(self):
        """Need this for the random graph"""
        x = Vertex('x')
        y = Vertex('y')
        z = Vertex('z')
        a = Vertex('a')
        edge_xy = Edge(x, y) 
        edge_ay = Edge(a, y) 
        g = Graph([x, y, z, a])
        g.add_edge(edge_ay)
        g.add_edge(edge_xy)

        self.assertTrue(len(g.out_vertices(y)) == 2)
        self.assertTrue(x in g.out_vertices(y))
        self.assertTrue(a in g.out_vertices(y))

    def test_add_all_edges(self):
        """Add edges foe each node"""
        empty_g = Graph()

        letters = ['l', 'm', 'o']
        for l in letters:
            empty_g.add_vertex(Vertex(l))

        empty_g.add_all_edges()
        self.assertTrue(len(empty_g.edges()) == 3)

    def test_regular_edges_works_with_6_vertices(self):
        """to build .add_regular_edges,
        we'll need a way tp count a Vertex's 
        number of edges"""
        vertices = [Vertex('x'), Vertex('y'), Vertex('z'), Vertex('a'), Vertex('b')]
        g = Graph(vertices, [])
        g.add_regular_edges()
        self.assertTrue(len(g.edges()) == 5)
        self.assertTrue(g.is_regular())

    def test_regular_edges_works_with_three_vertices(self):
        """to build .add_regular_edges,
        we'll need a way tp count a Vertex's 
        number of edges"""
        vertices = [Vertex('a'), Vertex('b'), Vertex('c')]
        g = Graph(vertices, [])
        g.add_regular_edges()
        self.assertTrue(len(g.edges()) == 3)
        self.assertTrue(g.is_regular())

    def test_regular_edges_works_with_six_vertices(self):
        """to build .add_regular_edges,
        we'll need a way tp count a Vertex's 
        number of edges"""
        import string
        six_letters = [c for c in string.ascii_lowercase][:6]
        vertices = [Vertex(l) for l in six_letters]
        g = Graph(vertices, [])
        g.add_regular_edges()
        #pp = pprint.PrettyPrinter()
        #pp.pprint(g)
        self.assertTrue(len(g.edges()) == 6)
        self.assertTrue(g.is_regular())

    def test_regular_edges_fail_when_edges_exist(self):
        """should raise exception when edge exists"""
        vertices = [Vertex('a'), Vertex('b')]
        e = Edge(*vertices)
        g = Graph(vertices)
        g.add_edge(e)

        with self.assertRaises(Exception):
            g.add_regular_edges()

    def test_choose_method(self):
        """The object should choose the righ method
        for populating edges based on graph rules"""
        g = Graph()
        self.assertFalse(g.choose_method(1, 1))
        self.assertEquals(g.choose_method(1, 2), g.add_all_edges)
        with self.assertRaises(Exception):
            g.choose_method(2, 2)
    
    def test_is_regular_should_return_true_for_regular(self):
        """Should return true when each node has the
        same number of edges"""
        vertices = [Vertex('a'), Vertex('b'), Vertex('9')]
        g = Graph(vertices, [])
        g.add_regular_edges()
        self.assertTrue(g.is_regular())

    def test_is_regular_should_return_false_for_nonregular(self):
        """Should return false when each node has the
        same number of edges"""
        vertices = Vertex('a'), Vertex('c')
        g = Graph(vertices, [])
        b = Vertex('b')
        g.add_vertex(b)
        e = Edge(b, vertices[1])
        g.add_edge(e)
        self.assertFalse(g.is_regular())

    def test_is_connected_sees_connected_graph(self):
        """a graph is connected if there is a path
        from every node to every other node"""
        a, b, c, d  = Vertex('a'), Vertex('b'), Vertex('c'), Vertex('d')
        g = Graph([a, b, c, d]) 
        g.add_edge(Edge(a, b))
        g.add_edge(Edge(b, c))
        g.add_edge(Edge(c, d))
        g.add_edge(Edge(d, a))
        self.assertTrue(g.is_connected())

    def test_is_connected_sees_non_connected_graph(self):
        a, b, c, d  = Vertex('a'), Vertex('b'), Vertex('c'), Vertex('d')
        e = Vertex('e')
        g = Graph([a, b, c, d, e]) 
        g.add_edge(Edge(a, b))
        g.add_edge(Edge(b, c))
        g.add_edge(Edge(c, d))
        g.add_edge(Edge(d, a))
        self.assertFalse(g.is_connected())
Example #36
0
def test_remove_edge_raises(g):
    with pytest.raises(KeyError):
        Graph().unlink(0, 1)
Example #37
0
def test_add_raises(g):
    g = Graph()
    g.insert(0)

    with pytest.raises(KeyError):
        g.insert(0)
Example #38
0
def test_remove_raises(g):
    with pytest.raises(KeyError):
        Graph().remove(0)
Example #39
0
def test_add_edge_raises(g):
    with pytest.raises(KeyError):
        Graph().link(0, 1)