diff --git a/networking_flow/Ford-Fulkerson.py b/networking_flow/Ford-Fulkerson.py deleted file mode 100644 index 0be0fa5f1..000000000 --- a/networking_flow/Ford-Fulkerson.py +++ /dev/null @@ -1,4 +0,0 @@ -# Ford-Fulkerson Algorithm for Maximum Flow Problem -""" -""" - diff --git a/networking_flow/Ford_Fulkerson.py b/networking_flow/Ford_Fulkerson.py new file mode 100644 index 000000000..08749b3da --- /dev/null +++ b/networking_flow/Ford_Fulkerson.py @@ -0,0 +1,56 @@ +# Ford-Fulkerson Algorithm for Maximum Flow Problem +""" +Description: + (1) Start with initial flow as 0; + (2) Choose augmenting path from source to sink and add path to flow; +""" + +def BFS(graph, s, t, parent): + # Return True if there is node that has not iterated. + visited = [False]*len(graph) + queue=[] + queue.append(s) + visited[s] = True + + while queue: + u = queue.pop(0) + for ind in range(len(graph[u])): + if visited[ind] == False and graph[u][ind] > 0: + queue.append(ind) + visited[ind] = True + parent[ind] = u + + return True if visited[t] else False + +def FordFulkerson(graph, source, sink): + # This array is filled by BFS and to store path + parent = [-1]*(len(graph)) + max_flow = 0 + while BFS(graph, source, sink, parent) : + path_flow = float("Inf") + s = sink + + while(s != source): + # Find the minimum value in select path + path_flow = min (path_flow, graph[parent[s]][s]) + s = parent[s] + + max_flow += path_flow + v = sink + + while(v != source): + u = parent[v] + graph[u][v] -= path_flow + graph[v][u] += path_flow + v = parent[v] + return max_flow + +graph = [[0, 16, 13, 0, 0, 0], + [0, 0, 10 ,12, 0, 0], + [0, 4, 0, 0, 14, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0]] + +source, sink = 0, 5 +print(FordFulkerson(graph, source, sink)) \ No newline at end of file diff --git a/networking_flow/Minimum_cut.py b/networking_flow/Minimum_cut.py new file mode 100644 index 000000000..0a61db49b --- /dev/null +++ b/networking_flow/Minimum_cut.py @@ -0,0 +1,59 @@ +# Minimum cut on Ford_Fulkerson algorithm. + +def BFS(graph, s, t, parent): + # Return True if there is node that has not iterated. + visited = [False]*len(graph) + queue=[] + queue.append(s) + visited[s] = True + + while queue: + u = queue.pop(0) + for ind in range(len(graph[u])): + if visited[ind] == False and graph[u][ind] > 0: + queue.append(ind) + visited[ind] = True + parent[ind] = u + + return True if visited[t] else False + +def mincut(graph, source, sink): + # This array is filled by BFS and to store path + parent = [-1]*(len(graph)) + max_flow = 0 + res = [] + temp = [i[:] for i in graph] # Record orignial cut, copy. + while BFS(graph, source, sink, parent) : + path_flow = float("Inf") + s = sink + + while(s != source): + # Find the minimum value in select path + path_flow = min (path_flow, graph[parent[s]][s]) + s = parent[s] + + max_flow += path_flow + v = sink + + while(v != source): + u = parent[v] + graph[u][v] -= path_flow + graph[v][u] += path_flow + v = parent[v] + + for i in range(len(graph)): + for j in range(len(graph[0])): + if graph[i][j] == 0 and temp[i][j] > 0: + res.append((i,j)) + + return res + +graph = [[0, 16, 13, 0, 0, 0], + [0, 0, 10 ,12, 0, 0], + [0, 4, 0, 0, 14, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0]] + +source, sink = 0, 5 +print(mincut(graph, source, sink)) \ No newline at end of file