# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1325413839 -3600
# Node ID 93613ef6006868094781f093184cf18ae2440712
# Parent 482fd1fcc962ac3de720e652d1da42c137c2a066
trac #12244 -- Empty graphs and new distance computations
diff -r 482fd1fcc962 -r 93613ef60068 sage/graphs/distances_all_pairs.pyx
a
|
b
|
|
276 | 276 | # Predecessors # |
277 | 277 | ################ |
278 | 278 | |
279 | | cdef unsigned short * c_shortest_path_all_pairs(G): |
| 279 | cdef unsigned short * c_shortest_path_all_pairs(G) except NULL: |
280 | 280 | r""" |
281 | 281 | Returns the matrix of predecessors in G. |
282 | 282 | |
… |
… |
|
327 | 327 | """ |
328 | 328 | |
329 | 329 | cdef int n = G.order() |
| 330 | |
| 331 | if n == 0: |
| 332 | return {} |
330 | 333 | |
331 | 334 | cdef unsigned short * predecessors = c_shortest_path_all_pairs(G) |
332 | 335 | cdef unsigned short * c_predecessors = predecessors |
… |
… |
|
404 | 407 | from sage.rings.infinity import Infinity |
405 | 408 | |
406 | 409 | cdef int n = G.order() |
| 410 | |
| 411 | if n == 0: |
| 412 | return {} |
| 413 | |
407 | 414 | cdef unsigned short * distances = c_distances_all_pairs(G) |
408 | 415 | cdef unsigned short * c_distances = distances |
409 | 416 | |
… |
… |
|
481 | 488 | |
482 | 489 | from sage.rings.infinity import Infinity |
483 | 490 | cdef int n = G.order() |
| 491 | |
| 492 | if n == 0: |
| 493 | return {}, {} |
| 494 | |
484 | 495 | cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *)) |
485 | 496 | cdef unsigned short * c_distances = distances |
486 | 497 | cdef unsigned short * predecessor = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *)) |
… |
… |
|
527 | 538 | # Eccentricity # |
528 | 539 | ################ |
529 | 540 | |
530 | | cdef unsigned short * c_eccentricity(G): |
| 541 | cdef unsigned short * c_eccentricity(G) except NULL: |
531 | 542 | r""" |
532 | 543 | Returns the vector of eccentricities in G. |
533 | 544 | |
… |
… |
|
559 | 570 | """ |
560 | 571 | from sage.rings.infinity import Infinity |
561 | 572 | cdef int n = G.order() |
| 573 | |
| 574 | if n == 0: |
| 575 | return [] |
| 576 | |
562 | 577 | cdef unsigned short * ecc = c_eccentricity(G) |
563 | 578 | |
564 | 579 | cdef list l_ecc = [] |
… |
… |
|
691 | 706 | |
692 | 707 | cdef list gverts = g.verts() |
693 | 708 | |
| 709 | if gverts == []: |
| 710 | if distances and paths: |
| 711 | return {}, {} |
| 712 | else: |
| 713 | return {} |
| 714 | |
694 | 715 | cdef int n = max(gverts) + 1 |
695 | 716 | |
696 | 717 | if n >= <unsigned short> -1: |
diff -r 482fd1fcc962 -r 93613ef60068 sage/graphs/generic_graph.py
a
|
b
|
|
10543 | 10543 | 2 |
10544 | 10544 | sage: G.diameter() |
10545 | 10545 | 2 |
10546 | | """ |
| 10546 | |
| 10547 | TEST:: |
| 10548 | |
| 10549 | sage: g = Graph() |
| 10550 | sage: g.radius() |
| 10551 | Traceback (most recent call last): |
| 10552 | ... |
| 10553 | ValueError: This method has no meaning on empty graphs. |
| 10554 | """ |
| 10555 | if self.order() == 0: |
| 10556 | raise ValueError("This method has no meaning on empty graphs.") |
| 10557 | |
10547 | 10558 | return min(self.eccentricity()) |
10548 | 10559 | |
10549 | 10560 | def center(self): |
… |
… |
|
11467 | 11478 | the theory of the Wiener number. INDIAN JOURNAL OF CHEMISTRY SECTION A |
11468 | 11479 | PUBLICATIONS & INFORMATION DIRECTORATE, CSIR |
11469 | 11480 | """ |
11470 | | |
11471 | 11481 | return sum([sum(v.itervalues()) for v in self.distance_all_pairs().itervalues()])/2 |
11472 | 11482 | |
11473 | 11483 | def average_distance(self): |
… |
… |
|
11493 | 11503 | .. [GYLL93] I. Gutman, Y.-N. Yeh, S.-L. Lee, and Y.-L. Luo. Some recent |
11494 | 11504 | results in the theory of the Wiener number. *Indian Journal of |
11495 | 11505 | Chemistry*, 32A:651--661, 1993. |
11496 | | """ |
| 11506 | |
| 11507 | TEST:: |
| 11508 | |
| 11509 | sage: g = Graph() |
| 11510 | sage: g.average_distance() |
| 11511 | Traceback (most recent call last): |
| 11512 | ... |
| 11513 | ValueError: The graph must have at least two vertices for this value to be defined |
| 11514 | """ |
| 11515 | if self.order() < 2: |
| 11516 | raise ValueError("The graph must have at least two vertices for this value to be defined") |
11497 | 11517 | |
11498 | 11518 | return Integer(self.wiener_index())/Integer((self.order()*(self.order()-1))/2) |
11499 | 11519 | |