Beispiel #1
0
 def test_bi_directional_search(self):
     self.graph = Graph()
     node_0 = GraphNode(0)
     node_1 = GraphNode(1)
     node_2 = GraphNode(2)
     node_3 = GraphNode(3)
     node_4 = GraphNode(4)
     node_5 = GraphNode(5)
     node_6 = GraphNode(6)
     node_0.children = [node_1, node_4, node_5]
     node_1.children = [node_3, node_4]
     node_2.children = [node_1, node_6]
     node_3.children = [node_2]
     node_4.children = [node_3]
     self.graph.nodes = [
         node_0, node_1, node_2, node_3, node_4, node_5, node_6
     ]
     self.assertEqual(
         self.graph.bi_directional_search(self.graph.nodes[0],
                                          self.graph.nodes[6]),
         [0, 1, 3, 2, 6], "path 0->6")
     self.assertEqual(
         self.graph.bi_directional_search(self.graph.nodes[0],
                                          self.graph.nodes[2]),
         [0, 1, 3, 2], "path 0->2")
Beispiel #2
0
 def init_graph(self):
     self.graph = Graph()
     node_0 = GraphNode(0)
     node_1 = GraphNode(1)
     node_2 = GraphNode(2)
     node_3 = GraphNode(3)
     node_4 = GraphNode(4)
     node_5 = GraphNode(5)
     node_0.children = [node_1, node_4, node_5]
     node_1.children = [node_3, node_4]
     node_2.children = [node_1]
     node_3.children = [node_2, node_4]
     self.graph.nodes = [node_0, node_1, node_2, node_3, node_4, node_5]
Beispiel #3
0
 def test_shortest_path(self):
     self.graph = Graph()
     node_0 = GraphNode(0)
     node_1 = GraphNode(1)
     node_2 = GraphNode(2)
     node_3 = GraphNode(3)
     node_4 = GraphNode(4)
     node_0.children = [node_1, node_3]
     node_0.child_weights = [2, 1]
     node_1.children = [node_2]
     node_1.child_weights = [2]
     node_2.children = [node_4]
     node_2.child_weights = [4]
     node_3.children = [node_4]
     node_3.child_weights = [1]
     node_4.children = [node_2]
     node_4.child_weights = [1]
     self.graph.nodes = [node_0, node_1, node_2, node_3, node_4]
     self.assertEqual(self.graph.shortest_path(0), [-1, 0, 4, 0, 3])
Beispiel #4
0
 def test_topological_sort(self):
     self.graph = Graph()
     node_undershorts = GraphNode("undershorts")
     node_pants = GraphNode("pants")
     node_belt = GraphNode("belt")
     node_shirt = GraphNode("shirt")
     node_tie = GraphNode("tie")
     node_jacket = GraphNode("jacket")
     node_socks = GraphNode("socks")
     node_shoes = GraphNode("shoes")
     node_watch = GraphNode("watch")
     node_undershorts.children = [node_pants, node_shoes]
     node_pants.children = [node_shoes, node_belt]
     node_belt.children = [node_jacket]
     node_shirt.children = [node_belt, node_tie]
     node_tie.children = [node_jacket]
     node_socks.children = [node_shoes]
     self.graph.nodes = [
         node_undershorts, node_pants, node_belt, node_shirt, node_tie,
         node_jacket, node_socks, node_shoes, node_watch
     ]
     self.assertEqual(self.graph.topological_sort(), [
         'watch', 'socks', 'shirt', 'tie', 'undershorts', 'pants', 'belt',
         'jacket', 'shoes'
     ])
Beispiel #5
0
def dfs(node, visited, sorted_graph):
    # time = time + 1
    # node.start = time
    visited.add(node)
    for child in node.children:
        if child not in visited:
            # traverse the child
            dfs(child, visited, sorted_graph)
        else:
            if child not in sorted_graph:
                raise Exception("Error, there is a loop")
    # time = time + 1
    # node.finish = time
    sorted_graph.insert(0, node)


graph = Graph()
node_a = GraphNode("a")
node_b = GraphNode("b")
node_c = GraphNode("c")
node_d = GraphNode("d")
node_e = GraphNode("e")
node_f = GraphNode("f")
node_a.children = [node_d]
node_b.children = [node_d]
node_f.children = [node_a, node_b]
node_d.children = [node_c]
graph.nodes = [node_a, node_b, node_c, node_d, node_e, node_f]
res = topological_sort(graph)
print(res)