-
Notifications
You must be signed in to change notification settings - Fork 1
/
acyclic.pl
58 lines (56 loc) · 1.11 KB
/
acyclic.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use List::MoreUtils qw(uniq);
sub AcyclicCheck {
my @edge_set = ();
foreach my $vertex (keys %FOREST){
my $length = scalar( @{ $FOREST{$vertex} } );
for ( my $m = 0; $m < $length; $m++) {
push @edge_set, $FOREST{$vertex}[$m];
}
}
my @uniq_edgeset = uniq(@edge_set);
my @vertices = @V;
sub Acyclic{
my @discovered = ('A');
my $index = 0;
my $start = 'A';
my @connected_component = ();
sub DFS{
foreach my $e (@uniq_edgeset){
my @edge = split(//, $e);
if($discovered[$index] eq $edge[0]){
my $edge = $edge[1];
#print $edge;
if ( grep( /^$edge$/, @discovered ) ) {
push @connected_component, $e;
# print "found it";
}
else{
# print "Add to discovered";
push @discovered, $edge;
push @connected_component, $e;
}
}
}
$index = $index + 1;
$start = $discovered[$index];
if($index < scalar @vertices + 1){
return DFS();
}
}
DFS();
if(scalar @discovered == scalar @connected_component - 1){
return 0;
}
else{
return 1;
}
}
Acyclic();
}
if(AcyclicCheck() == 0){
print "Acyclic";
}
else{
print "Not Acyclic";
}
exit;