def test_get_nodes():
    graph = Graph()
    node1 = graph.add_node(1)
    node2 = graph.add_node(2)
    actual = len(graph.get_nodes())
    expected = 2
    assert actual == expected
Example #2
0
def test_get_size():
    """test number of vertexes"""
    graph = Graph()
    apples = graph.add_vertex('apples')
    bananas = graph.add_vertex('bananas')

    assert len(graph) == 2
def test_add_vertex():

    graph = Graph()
    expected = 'test'
    vertex = graph.add_vertex('test')
    actual = vertex.value
    assert actual == expected
def test_node_graph_2():
    g = Graph()
    node1 = g.add_node('Test')
    node2 = g.add_node('random')
    actual = len(g.get_nodes())
    expected = 2
    assert actual == expected
Example #5
0
def test_get_vertices():
    # returns collection of all vertices
    graph = Graph()
    apples = graph.add_vertex('apples')
    bananas = graph.add_vertex('bananas')

    actual = graph.get_vertex()
    assert len(actual) == 2
Example #6
0
def graph():
    graph = Graph()
    graph.add_vertex("apples")
    graph.add_vertex("bananas")
    graph.add_vertex("oranges")
    graph.add_edge("apples", "bananas")
    graph.add_edge("oranges", "bananas", 5)
    return graph
Example #7
0
def test_add_edge_outsider():
    """edge can only be created between two vertexes inside graph"""
    graph = Graph()
    insider = graph.add_vertex('insider')
    outsider = Vertex('outsider')

    with pytest.raises(KeyError):
        graph.add_edge(outsider, insider)
def test_get_neighbors_none():
    g = Graph()
    node_a = g.add_node('a')
    node_b = g.add_node('b')
    node_c = g.add_node('c')
    node_d = g.add_node('d')
    actual = g.get_neighbors(node_a)
    expected = []
    assert actual == expected
def test_get_size():

    graph = Graph()

    test1 = graph.add_vertex('test1')

    test2 = graph.add_vertex('test2')

    assert graph.size() == 2
def test_get_vertices():

    graph = Graph()

    test1 = graph.add_vertex('test1')

    test2 = graph.add_vertex('test2')

    assert len(graph.get_vertices()) == 2
 def __init__(self):
     self.maps = Graph()
     self.A = self.maps.add_vertex('A')
     self.B = self.maps.add_vertex('B')
     self.C = self.maps.add_vertex('C')
     self.D = self.maps.add_vertex('D')
     self.E = self.maps.add_vertex('E')
     self.F = self.maps.add_vertex('F')
     self.G = self.maps.add_vertex('G')
     self.H = self.maps.add_vertex('H')
def test_size_2():
    g = Graph()
    g.add_node(235235)
    g.add_node(3232)
    g.add_node(222)
    actual = g.size()
    expected = 3
    assert actual == expected
def test_add_edge_exception_2():
    with pytest.raises(KeyError):
        g1 = Graph()
        g2 = Graph()
        node1 = g1.add_node(1)
        node2 = g2.add_node(2)
        g2.add_edge(node1, node2)
def test_add_edge():

    graph = Graph()
    vertex_1 = graph.add_vertex('test1')
    vertex_2 = graph.add_vertex('test2')
    assert len(graph.get_neighbors(vertex_1)) == 0
    graph.add_edge(vertex_1, vertex_2)
    assert len(graph.get_neighbors(vertex_1)) == 1
def test_get_neighbors_1():
    g = Graph()
    node_a = g.add_node('a')
    node_b = g.add_node('b')
    node_c = g.add_node('c')
    node_d = g.add_node('d')
    g.add_edge(node_a, node_b)
    actual = len(g.get_neighbors(node_a))
    expected = 1
    assert actual == expected
Example #16
0
def test_add_edge():
    graph = Graph()

    apples = graph.add_vertex('apples')
    bananas = graph.add_vertex('bananas')
    graph.add_edge(apples, bananas)

    assert True, ('will be fully excersized in get neighbors test')
def test_get_neighbors():

    graph = Graph()

    test1 = graph.add_vertex('test1')

    test2 = graph.add_vertex('test2')

    graph.add_edge(test1, test2)

    neighbors = graph.get_neighbors(test1)

    assert len(neighbors) == 1

    neighbor = neighbors[0]

    assert neighbor.vertex.value == 'test2'

    assert neighbor.weight == 1
Example #18
0
def test_add_vertex():
    graph = Graph()
    vertex = graph.add_vertex('spam')
    actual = vertex.value
    expected = 'spam'
    assert actual == expected
Example #19
0
def test_add_edge_raise_error():
    graph = Graph()
    graph.add_vertex("apples")
    with pytest.raises(KeyError):
        graph.add_edge("apples", "bananas")
Example #20
0
def test_graph_add_vertex_raise_error():
    graph = Graph()
    graph.add_vertex("apples")
    with pytest.raises(ValueError):
        graph.add_vertex("apples")
Example #21
0
def test_graph_add_vertex():
    graph = Graph()
    actual = str(graph.add_vertex("apples"))
    expected = "apples"
    assert actual == expected
Example #22
0
def test_empty_graph():
    graph = Graph()
    assert graph.get_vertices() == None
    assert graph.get_neighbors("key") == None
    assert len(graph) == 0
def test_size_1():
    g = Graph()
    g.add_node(1)
    actual = g.size()
    expected = 1
    assert actual == expected
def test_size_none():
    g = Graph()
    actual = g.size()
    expected = None
    assert actual == expected
class Test_Builder:
    def __init__(self):
        self.maps = Graph()
        self.A = self.maps.add_vertex('A')
        self.B = self.maps.add_vertex('B')
        self.C = self.maps.add_vertex('C')
        self.D = self.maps.add_vertex('D')
        self.E = self.maps.add_vertex('E')
        self.F = self.maps.add_vertex('F')
        self.G = self.maps.add_vertex('G')
        self.H = self.maps.add_vertex('H')

    def build_edges(self):
        self.maps.add_edge(self.A, self.B)
        self.maps.add_edge(self.A, self.D)
        self.maps.add_edge(self.B, self.C)
        self.maps.add_edge(self.B, self.D)
        self.maps.add_edge(self.C, self.G)
        self.maps.add_edge(self.D, self.E)
        self.maps.add_edge(self.D, self.H)
        self.maps.add_edge(self.D, self.F)
        self.maps.add_edge(self.F, self.H)
        return self.maps
Example #26
0
            return f"{destination} is not in graph"
        for edge in graph.get_neighbors(origin):
            found = False
            print("this is edge", edge)
            city, price = edge.split(" ")
            if city == destination:
                found = True
                sum += int(price)
                print("true sum", sum)
                break
        if found:
            print("sum", sum)
            origin = destination
        else:
            sum = 0
            break
    places = original + places
    return f"{places}   {found} ${sum}"


if __name__ == "__main__":
    graph = Graph()
    graph.add_vertex("Pandora")
    graph.add_vertex("Narnia")
    graph.add_vertex("Arendelle")
    graph.add_vertex("Naboo")
    graph.add_edge("Narnia", "Naboo", 100)
    graph.add_edge("Pandora", "Narnia", 100)
    graph.add_edge("Pandora", "Arendelle", 100)
    print(get_direct_route(graph, "Naboo", "Narnia", "Pandora"))
Example #27
0
def graph_depth_first(self, vertex):
    if vertex not in self.get_vertices():
        return f"{vertex} is not in graph"

    vertex_stack = Stack()
    visited_list = [vertex]
    vertex_stack.push(vertex)
    while not vertex_stack.isEmpty():
        new_vertex = vertex_stack.peek()
        new_vertex_neighbors = self.get_neighbors(new_vertex)
        counter = 0
        for edge in new_vertex_neighbors:
            vert, wt = edge.split(" ")
            if vert not in visited_list:
                vertex_stack.push(vert)
                visited_list.append(vert)
                counter += 1
        if counter == 0:
            vertex_stack.pop()
    return visited_list


Graph.depth_first = graph_depth_first
Graph.breadth_first = graph_breadth_first

if __name__ == "__main__":
    graph = Graph()
    graph.add_vertex("bananas")
    print(graph.depth_first("apples"))
def test_node_graph():
    graph = Graph()
    node = graph.add_node('Test Node')
    actual = str(node)
    expected = 'Test Node'
    assert actual == expected