Last active
April 8, 2019 17:14
-
-
Save radzionc/6a1b88c8c0da1c2a61bc0696d0ad9714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def north_west_corner(supply, demand):\n", | |
" supply_copy = supply.copy()\n", | |
" demand_copy = demand.copy()\n", | |
" i = 0\n", | |
" j = 0\n", | |
" bfs = []\n", | |
" while len(bfs) < len(supply) + len(demand) - 1:\n", | |
" s = supply_copy[i]\n", | |
" d = demand_copy[j]\n", | |
" v = min(s, d)\n", | |
" supply_copy[i] -= v\n", | |
" demand_copy[j] -= v\n", | |
" bfs.append(((i, j), v))\n", | |
" if supply_copy[i] == 0 and i < len(supply) - 1:\n", | |
" i += 1\n", | |
" elif demand_copy[j] == 0 and j < len(demand) - 1:\n", | |
" j += 1\n", | |
" return bfs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[((0, 0), 30), ((1, 0), 10), ((1, 1), 30), ((1, 2), 30), ((2, 2), 10), ((2, 3), 40)]\n" | |
] | |
} | |
], | |
"source": [ | |
"supply = [30, 70, 50]\n", | |
"demand = [40, 30, 40, 40]\n", | |
"bfs = north_west_corner(supply, demand)\n", | |
"print(bfs)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment