#!/usr/bin/env perl

use strict;
use warnings;

use Graph::Weighted;
use Math::Partition::Rand;

my $attr = 'probability';

my $gw = Graph::Weighted->new();

$gw->populate( random_graph([qw( A B C D )]), $attr );

$gw->dump($attr);

sub random_graph {
    my $labels = shift;

    my $graph = {};
    my $i = 0;

    for my $label ( @$labels ) {
        $graph->{$i} = { label => $label, choose_n( scalar @$labels ) };
        $i++;
    }

    return $graph;
}

sub choose_n {
    my $labels = shift;

    my %seen;
    my @labels = map { get_label( $labels, \%seen ) } 1 .. $labels;

    my %distribution;
    my $partition = Math::Partition::Rand->new( top => 1, n => $labels );
    @distribution{@labels} = @{ $partition->choose() };

    return %distribution;
}

sub get_label {
    my ( $labels, $seen ) = @_;

    my $label = int( rand $labels );

    if ( exists $seen->{$label} ) {
        while ( exists $seen->{$label} ) {
            $label = int( rand $labels );
        }
    }

    $seen->{$label} = 1;
    return $label;
}
