Пример #1
0
 def testDFS(self):
     g = {"a": ["d", "f"],
          "b": ["c"],
          "c": ["b", "c", "d", "e"],
          "d": ["a", "c"],
          "e": ["c"],
          "f": ["d"]
          }
     graph = Graph(g)
     graph.DFS("a")
Пример #2
0
 def test_graph_DFS(self) -> None:
     graph = Graph()
     Node_0 = Node(0)
     Node_1 = Node(1)
     Node_2 = Node(2)
     Node_3 = Node(3)
     graph.add_directed_edge(Node_0, Node_1)
     graph.add_directed_edge(Node_0, Node_2)
     graph.add_directed_edge(Node_1, Node_2)
     graph.add_directed_edge(Node_2, Node_0)
     graph.add_directed_edge(Node_2, Node_3)
     graph.add_directed_edge(Node_3, Node_3)
     graph.DFS(Node_2)
Пример #3
0
def main():
    graph = Graph()  # Instantiate your graph
    graph.add_vertex('0')
    graph.add_vertex('1')
    graph.add_vertex('2')
    graph.add_vertex('3')
    graph.add_edge('0', '1')
    graph.add_edge('0', '3')
    print(graph.vertices)
    print(graph.BFT("0"))
    print(graph.DFT("0"))
    print(graph.DFTR("0"))
    print(graph.BFS("0", "1"))
    print(graph.DFS("0", "1"))
Пример #4
0
def main():
    graph = Graph()

    print("O grafo gerado foi: ")
    print("\n")
    graph.DFS()
    print("\n")

    print('-' * 80)

    first = input('Digite o primeiro nome de usuário: ')
    second = input('Digite o segundo nome de usuário: ')

    print('-' * 80)

    print("\n")
    print("Distância entre {} e {}: ".format(first, second), end=" ")
    print(graph.distance(first, second))
    print("\n")
Пример #5
0
def main():
    g = Graph({0: [(2, 1), (1, 10)], 1: [(0, 1)], 2: [(2, 1), (1, 1)]})
    h = Graph({
        0: [(1, 2), (8, 866)],
        1: [(0, 3), (2, 1)],
        2: [(3, 4), (4, 1), (5, 2), (8, 1)],
        3: [(2, 2)],
        4: [(2, 1), (7, 3)],
        5: [(2, 3), (6, 2), (4, 1)],
        6: [(8, 1), (5, 1)],
        7: [(6, 4), (4, 4)],
        8: [(0, 4), (2, 1), (6, 6)]
    })

    print("DFS: %s" % str(h.DFS(0, 7)))
    print("BFS: %s" % str(h.BFS(0, 7)))
    print("Dikstra: %s" % str(h.dikstra(0, 7)))
    print("Heuristics: %s" % h.h(7))
    print("Astar: %s" % str(h.astar(0, 7, h.h(7))))
Пример #6
0
def main():
    num = 68; numVertices = 320000
    df = pd.read_csv('inputdata\input_mostlyCycles_%d_%d.txt'%(num,numVertices),sep=' ',header=None)
    dfNum = len(df)

    g = Graph(numVertices)
    for i in range(dfNum):
        x = df.iloc[i,0] -1 ; y = df.iloc[i,1] -1;
        g.addEdge(x,y)
    g.DFS()

    grev = g.getTranspose()
    grev.DFS()


    #print(grev.leader)
    arrayOut = countandoutmax5(grev.leader,numFind = 5)
    print(arrayOut)
    with open('inputdata\output_mostlyCycles_%d_%d.txt'%(num,numVertices),'w') as f:
        f.write(str(arrayOut))
Пример #7
0
def test013_DFS2_BFS2_xDFS2_xDFS2_aview2_wview2():
    g = Graph()
    g.addClique("abcdefg", w=0)
    assert g.short(
    ) == "a>b,c,d,e,f,g  b>a,c,d,e,f,g  c>a,b,d,e,f,g  d>a,b,c,e,f,g  e>a,b,c,d,f,g  f>a,b,c,d,e,g  g>a,b,c,d,e,f"
    assert g.short(
        out=False
    ) == "a<b,c,d,e,f,g  b<a,c,d,e,f,g  c<a,b,d,e,f,g  d<a,b,c,e,f,g  e<a,b,c,d,f,g  f<a,b,c,d,e,g  g<a,b,c,d,e,f"
    a = """\
Adjacency matrix (1=arc present, 2=self loop, "." or 0=absent):
     a b c d e f g
a: | . 1 1 1 1 1 1 |
b: | 1 . 1 1 1 1 1 |
c: | 1 1 . 1 1 1 1 |
d: | 1 1 1 . 1 1 1 |
e: | 1 1 1 1 . 1 1 |
f: | 1 1 1 1 1 . 1 |
g: | 1 1 1 1 1 1 . |"""
    assert g.aview() == a

    w = """\
Adjacency Matrix of weights (values = arc weights, "." = absent arc):
     a b c d e f g
a: | . 0 0 0 0 0 0 |
b: | 0 . 0 0 0 0 0 |
c: | 0 0 . 0 0 0 0 |
d: | 0 0 0 . 0 0 0 |
e: | 0 0 0 0 . 0 0 |
f: | 0 0 0 0 0 . 0 |
g: | 0 0 0 0 0 0 . |"""
    assert g.wview() == w
    assert g.DFS("a", sort=True) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert g.DFS("b", sort=True) == ['b', 'a', 'c', 'd', 'e', 'f', 'g']
    assert g.BFS("a", sort=True) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert g.BFS("b", sort=True) == ['b', 'a', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xDFS("a", sort=True)) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xDFS("b", sort=True)) == ['b', 'a', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xBFS("a", sort=True)) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xBFS("b", sort=True)) == ['b', 'a', 'c', 'd', 'e', 'f', 'g']
Пример #8
0
def test014_DFS3_BFS3_xDFS3_xDFS3_aview3_wview3_addPath1():
    g = Graph()
    g.addPath("abcdefg", w=0)
    assert g.short() == "a>b  b>c  c>d  d>e  e>f  f>g  g"
    assert g.short(out=False) == "a  b<a  c<b  d<c  e<d  f<e  g<f"
    a = """\
Adjacency matrix (1=arc present, 2=self loop, "." or 0=absent):
     a b c d e f g
a: | . 1 . . . . . |
b: | . . 1 . . . . |
c: | . . . 1 . . . |
d: | . . . . 1 . . |
e: | . . . . . 1 . |
f: | . . . . . . 1 |
g: | . . . . . . . |"""
    assert g.aview() == a

    w = """\
Adjacency Matrix of weights (values = arc weights, "." = absent arc):
     a b c d e f g
a: | . 0 . . . . . |
b: | . . 0 . . . . |
c: | . . . 0 . . . |
d: | . . . . 0 . . |
e: | . . . . . 0 . |
f: | . . . . . . 0 |
g: | . . . . . . . |"""
    assert g.wview() == w
    assert g.DFS("a", sort=True) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert g.DFS("b", sort=True) == ['b', 'c', 'd', 'e', 'f', 'g']
    assert g.BFS("a", sort=True) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert g.BFS("b", sort=True) == ['b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xDFS("a", sort=True)) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xDFS("b", sort=True)) == ['b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xBFS("a", sort=True)) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xBFS("b", sort=True)) == ['b', 'c', 'd', 'e', 'f', 'g']
Пример #9
0
    
    for iter in range(500):
        query = graph.randomSample()
        if VisSafe( query  ):
            graph.vertices_graph.append( Vertice(query) )
            graph.connectKnearestVertices( Vertice(query) )
        print(iter)
    print("Graph Connected")

    #src     = Vertice(( 0, -1.3962,       0,      0,      0, 0))
    #goal    = Vertice(( 0, 0      ,   1.047, -1.039, -1.039, 0))

    rad_from_deg = np.pi/180.
    start_joints_deg = np.array([0.,   0., 20., 50., -80., 0.])
    final_joints_deg = np.array([0., 135., 20., 50., -80., 0.])

    start_joints_rad = start_joints_deg * rad_from_deg
    final_joints_rad = final_joints_deg * rad_from_deg

    src = Vertice(totuple(start_joints_rad))
    goal = Vertice(totuple(final_joints_rad))



    graph.access(src)
    graph.depart(goal)
    path = graph.DFS(src, goal)
    np.save("execute_path.npy", path)  
    
    execute()
Пример #10
0
from graph import Graph

graph = {
    'A': ['B', 'E', 'H'],
    'B': ['C', 'D'],
    'E': ['F', 'G'],
    'H': ['G', 'I'],
    'C': [],
    'D': [],
    'F': [],
    'I': [],
    'G': []
}
if __name__ == '__main__':
    g = Graph()
    g.set_graph(graph)
    g.print_graph()
    print("Visited node :", g.DFS('A', 'F'))
    print('Path to goal', g.path_to_goal('A', 'F'))
Пример #11
0
def main():
    g = Graph({0: [(2, 1), (1, 10)], 1: [(0, 1)], 2: [(2, 1), (1, 1)]})
    print("DFS: %s" % str(g.DFS(0, 1)))
    print("BFS: %s" % str(g.BFS(0, 1)))
    print("Dijkstra: %s" % str(g.dijkstra(0, 1)))
    print("A*: %s" % str(g.astar(0, 1)))
Пример #12
0
    v[0].set_adj([v[1], v[2]])
    v[1].set_adj([v[3]])
    v[2].set_adj([v[1]])
    v[3].set_adj([v[2]])
    v[4].set_adj([v[3], v[5]])
    v[5].set_adj([v[5]])

    for vtx in v:
        gd.insert(vtx)

    for u in gd.v:
        u.print_adj()
    print()

    gd.DFS()

    print("DFS vremena: posećen|završen")
    for u in gd.v:
        print(f"Čvor {u}: {u.d}|{u.f}")
    print()

    # Zadatak 4

    print("Zadatak 4\n")

    gt = Graph()
    v = []
    for i in range(0, 9):
        v.append(Vertex(Data(i), VertexColor.WHITE))
Пример #13
0
            elif 'addEdge' in commands:
                u = int(commands[1])
                v = int(commands[2])
                graph.addEdge(u, v)
                print('Added Edge')
                print("(u,v) \t", u, '->', v)

            elif 'BFS' in commands:
                # print("BFS:")
                s = int(commands[1])
                print("Run Bfs on Source:", s)
                graph.BFS(s)
                graph.printParents()
                graph.printDistance()
            elif 'DFS' in commands:
                graph.DFS()
                graph.printParents()
                graph.printColors()
                graph.printStamps()

            elif 'printGraph' in commands:
                graph.printGraph()

            else:
                print('ERROR:\t command {} not found'.format(line))

        # for cmd in lines:
        #     # print("CMD>> " , cmd)
        #     if cmd == 'addEdge':
        #         print(cmd)
        #     elif cmd == 'BFS':
Пример #14
0
def test012_DFS1_BFS1_xDFS1_xDFS1_aview1_wview1():
    """
    >>> g = Graph()
    >>> g.DFS("a")
    Traceback (most recent call last):
    ...
        assert startNode in self_a
    AssertionError
    """
    g = Graph({
        'a': {
            'c': 0,
            'b': 0
        },
        'c': {
            'e': 0
        },
        'b': {
            'e': 0,
            'd': 0,
            'f': 0
        },
        'e': {
            'f': 0
        },
        'd': {
            'f': 0
        },
        'g': {},
        'f': {
            'g': 0
        }
    })
    assert g.short() == "a>b,c  b>d,e,f  c>e  d>f  e>f  f>g  g"
    assert g.short(out=False) == "a  b<a  c<a  d<b  e<b,c  f<b,d,e  g<f"
    a = """\
Adjacency matrix (1=arc present, 2=self loop, "." or 0=absent):
     a b c d e f g
a: | . 1 1 . . . . |
b: | . . . 1 1 1 . |
c: | . . . . 1 . . |
d: | . . . . . 1 . |
e: | . . . . . 1 . |
f: | . . . . . . 1 |
g: | . . . . . . . |"""
    assert g.aview() == a

    w = """\
Adjacency Matrix of weights (values = arc weights, "." = absent arc):
     a b c d e f g
a: | . 0 0 . . . . |
b: | . . . 0 0 0 . |
c: | . . . . 0 . . |
d: | . . . . . 0 . |
e: | . . . . . 0 . |
f: | . . . . . . 0 |
g: | . . . . . . . |"""
    assert g.wview() == w
    assert g.DFS("a", sort=True) == ['a', 'b', 'd', 'f', 'g', 'e', 'c']
    assert g.DFS("b", sort=True) == ['b', 'd', 'f', 'g', 'e']
    assert g.BFS("a", sort=True) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert g.BFS("b", sort=True) == ['b', 'd', 'e', 'f', 'g']
    assert list(g.xDFS("a", sort=True)) == ['a', 'b', 'd', 'f', 'g', 'e', 'c']
    assert list(g.xDFS("b", sort=True)) == ['b', 'd', 'f', 'g', 'e']
    assert list(g.xBFS("a", sort=True)) == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    assert list(g.xBFS("b", sort=True)) == ['b', 'd', 'e', 'f', 'g']