加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

algorithm – 如何找到并行树中的哪些作业可以并行运行?

发布时间:2020-12-15 23:37:19 所属栏目:大数据 来源:网络整理
导读:目前,我使用图形来存储依赖项,然后运行没有任何依赖项的所有顶点.这有效,但感觉很糟糕.我应该使用更好的算法或数据结构吗? #!/usr/bin/perluse strict;use warnings;use Graph;#FIXME: naive implementation,there may be a much better way to do thissub
目前,我使用图形来存储依赖项,然后运行没有任何依赖项的所有顶点.这有效,但感觉很糟糕.我应该使用更好的算法或数据结构吗?

#!/usr/bin/perl

use strict;
use warnings;

use Graph;

#FIXME: naive implementation,there may be a much better way to do this
sub run_in_parallel {
       my $g = shift->copy;

       while (my @v = $g->vertices) {
               my @run = grep { $g->is_successorless_vertex($_) } @v;
               print "running ",join(",",@run)," in paralleln";
               for my $compenent (@run) {
                       $g->delete_vertex($compenent);
               };
       }
}

my $g = Graph->new;
while (<DATA>) {
       my ($component,@dependencies) = split;
       unless ($g->has_vertex($component)) {
               $g->add_vertex($component);
       }
       for my $dependency (@dependencies) {
               unless ($g->has_vertex($dependency)) {
                       $g->add_vertex($dependency);
               }
               $g->add_edge($component,$dependency);
       }
}

run_in_parallel($g);

#component  dependency list
__DATA__
a           b c d
b           e
c           f g
d
e
f
g

解决方法

另一个想法是到 use a Petri net.
图中的节点是它的位置,动作是它的过渡.
每个地方都应该只有一个启用转换,即使它没有依赖关系.
这样你甚至不需要放置任何初始令牌.

这也包含了Karl Bielefeldt的建议.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读