# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1299851243 -28800
# Node ID f6845ff13199ead24788ea586ec3f0749f7f51d9
# Parent 189e11e80258728a10e0fa5f41dddb4a3c5a38b8
trac 10885 -- Floyd-Warshall algorithm in Cython (documentation)
diff -r 189e11e80258 -r f6845ff13199 sage/graphs/base/c_graph.pyx
a
|
b
|
|
2890 | 2890 | for a total of `2^{34}` bytes or `16` gigabytes. Let us also remember |
2891 | 2891 | that if the memory size is quadratic, the algorithm runs in cubic time. |
2892 | 2892 | |
| 2893 | .. NOTE:: |
| 2894 | |
| 2895 | When ``paths = False`` the algorithm saves roughly half of the memory as |
| 2896 | it does not have to maintain the matrix of predecessors. However, |
| 2897 | setting ``distances=False`` produces no such effect as the algorithm can |
| 2898 | not run without computing them. They will not be returned, but they will |
| 2899 | be stored while the method is running. |
| 2900 | |
2893 | 2901 | EXAMPLES: |
2894 | 2902 | |
2895 | 2903 | Shortest paths in a small grid :: |
… |
… |
|
2917 | 2925 | ... p.insert(0,path[u][p[0]]) |
2918 | 2926 | sage: len(p) == dist[u][v] + 2 |
2919 | 2927 | True |
| 2928 | |
| 2929 | Distances for all pairs of vertices in a diamond:: |
| 2930 | |
| 2931 | sage: g = graphs.DiamondGraph() |
| 2932 | sage: floyd_warshall(g, paths = False, distances = True) |
| 2933 | {0: {0: 0, 1: 1, 2: 1, 3: 2}, |
| 2934 | 1: {0: 1, 1: 0, 2: 1, 3: 1}, |
| 2935 | 2: {0: 1, 1: 1, 2: 0, 3: 1}, |
| 2936 | 3: {0: 2, 1: 1, 2: 1, 3: 0}} |
| 2937 | |
| 2938 | TESTS: |
| 2939 | |
| 2940 | Too large graphs:: |
| 2941 | |
| 2942 | sage: from sage.graphs.base.c_graph import floyd_warshall |
| 2943 | sage: floyd_warshall(Graph(65536)) |
| 2944 | Traceback (most recent call last): |
| 2945 | ... |
| 2946 | ValueError: The graph backend contains more than 65535 nodes |
2920 | 2947 | """ |
| 2948 | |
2921 | 2949 | from sage.rings.infinity import Infinity |
2922 | 2950 | cdef CGraph g = <CGraph> gg._backend._cg |
2923 | 2951 | |
… |
… |
|
2925 | 2953 | |
2926 | 2954 | cdef int n = max(gverts) + 1 |
2927 | 2955 | |
2928 | | if n > <unsigned short> -1: |
2929 | | raise ValueError("The graph backend contains more than "+(<unsigned short> -1)+" nodes") |
| 2956 | if n >= <unsigned short> -1: |
| 2957 | raise ValueError("The graph backend contains more than "+str(<unsigned short> -1)+" nodes") |
2930 | 2958 | |
2931 | 2959 | # All this just creates two tables prec[n][n] and dist[n][n] |
2932 | 2960 | cdef unsigned short * t_prec |