Skip Menu |

This queue is for tickets about the Graph CPAN distribution.

Report information
The Basics
Id: 120612
Status: resolved
Priority: 0/
Queue: Graph

People
Owner: Nobody in particular
Requestors: user42_kevin [...] yahoo.com.au
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: suggest total path length
Date: Tue, 14 Mar 2017 11:20:37 +1100
To: bug-Graph [...] rt.cpan.org
From: Kevin Ryde <user42_kevin [...] yahoo.com.au>
As an idea for a feature, it could be good to have some sort of total path length method, like what average_path_length() does but giving total distance and count of paths. (The average then the ratio.) Not sure what a good name would be. total_path_length_and_count() would be suggestive but a bit of a mouthful. Maybe total_path_length() which in scalar context just the total length or in array context both total length and count. For an undirected graph, I think paths could be counted in one direction only, so u to v and not back v to u. That would seem sensible as they're the same, and the total length is then the Wiener index. (In the code it could be easier to divide total/2 and count/2 at the end instead of tracking which direction to count and not.) In any case, total and count separately is nice to work with average as an exact rational (one of the rationals classes or just own num,den).
On Mon Mar 13 20:20:50 2017, user42_kevin@yahoo.com.au wrote: Show quoted text
> As an idea for a feature, it could be good to have some sort of total > path length method, like what average_path_length() does but giving > total distance and count of paths. (The average then the ratio.) > > Not sure what a good name would be. total_path_length_and_count() would > be suggestive but a bit of a mouthful. Maybe total_path_length() which > in scalar context just the total length or in array context both total > length and count. > > For an undirected graph, I think paths could be counted in one direction > only, so u to v and not back v to u. That would seem sensible as > they're the same, and the total length is then the Wiener index. > (In the code it could be easier to divide total/2 and count/2 at the end > instead of tracking which direction to count and not.) > > In any case, total and count separately is nice to work with average as > an exact rational (one of the rationals classes or just own num,den).
I am somewhat open to suggestions, but given that average_path_length is now (after being simplified): sub average_path_length { my $g = shift; my @A = @_; my $d = 0; my $m = 0; $g->for_shortest_paths(sub { my ($t, $u, $v, $n) = @_; return unless my $l = $t->path_length($u, $v); return if defined $A[0] && $u ne $A[0]; return if defined $A[1] && $v ne $A[1]; $d += $l; $m++; }); return $m ? $d / $m : undef; } Your proposed method could be simply a "reduce" over all the paths in a graph, just using for_shortest_paths. You could then return an array-ref with the two values. No need for huge complexity. I am therefore closing this, but please open a GitHub issue if you feel strongly enough to make other suggestions!