Exemple #1
0
def src_operations(f_code, v1, v2):
    if f_code == 1:
        res = binary_search.search(v1, v2)
        return res
    elif f_code == 2:
        try:
            res = bmh_search.search(v1, v2)
            return res
        except IndexError:
            res = "Your key either occurs throughout the string, or more than 1 time."
            return res
    elif f_code == 3:
        try:
            res = depth_first_search.dfs(v1, v2)
            return res
        except TypeError:
            res = "Enter all edges for all nodes for the DFS algorithm to work correctly."
            return res
    elif f_code == 4:
        res = kmp_search.search(v1, v2)
        return res
    elif f_code == 5:
        res = rabinkarp_search.search(v1, v2)
        return res
    elif f_code == 6:
        try:
            res = breadth_first_search.bfs(v1, v2)
            return res
        except TypeError:
            res = "Enter all edges for all nodes for the BFS algorithm to work correctly."
            return res
 def test_dfs(self):
     self.graph = {
         'A': ['B', 'C', 'E'],
         'B': ['A', 'D', 'F'],
         'C': ['A', 'G'],
         'D': ['B'],
         'F': ['B'],
         'E': ['A'],
         'G': ['C']
     }
     rv1 = depth_first_search.dfs(self.graph, "A")
     rv2 = depth_first_search.dfs(self.graph, "G")
     rv1e = depth_first_search.dfs(self.graph, "Z")
     self.assertEqual(rv1, ['A', 'B', 'D', 'F', 'C', 'G', 'E'])
     self.assertEqual(rv2, ['G', 'C', 'A', 'B', 'D', 'F', 'E'])
     self.assertEqual(rv1e, None)
     self.graph = {
         1: [2, 3, 4],
         2: [1, 6, 10],
         3: [1, 5, 10],
         4: [1, 10, 11],
         5: [3, 10],
         6: [2, 7, 8, 9],
         7: [6, 8],
         8: [6, 7],
         9: [6, 10],
         10: [3, 5, 9, 12],
         11: [4],
         12: [10]
     }
     rv3 = depth_first_search.dfs(self.graph, 1)
     rv4 = depth_first_search.dfs(self.graph, 5)
     rv5 = depth_first_search.dfs(self.graph, 6)
     rv2e = depth_first_search.dfs(self.graph, 99)
     self.assertEqual(rv3, [1, 2, 6, 7, 8, 9, 10, 3, 5, 12, 4, 11])
     self.assertEqual(rv4, [5, 3, 1, 2, 6, 7, 8, 9, 10, 12, 4, 11])
     self.assertEqual(rv5, [6, 2, 1, 3, 5, 10, 9, 12, 4, 11, 7, 8])
     self.assertEqual(rv2e, None)
     self.graph = {
         1: [2, 3, 4, 5, 6],
         2: [1, 4, 7, 8, 9],
         3: [1, 10],
         4: [1, 2, 11, 12],
         5: [1, 13, 14, 15],
         6: [1, 15],
         7: [2],
         8: [2],
         9: [2, 10],
         10: [3, 9],
         11: [4],
         12: [4],
         13: [5],
         14: [5],
         15: [5, 6]}
     rv6 = depth_first_search.dfs(self.graph, 1)
     rv7 = depth_first_search.dfs(self.graph, 10)
     rv8 = depth_first_search.dfs(self.graph, 5)
     rv3e = depth_first_search.dfs(self.graph, -1)
     self.assertEqual(
         rv6,
         [1, 2, 4, 11, 12, 7, 8, 9, 10, 3, 5, 13, 14, 15, 6]
     )
     self.assertEqual(
         rv7,
         [10, 3, 1, 2, 4, 11, 12, 7, 8, 9, 5, 13, 14, 15, 6]
     )
     self.assertEqual(
         rv8,
         [5, 1, 2, 4, 11, 12, 7, 8, 9, 10, 3, 6, 15, 13, 14]
     )
     self.assertEqual(rv3e, None)
Exemple #3
0
 def test_dfs(self):
     self.graph = {
         'A': ['B', 'C', 'E'],
         'B': ['A', 'D', 'F'],
         'C': ['A', 'G'],
         'D': ['B'],
         'F': ['B'],
         'E': ['A'],
         'G': ['C']
     }
     rv1 = depth_first_search.dfs(self.graph, "A")
     rv2 = depth_first_search.dfs(self.graph, "G")
     rv1e = depth_first_search.dfs(self.graph, "Z")
     self.assertEqual(rv1, ['A', 'B', 'D', 'F', 'C', 'G', 'E'])
     self.assertEqual(rv2, ['G', 'C', 'A', 'B', 'D', 'F', 'E'])
     self.assertEqual(rv1e, None)
     self.graph = {
         1: [2, 3, 4],
         2: [1, 6, 10],
         3: [1, 5, 10],
         4: [1, 10, 11],
         5: [3, 10],
         6: [2, 7, 8, 9],
         7: [6, 8],
         8: [6, 7],
         9: [6, 10],
         10: [3, 5, 9, 12],
         11: [4],
         12: [10]
     }
     rv3 = depth_first_search.dfs(self.graph, 1)
     rv4 = depth_first_search.dfs(self.graph, 5)
     rv5 = depth_first_search.dfs(self.graph, 6)
     rv2e = depth_first_search.dfs(self.graph, 99)
     self.assertEqual(rv3, [1, 2, 6, 7, 8, 9, 10, 3, 5, 12, 4, 11])
     self.assertEqual(rv4, [5, 3, 1, 2, 6, 7, 8, 9, 10, 12, 4, 11])
     self.assertEqual(rv5, [6, 2, 1, 3, 5, 10, 9, 12, 4, 11, 7, 8])
     self.assertEqual(rv2e, None)
     self.graph = {
         1: [2, 3, 4, 5, 6],
         2: [1, 4, 7, 8, 9],
         3: [1, 10],
         4: [1, 2, 11, 12],
         5: [1, 13, 14, 15],
         6: [1, 15],
         7: [2],
         8: [2],
         9: [2, 10],
         10: [3, 9],
         11: [4],
         12: [4],
         13: [5],
         14: [5],
         15: [5, 6]}
     rv6 = depth_first_search.dfs(self.graph, 1)
     rv7 = depth_first_search.dfs(self.graph, 10)
     rv8 = depth_first_search.dfs(self.graph, 5)
     rv3e = depth_first_search.dfs(self.graph, -1)
     self.assertEqual(
         rv6,
         [1, 2, 4, 11, 12, 7, 8, 9, 10, 3, 5, 13, 14, 15, 6]
     )
     self.assertEqual(
         rv7,
         [10, 3, 1, 2, 4, 11, 12, 7, 8, 9, 5, 13, 14, 15, 6]
     )
     self.assertEqual(
         rv8,
         [5, 1, 2, 4, 11, 12, 7, 8, 9, 10, 3, 6, 15, 13, 14]
     )
     self.assertEqual(rv3e, None)