예제 #1
0
 def test_find_shortest_path1(self):
     graph1 = txt2dic("graph1.txt")
     expected_graph1 = {
         1: {
             2: 2,
             3: 4,
             4: 3
         },
         2: {
             5: 1
         },
         3: {
             6: 2,
             4: 3
         },
         4: {
             6: 4,
             1: 1
         },
         5: {
             6: 1
         },
         6: None
     }
     self.assertDictEqual(graph1, expected_graph1)
     assert find_shortest_path("graph1.txt", 1, 6) == (4, [1, 2, 5, 6])
     try:
         find_shortest_path("graph1.txt", 2, 1)
     except KeyError:
         print("No solution!")
     assert find_shortest_path("graph1.txt", 3, 2) == (6, [3, 4, 1, 2])
예제 #2
0
def test_finds_shortest_path_from_A_to_C():
    graph = {
        'A': [Vertex('B', 7), Vertex('C', 9)],
        'B': [Vertex('A', 7), Vertex('C', 1)],
        'C': [Vertex('A', 9), Vertex('B', 1)]
    } 
    start = 'A'
    end = 'C'
    expected = {'distance': 8, 'path': ['A', 'B', 'C']}
    assert_that(find_shortest_path(graph, start, end), equal_to(expected))
예제 #3
0
def test_finds_shortest_path_from_A_to_E():
    graph = {
        'A': [Vertex('C', 9), Vertex('F', 14)],
        'C': [Vertex('A', 9), Vertex('D', 11), Vertex('F', 2)],
        'D': [Vertex('C', 11), Vertex('E', 6)],
        'E': [Vertex('D', 6), Vertex('F', 9)],
        'F': [Vertex('A', 14), Vertex('C', 2), Vertex('E', 9)],
    } 
    start = 'A'
    end = 'E'
    expected = {'distance': 20, 'path': ['A', 'C', 'F', 'E']}
    assert_that(find_shortest_path(graph, start, end), equal_to(expected))
예제 #4
0
 def test_find_shortest_path2(self):
     graph2 = txt2dic("graph2.txt")
     expected_graph2 = {
         1: {
             2: 4,
             3: 4
         },
         2: {},
         3: {
             5: 4,
             6: 2
         },
         4: {
             3: 2
         },
         5: {
             7: 2
         },
         6: {
             5: 3
         },
         7: {
             8: 2,
             6: 2
         },
         8: {
             5: 2
         }
     }
     self.assertDictEqual(graph2, expected_graph2)
     assert find_shortest_path("graph2.txt", 1, 8) == (12, [1, 3, 5, 7, 8])
     try:
         find_shortest_path("graph2.txt", 2, 7)
     except KeyError:
         print("No solution!")
     assert find_shortest_path("graph2.txt", 7, 5) == (4, [7, 8, 5])
     assert find_shortest_path("graph2.txt", 4, 8) == (10, [4, 3, 5, 7, 8])
예제 #5
0
 def test_find_shortest_path3(self):
     graph3 = txt2dic("graph3.txt")
     expected_graph3 = {
         1: {
             2: -1,
             3: 2
         },
         2: {
             4: -1
         },
         3: {
             1: 4
         },
         4: {
             1: -1,
             5: 4
         },
         5: None
     }
     self.assertDictEqual(graph3, expected_graph3)
     assert find_shortest_path("graph3.txt", 1, 5) == (2, [1, 2, 4, 5])
예제 #6
0
파일: script.py 프로젝트: swalker-/Dijkstra
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0]]

start = Point(0,0)
start2 = Point(2,2)
start3 = Point(0,3)
end = Point(4,4)
end2 = Point(4,6)
end3 = Point(24, 20)

graph_info_list = [(graph0, start, end), (graph1, start, end), (graph2, start, end),
                   (graph0, start2, end), (graph1, start2, end), (graph2, start2, end),
                   (graph3, start2, end), (graph4, start2, end2), (graph5, start, end3)]
for graph_info in graph_info_list:
    result = dijkstra.find_shortest_path(graph_info, 5)

    new_graph = result[0]
    shortest_distance = result[1]
    shortest_paths = result[2]

    print("Start point: ", graph_info[1])
    print("End point: ", graph_info[2])
    print()

    for graph in graph_info[0]:
        for i in graph:
            print(repr(i).rjust(2), end=' ')
        print()
    print()
    for graph in new_graph:
예제 #7
0
    # Finds the Topological order of nodes in the graph
    print("Topological Order:", topological_sort(graph))
    # Expected Output = ['E', 'F', 'K', 'C', 'B', 'A', 'D', 'H', 'J', 'M', 'G', 'I', 'L']

    graph = {
        "A": [("B", 5), ("C", 1)],
        "B": [("C", 2), ("D", 3), ("E", 20)],
        "C": [("B", 3), ("E", 12)],
        "D": [("C", 3), ("E", 2), ("F", 6)],
        "E": [("F", 1)],
        "F": [],
    }

    # Use Djikstra to find shortest path between two nodes in a graph with no negative weights
    print("Shortest Path from A to F:", find_shortest_path(graph, "A", "F"))

    graph = {
        "A": [("B", 5)],
        "B": [("C", 20), ("G", 60), ("F", 30)],
        "C": [("D", 10), ("E", 75)],
        "D": [("C", -15)],
        "E": [("J", 100)],
        "F": [("G", 5), ("E", 25), ("I", 50)],
        "G": [("H", -50)],
        "H": [("I", -10)],
        "I": [],
        "J": []
    }

    # Finds the distance to all nodes from a single source in a graph with negative weights
예제 #8
0
 def test_2(self):
     assert find_shortest_path("dijkstra_2.txt", 1, 8) == (5, [1, 3, 6, 5, 7, 8])
예제 #9
0
 def test_1(self):
     assert find_shortest_path("dijkstra_1.txt", 1, 4) == (3, [1, 2, 4])