1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
|
"""
Routines for performing shortest-path graph searches
The main interface is in the function :func:`shortest_path`. This
calls cython routines that compute the shortest path using
the Floyd-Warshall algorithm, Dijkstra's algorithm with Fibonacci Heaps,
the Bellman-Ford algorithm, or Johnson's Algorithm.
"""
# Author: Jake Vanderplas -- <vanderplas@astro.washington.edu>
# License: BSD, (C) 2011
from __future__ import absolute_import
import warnings
import numpy as np
cimport numpy as np
from scipy.sparse import csr_matrix, isspmatrix, isspmatrix_csr, isspmatrix_csc
from scipy.sparse.csgraph._validation import validate_graph
cimport cython
from libc.stdlib cimport malloc, free
from numpy.math cimport INFINITY
include 'parameters.pxi'
class NegativeCycleError(Exception):
def __init__(self, message=''):
Exception.__init__(self, message)
def shortest_path(csgraph, method='auto',
directed=True,
return_predecessors=False,
unweighted=False,
overwrite=False,
indices=None):
"""
shortest_path(csgraph, method='auto', directed=True, return_predecessors=False,
unweighted=False, overwrite=False, indices=None)
Perform a shortest-path graph search on a positive directed or
undirected graph.
.. versionadded:: 0.11.0
Parameters
----------
csgraph : array, matrix, or sparse matrix, 2 dimensions
The N x N array of distances representing the input graph.
method : string ['auto'|'FW'|'D'], optional
Algorithm to use for shortest paths. Options are:
'auto' -- (default) select the best among 'FW', 'D', 'BF', or 'J'
based on the input data.
'FW' -- Floyd-Warshall algorithm. Computational cost is
approximately ``O[N^3]``. The input csgraph will be
converted to a dense representation.
'D' -- Dijkstra's algorithm with Fibonacci heaps. Computational
cost is approximately ``O[N(N*k + N*log(N))]``, where
``k`` is the average number of connected edges per node.
The input csgraph will be converted to a csr
representation.
'BF' -- Bellman-Ford algorithm. This algorithm can be used when
weights are negative. If a negative cycle is encountered,
an error will be raised. Computational cost is
approximately ``O[N(N^2 k)]``, where ``k`` is the average
number of connected edges per node. The input csgraph will
be converted to a csr representation.
'J' -- Johnson's algorithm. Like the Bellman-Ford algorithm,
Johnson's algorithm is designed for use when the weights
are negative. It combines the Bellman-Ford algorithm
with Dijkstra's algorithm for faster computation.
directed : bool, optional
If True (default), then find the shortest path on a directed graph:
only move from point i to point j along paths csgraph[i, j].
If False, then find the shortest path on an undirected graph: the
algorithm can progress from point i to j along csgraph[i, j] or
csgraph[j, i]
return_predecessors : bool, optional
If True, return the size (N, N) predecesor matrix
unweighted : bool, optional
If True, then find unweighted distances. That is, rather than finding
the path between each point such that the sum of weights is minimized,
find the path such that the number of edges is minimized.
overwrite : bool, optional
If True, overwrite csgraph with the result. This applies only if
method == 'FW' and csgraph is a dense, c-ordered array with
dtype=float64.
indices : array_like or int, optional
If specified, only compute the paths for the points at the given
indices. Incompatible with method == 'FW'.
Returns
-------
dist_matrix : ndarray
The N x N matrix of distances between graph nodes. dist_matrix[i,j]
gives the shortest distance from point i to point j along the graph.
predecessors : ndarray
Returned only if return_predecessors == True.
The N x N matrix of predecessors, which can be used to reconstruct
the shortest paths. Row i of the predecessor matrix contains
information on the shortest paths from point i: each entry
predecessors[i, j] gives the index of the previous node in the
path from point i to point j. If no path exists between point
i and j, then predecessors[i, j] = -9999
Raises
------
NegativeCycleError:
if there are negative cycles in the graph
Notes
-----
As currently implemented, Dijkstra's algorithm and Johnson's algorithm
do not work for graphs with direction-dependent distances when
directed == False. i.e., if csgraph[i,j] and csgraph[j,i] are non-equal
edges, method='D' may yield an incorrect result.
"""
# validate here to catch errors early but don't store the result;
# we'll validate again later
validate_graph(csgraph, directed, DTYPE,
copy_if_dense=(not overwrite),
copy_if_sparse=(not overwrite))
cdef bint issparse
cdef ssize_t N # XXX cdef ssize_t Nk fails in Python 3 (?)
if method == 'auto':
# guess fastest method based on number of nodes and edges
N = csgraph.shape[0]
issparse = isspmatrix(csgraph)
if issparse:
Nk = csgraph.nnz
else:
Nk = np.sum(csgraph > 0)
if indices is not None or Nk < N * N / 4:
if ((issparse and np.any(csgraph.data < 0))
or (not issparse and np.any(csgraph < 0))):
method = 'J'
else:
method = 'D'
else:
method = 'FW'
if method == 'FW':
if indices is not None:
raise ValueError("Cannot specify indices with method == 'FW'.")
return floyd_warshall(csgraph, directed,
return_predecessors=return_predecessors,
unweighted=unweighted,
overwrite=overwrite)
elif method == 'D':
return dijkstra(csgraph, directed,
return_predecessors=return_predecessors,
unweighted=unweighted, indices=indices)
elif method == 'BF':
return bellman_ford(csgraph, directed,
return_predecessors=return_predecessors,
unweighted=unweighted, indices=indices)
elif method == 'J':
return johnson(csgraph, directed,
return_predecessors=return_predecessors,
unweighted=unweighted, indices=indices)
else:
raise ValueError("unrecognized method '%s'" % method)
def floyd_warshall(csgraph, directed=True,
return_predecessors=False,
unweighted=False,
overwrite=False):
"""
floyd_warshall(csgraph, directed=True, return_predecessors=False,
unweighted=False, overwrite=False)
Compute the shortest path lengths using the Floyd-Warshall algorithm
.. versionadded:: 0.11.0
Parameters
----------
csgraph : array, matrix, or sparse matrix, 2 dimensions
The N x N array of distances representing the input graph.
directed : bool, optional
If True (default), then find the shortest path on a directed graph:
only move from point i to point j along paths csgraph[i, j].
If False, then find the shortest path on an undirected graph: the
algorithm can progress from point i to j along csgraph[i, j] or
csgraph[j, i]
return_predecessors : bool, optional
If True, return the size (N, N) predecesor matrix
unweighted : bool, optional
If True, then find unweighted distances. That is, rather than finding
the path between each point such that the sum of weights is minimized,
find the path such that the number of edges is minimized.
overwrite : bool, optional
If True, overwrite csgraph with the result. This applies only if
csgraph is a dense, c-ordered array with dtype=float64.
Returns
-------
dist_matrix : ndarray
The N x N matrix of distances between graph nodes. dist_matrix[i,j]
gives the shortest distance from point i to point j along the graph.
predecessors : ndarray
Returned only if return_predecessors == True.
The N x N matrix of predecessors, which can be used to reconstruct
the shortest paths. Row i of the predecessor matrix contains
information on the shortest paths from point i: each entry
predecessors[i, j] gives the index of the previous node in the
path from point i to point j. If no path exists between point
i and j, then predecessors[i, j] = -9999
Raises
------
NegativeCycleError:
if there are negative cycles in the graph
"""
dist_matrix = validate_graph(csgraph, directed, DTYPE,
csr_output=False,
copy_if_dense=not overwrite)
if unweighted:
dist_matrix[~np.isinf(dist_matrix)] = 1
if return_predecessors:
predecessor_matrix = np.empty(dist_matrix.shape,
dtype=ITYPE, order='C')
else:
predecessor_matrix = np.empty((0, 0), dtype=ITYPE)
_floyd_warshall(dist_matrix,
predecessor_matrix,
int(directed))
if np.any(dist_matrix.diagonal() < 0):
raise NegativeCycleError("Negative cycle in nodes %s"
% np.where(dist_matrix.diagonal() < 0)[0])
if return_predecessors:
return dist_matrix, predecessor_matrix
else:
return dist_matrix
@cython.boundscheck(False)
cdef void _floyd_warshall(
np.ndarray[DTYPE_t, ndim=2, mode='c'] dist_matrix,
np.ndarray[ITYPE_t, ndim=2, mode='c'] predecessor_matrix,
int directed=0):
# dist_matrix : in/out
# on input, the graph
# on output, the matrix of shortest paths
# dist_matrix should be a [N,N] matrix, such that dist_matrix[i, j]
# is the distance from point i to point j. Zero-distances imply that
# the points are not connected.
cdef int N = dist_matrix.shape[0]
assert dist_matrix.shape[1] == N
cdef unsigned int i, j, k
cdef DTYPE_t d_ijk
#----------------------------------------------------------------------
# Initialize distance matrix
# - set non-edges to infinity
# - set diagonal to zero
# - symmetrize matrix if non-directed graph is desired
dist_matrix[dist_matrix == 0] = INFINITY
dist_matrix.flat[::N + 1] = 0
if not directed:
for i in range(N):
for j in range(i + 1, N):
if dist_matrix[j, i] <= dist_matrix[i, j]:
dist_matrix[i, j] = dist_matrix[j, i]
else:
dist_matrix[j, i] = dist_matrix[i, j]
#----------------------------------------------------------------------
# Initialize predecessor matrix
# - check matrix size
# - initialize diagonal and all non-edges to NULL
# - initialize all edges to the row index
cdef int store_predecessors = False
if predecessor_matrix.size > 0:
store_predecessors = True
assert predecessor_matrix.shape[0] == N
assert predecessor_matrix.shape[1] == N
predecessor_matrix.fill(NULL_IDX)
i_edge = np.where(~np.isinf(dist_matrix))
predecessor_matrix[i_edge] = i_edge[0]
predecessor_matrix.flat[::N + 1] = NULL_IDX
# Now perform the Floyd-Warshall algorithm.
# In each loop, this finds the shortest path from point i
# to point j using intermediate nodes 0 ... k
if store_predecessors:
for k in range(N):
for i in range(N):
if dist_matrix[i, k] == INFINITY:
continue
for j in range(N):
d_ijk = dist_matrix[i, k] + dist_matrix[k, j]
if d_ijk < dist_matrix[i, j]:
dist_matrix[i, j] = d_ijk
predecessor_matrix[i, j] = predecessor_matrix[k, j]
else:
for k in range(N):
for i in range(N):
if dist_matrix[i, k] == INFINITY:
continue
for j in range(N):
d_ijk = dist_matrix[i, k] + dist_matrix[k, j]
if d_ijk < dist_matrix[i, j]:
dist_matrix[i, j] = d_ijk
def dijkstra(csgraph, directed=True, indices=None,
return_predecessors=False,
unweighted=False, limit=np.inf):
"""
dijkstra(csgraph, directed=True, indices=None, return_predecessors=False,
unweighted=False, limit=np.inf)
Dijkstra algorithm using Fibonacci Heaps
.. versionadded:: 0.11.0
Parameters
----------
csgraph : array, matrix, or sparse matrix, 2 dimensions
The N x N array of non-negative distances representing the input graph.
directed : bool, optional
If True (default), then find the shortest path on a directed graph:
only move from point i to point j along paths csgraph[i, j].
If False, then find the shortest path on an undirected graph: the
algorithm can progress from point i to j along csgraph[i, j] or
csgraph[j, i]
indices : array_like or int, optional
if specified, only compute the paths for the points at the given
indices.
return_predecessors : bool, optional
If True, return the size (N, N) predecesor matrix
unweighted : bool, optional
If True, then find unweighted distances. That is, rather than finding
the path between each point such that the sum of weights is minimized,
find the path such that the number of edges is minimized.
limit : float, optional
The maximum distance to calculate, must be >= 0. Using a smaller limit
will decrease computation time by aborting calculations between pairs
that are separated by a distance > limit. For such pairs, the distance
will be equal to np.inf (i.e., not connected).
.. versionadded:: 0.14.0
Returns
-------
dist_matrix : ndarray
The matrix of distances between graph nodes. dist_matrix[i,j]
gives the shortest distance from point i to point j along the graph.
predecessors : ndarray
Returned only if return_predecessors == True.
The matrix of predecessors, which can be used to reconstruct
the shortest paths. Row i of the predecessor matrix contains
information on the shortest paths from point i: each entry
predecessors[i, j] gives the index of the previous node in the
path from point i to point j. If no path exists between point
i and j, then predecessors[i, j] = -9999
Notes
-----
As currently implemented, Dijkstra's algorithm does not work for
graphs with direction-dependent distances when directed == False.
i.e., if csgraph[i,j] and csgraph[j,i] are not equal and
both are nonzero, setting directed=False will not yield the correct
result.
Also, this routine does not work for graphs with negative
distances. Negative distances can lead to infinite cycles that must
be handled by specialized algorithms such as Bellman-Ford's algorithm
or Johnson's algorithm.
"""
#------------------------------
# validate csgraph and convert to csr matrix
csgraph = validate_graph(csgraph, directed, DTYPE,
dense_output=False)
if np.any(csgraph.data < 0):
warnings.warn("Graph has negative weights: dijkstra will give "
"inaccurate results if the graph contains negative "
"cycles. Consider johnson or bellman_ford.")
N = csgraph.shape[0]
#------------------------------
# intitialize/validate indices
if indices is None:
indices = np.arange(N, dtype=ITYPE)
return_shape = indices.shape + (N,)
else:
indices = np.array(indices, order='C', dtype=ITYPE, copy=True)
return_shape = indices.shape + (N,)
indices = np.atleast_1d(indices).reshape(-1)
indices[indices < 0] += N
if np.any(indices < 0) or np.any(indices >= N):
raise ValueError("indices out of range 0...N")
cdef DTYPE_t limitf = limit
if limitf < 0:
raise ValueError('limit must be >= 0')
#------------------------------
# initialize dist_matrix for output
dist_matrix = np.zeros((len(indices), N), dtype=DTYPE)
dist_matrix.fill(np.inf)
dist_matrix[np.arange(len(indices)), indices] = 0
#------------------------------
# initialize predecessors for output
if return_predecessors:
predecessor_matrix = np.empty((len(indices), N), dtype=ITYPE)
predecessor_matrix.fill(NULL_IDX)
else:
predecessor_matrix = np.empty((0, N), dtype=ITYPE)
if unweighted:
csr_data = np.ones(csgraph.data.shape)
else:
csr_data = csgraph.data
if directed:
_dijkstra_directed(indices,
csr_data, csgraph.indices, csgraph.indptr,
dist_matrix, predecessor_matrix, limitf)
else:
csgraphT = csgraph.T.tocsr()
if unweighted:
csrT_data = csr_data
else:
csrT_data = csgraphT.data
_dijkstra_undirected(indices,
csr_data, csgraph.indices, csgraph.indptr,
csrT_data, csgraphT.indices, csgraphT.indptr,
dist_matrix, predecessor_matrix, limitf)
if return_predecessors:
return (dist_matrix.reshape(return_shape),
predecessor_matrix.reshape(return_shape))
else:
return dist_matrix.reshape(return_shape)
cdef _dijkstra_directed(
np.ndarray[ITYPE_t, ndim=1, mode='c'] source_indices,
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=2, mode='c'] dist_matrix,
np.ndarray[ITYPE_t, ndim=2, mode='c'] pred,
DTYPE_t limit):
cdef unsigned int Nind = dist_matrix.shape[0]
cdef unsigned int N = dist_matrix.shape[1]
cdef unsigned int i, k, j_source, j_current
cdef ITYPE_t j
cdef DTYPE_t next_val
cdef int return_pred = (pred.size > 0)
cdef FibonacciHeap heap
cdef FibonacciNode *v
cdef FibonacciNode *current_node
cdef FibonacciNode* nodes = <FibonacciNode*> malloc(N *
sizeof(FibonacciNode))
for i in range(Nind):
j_source = source_indices[i]
for k in range(N):
initialize_node(&nodes[k], k)
dist_matrix[i, j_source] = 0
heap.min_node = NULL
insert_node(&heap, &nodes[j_source])
while heap.min_node:
v = remove_min(&heap)
v.state = SCANNED
for j in range(csr_indptr[v.index], csr_indptr[v.index + 1]):
j_current = csr_indices[j]
current_node = &nodes[j_current]
if current_node.state != SCANNED:
next_val = v.val + csr_weights[j]
if next_val <= limit:
if current_node.state == NOT_IN_HEAP:
current_node.state = IN_HEAP
current_node.val = next_val
insert_node(&heap, current_node)
if return_pred:
pred[i, j_current] = v.index
elif current_node.val > next_val:
decrease_val(&heap, current_node,
next_val)
if return_pred:
pred[i, j_current] = v.index
#v has now been scanned: add the distance to the results
dist_matrix[i, v.index] = v.val
free(nodes)
cdef _dijkstra_undirected(
np.ndarray[ITYPE_t, ndim=1, mode='c'] source_indices,
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=1, mode='c'] csrT_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csrT_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csrT_indptr,
np.ndarray[DTYPE_t, ndim=2, mode='c'] dist_matrix,
np.ndarray[ITYPE_t, ndim=2, mode='c'] pred,
DTYPE_t limit):
cdef unsigned int Nind = dist_matrix.shape[0]
cdef unsigned int N = dist_matrix.shape[1]
cdef unsigned int i, k, j_source, j_current
cdef ITYPE_t j
cdef DTYPE_t next_val
cdef int return_pred = (pred.size > 0)
cdef FibonacciHeap heap
cdef FibonacciNode *v
cdef FibonacciNode *current_node
cdef FibonacciNode* nodes = <FibonacciNode*> malloc(N *
sizeof(FibonacciNode))
for i in range(Nind):
j_source = source_indices[i]
for k in range(N):
initialize_node(&nodes[k], k)
dist_matrix[i, j_source] = 0
heap.min_node = NULL
insert_node(&heap, &nodes[j_source])
while heap.min_node:
v = remove_min(&heap)
v.state = SCANNED
for j in range(csr_indptr[v.index], csr_indptr[v.index + 1]):
j_current = csr_indices[j]
current_node = &nodes[j_current]
if current_node.state != SCANNED:
next_val = v.val + csr_weights[j]
if next_val <= limit:
if current_node.state == NOT_IN_HEAP:
current_node.state = IN_HEAP
current_node.val = next_val
insert_node(&heap, current_node)
if return_pred:
pred[i, j_current] = v.index
elif current_node.val > next_val:
decrease_val(&heap, current_node,
next_val)
if return_pred:
pred[i, j_current] = v.index
for j in range(csrT_indptr[v.index], csrT_indptr[v.index + 1]):
j_current = csrT_indices[j]
current_node = &nodes[j_current]
if current_node.state != SCANNED:
next_val = v.val + csrT_weights[j]
if next_val <= limit:
if current_node.state == NOT_IN_HEAP:
current_node.state = IN_HEAP
current_node.val = next_val
insert_node(&heap, current_node)
if return_pred:
pred[i, j_current] = v.index
elif current_node.val > next_val:
decrease_val(&heap, current_node, next_val)
if return_pred:
pred[i, j_current] = v.index
#v has now been scanned: add the distance to the results
dist_matrix[i, v.index] = v.val
free(nodes)
def bellman_ford(csgraph, directed=True, indices=None,
return_predecessors=False,
unweighted=False):
"""
bellman_ford(csgraph, directed=True, indices=None, return_predecessors=False,
unweighted=False)
Compute the shortest path lengths using the Bellman-Ford algorithm.
The Bellman-ford algorithm can robustly deal with graphs with negative
weights. If a negative cycle is detected, an error is raised. For
graphs without negative edge weights, dijkstra's algorithm may be faster.
.. versionadded:: 0.11.0
Parameters
----------
csgraph : array, matrix, or sparse matrix, 2 dimensions
The N x N array of distances representing the input graph.
directed : bool, optional
If True (default), then find the shortest path on a directed graph:
only move from point i to point j along paths csgraph[i, j].
If False, then find the shortest path on an undirected graph: the
algorithm can progress from point i to j along csgraph[i, j] or
csgraph[j, i]
indices : array_like or int, optional
if specified, only compute the paths for the points at the given
indices.
return_predecessors : bool, optional
If True, return the size (N, N) predecesor matrix
unweighted : bool, optional
If True, then find unweighted distances. That is, rather than finding
the path between each point such that the sum of weights is minimized,
find the path such that the number of edges is minimized.
Returns
-------
dist_matrix : ndarray
The N x N matrix of distances between graph nodes. dist_matrix[i,j]
gives the shortest distance from point i to point j along the graph.
predecessors : ndarray
Returned only if return_predecessors == True.
The N x N matrix of predecessors, which can be used to reconstruct
the shortest paths. Row i of the predecessor matrix contains
information on the shortest paths from point i: each entry
predecessors[i, j] gives the index of the previous node in the
path from point i to point j. If no path exists between point
i and j, then predecessors[i, j] = -9999
Raises
------
NegativeCycleError:
if there are negative cycles in the graph
Notes
-----
This routine is specially designed for graphs with negative edge weights.
If all edge weights are positive, then Dijkstra's algorithm is a better
choice.
"""
#------------------------------
# validate csgraph and convert to csr matrix
csgraph = validate_graph(csgraph, directed, DTYPE,
dense_output=False)
N = csgraph.shape[0]
#------------------------------
# intitialize/validate indices
if indices is None:
indices = np.arange(N, dtype=ITYPE)
else:
indices = np.array(indices, order='C', dtype=ITYPE)
indices[indices < 0] += N
if np.any(indices < 0) or np.any(indices >= N):
raise ValueError("indices out of range 0...N")
return_shape = indices.shape + (N,)
indices = np.atleast_1d(indices).reshape(-1)
#------------------------------
# initialize dist_matrix for output
dist_matrix = np.empty((len(indices), N), dtype=DTYPE)
dist_matrix.fill(np.inf)
dist_matrix[np.arange(len(indices)), indices] = 0
#------------------------------
# initialize predecessors for output
if return_predecessors:
predecessor_matrix = np.empty((len(indices), N), dtype=ITYPE)
predecessor_matrix.fill(NULL_IDX)
else:
predecessor_matrix = np.empty((0, N), dtype=ITYPE)
if unweighted:
csr_data = np.ones(csgraph.data.shape)
else:
csr_data = csgraph.data
if directed:
ret = _bellman_ford_directed(indices,
csr_data, csgraph.indices,
csgraph.indptr,
dist_matrix, predecessor_matrix)
else:
ret = _bellman_ford_undirected(indices,
csr_data, csgraph.indices,
csgraph.indptr,
dist_matrix, predecessor_matrix)
if ret >= 0:
raise NegativeCycleError("Negative cycle detected on node %i" % ret)
if return_predecessors:
return (dist_matrix.reshape(return_shape),
predecessor_matrix.reshape(return_shape))
else:
return dist_matrix.reshape(return_shape)
cdef int _bellman_ford_directed(
np.ndarray[ITYPE_t, ndim=1, mode='c'] source_indices,
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=2, mode='c'] dist_matrix,
np.ndarray[ITYPE_t, ndim=2, mode='c'] pred):
cdef unsigned int Nind = dist_matrix.shape[0]
cdef unsigned int N = dist_matrix.shape[1]
cdef unsigned int i, j, k, j_source, count
cdef DTYPE_t d1, d2, w12
cdef int return_pred = (pred.size > 0)
for i in range(Nind):
j_source = source_indices[i]
# relax all edges N-1 times
for count in range(N - 1):
for j in range(N):
d1 = dist_matrix[i, j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
d2 = dist_matrix[i, csr_indices[k]]
if d1 + w12 < d2:
dist_matrix[i, csr_indices[k]] = d1 + w12
if return_pred:
pred[i, csr_indices[k]] = j
# check for negative-weight cycles
for j in range(N):
d1 = dist_matrix[i, j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
d2 = dist_matrix[i, csr_indices[k]]
if d1 + w12 + DTYPE_EPS < d2:
return j_source
return -1
cdef int _bellman_ford_undirected(
np.ndarray[ITYPE_t, ndim=1, mode='c'] source_indices,
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=2, mode='c'] dist_matrix,
np.ndarray[ITYPE_t, ndim=2, mode='c'] pred):
cdef unsigned int Nind = dist_matrix.shape[0]
cdef unsigned int N = dist_matrix.shape[1]
cdef unsigned int i, j, k, j_source, ind_k, count
cdef DTYPE_t d1, d2, w12
cdef int return_pred = (pred.size > 0)
for i in range(Nind):
j_source = source_indices[i]
# relax all edges N-1 times
for count in range(N - 1):
for j in range(N):
d1 = dist_matrix[i, j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
ind_k = csr_indices[k]
d2 = dist_matrix[i, ind_k]
if d1 + w12 < d2:
dist_matrix[i, ind_k] = d2 = d1 + w12
if return_pred:
pred[i, ind_k] = j
if d2 + w12 < d1:
dist_matrix[i, j] = d1 = d2 + w12
if return_pred:
pred[i, j] = ind_k
# check for negative-weight cycles
for j in range(N):
d1 = dist_matrix[i, j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
d2 = dist_matrix[i, csr_indices[k]]
if abs(d2 - d1) > w12 + DTYPE_EPS:
return j_source
return -1
def johnson(csgraph, directed=True, indices=None,
return_predecessors=False,
unweighted=False):
"""
johnson(csgraph, directed=True, indices=None, return_predecessors=False,
unweighted=False)
Compute the shortest path lengths using Johnson's algorithm.
Johnson's algorithm combines the Bellman-Ford algorithm and Dijkstra's
algorithm to quickly find shortest paths in a way that is robust to
the presence of negative cycles. If a negative cycle is detected,
an error is raised. For graphs without negative edge weights,
dijkstra() may be faster.
.. versionadded:: 0.11.0
Parameters
----------
csgraph : array, matrix, or sparse matrix, 2 dimensions
The N x N array of distances representing the input graph.
directed : bool, optional
If True (default), then find the shortest path on a directed graph:
only move from point i to point j along paths csgraph[i, j].
If False, then find the shortest path on an undirected graph: the
algorithm can progress from point i to j along csgraph[i, j] or
csgraph[j, i]
indices : array_like or int, optional
if specified, only compute the paths for the points at the given
indices.
return_predecessors : bool, optional
If True, return the size (N, N) predecesor matrix
unweighted : bool, optional
If True, then find unweighted distances. That is, rather than finding
the path between each point such that the sum of weights is minimized,
find the path such that the number of edges is minimized.
Returns
-------
dist_matrix : ndarray
The N x N matrix of distances between graph nodes. dist_matrix[i,j]
gives the shortest distance from point i to point j along the graph.
predecessors : ndarray
Returned only if return_predecessors == True.
The N x N matrix of predecessors, which can be used to reconstruct
the shortest paths. Row i of the predecessor matrix contains
information on the shortest paths from point i: each entry
predecessors[i, j] gives the index of the previous node in the
path from point i to point j. If no path exists between point
i and j, then predecessors[i, j] = -9999
Raises
------
NegativeCycleError:
if there are negative cycles in the graph
Notes
-----
This routine is specially designed for graphs with negative edge weights.
If all edge weights are positive, then Dijkstra's algorithm is a better
choice.
"""
#------------------------------
# if unweighted, there are no negative weights: we just use dijkstra
if unweighted:
return dijkstra(csgraph, directed, indices,
return_predecessors, unweighted)
#------------------------------
# validate csgraph and convert to csr matrix
csgraph = validate_graph(csgraph, directed, DTYPE,
dense_output=False)
N = csgraph.shape[0]
#------------------------------
# initialize/validate indices
if indices is None:
indices = np.arange(N, dtype=ITYPE)
return_shape = indices.shape + (N,)
else:
indices = np.array(indices, order='C', dtype=ITYPE)
return_shape = indices.shape + (N,)
indices = np.atleast_1d(indices).reshape(-1)
indices[indices < 0] += N
if np.any(indices < 0) or np.any(indices >= N):
raise ValueError("indices out of range 0...N")
#------------------------------
# initialize dist_matrix for output
dist_matrix = np.empty((len(indices), N), dtype=DTYPE)
dist_matrix.fill(np.inf)
dist_matrix[np.arange(len(indices)), indices] = 0
#------------------------------
# initialize predecessors for output
if return_predecessors:
predecessor_matrix = np.empty((len(indices), N), dtype=ITYPE)
predecessor_matrix.fill(NULL_IDX)
else:
predecessor_matrix = np.empty((0, N), dtype=ITYPE)
#------------------------------
# initialize distance array
dist_array = np.zeros(N, dtype=DTYPE)
csr_data = csgraph.data.copy()
#------------------------------
# here we first add a single node to the graph, connected by a
# directed edge of weight zero to each node, and perform bellman-ford
if directed:
ret = _johnson_directed(csr_data, csgraph.indices,
csgraph.indptr, dist_array)
else:
ret = _johnson_undirected(csr_data, csgraph.indices,
csgraph.indptr, dist_array)
if ret >= 0:
raise NegativeCycleError("Negative cycle detected on node %i" % ret)
#------------------------------
# add the bellman-ford weights to the data
_johnson_add_weights(csr_data, csgraph.indices,
csgraph.indptr, dist_array)
if directed:
_dijkstra_directed(indices,
csr_data, csgraph.indices, csgraph.indptr,
dist_matrix, predecessor_matrix, np.inf)
else:
csgraphT = csr_matrix((csr_data, csgraph.indices, csgraph.indptr),
csgraph.shape).T.tocsr()
_johnson_add_weights(csgraphT.data, csgraphT.indices,
csgraphT.indptr, dist_array)
_dijkstra_undirected(indices,
csr_data, csgraph.indices, csgraph.indptr,
csgraphT.data, csgraphT.indices, csgraphT.indptr,
dist_matrix, predecessor_matrix, np.inf)
#------------------------------
# correct the distance matrix for the bellman-ford weights
dist_matrix += dist_array
dist_matrix -= dist_array[:, None][indices]
if return_predecessors:
return (dist_matrix.reshape(return_shape),
predecessor_matrix.reshape(return_shape))
else:
return dist_matrix.reshape(return_shape)
cdef void _johnson_add_weights(
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=1, mode='c'] dist_array):
# let w(u, v) = w(u, v) + h(u) - h(v)
cdef unsigned int j, k, N = dist_array.shape[0]
for j in range(N):
for k in range(csr_indptr[j], csr_indptr[j + 1]):
csr_weights[k] += dist_array[j]
csr_weights[k] -= dist_array[csr_indices[k]]
cdef int _johnson_directed(
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=1, mode='c'] dist_array):
cdef unsigned int N = dist_array.shape[0]
cdef unsigned int j, k, j_source, count
cdef DTYPE_t d1, d2, w12
# relax all edges (N+1) - 1 times
for count in range(N):
for k in range(N):
if dist_array[k] < 0:
dist_array[k] = 0
for j in range(N):
d1 = dist_array[j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
d2 = dist_array[csr_indices[k]]
if d1 + w12 < d2:
dist_array[csr_indices[k]] = d1 + w12
# check for negative-weight cycles
for j in range(N):
d1 = dist_array[j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
d2 = dist_array[csr_indices[k]]
if d1 + w12 + DTYPE_EPS < d2:
return j
return -1
cdef int _johnson_undirected(
np.ndarray[DTYPE_t, ndim=1, mode='c'] csr_weights,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indices,
np.ndarray[ITYPE_t, ndim=1, mode='c'] csr_indptr,
np.ndarray[DTYPE_t, ndim=1, mode='c'] dist_array):
cdef unsigned int N = dist_array.shape[0]
cdef unsigned int j, k, j_source, count
cdef DTYPE_t d1, d2, w12
# relax all edges (N+1) - 1 times
for count in range(N):
for k in range(N):
if dist_array[k] < 0:
dist_array[k] = 0
for j in range(N):
d1 = dist_array[j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
ind_k = csr_indices[k]
d2 = dist_array[ind_k]
if d1 + w12 < d2:
dist_array[ind_k] = d1 + w12
if d2 + w12 < d1:
dist_array[j] = d1 = d2 + w12
# check for negative-weight cycles
for j in range(N):
d1 = dist_array[j]
for k in range(csr_indptr[j], csr_indptr[j + 1]):
w12 = csr_weights[k]
d2 = dist_array[csr_indices[k]]
if abs(d2 - d1) > w12 + DTYPE_EPS:
return j
return -1
######################################################################
# FibonacciNode structure
# This structure and the operations on it are the nodes of the
# Fibonacci heap.
#
cdef enum FibonacciState:
SCANNED
NOT_IN_HEAP
IN_HEAP
cdef struct FibonacciNode:
unsigned int index
unsigned int rank
FibonacciState state
DTYPE_t val
FibonacciNode* parent
FibonacciNode* left_sibling
FibonacciNode* right_sibling
FibonacciNode* children
cdef void initialize_node(FibonacciNode* node,
unsigned int index,
DTYPE_t val=0):
# Assumptions: - node is a valid pointer
# - node is not currently part of a heap
node.index = index
node.val = val
node.rank = 0
node.state = NOT_IN_HEAP
node.parent = NULL
node.left_sibling = NULL
node.right_sibling = NULL
node.children = NULL
cdef FibonacciNode* rightmost_sibling(FibonacciNode* node):
# Assumptions: - node is a valid pointer
cdef FibonacciNode* temp = node
while(temp.right_sibling):
temp = temp.right_sibling
return temp
cdef FibonacciNode* leftmost_sibling(FibonacciNode* node):
# Assumptions: - node is a valid pointer
cdef FibonacciNode* temp = node
while(temp.left_sibling):
temp = temp.left_sibling
return temp
cdef void add_child(FibonacciNode* node, FibonacciNode* new_child):
# Assumptions: - node is a valid pointer
# - new_child is a valid pointer
# - new_child is not the sibling or child of another node
new_child.parent = node
if node.children:
add_sibling(node.children, new_child)
else:
node.children = new_child
new_child.right_sibling = NULL
new_child.left_sibling = NULL
node.rank = 1
cdef void add_sibling(FibonacciNode* node, FibonacciNode* new_sibling):
# Assumptions: - node is a valid pointer
# - new_sibling is a valid pointer
# - new_sibling is not the child or sibling of another node
cdef FibonacciNode* temp = rightmost_sibling(node)
temp.right_sibling = new_sibling
new_sibling.left_sibling = temp
new_sibling.right_sibling = NULL
new_sibling.parent = node.parent
if new_sibling.parent:
new_sibling.parent.rank += 1
cdef void remove(FibonacciNode* node):
# Assumptions: - node is a valid pointer
if node.parent:
node.parent.rank -= 1
if node.left_sibling:
node.parent.children = node.left_sibling
elif node.right_sibling:
node.parent.children = node.right_sibling
else:
node.parent.children = NULL
if node.left_sibling:
node.left_sibling.right_sibling = node.right_sibling
if node.right_sibling:
node.right_sibling.left_sibling = node.left_sibling
node.left_sibling = NULL
node.right_sibling = NULL
node.parent = NULL
######################################################################
# FibonacciHeap structure
# This structure and operations on it use the FibonacciNode
# routines to implement a Fibonacci heap
ctypedef FibonacciNode* pFibonacciNode
cdef struct FibonacciHeap:
FibonacciNode* min_node
pFibonacciNode[100] roots_by_rank # maximum number of nodes is ~2^100.
cdef void insert_node(FibonacciHeap* heap,
FibonacciNode* node):
# Assumptions: - heap is a valid pointer
# - node is a valid pointer
# - node is not the child or sibling of another node
if heap.min_node:
add_sibling(heap.min_node, node)
if node.val < heap.min_node.val:
heap.min_node = node
else:
heap.min_node = node
cdef void decrease_val(FibonacciHeap* heap,
FibonacciNode* node,
DTYPE_t newval):
# Assumptions: - heap is a valid pointer
# - newval <= node.val
# - node is a valid pointer
# - node is not the child or sibling of another node
# - node is in the heap
node.val = newval
if node.parent and (node.parent.val >= newval):
remove(node)
insert_node(heap, node)
elif heap.min_node.val > node.val:
heap.min_node = node
cdef void link(FibonacciHeap* heap, FibonacciNode* node):
# Assumptions: - heap is a valid pointer
# - node is a valid pointer
# - node is already within heap
cdef FibonacciNode *linknode
cdef FibonacciNode *parent
cdef FibonacciNode *child
if heap.roots_by_rank[node.rank] == NULL:
heap.roots_by_rank[node.rank] = node
else:
linknode = heap.roots_by_rank[node.rank]
heap.roots_by_rank[node.rank] = NULL
if node.val < linknode.val or node == heap.min_node:
remove(linknode)
add_child(node, linknode)
link(heap, node)
else:
remove(node)
add_child(linknode, node)
link(heap, linknode)
cdef FibonacciNode* remove_min(FibonacciHeap* heap):
# Assumptions: - heap is a valid pointer
# - heap.min_node is a valid pointer
cdef FibonacciNode *temp
cdef FibonacciNode *temp_right
cdef FibonacciNode *out
cdef unsigned int i
# make all min_node children into root nodes
if heap.min_node.children:
temp = leftmost_sibling(heap.min_node.children)
temp_right = NULL
while temp:
temp_right = temp.right_sibling
remove(temp)
add_sibling(heap.min_node, temp)
temp = temp_right
heap.min_node.children = NULL
# choose a root node other than min_node
temp = leftmost_sibling(heap.min_node)
if temp == heap.min_node:
if heap.min_node.right_sibling:
temp = heap.min_node.right_sibling
else:
out = heap.min_node
heap.min_node = NULL
return out
# remove min_node, and point heap to the new min
out = heap.min_node
remove(heap.min_node)
heap.min_node = temp
# re-link the heap
for i in range(100):
heap.roots_by_rank[i] = NULL
while temp:
if temp.val < heap.min_node.val:
heap.min_node = temp
temp_right = temp.right_sibling
link(heap, temp)
temp = temp_right
return out
######################################################################
# Debugging: Functions for printing the Fibonacci heap
#
#cdef void print_node(FibonacciNode* node, int level=0):
# print '%s(%i,%i) %i' % (level*' ', node.index, node.val, node.rank)
# if node.children:
# print_node(leftmost_sibling(node.children), level+1)
# if node.right_sibling:
# print_node(node.right_sibling, level)
#
#
#cdef void print_heap(FibonacciHeap* heap):
# print "---------------------------------"
# print "min node: (%i, %i)" % (heap.min_node.index, heap.min_node.val)
# if heap.min_node:
# print_node(leftmost_sibling(heap.min_node))
# else:
# print "[empty heap]"
|