in reply to using Graph::Undirected

You're calling an object method without constructing the object.

From your description, it sounds like you want a complete graph for the vertices listed in each element of list1.

use Graph::Undirected; my @colors = qw( black white red green blue cyan magenta yellow); my @combos = ( [qw(black cyan yellow)], [qw(red white blue)], [qw(white blue)], [qw(green red)], [qw(cyan magenta)], [qw(yellow magenta)], [qw(magenta cyan)], [qw(magenta black yellow)], ); my $g = Graph::Undirected->new(@colors); for my $combo (@combos) { # Special case of all @$combo <= 3, the following # can be replaced by: # $g->add_cycle( @$combo); for my $pick (0..$#$combo-1) { $g->add_edge( $combo->[$pick], $combo->[$_]) for $pick+1..$#$ +combo; } } my @components = $g->strongly_connected_components; print "@$_", $/ for @components;
That adds all the elements of list2 as vertices in the constructor, then adds edges for all pairs in each element of list1. Is that close to what you want?

Update: Repaired a couple of typos and a harmless fencepost error in the inner loop which added self-edges to most vertices. Added comment with simpler code for the case that the @combos elements don't have more than three colors. In response to glwtta's reply, modified data to exhibit disconnected components and added component extraction code with the &Graph::Base::strongly_connected_components method. The Graph module has lots of methods like this, documented in Graph::Base

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: using Graph::Undirected
by glwtta (Hermit) on Feb 21, 2003 at 15:59 UTC
    tall man is right, my example is incomplete and incoherent ( it was late), but luckily, you got it exactly, Zaxo.

    btw, I do construct the graph, I just missed that in the example, trying to keep it minimal.

    One clarification, my input data would look more like:

    my @combos = ( [qw(black white)], [qw(red white)], [qw(cyan red)], [qw(yellow blue)], [qw(magenta green blue)], [qw(cyan periwinkle raspberry)], );
    so not all the vertices are connected, and in fact what I need to get out of this are the sets that are. so what I do is:
    sub r { my $g = shift; my $v = shift; my @ns = $g->neighbors($v); print $v."\n"; $g->delete_vertex($v); foreach my $n (@ns){ r($g, $n); } } while (scalar $g->vertices > 0){ print "next set:\n"; r($g, ($g->vertices)[0]); }
    This however doesn't seem terribly elegant (or efficient), am I anywhere near what I should be doing in this case?

    and thanks for your great help!