作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 tsort
algorithm对库列表及其依赖项进行排序。在依赖项不禁止的情况下,我希望排序顺序保持不变。此库列表不会发生这种情况:
this
和
that
没有依赖关系。
other
取决于
that
, 和
thing
取决于
that
和
this
.申请后
tsort
,我希望列表输出为:
#!/usr/bin/perl -w
use v5.10;
sub sortem {
my %pairs; # all pairs ($l, $r)
my %npred; # number of predecessors
my %succ; # list of successors
for my $lib (@_) {
my $name = $lib->[0];
$pairs{$name} = {};
$npred{$name} += 0;
for my $dep (@{ $lib->[1] }) {
next if exists $pairs{$name}{$dep};
$pairs{$name}{$dep}++;
$npred{$dep}++;
push @{ $succ{$name} } => $dep;
}
}
# create a list of nodes without predecessors
my @list = grep {!$npred{$_}} keys %npred;
my @ret;
while (@list) {
my $lib = pop @list;
unshift @ret => $lib;
foreach my $child (@{$succ{$lib}}) {
push @list, $child unless --$npred{$child};
}
}
if ( my @cycles = grep { $npred{$_} } @_ ) {
die "Cycle detected between changes @cycles\n";
}
return @ret;
}
say for sortem(
['this', []],
['that', []],
['other', [qw(that)]],
['thing', [qw(that this)]],
);
tsort
获得相同的、不保留顺序的输出:
that thing
this thing
that other
that this
最佳答案
让我们像这样写你的最后一个循环:
while (my @list = grep { !$npred{$_} } keys %npred) {
push(@ret, @list); # we will change this later
for my $lib (@list) {
delete $npred{$lib};
for my $child ( @{ $succ{$ib} } ) {
$npred{$child}--;
}
}
}
if (%npred) {
...we have a loop...
}
keys %npred
寻找零。当
grep
不返回我们完成或循环的元素。
push(@ret, @list)
到:
push(@ret, sort {...} @list);
{...}
是指定初始排序的比较函数。
use strict;
use warnings;
use Data::Dump qw/pp dd/;
my %deps = (
# node => [ others pointing to node ]
this => [],
that => [],
other => [qw/that/],
thing => [qw/that this other/],
yowza => [qw/that/],
);
# How to interpret %deps as a DAG:
#
# that ---> other ---+
# | V
# +------------> thing
# | ^
# +---> yowza |
# |
# this --------------+
#
# There are two choices for the first node in the topological sort: "this" and "that".
# Once "that' has been chosen, "yowza" and "other" become available.
# Either "yowza" or "thing" will be the last node in any topological sort.
sub tsort {
my ($deps, $order) = @_;
# $deps is the DAG
# $order is the preferred order of the nodes if there is a choice
# Initialize counts and reverse links.
my %ord;
my %count;
my %rdep;
my $nnodes = scalar(keys %$deps);
for (keys %$deps) {
$count{$_} = 0;
$rdep{$_} = [];
$ord{$_} = $nnodes;
}
for my $n (keys %$deps) {
$count{$n}++ for (@{ $deps->{$n} });
push(@{$rdep{$_}}, $n) for (@{ $deps->{$n} });
}
for (my $i = 0; $i <= $#$order; $i++) {
$ord{ $order->[$i] } = $i;
}
my @tsort;
# pp(%$deps);
# pp(%rdep);
while (1) {
# print "counts: ", pp(%count), "\n";
my @list = grep { $count{$_} == 0 } (keys %count);
last unless @list;
my @ord = sort { $ord{$a} <=> $ord{$b} } @list;
push(@tsort, @ord);
for my $n (@list) {
delete $count{$n};
$count{$_}-- for (@{ $rdep{$n} });
}
}
return @tsort;
}
sub main {
my @t1 = tsort(\%deps, [qw/this that other thing yowza/]);
print "t1: ", pp(@t1), "\n";
my @t2 = tsort(\%deps, [qw/this that yowza other thing/]);
print "t2: ", pp(@t2), "\n";
my @t3 = tsort(\%deps, [qw/that this yowza other thing/]);
print "t3: ", pp(@t3), "\n";
}
main();
t1: ("this", "that", "other", "yowza", "thing")
t2: ("this", "that", "yowza", "other", "thing")
t3: ("that", "this", "yowza", "other", "thing")
关于perl - 使用 tsort 算法时可以更好地保留顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369187/
最近我在 spoj 上解决一个关于排序的问题,我使用 python 解决了它,但它给了我 tle,然后我遇到了同样问题的代码,该代码工作正常,但任何人都可以向我解释它的工作原理,特别是ar[a]++
我正在使用 tsort algorithm对库列表及其依赖项进行排序。在依赖项不禁止的情况下,我希望排序顺序保持不变。此库列表不会发生这种情况: 此 那个 其他 [那个] 事情[那个] 依赖项在括号中
我正在尝试使用 tSort 对 jQuery 选择的结果进行排序。 HTML: Javascript: $sort_order = $('div').tsort({attr:'s
希望我能在 Ruby 中的这个特定的重新排序/排序问题上得到一些帮助。 我有一个数组数组,像这样: [['b', 'f'], ['f', 'h'], ['a', 'e'], ['b', 'c'], [
我的 Rails 4.2 在开发中运行良好,但在生产环境中我有以下警告: DEPRECATION WARNING: The configuration option `config.serve_sta
我是一名优秀的程序员,十分优秀!