Skip to content

Instantly share code, notes, and snippets.

@radzionc
Last active April 8, 2019 03:11
Show Gist options
  • Save radzionc/f1afa0d5307335e212a3b1287feb1a02 to your computer and use it in GitHub Desktop.
Save radzionc/f1afa0d5307335e212a3b1287feb1a02 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"def get_loop(bv_positions, ev_position):\n",
" def inner(loop):\n",
" if len(loop) > 3:\n",
" can_be_closed = len(get_possible_next_nodes(loop, [ev_position])) == 1\n",
" if can_be_closed: return loop\n",
" \n",
" not_visited = list(set(bv_positions) - set(loop))\n",
" possible_next_nodes = get_possible_next_nodes(loop, not_visited)\n",
" for next_node in possible_next_nodes:\n",
" new_loop = inner(loop + [next_node])\n",
" if new_loop: return new_loop\n",
" \n",
" return inner([ev_position])"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(2, 0), (2, 2), (1, 2), (1, 0)]"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"costs = [\n",
" [ 2, 2, 2, 1],\n",
" [10, 8, 5, 4],\n",
" [ 7, 6, 6, 8]\n",
"]\n",
"supply = [30, 70, 50]\n",
"demand = [40, 30, 40, 40]\n",
"bfs = north_west_corner(supply, demand)\n",
"us, vs = get_us_and_vs(bfs, costs)\n",
"ws = get_ws(bfs, costs, us, vs)\n",
"can_be_improved(ws)\n",
"\n",
"ev_position = get_entering_variable_position(ws)\n",
"get_loop([p for p, v in bfs], ev_position)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment