|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "d8c341f4-72ec-41d9-8094-82d124d0d245", |
| 7 | + "metadata": { |
| 8 | + "scrolled": true |
| 9 | + }, |
| 10 | + "outputs": [], |
| 11 | + "source": [ |
| 12 | + "exec(open(\"./funcs/mmj_cal_functions.py\").read())\n", |
| 13 | + "exec(open(\"./funcs/Prim_Minimum_Spanning_Tree.py\").read())\n", |
| 14 | + " " |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 2, |
| 20 | + "id": "cc47a24e", |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "# wides_path_exam_graph, widest path problem example graph, \n", |
| 25 | + "# see the upper right graph on https://en.wikipedia.org/wiki/Widest_path_problem\n", |
| 26 | + "\n", |
| 27 | + "pairwise_bandwidth_matrix = pickle.load( open( \"./data/wides_path_exam_graph.p\", \"rb\" )) \n", |
| 28 | + " " |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 3, |
| 34 | + "id": "0dc0c24a", |
| 35 | + "metadata": { |
| 36 | + "scrolled": true |
| 37 | + }, |
| 38 | + "outputs": [ |
| 39 | + { |
| 40 | + "data": { |
| 41 | + "text/plain": [ |
| 42 | + "array([[inf, 15., 53., 0., 0., 0., 0.],\n", |
| 43 | + " [15., inf, 40., 0., 0., 46., 0.],\n", |
| 44 | + " [53., 40., inf, 17., 31., 0., 0.],\n", |
| 45 | + " [ 0., 0., 17., inf, 29., 0., 40.],\n", |
| 46 | + " [ 0., 0., 31., 29., inf, 3., 8.],\n", |
| 47 | + " [ 0., 46., 0., 0., 3., inf, 11.],\n", |
| 48 | + " [ 0., 0., 0., 40., 8., 11., inf]])" |
| 49 | + ] |
| 50 | + }, |
| 51 | + "execution_count": 3, |
| 52 | + "metadata": {}, |
| 53 | + "output_type": "execute_result" |
| 54 | + } |
| 55 | + ], |
| 56 | + "source": [ |
| 57 | + "pairwise_bandwidth_matrix" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": 4, |
| 63 | + "id": "31beb80a", |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "graph_nodes_names = {'Dunwich':0, 'Blaxhall':1, 'Harwich':2, 'Clacton':3, 'Tiptree':4, 'Feering':5, 'Maldon':6}\n", |
| 68 | + "\n" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "id": "a630373e", |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 5, |
| 82 | + "id": "74239dd0", |
| 83 | + "metadata": { |
| 84 | + "scrolled": false |
| 85 | + }, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "data": { |
| 89 | + "text/plain": [ |
| 90 | + "array([[ 0., 40., 53., 29., 31., 40., 29.],\n", |
| 91 | + " [40., 0., 40., 29., 31., 46., 29.],\n", |
| 92 | + " [53., 40., 0., 29., 31., 40., 29.],\n", |
| 93 | + " [29., 29., 29., 0., 29., 29., 40.],\n", |
| 94 | + " [31., 31., 31., 29., 0., 31., 29.],\n", |
| 95 | + " [40., 46., 40., 29., 31., 0., 29.],\n", |
| 96 | + " [29., 29., 29., 40., 29., 29., 0.]])" |
| 97 | + ] |
| 98 | + }, |
| 99 | + "execution_count": 5, |
| 100 | + "metadata": {}, |
| 101 | + "output_type": "execute_result" |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "Widest_path_matrix_algo_4 = cal_Widest_path_problem_matrix_by_algo_4(pairwise_bandwidth_matrix)\n", |
| 106 | + "Widest_path_matrix_algo_4\n", |
| 107 | + "\n" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": 6, |
| 113 | + "id": "1567b10d", |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [ |
| 116 | + { |
| 117 | + "data": { |
| 118 | + "text/plain": [ |
| 119 | + "29.0" |
| 120 | + ] |
| 121 | + }, |
| 122 | + "execution_count": 6, |
| 123 | + "metadata": {}, |
| 124 | + "output_type": "execute_result" |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "i = graph_nodes_names[\"Maldon\"]\n", |
| 129 | + "j = graph_nodes_names[\"Feering\"]\n", |
| 130 | + "\n", |
| 131 | + "Widest_path_matrix_algo_4[i,j]\n" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 7, |
| 137 | + "id": "fd13e1fe", |
| 138 | + "metadata": {}, |
| 139 | + "outputs": [], |
| 140 | + "source": [ |
| 141 | + "N = len(pairwise_bandwidth_matrix)\n", |
| 142 | + "for i in range(N):\n", |
| 143 | + " Widest_path_matrix_algo_4[i,i] = np.inf" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": 8, |
| 149 | + "id": "f405f107", |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [ |
| 152 | + { |
| 153 | + "data": { |
| 154 | + "text/plain": [ |
| 155 | + "array([[inf, 40., 53., 29., 31., 40., 29.],\n", |
| 156 | + " [40., inf, 40., 29., 31., 46., 29.],\n", |
| 157 | + " [53., 40., inf, 29., 31., 40., 29.],\n", |
| 158 | + " [29., 29., 29., inf, 29., 29., 40.],\n", |
| 159 | + " [31., 31., 31., 29., inf, 31., 29.],\n", |
| 160 | + " [40., 46., 40., 29., 31., inf, 29.],\n", |
| 161 | + " [29., 29., 29., 40., 29., 29., inf]])" |
| 162 | + ] |
| 163 | + }, |
| 164 | + "execution_count": 8, |
| 165 | + "metadata": {}, |
| 166 | + "output_type": "execute_result" |
| 167 | + } |
| 168 | + ], |
| 169 | + "source": [ |
| 170 | + "Widest_path_matrix_algo_4" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": 9, |
| 176 | + "id": "c1f29450", |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "# See Section 7 (SOLVING THE WIDEST PATH PROBLEM) of the paper:\n", |
| 181 | + "# https://arxiv.org/abs/2301.05994\n", |
| 182 | + "\n", |
| 183 | + "def wpp_n_to_r(distance_matrix, widest_path_matrix, n, r):\n", |
| 184 | + " max_jump_list = []\n", |
| 185 | + " for ttt in range(n):\n", |
| 186 | + " m_jump = np.min((distance_matrix[n,ttt],widest_path_matrix[ttt,r]))\n", |
| 187 | + " max_jump_list.append(m_jump)\n", |
| 188 | + " return np.max(max_jump_list)\n", |
| 189 | + "\n", |
| 190 | + "def wpp_r_to_n(distance_matrix, widest_path_matrix, n, r):\n", |
| 191 | + " max_jump_list = []\n", |
| 192 | + " for ttt in range(n):\n", |
| 193 | + " m_jump = np.min((widest_path_matrix[r,ttt], distance_matrix[ttt, n]))\n", |
| 194 | + " max_jump_list.append(m_jump)\n", |
| 195 | + " return np.max(max_jump_list)\n", |
| 196 | + "\n", |
| 197 | + " \n", |
| 198 | + "def cal_n_wpp(distance_matrix, widest_path_matrix, n):\n", |
| 199 | + " for i in range(n):\n", |
| 200 | + " widest_path_matrix[n,i] = wpp_n_to_r(distance_matrix, widest_path_matrix, n, i)\n", |
| 201 | + " widest_path_matrix[i,n] = wpp_r_to_n(distance_matrix, widest_path_matrix, n, i)\n", |
| 202 | + " \n", |
| 203 | + " for i in range(n): \n", |
| 204 | + " for j in range(n):\n", |
| 205 | + " if i < j:\n", |
| 206 | + " widest_path_matrix[i,j] = update_wpp_ij(distance_matrix, widest_path_matrix, n, i, j)\n", |
| 207 | + " widest_path_matrix[j,i] = update_wpp_ij(distance_matrix, widest_path_matrix, n, j, i)\n", |
| 208 | + " \n", |
| 209 | + "def update_wpp_ij(distance_matrix, widest_path_matrix, n, i,j):\n", |
| 210 | + " m1 = widest_path_matrix[i,j]\n", |
| 211 | + " m2 = np.min((widest_path_matrix[i,n],widest_path_matrix[n,j]))\n", |
| 212 | + " return np.max((m1,m2))\n", |
| 213 | + "\n", |
| 214 | + "def cal_widest_path_matrix_algo_1_python(distance_matrix):\n", |
| 215 | + " \n", |
| 216 | + " N = len(distance_matrix)\n", |
| 217 | + " \n", |
| 218 | + " widest_path_matrix = np.ones((N,N))*np.inf\n", |
| 219 | + "\n", |
| 220 | + " widest_path_matrix[0,1] = distance_matrix[0,1]\n", |
| 221 | + " widest_path_matrix[1,0] = distance_matrix[1,0]\n", |
| 222 | + " \n", |
| 223 | + " for kk in range(2, N):\n", |
| 224 | + " cal_n_wpp(distance_matrix, widest_path_matrix, kk)\n", |
| 225 | + " return widest_path_matrix" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": 10, |
| 231 | + "id": "2fdb1448", |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [ |
| 234 | + { |
| 235 | + "data": { |
| 236 | + "text/plain": [ |
| 237 | + "array([[inf, 40., 53., 29., 31., 40., 29.],\n", |
| 238 | + " [40., inf, 40., 29., 31., 46., 29.],\n", |
| 239 | + " [53., 40., inf, 29., 31., 40., 29.],\n", |
| 240 | + " [29., 29., 29., inf, 29., 29., 40.],\n", |
| 241 | + " [31., 31., 31., 29., inf, 31., 29.],\n", |
| 242 | + " [40., 46., 40., 29., 31., inf, 29.],\n", |
| 243 | + " [29., 29., 29., 40., 29., 29., inf]])" |
| 244 | + ] |
| 245 | + }, |
| 246 | + "execution_count": 10, |
| 247 | + "metadata": {}, |
| 248 | + "output_type": "execute_result" |
| 249 | + } |
| 250 | + ], |
| 251 | + "source": [ |
| 252 | + "Widest_path_matrix_algo_1 = cal_widest_path_matrix_algo_1_python(pairwise_bandwidth_matrix)\n", |
| 253 | + "Widest_path_matrix_algo_1" |
| 254 | + ] |
| 255 | + }, |
| 256 | + { |
| 257 | + "cell_type": "code", |
| 258 | + "execution_count": 11, |
| 259 | + "id": "5cd76527", |
| 260 | + "metadata": {}, |
| 261 | + "outputs": [ |
| 262 | + { |
| 263 | + "data": { |
| 264 | + "text/plain": [ |
| 265 | + "29.0" |
| 266 | + ] |
| 267 | + }, |
| 268 | + "execution_count": 11, |
| 269 | + "metadata": {}, |
| 270 | + "output_type": "execute_result" |
| 271 | + } |
| 272 | + ], |
| 273 | + "source": [ |
| 274 | + "i = graph_nodes_names[\"Maldon\"]\n", |
| 275 | + "j = graph_nodes_names[\"Feering\"]\n", |
| 276 | + "\n", |
| 277 | + "Widest_path_matrix_algo_1[i,j]" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": 12, |
| 283 | + "id": "658d4389", |
| 284 | + "metadata": {}, |
| 285 | + "outputs": [ |
| 286 | + { |
| 287 | + "name": "stdout", |
| 288 | + "output_type": "stream", |
| 289 | + "text": [ |
| 290 | + "True\n" |
| 291 | + ] |
| 292 | + } |
| 293 | + ], |
| 294 | + "source": [ |
| 295 | + "print(np.allclose(Widest_path_matrix_algo_1, Widest_path_matrix_algo_4))\n" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "cell_type": "code", |
| 300 | + "execution_count": null, |
| 301 | + "id": "8e0b76a7", |
| 302 | + "metadata": {}, |
| 303 | + "outputs": [], |
| 304 | + "source": [] |
| 305 | + } |
| 306 | + ], |
| 307 | + "metadata": { |
| 308 | + "kernelspec": { |
| 309 | + "display_name": "Python 3 (ipykernel)", |
| 310 | + "language": "python", |
| 311 | + "name": "python3" |
| 312 | + }, |
| 313 | + "language_info": { |
| 314 | + "codemirror_mode": { |
| 315 | + "name": "ipython", |
| 316 | + "version": 3 |
| 317 | + }, |
| 318 | + "file_extension": ".py", |
| 319 | + "mimetype": "text/x-python", |
| 320 | + "name": "python", |
| 321 | + "nbconvert_exporter": "python", |
| 322 | + "pygments_lexer": "ipython3", |
| 323 | + "version": "3.12.3" |
| 324 | + } |
| 325 | + }, |
| 326 | + "nbformat": 4, |
| 327 | + "nbformat_minor": 5 |
| 328 | +} |
0 commit comments