From 597e4acffab612d425c48055e5f75e6ff4475e9c Mon Sep 17 00:00:00 2001 From: Wagner Fontes Date: Thu, 7 Feb 2019 16:11:25 -0300 Subject: [PATCH 1/4] Add Dinic's algorithm --- networking_flow/dinic.py | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 networking_flow/dinic.py diff --git a/networking_flow/dinic.py b/networking_flow/dinic.py new file mode 100644 index 000000000000..3a45b121e644 --- /dev/null +++ b/networking_flow/dinic.py @@ -0,0 +1,49 @@ +# Dinic Algorithm + + +def bfs(matrix_capacity, matrix_flow, s, t): + n = len(matrix_capacity) + queue = [s] + global level + level = n * [0] + level[s] = 1 + while queue: + k = queue.pop(0) + for i in range(n): + if (matrix_flow[k][i] < matrix_capacity[k][i]) and (level[i] == 0): + level[i] = level[k] + 1 + queue.append(i) + return level[t] > 0 + + +def dfs(matrix_capacity, matrix_flow, k, cp): + tmp = cp + if k == len(matrix_capacity) - 1: + return cp + for i in range(len(matrix_capacity)): + if (level[i] == level[k] + 1) and (matrix_flow[k][i] < matrix_capacity[k][i]): + f = dfs(matrix_capacity, matrix_flow, i, min(tmp, matrix_capacity[k][i] - matrix_flow[k][i])) + matrix_flow[k][i] = matrix_flow[k][i] + f + matrix_flow[i][k] = matrix_flow[i][k] - f + tmp = tmp - f + return cp - tmp + + +def max_flow(graph, s, t): + n = len(graph) + matrix_flow = [n * [0] for _ in range(n)] # F is the flow matrix + flow = 0 + while bfs(graph, matrix_flow, s, t): + flow = flow + dfs(graph, matrix_flow, s, 100000) + return flow + + +graph = [[0, 16, 13, 0, 0, 0], + [0, 0, 10, 12, 0, 0], + [0, 4, 0, 0, 14, 0, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0]] + +source, sink = 0, 5 +print(max_flow(graph, source, sink)) From 45dfdc45382b8c595d6b67f209611024e61a1e48 Mon Sep 17 00:00:00 2001 From: Wagner Fontes Date: Fri, 8 Feb 2019 10:14:12 -0300 Subject: [PATCH 2/4] Add Lowest Common Ancestor in a Binary Tree Algorithm --- binary_tree/LCA.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 binary_tree/LCA.py diff --git a/binary_tree/LCA.py b/binary_tree/LCA.py new file mode 100644 index 000000000000..46b3af60ef86 --- /dev/null +++ b/binary_tree/LCA.py @@ -0,0 +1,66 @@ +# Lowest Common Ancestor in a Binary Tree + + +class Node: + + def __init__(self, key): + self.key = key + self.left = None + self.right = None + + +def find_path(root, path, k): + if root is None: + return False + + path.append(root.key) + + if root.key == k: + return True + + if ((root.left is not None and find_path(root.left, path, k)) or + (root.right is not None and find_path(root.right, path, k))): + return True + + path.pop() + return False + + +def LCA(root, n1, n2): + path1 = [] + path2 = [] + + if not find_path(root, path1, n1) or not find_path(root, path2, n2): + return -1 + + i = 0 + while i < len(path1) and i < len(path2): + if path1[i] != path2[i]: + break + i += 1 + return path1[i - 1] + + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) +root.right.left = Node(6) +root.right.right = Node(7) +root.left.left.left = Node(8) +root.left.left.right = Node(9) +root.left.right.left = Node(10) +root.left.right.right = Node(11) +root.right.left.left = Node(12) +root.right.left.right = Node(13) +root.right.right.left = Node(14) +root.right.right.right = Node(15) + +print("LCA(10, 11) = " + str(LCA(root, 10, 11))) + +print("LCA(12, 15) = " + str(LCA(root, 12, 15))) + +print("LCA(7, 13) = " + str(LCA(root, 7, 13))) + +print("LCA(5, 14) = " + str(LCA(root, 5, 14))) From e743fcc165c2ece4ae27c6a0f941e3ae7e7af5a7 Mon Sep 17 00:00:00 2001 From: Wagner Fontes Date: Fri, 8 Feb 2019 10:27:21 -0300 Subject: [PATCH 3/4] Add Lowest Common Ancestor in a Binary Search Tree Algorithm --- binary_tree/LCA_in_binary_search_tree.py | 38 +++++++++++++++++++ binary_tree/{LCA.py => LCA_in_binary_tree.py} | 0 2 files changed, 38 insertions(+) create mode 100644 binary_tree/LCA_in_binary_search_tree.py rename binary_tree/{LCA.py => LCA_in_binary_tree.py} (100%) diff --git a/binary_tree/LCA_in_binary_search_tree.py b/binary_tree/LCA_in_binary_search_tree.py new file mode 100644 index 000000000000..dc1d6d361f53 --- /dev/null +++ b/binary_tree/LCA_in_binary_search_tree.py @@ -0,0 +1,38 @@ +# Lowest Common Ancestor in a Binary Search Tree + + +class Node: + + def __init__(self, key): + self.key = key + self.left = None + self.right = None + + +def LCA(root, n1, n2): + + if root is None: + return None + + if root.key > n1 and root.key > n2: + return LCA(root.left, n1, n2) + + if root.key < n1 and root.key < n2: + return LCA(root.right, n1, n2) + + return root + + +root = Node(20) +root.left = Node(8) +root.right = Node(22) +root.left.left = Node(4) +root.left.right = Node(12) +root.left.right.left = Node(10) +root.left.right.right = Node(14) + +print("LCA(10, 14) = " + str(LCA(root, 10, 14).key)) + +print("LCA(8, 14) = " + str(LCA(root, 8, 14).key)) + +print("LCA(10, 22) = " + str(LCA(root, 10, 22).key)) diff --git a/binary_tree/LCA.py b/binary_tree/LCA_in_binary_tree.py similarity index 100% rename from binary_tree/LCA.py rename to binary_tree/LCA_in_binary_tree.py From 6a952e284f7ed7ff407baa28ce1e1ccd0e377f3b Mon Sep 17 00:00:00 2001 From: Wagner Fontes Date: Fri, 8 Feb 2019 15:16:53 -0300 Subject: [PATCH 4/4] Delete LCA_in_binary_search_tree.py --- binary_tree/LCA_in_binary_search_tree.py | 38 ------------------------ 1 file changed, 38 deletions(-) delete mode 100644 binary_tree/LCA_in_binary_search_tree.py diff --git a/binary_tree/LCA_in_binary_search_tree.py b/binary_tree/LCA_in_binary_search_tree.py deleted file mode 100644 index dc1d6d361f53..000000000000 --- a/binary_tree/LCA_in_binary_search_tree.py +++ /dev/null @@ -1,38 +0,0 @@ -# Lowest Common Ancestor in a Binary Search Tree - - -class Node: - - def __init__(self, key): - self.key = key - self.left = None - self.right = None - - -def LCA(root, n1, n2): - - if root is None: - return None - - if root.key > n1 and root.key > n2: - return LCA(root.left, n1, n2) - - if root.key < n1 and root.key < n2: - return LCA(root.right, n1, n2) - - return root - - -root = Node(20) -root.left = Node(8) -root.right = Node(22) -root.left.left = Node(4) -root.left.right = Node(12) -root.left.right.left = Node(10) -root.left.right.right = Node(14) - -print("LCA(10, 14) = " + str(LCA(root, 10, 14).key)) - -print("LCA(8, 14) = " + str(LCA(root, 8, 14).key)) - -print("LCA(10, 22) = " + str(LCA(root, 10, 22).key))