- 论坛徽章:
- 0
|
本帖最后由 riverlee2008 于 2010-05-13 21:29 编辑
有个文件,两列,第一列是子节点,第二列表示父亲几点,程序出来就是一个树(有向无环图,如下图), 想做的事情是给定一个节点,想找出它的所有子孙节点, 程序已经实现,通过递归,对于树中较底层的结点,获取子孙节点没有问题, 但对于树中较上层的节点,递归出现 Deep recursion on subroutine "main::findnodes",内存飙到48G以上(本机内存大小48G), 且递归次数超过140000000次多.
现将程序和数据文件附上,忘论坛内高手指点指点,或者给个别的思路(详细程序数据见附件).
- #!/usr/bin/perl
- use strict;
- use warnings;
- ###############################
- #usage
- #test1: perl findoffspring.pl do_ontology_926.txt DOID:3211
- #test2: perl findoffspring.pl do_ontology_926.txt DOID:4
- #test1 work fine, but test2 didnot work fine, where DOID:4 is the top node in the tree
- my($child2parentfile,$searchnode) = @ARGV;
- #child id 2 parent id
- #"do_id","parent_do_id"
- my %parent2child;
- my %doids;
- open(IN,$child2parentfile) or die $!;
- <IN>;#skip the header
- while(<IN>){
- s/[\r\n"]//g;
- my($c,$p) = split "\t";
- $parent2child{$p}->{$c}=1;
- $doids{$c}=1;
- $doids{$p}=1;
- }
- print "DO num:",scalar keys %doids,"\n";
- #######################################
- #构建一个变量用来记录其所有的offspring
- my $rr={};
- findnodes("DOID:4",\%parent2child,$rr);
- print join "\n",(keys %{$rr});
- #use recursion to find the offspring nodes
- #here defined the variable to see how many time we call the findnodes
- my $count=0;
- sub findnodes{
- my($search,$ref,$resultref)=@_;
- $count++;
- if($count % 20000 ==0){
- print "recursion:",$count,"\n";
- }
- if(exists($ref->{$search})){
- foreach my $node(keys %{$ref->{$search}}){
- $resultref->{$node}=1;
- findnodes($node,$ref,$resultref);
- }
- }else{
- return $resultref;
- }
- }
复制代码
ask_help_CU.tar.bz2
(55.38 KB, 下载次数: 75)
![]() |
|