예제 #1
0
파일: test_graph.py 프로젝트: mmweber2/adm
def test_dfs_chain():
    a = Vertex("A23")
    b = Vertex("A24")
    c = Vertex("A25")
    a.add_edge(Edge(b))
    b.add_edge(Edge(c))
    assert dfs(a, c)
예제 #2
0
파일: main.py 프로젝트: dgcnz/grakn-twitter
def grakn_save(root):
    with GraknClient(uri="localhost:48555") as client:
        with client.session(keyspace="twitter") as session:
            insert_person = partial(
                grakn_write, session,
                'insert $x isa person, has full-name "{v.name}", has uuid "{v.id}";',
                1)
            insert_friendship = partial(
                grakn_write, session,
                'match $p1 isa person, has uuid "{u.id}"; $p2 isa person, has uuid "{v.id}";insert $new_friendship (friend: $p1, friend: $p2) isa friendship;',
                2)

            print("INSERTING PEOPLE")
            dfs(root, lambda v: v.neighbors, None, insert_person)
            print("INSERTING FRIENDSHIPS")
            dfs(root, lambda v: v.neighbors, None, insert_friendship)
예제 #3
0
파일: test_graph.py 프로젝트: mmweber2/adm
def test_dfs_cycle():
    a = Vertex("A30")
    b = Vertex("A31")
    c = Vertex("A32")
    a.add_edge(Edge(b))
    b.add_edge(Edge(a))
    b.add_edge(Edge(c))
    assert dfs(a, c)
예제 #4
0
def solution(tree):
    final_path, paths = dfs(tree, return_paths=True)
    result = 0
    for path in paths:
        if path:
            if max(path) == path[-1]:
                result += 1
    return result
예제 #5
0
파일: main.py 프로젝트: dgcnz/grakn-twitter
def main():
    check_rate_limit()

    user_id, user_name = get_current_id_name()
    root = Node(user_id, user_name)
    root = dfs(root, get_next_mutuals, 2)
    bfs(root, lambda v: v.neighbors, print, 3)
    check_rate_limit()

    grakn_save(root)
예제 #6
0
파일: test_graph.py 프로젝트: mmweber2/adm
def test_dfs_multiple_paths():
    a = Vertex("A26")
    b = Vertex("A27")
    c = Vertex("A28")
    d = Vertex("A29")
    a.add_edge(Edge(b))
    b.add_edge(Edge(c))
    c.add_edge(Edge(d))
    a.add_edge(Edge(d))
    assert dfs(a, d)
예제 #7
0
파일: testgraph.py 프로젝트: altoid/graph
    def test_getpartitions(self):
        # sedgewick, p. 374

        subgraphs = graph.getpartitions(self.sedgewick)
        self.assertEqual(3, len(subgraphs))
        results = []
        for sgr in subgraphs:
            # gimme a node from the graph, i don't care which
            nd = next(iter(sgr.nodes()))
            r = ''.join(sorted(list(graph.dfs(sgr, nd))))
            results.append(r)
        self.assertEqual(['abcdefg', 'hi', 'jklm'], sorted(results))
    def buildGraph(self):
        '''
        练习22.3-1

        练习22.5-2
        '''
        g = _g.Graph()
        g.veterxs.clear()
        g.edges.clear()
        v = ['q', 's', 'v', 'w', 't', 'x', 'y', 'z', 'u', 'r']
        g.addvertex(v)
        g.addedge('q', 's', _g.DIRECTION_TO)
        g.addedge('q', 't', _g.DIRECTION_TO)
        g.addedge('q', 'w', _g.DIRECTION_TO)
        g.addedge('s', 'v', _g.DIRECTION_TO)
        g.addedge('v', 'w', _g.DIRECTION_TO)
        g.addedge('w', 's', _g.DIRECTION_TO)
        g.addedge('t', 'x', _g.DIRECTION_TO)
        g.addedge('t', 'y', _g.DIRECTION_TO)
        g.addedge('x', 'z', _g.DIRECTION_TO)
        g.addedge('y', 'q', _g.DIRECTION_TO)
        g.addedge('z', 'x', _g.DIRECTION_TO)
        g.addedge('u', 'y', _g.DIRECTION_TO)
        g.addedge('r', 'y', _g.DIRECTION_TO)
        g.addedge('r', 'u', _g.DIRECTION_TO)
        _g.dfs(g)
        for e in g.edges:
            u, v = g.getvertexfromedge(e)         
            if u.d < v.d and v.d < v.f and v.f < u.f:
                print("边({},{})是树边或者前向边".format(u.key, v.key))
            elif v.d < u.d and u.d < u.f and u.f < v.f:
                print("边({},{})是反向边".format(u.key, v.key))
            elif v.d < v.f and v.f < u.d and u.d < u.f:
                print("边({},{})是交叉边".format(u.key, v.key))
        print('')
        del g
예제 #9
0
파일: testgraph.py 프로젝트: altoid/graph
    def test_dfs(self):

        # cf. https://www.youtube.com/watch?v=zLZhSSXAwxI

        gr = graph.UGraph()
        a = graph.Node('a')
        b = graph.Node('b')
        c = graph.Node('c')
        d = graph.Node('d')
        e = graph.Node('e')
        f = graph.Node('f')
        g = graph.Node('g')
        h = graph.Node('h')

        gr.addnodes(a, b, c, d, e, f, g, h)
        
        gr.addedge(a, b)
        gr.addedge(a, g)
        gr.addedge(a, d)

        gr.addedge(b, e)
        gr.addedge(b, f)

        gr.addedge(c, f)
        gr.addedge(c, h)

        gr.addedge(d, f)
        gr.addedge(e, g)

        outsider = graph.Node('mr_lonely')
        
        with self.assertRaises(graph.GraphException):
            graph.dfs(gr, outsider)

        r = graph.dfs(gr, a)
        self.assertEqual('abegfchd', r)
예제 #10
0
파일: test_graph.py 프로젝트: mmweber2/adm
def test_dfs_no_path():
    a = Vertex("A21")
    b = Vertex("A22")
    assert not dfs(a, b)
예제 #11
0
파일: test_graph.py 프로젝트: mmweber2/adm
def test_dfs_direct_connection():
    a = Vertex("A19")
    b = Vertex("A20")
    a.add_edge(Edge(b))
    assert dfs(a, b)
예제 #12
0
파일: test_graph.py 프로젝트: mmweber2/adm
def test_dfs_start_node():
    a = Vertex("A18")
    assert dfs(a, a)
예제 #13
0
def test_all_alg():
    """
    Test all algorithms dfs, bfs, and a* with all heuristics used
    on a single randomly generated 8 puzzle
    """
    print("Testing all algorithms on a random 8 puzzle")
    random_puzzle = _gen_1_puzzle(3, EIGHT_PSOL)
    printPuzzle(random_puzzle)

    print("\nTesting DFS")
    start_time = time.time()
    states_taken, final = dfs(random_puzzle, EIGHT_PSOL)
    if final is None:
        print("Error this puzzle is not solvable trying again\n")
        test_all_alg()
        return
    print("%s seconds to solve with DFS" % (time.time() - start_time))
    print("{} steps to find the solution".format(states_taken))
    print("{} steps for the simple path to the solution".format(
        final.num_parents))
    print("\n----------- End DFS Testing ---------------\n")

    print("Testing BFS")
    start_time = time.time()
    states_taken, final = bfs(random_puzzle, EIGHT_PSOL)
    print("%s seconds to solve with BFS" % (time.time() - start_time))
    print("{} steps to find the solution".format(states_taken))
    print("{} steps for the simple path to the solution".format(
        final.num_parents))
    print("\n----------- End BFS Testing ---------------\n")

    print("Testing A* with Hamming Heuristic")
    start_time = time.time()
    states_taken, final = a_star(random_puzzle, EIGHT_PSOL, hamming_distance)
    print("%s seconds to solve with A* Hamming Distance Heuristic" %
          (time.time() - start_time))
    print("{} steps to find the solution".format(states_taken))
    print("{} steps for simple path to the solution".format(final.num_parents))
    print("\n----------- End A* Hamming Testing ---------------\n")

    print("Testing A* with Manhattan Heuristic")
    start_time = time.time()
    states_taken, final = a_star(random_puzzle, EIGHT_PSOL, manhattan_distance)
    print("%s seconds to solve with A* with Manhattan Distance Heuristic" %
          (time.time() - start_time))
    print("{} steps to find the solution".format(states_taken))
    print("{} steps for the simple path to the solution ".format(
        final.num_parents))
    print("\n----------- End A* Manhattan Testing ---------------\n")

    print("Testing A* with Linear Conflict Heuristic")
    start_time = time.time()
    states_taken, final = a_star(random_puzzle, EIGHT_PSOL, linear_conflict)
    print("%s seconds to solve with A* with Linear Conflict Heuristic" %
          (time.time() - start_time))
    print("{} steps to find the solution".format(states_taken))
    print("{} steps for simple path to the solution".format(final.num_parents))
    print("\n----------- End A* Manhattan Testing ---------------\n")

    print("Now showing simple path to solution")
    print("Initial state of the puzzle")
    printPuzzle(random_puzzle)
    print("-------------")
    path = reconstruct_path(final)
    for next in path:
        print("Take {} from previous puzzle".format(next.direction))
        printPuzzle(next.state)
        print("-------------")
    print("Final State above")
예제 #14
0
def test_invalid_traversal(graph):
    with pytest.raises(ValueError):
        dfs(graph)
    with pytest.raises(ValueError):
        bfs(graph)
예제 #15
0
def test_traversal(graph, expected):
    actual = [node.data for node in dfs(graph)]
    assert actual == expected