- 論壇徽章:
- 0
|
本帖最后由 riverlee2008 于 2010-05-13 21:29 編輯
有個(gè)文件,兩列,第一列是子節(jié)點(diǎn),第二列表示父親幾點(diǎn),程序出來就是一個(gè)樹(有向無環(huán)圖,如下圖), 想做的事情是給定一個(gè)節(jié)點(diǎn),想找出它的所有子孫節(jié)點(diǎn), 程序已經(jīng)實(shí)現(xiàn),通過遞歸,對(duì)于樹中較底層的結(jié)點(diǎn),獲取子孫節(jié)點(diǎn)沒有問題, 但對(duì)于樹中較上層的節(jié)點(diǎn),遞歸出現(xiàn) Deep recursion on subroutine "main::findnodes",內(nèi)存飆到48G以上(本機(jī)內(nèi)存大小48G), 且遞歸次數(shù)超過140000000次多.
現(xiàn)將程序和數(shù)據(jù)文件附上,忘論壇內(nèi)高手指點(diǎn)指點(diǎn),或者給個(gè)別的思路(詳細(xì)程序數(shù)據(jù)見附件).
- #!/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";
- #######################################
- #構(gòu)建一個(gè)變量用來記錄其所有的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;
- }
- }
復(fù)制代碼
ask_help_CU.tar.bz2
(55.38 KB, 下載次數(shù): 75)
2010-05-13 21:27 上傳
點(diǎn)擊文件名下載附件
測(cè)試數(shù)據(jù)
![]() |
|