{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Copyright 2010-2018 Google LLC\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "# [START program]\n", "\"\"\"From Taha 'Introduction to Operations Research', example 6.4-2.\"\"\"\n", "# [START import]\n", "from __future__ import print_function\n", "from ortools.graph import pywrapgraph\n", "# [END import]\n", "\n", "\n", "\"\"\"MaxFlow simple interface example.\"\"\"\n", "\n", "# [START data]\n", "# Define three parallel arrays: start_nodes, end_nodes, and the capacities\n", "# between each pair. For instance, the arc from node 0 to node 1 has a\n", "# capacity of 20.\n", "\n", "start_nodes = [0, 0, 0, 1, 1, 2, 2, 3, 3]\n", "end_nodes = [1, 2, 3, 2, 4, 3, 4, 2, 4]\n", "capacities = [20, 30, 10, 40, 30, 10, 20, 5, 20]\n", "# [END data]\n", "\n", "# Instantiate a SimpleMaxFlow solver.\n", "# [START constraints]\n", "max_flow = pywrapgraph.SimpleMaxFlow()\n", "# Add each arc.\n", "for arc in zip(start_nodes, end_nodes, capacities):\n", " max_flow.AddArcWithCapacity(arc[0], arc[1], arc[2])\n", "# [END constraints]\n", "\n", "# [START solve]\n", "# Find the maximum flow between node 0 and node 4.\n", "if max_flow.Solve(0, 4) == max_flow.OPTIMAL:\n", " print('Max flow:', max_flow.OptimalFlow())\n", " print('')\n", " print(' Arc Flow / Capacity')\n", " for i in range(max_flow.NumArcs()):\n", " print('%1s -> %1s %3s / %3s' %\n", " (max_flow.Tail(i), max_flow.Head(i), max_flow.Flow(i),\n", " max_flow.Capacity(i)))\n", " print('Source side min-cut:', max_flow.GetSourceSideMinCut())\n", " print('Sink side min-cut:', max_flow.GetSinkSideMinCut())\n", "else:\n", " print('There was an issue with the max flow input.')\n", "# [END solve]\n", "\n" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 4 }