| 1 | # Copyright 2015, 2016, 2017 Kevin Ryde | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This file is part of Graph-Graph6. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Graph-Graph6 is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 6 | # the terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||
| 8 | # version. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Graph-Graph6 is distributed in the hope that it will be useful, but WITHOUT | ||||||||||||||||||||||||||||
| 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||||||||||||||||||||||||||
| 12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||||||||||||||||||||||||||||
| 13 | # more details. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||
| 16 | # with Graph-Graph6. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package Graph::Easy::As_graph6; | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use 5.006; # Graph::Easy is 5.008 anyway | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Graph::Graph6; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 25 | # use Smart::Comments; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | our $VERSION = 8; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package Graph::Easy; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # In Graph::Easy 0.75 and 0.76, an undirected graph has_edge($v1,$v2) is | ||||||||||||||||||||||||||||
| 33 | # true only of $v1,$v2 passed in the original edge order, not as $v2,$v1. | ||||||||||||||||||||||||||||
| 34 | # That's probably a bug. | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # To get the right result, _has_edge_either() is always used for undirected | ||||||||||||||||||||||||||||
| 37 | # graphs. | ||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||
| 39 | # output $graph method | ||||||||||||||||||||||||||||
| 40 | # ------ ------ ------ | ||||||||||||||||||||||||||||
| 41 | # graph6 undirected _has_edge_either() due to has_edge() | ||||||||||||||||||||||||||||
| 42 | # graph6 directed _has_edge_either() write either way | ||||||||||||||||||||||||||||
| 43 | # digraph6 undirected _has_edge_either() to write both ways | ||||||||||||||||||||||||||||
| 44 | # digraph6 directed has_edge() | ||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | # For graph6 output, maybe some sort of Graph::Easy ->has_neighbour() could | ||||||||||||||||||||||||||||
| 47 | # answer $v1,$v2 either way round for both directed and undirected graphs. | ||||||||||||||||||||||||||||
| 48 | # Name has_neighbour() would correspond to Graph.pm neighbours(), so | ||||||||||||||||||||||||||||
| 49 | # has_neighbour() would be whether a given $v2 is among the neighbours of | ||||||||||||||||||||||||||||
| 50 | # $v1. (neighbours() are all adjacent vertices, for edges in either | ||||||||||||||||||||||||||||
| 51 | # direction.) | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub _has_edge_either { | ||||||||||||||||||||||||||||
| 54 | 39 | ▶ | my ($graph, $from, $to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_has_edge_either: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | 39 | ▶ | return $graph->has_edge($from,$to) || $graph->has_edge($to,$from); | ||||||||||||||||||||||||||
|
Condition100%3/3
$graph->has_edge($from, $to) || $graph->has_edge($to, $from)
MC/DC100%2/2
$graph->has_edge($from, $to) || $graph->has_edge($to, $from)
$graph->has_edge($from, $to)
$graph->has_edge($to, $from)
Truth table100%3/3
A || B
A= $graph->has_edge($from, $to)B= $graph->has_edge($to, $from)
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub as_graph6 { | ||||||||||||||||||||||||||||
| 59 | 31 | ▶ | my ($graph, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_graph6: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | ### Graph-Easy as_graph6() ... | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 31 | my @vertices = $graph->sorted_nodes('name'); | |||||||||||||||||||||||||||
| 63 | 31 | ▶ | my $format = $options{'format'} || 'graph6'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'format'} || 'graph6'
MC/DC100%1/1
$options{'format'} || 'graph6'
$options{'format'}
Truth table100%2/2
A
A= $options{'format'}
| |||||||||||||||||||||||||||||
| 64 | ### $format | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 31 | my @edge_options; | |||||||||||||||||||||||||||
| 67 | 31 | ▶ | if ($format eq 'sparse6') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 18 | my @edges = $graph->edges; # Graph::Easy::Edge objects | |||||||||||||||||||||||||||
| 69 | 18 | my %name_to_num = map { $vertices[$_]->name => $_ } 0 .. $#vertices; | |||||||||||||||||||||||||||
| 70 | 18 | @edges = map { [map{$name_to_num{$_->name}} $_->nodes] } @edges; | |||||||||||||||||||||||||||
| 71 | ### @edges | ||||||||||||||||||||||||||||
| 72 | 18 | @edge_options = (edge_aref => \@edges) | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 13 | ▶ | my $has_edge_method = ($graph->is_directed && $format eq 'digraph6' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$graph->is_directed and $format eq "digraph6"
MC/DC100%2/2
$graph->is_directed and $format eq "digraph6"
$graph->is_directed
$format eq "digraph6"
Truth table100%3/3
A and B
A= $graph->is_directedB= $format eq "digraph6"
| |||||||||||||||||||||||||||||
| 76 | ? 'has_edge' | ||||||||||||||||||||||||||||
| 77 | : \&_has_edge_either); | ||||||||||||||||||||||||||||
| 78 | @edge_options | ||||||||||||||||||||||||||||
| 79 | = (edge_predicate | ||||||||||||||||||||||||||||
| 80 | => sub { | ||||||||||||||||||||||||||||
| 81 | 107 | ▶ | my ($from, $to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 107 | return $graph->$has_edge_method($vertices[$from], $vertices[$to]); | |||||||||||||||||||||||||||
| 83 | 13 | }); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Graph::Graph6::write_graph | ||||||||||||||||||||||||||||
| 87 | (format => $format, | ||||||||||||||||||||||||||||
| 88 | 31 | header => $options{'header'}, | |||||||||||||||||||||||||||
| 89 | str_ref => \my $str, | ||||||||||||||||||||||||||||
| 90 | num_vertices => scalar(@vertices), | ||||||||||||||||||||||||||||
| 91 | @edge_options); | ||||||||||||||||||||||||||||
| 92 | ### $str | ||||||||||||||||||||||||||||
| 93 | 31 | return $str; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | 1; | |||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||