Skip to content

Commit 1b7d9ad

Browse files
committed
feat:Implemented Harary graph creation.
1 parent 9dc3c54 commit 1b7d9ad

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

lib/Graph/Classes.rakumod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ sub EXPORT {
77
use Graph::CompleteKaryTree;
88
use Graph::Cycle;
99
use Graph::Grid;
10+
use Graph::Harary;
1011
use Graph::HexagonalGrid;
1112
use Graph::Hypercube;
1213
use Graph::Indexed;
@@ -29,6 +30,7 @@ sub EXPORT {
2930
'Graph::CompleteKaryTree' => Graph::CompleteKaryTree,
3031
'Graph::Cycle' => Graph::Cycle,
3132
'Graph::Grid' => Graph::Grid,
33+
'Graph::Harary' => Graph::Harary,
3234
'Graph::HexagonalGrid' => Graph::HexagonalGrid,
3335
'Graph::Hypercube' => Graph::Hypercube,
3436
'Graph::Indexed' => Graph::Indexed,

lib/Graph/Harary.rakumod

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use v6.d;
2+
3+
use Graph;
4+
5+
class Graph::Harary is Graph {
6+
has Int:D $.k is required;
7+
has Int:D $.n is required;
8+
9+
submethod BUILD(:$!k!, :$!n!, Str:D :$prefix = '') {
10+
11+
for ^$!n -> $i {
12+
for (1 ... $!k div 2) -> $j {
13+
self.edge-add($prefix ~ $i.Str, $prefix ~ (($i + $j) % $!n).Str, :!directed);
14+
self.edge-add($prefix ~ $i.Str, $prefix ~ (($i - $j) % $!n).Str, :!directed);
15+
}
16+
}
17+
18+
if $!k !%% 2 {
19+
if $!n %% 2 {
20+
for ^($!n div 2) -> $i {
21+
self.edge-add($prefix ~ $i.Str, $prefix ~ ($i + $!n div 2).Str, :!directed);
22+
}
23+
} else {
24+
for ^(($!n - 1) div 2 + 1) -> $i {
25+
self.edge-add($prefix ~ $i.Str, $prefix ~ ($i + ($!n + 1) div 2).Str, :!directed);
26+
}
27+
self.edge-add($prefix ~ '0', $prefix ~ (($!n - 1) div 2).Str, :!directed);
28+
self.edge-add($prefix ~ '0', $prefix ~ (($!n + 1) div 2).Str, :!directed)
29+
}
30+
}
31+
32+
# Same as Circulant
33+
self.vertex-coordinates =
34+
self.vertex-list.map({ my $i = $_.subst($prefix).Int - 1; $_ => [cos(2 * pi / $!n * $i), sin(2 * pi / $!n * $i)]}).Hash;
35+
}
36+
37+
multi method new(Int:D $k, Int:D $n, Str:D :$prefix = '') {
38+
self.bless(:$k, :$n, :$prefix);
39+
}
40+
41+
multi method new(Int:D :$n, Int:D :$k, Str:D :$prefix = '') {
42+
self.new($k, $n, :$prefix);
43+
}
44+
}
45+

0 commit comments

Comments
 (0)