#!/usr/bin/perl use warnings; use strict; my $couldy=['cloudy','t','f']; # 'cloudy' is the node name and 't','f' are the values this node can assume. Likewise for other nodes. my $sprinkler=['sprinkler','t','f']; my $rain=['rain','t','f']; my $wetgrass=['wetgrass','t','f']; my $values=[$sprinkler,$couldy,$rain,$wetgrass]; my @nodes=('sprinkler','cloudy','wetgrass','rain'); foreach my $i (@nodes) { my $nodeparents=[parentchildrelationship($i)]; print "$i $nodeparents $values\n"; my $hash1=cpt($i,$nodeparents,$values); foreach my $i (keys %{$hash1}) { print "$i=>${$hash1}{$i}\n"; #print "$i\n"; } } sub cpt { my $node=$_[0]; my @parents=@{$_[1]}; my @nodevalues=@{$_[2]}; my %hash=(); my @nodeindex=(); my @temparray=(); my $string=""; my @temp=(); my $s; foreach my $i (@parents) { for (my $j=0;$j<=$#nodevalues;$j++) { if ($i eq ${$nodevalues[$j]}[0]) { push (@nodeindex, $j); } } } foreach my $i (reverse @nodeindex) { push (@temparray, $nodevalues[$i]); } for (my $i=0;$i<=$#temparray;$i++) { $string="$string"."${$temparray[$i]}[0] "; shift @{$temparray[$i]}; $temp[$i]=join (",",@{$temparray[$i]}); } $hash{$string}=1; #fill %hash with the header (header is simply the name of the node along with its parents) $s = join "\\ ", map "{$_}", @temp; #print "$s\n"; $hash{$_}=1 for glob $s; return {%hash}; $node=""; @parents=(); @nodevalues=(); %hash=(); @nodeindex=(); @temparray=(); $string=""; @temp=(); $s=""; } sub parentchildrelationship { my $node=$_[0]; #or use "shift" my %parentchild=(); my @nodeset=(); %parentchild=('cloudy'=>['none'], 'sprinkler'=>['cloudy'], 'rain'=>['cloudy'], 'wetgrass'=>['rain','sprinkler']); #This is the structure of the graph, for example node 'cloudy' has no parent; node 'sprinkler' has 'cloudy' as its parents; node 'rain' has 'cloudy' as its parents; node 'weygrass' has 'rain' and 'sprinkler' as its parents; (directionality of the arrow determines the child-parent relationship). push (@nodeset, $node, @{$parentchild{$node}}); return @nodeset; }