jumoku 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -1
- data/lib/jumoku.rb +2 -3
- data/lib/jumoku/builders/extended.rb +15 -23
- data/lib/jumoku/builders/raw_directed_tree.rb +15 -0
- data/lib/jumoku/builders/raw_undirected_tree.rb +15 -0
- data/lib/jumoku/builders/shared.rb +2 -5
- data/lib/jumoku/support/ruby_compatibility.rb +19 -0
- data/lib/jumoku/version.rb +1 -1
- data/spec/arborescence_spec.rb +14 -0
- data/spec/behaviors/core_tree.rb +281 -0
- data/spec/behaviors/extended.rb +530 -0
- data/spec/raw_directed_tree_spec.rb +14 -0
- data/spec/raw_undirected_tree_spec.rb +9 -310
- data/spec/spec_helper.rb +2 -0
- data/spec/tree_spec.rb +8 -535
- metadata +21 -86
- data/lib/jumoku/tree_api.rb +0 -27
- data/vendor/git/plexus/CREDITS.md +0 -31
- data/vendor/git/plexus/Gemfile +0 -3
- data/vendor/git/plexus/Gemfile.lock +0 -28
- data/vendor/git/plexus/LICENSE +0 -37
- data/vendor/git/plexus/README.md +0 -208
- data/vendor/git/plexus/Rakefile +0 -25
- data/vendor/git/plexus/TODO.md +0 -20
- data/vendor/git/plexus/VERSION +0 -1
- data/vendor/git/plexus/examples/graph_self.rb +0 -56
- data/vendor/git/plexus/examples/module_graph.jpg +0 -0
- data/vendor/git/plexus/examples/module_graph.rb +0 -14
- data/vendor/git/plexus/examples/self_graph.jpg +0 -0
- data/vendor/git/plexus/examples/visualize.jpg +0 -0
- data/vendor/git/plexus/examples/visualize.rb +0 -10
- data/vendor/git/plexus/lib/plexus.rb +0 -90
- data/vendor/git/plexus/lib/plexus/adjacency_graph.rb +0 -224
- data/vendor/git/plexus/lib/plexus/arc.rb +0 -59
- data/vendor/git/plexus/lib/plexus/arc_number.rb +0 -52
- data/vendor/git/plexus/lib/plexus/biconnected.rb +0 -84
- data/vendor/git/plexus/lib/plexus/chinese_postman.rb +0 -91
- data/vendor/git/plexus/lib/plexus/classes/graph_classes.rb +0 -28
- data/vendor/git/plexus/lib/plexus/common.rb +0 -63
- data/vendor/git/plexus/lib/plexus/comparability.rb +0 -63
- data/vendor/git/plexus/lib/plexus/directed_graph.rb +0 -78
- data/vendor/git/plexus/lib/plexus/directed_graph/algorithms.rb +0 -95
- data/vendor/git/plexus/lib/plexus/directed_graph/distance.rb +0 -167
- data/vendor/git/plexus/lib/plexus/dot.rb +0 -94
- data/vendor/git/plexus/lib/plexus/edge.rb +0 -36
- data/vendor/git/plexus/lib/plexus/ext.rb +0 -79
- data/vendor/git/plexus/lib/plexus/graph.rb +0 -626
- data/vendor/git/plexus/lib/plexus/graph_api.rb +0 -35
- data/vendor/git/plexus/lib/plexus/labels.rb +0 -113
- data/vendor/git/plexus/lib/plexus/maximum_flow.rb +0 -77
- data/vendor/git/plexus/lib/plexus/ruby_compatibility.rb +0 -17
- data/vendor/git/plexus/lib/plexus/search.rb +0 -510
- data/vendor/git/plexus/lib/plexus/strong_components.rb +0 -93
- data/vendor/git/plexus/lib/plexus/support/support.rb +0 -9
- data/vendor/git/plexus/lib/plexus/undirected_graph.rb +0 -56
- data/vendor/git/plexus/lib/plexus/undirected_graph/algorithms.rb +0 -90
- data/vendor/git/plexus/lib/plexus/version.rb +0 -6
- data/vendor/git/plexus/plexus.gemspec +0 -24
- data/vendor/git/plexus/spec/biconnected_spec.rb +0 -27
- data/vendor/git/plexus/spec/chinese_postman_spec.rb +0 -27
- data/vendor/git/plexus/spec/community_spec.rb +0 -44
- data/vendor/git/plexus/spec/complement_spec.rb +0 -27
- data/vendor/git/plexus/spec/digraph_distance_spec.rb +0 -121
- data/vendor/git/plexus/spec/digraph_spec.rb +0 -339
- data/vendor/git/plexus/spec/dot_spec.rb +0 -48
- data/vendor/git/plexus/spec/edge_spec.rb +0 -158
- data/vendor/git/plexus/spec/inspection_spec.rb +0 -38
- data/vendor/git/plexus/spec/multi_edge_spec.rb +0 -32
- data/vendor/git/plexus/spec/neighborhood_spec.rb +0 -36
- data/vendor/git/plexus/spec/properties_spec.rb +0 -146
- data/vendor/git/plexus/spec/search_spec.rb +0 -227
- data/vendor/git/plexus/spec/spec.opts +0 -4
- data/vendor/git/plexus/spec/spec_helper.rb +0 -59
- data/vendor/git/plexus/spec/strong_components_spec.rb +0 -61
- data/vendor/git/plexus/spec/triangulated_spec.rb +0 -125
- data/vendor/git/plexus/spec/undirected_graph_spec.rb +0 -220
- data/vendor/git/plexus/vendor/priority-queue/CHANGELOG +0 -33
- data/vendor/git/plexus/vendor/priority-queue/Makefile +0 -140
- data/vendor/git/plexus/vendor/priority-queue/README +0 -133
- data/vendor/git/plexus/vendor/priority-queue/benchmark/dijkstra.rb +0 -171
- data/vendor/git/plexus/vendor/priority-queue/compare_comments.rb +0 -49
- data/vendor/git/plexus/vendor/priority-queue/doc/c-vs-rb.png +0 -0
- data/vendor/git/plexus/vendor/priority-queue/doc/compare_big.gp +0 -14
- data/vendor/git/plexus/vendor/priority-queue/doc/compare_big.png +0 -0
- data/vendor/git/plexus/vendor/priority-queue/doc/compare_small.gp +0 -15
- data/vendor/git/plexus/vendor/priority-queue/doc/compare_small.png +0 -0
- data/vendor/git/plexus/vendor/priority-queue/doc/results.csv +0 -37
- data/vendor/git/plexus/vendor/priority-queue/ext/priority_queue/CPriorityQueue/extconf.rb +0 -2
- data/vendor/git/plexus/vendor/priority-queue/ext/priority_queue/CPriorityQueue/priority_queue.c +0 -947
- data/vendor/git/plexus/vendor/priority-queue/lib/priority_queue.rb +0 -14
- data/vendor/git/plexus/vendor/priority-queue/lib/priority_queue/c_priority_queue.rb +0 -1
- data/vendor/git/plexus/vendor/priority-queue/lib/priority_queue/poor_priority_queue.rb +0 -46
- data/vendor/git/plexus/vendor/priority-queue/lib/priority_queue/ruby_priority_queue.rb +0 -526
- data/vendor/git/plexus/vendor/priority-queue/priority_queue.so +0 -0
- data/vendor/git/plexus/vendor/priority-queue/setup.rb +0 -1551
- data/vendor/git/plexus/vendor/priority-queue/test/priority_queue_test.rb +0 -371
- data/vendor/git/plexus/vendor/rdot.rb +0 -360
data/vendor/git/plexus/Rakefile
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'rubygems'
|
3
|
-
begin
|
4
|
-
require 'bundler/setup'
|
5
|
-
rescue LoadError
|
6
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
-
end
|
8
|
-
|
9
|
-
require 'rake'
|
10
|
-
require 'rake/rdoctask'
|
11
|
-
|
12
|
-
require 'rspec/core'
|
13
|
-
require 'rspec/core/rake_task'
|
14
|
-
|
15
|
-
RSpec::Core::RakeTask.new(:spec)
|
16
|
-
|
17
|
-
task :default => :spec
|
18
|
-
|
19
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
20
|
-
rdoc.rdoc_dir = 'rdoc'
|
21
|
-
rdoc.title = 'Plexus'
|
22
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
-
rdoc.rdoc_files.include('README.rdoc')
|
24
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
-
end
|
data/vendor/git/plexus/TODO.md
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# TODO
|
2
|
-
|
3
|
-
## From the current Graphy project
|
4
|
-
|
5
|
-
* code cleanup: syntax, typo
|
6
|
-
* YARD documentation
|
7
|
-
* hunt for buggy behaviors
|
8
|
-
* a solid helper to check graphy object type in replacement of the current `is_a?` hack
|
9
|
-
* have the `[]` class method propagate through nested module inclusions [see this topic](http://www.ruby-forum.com/topic/68638)
|
10
|
-
* [Incremental heuristic search algorithms](http://en.wikipedia.org/wiki/Incremental_heuristic_search)?
|
11
|
-
|
12
|
-
## From GRATR
|
13
|
-
|
14
|
-
The following list was present in GRATR, and items may (or may not) be added to this library:
|
15
|
-
|
16
|
-
* Primal Dual for combinatorial optimization problems
|
17
|
-
* Min-Max Flow
|
18
|
-
* Near optimal traveling salesman problem
|
19
|
-
* Orientation of undirected graphs
|
20
|
-
* Undirected graphs from ActiveRecord
|
data/vendor/git/plexus/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.5.2
|
@@ -1,56 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby -I../lib
|
2
|
-
|
3
|
-
require 'gratr'
|
4
|
-
require 'gratr/dot'
|
5
|
-
|
6
|
-
include GRATR
|
7
|
-
|
8
|
-
# This program gives an example of dynamic analysis of a program's call stack,
|
9
|
-
# that exports to dot and creates a jpg visualization of the call diagram.
|
10
|
-
|
11
|
-
class GraphSelf
|
12
|
-
|
13
|
-
# Setup some data to call Dijkstra's Algorithm
|
14
|
-
def initialize
|
15
|
-
@d = Digraph[ [:a,:b] => 9, [:a,:e] => 3,
|
16
|
-
[:b,:c] => 2, [:b,:e] => 6,
|
17
|
-
[:c,:d] => 1,
|
18
|
-
[:d,:c] => 2,
|
19
|
-
[:e,:b] => 2, [:e,:f] => 1,
|
20
|
-
[:f,:c] => 2, [:f,:d] => 7, [:f,:e] => 2 ]
|
21
|
-
|
22
|
-
@call_stack = []
|
23
|
-
@call_graph = Digraph.new
|
24
|
-
end
|
25
|
-
|
26
|
-
# Get the call graph variable
|
27
|
-
def call_graph() @call_graph; end
|
28
|
-
|
29
|
-
# Turn capturing of call graph on
|
30
|
-
def capture_func
|
31
|
-
Proc.new do |event, f, l, id, b, klass|
|
32
|
-
# Only interested in the GRATR library itself
|
33
|
-
if ( klass.to_s =~ /GRATR/ )
|
34
|
-
case event.to_s
|
35
|
-
when /call/
|
36
|
-
method = "#{klass.to_s.split('::')[1]}.#{id}" # Removes GRATR::
|
37
|
-
@call_graph.add_edge!(@call_stack[-1],method) if @call_stack[-1]
|
38
|
-
@call_stack.push(method)
|
39
|
-
when /return/ : @call_stack.pop
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Run a capture of the call graph for Dijkstra's algorithm
|
46
|
-
def run
|
47
|
-
set_trace_func capture_func
|
48
|
-
@d.dijkstras_algorithm(:a)
|
49
|
-
set_trace_func nil
|
50
|
-
self
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
# Run a capture and generate the resulting jpg file
|
56
|
-
GraphSelf.new.run.call_graph.write_to_graphic_file('jpg','self_graph')
|
Binary file
|
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby -I../lib
|
2
|
-
|
3
|
-
require 'gratr'
|
4
|
-
require 'gratr/dot'
|
5
|
-
|
6
|
-
include GRATR
|
7
|
-
|
8
|
-
module_graph=Digraph.new
|
9
|
-
ObjectSpace.each_object(Module) do |m|
|
10
|
-
m.ancestors.each {|a| module_graph.add_edge!(m,a) if m != a}
|
11
|
-
end
|
12
|
-
|
13
|
-
gv = module_graph.vertices.select {|v| v.to_s.match(/GRATR/) && v.to_s != 'GRATR'}
|
14
|
-
module_graph.induced_subgraph(gv).write_to_graphic_file('jpg','module_graph')
|
Binary file
|
Binary file
|
@@ -1,90 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2006 Shawn Patrick Garbett
|
3
|
-
# Copyright (c) 2002,2004,2005 by Horst Duchene
|
4
|
-
#
|
5
|
-
# Redistribution and use in source and binary forms, with or without modification,
|
6
|
-
# are permitted provided that the following conditions are met:
|
7
|
-
#
|
8
|
-
# * Redistributions of source code must retain the above copyright notice(s),
|
9
|
-
# this list of conditions and the following disclaimer.
|
10
|
-
# * Redistributions in binary form must reproduce the above copyright notice,
|
11
|
-
# this list of conditions and the following disclaimer in the documentation
|
12
|
-
# and/or other materials provided with the distribution.
|
13
|
-
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
14
|
-
# may be used to endorse or promote products derived from this software
|
15
|
-
# without specific prior written permission.
|
16
|
-
#
|
17
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
18
|
-
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
-
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
21
|
-
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
-
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
23
|
-
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
24
|
-
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
25
|
-
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26
|
-
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
-
#++
|
28
|
-
|
29
|
-
require 'set'
|
30
|
-
|
31
|
-
module Plexus
|
32
|
-
# Plexus internals: graph builders and additionnal behaviors
|
33
|
-
autoload :GraphAPI, 'plexus/graph_api'
|
34
|
-
|
35
|
-
autoload :GraphBuilder, 'plexus/graph'
|
36
|
-
autoload :AdjacencyGraphBuilder, 'plexus/adjacency_graph'
|
37
|
-
|
38
|
-
autoload :DirectedGraphBuilder, 'plexus/directed_graph'
|
39
|
-
autoload :DigraphBuilder, 'plexus/directed_graph'
|
40
|
-
autoload :DirectedPseudoGraphBuilder, 'plexus/directed_graph'
|
41
|
-
autoload :DirectedMultiGraphBuilder, 'plexus/directed_graph'
|
42
|
-
|
43
|
-
autoload :UndirectedGraphBuilder, 'plexus/undirected_graph'
|
44
|
-
autoload :UndirectedPseudoGraphBuilder, 'plexus/undirected_graph'
|
45
|
-
autoload :UndirectedMultiGraphBuilder, 'plexus/undirected_graph'
|
46
|
-
|
47
|
-
autoload :Arc, 'plexus/arc'
|
48
|
-
autoload :ArcNumber, 'plexus/arc_number'
|
49
|
-
autoload :Biconnected, 'plexus/biconnected'
|
50
|
-
autoload :ChinesePostman, 'plexus/chinese_postman'
|
51
|
-
autoload :Common, 'plexus/common'
|
52
|
-
autoload :Comparability, 'plexus/comparability'
|
53
|
-
|
54
|
-
autoload :Dot, 'plexus/dot'
|
55
|
-
autoload :Edge, 'plexus/edge'
|
56
|
-
autoload :Labels, 'plexus/labels'
|
57
|
-
autoload :MaximumFlow, 'plexus/maximum_flow'
|
58
|
-
#autoload :Rdot, 'plexus/dot'
|
59
|
-
autoload :Search, 'plexus/search'
|
60
|
-
autoload :StrongComponents, 'plexus/strong_components'
|
61
|
-
|
62
|
-
# Plexus classes
|
63
|
-
autoload :AdjacencyGraph, 'plexus/classes/graph_classes'
|
64
|
-
autoload :DirectedGraph, 'plexus/classes/graph_classes'
|
65
|
-
autoload :Digraph, 'plexus/classes/graph_classes'
|
66
|
-
autoload :DirectedPseudoGraph, 'plexus/classes/graph_classes'
|
67
|
-
autoload :DirectedMultiGraph, 'plexus/classes/graph_classes'
|
68
|
-
autoload :UndirectedGraph, 'plexus/classes/graph_classes'
|
69
|
-
autoload :UndirectedPseudoGraph, 'plexus/classes/graph_classes'
|
70
|
-
autoload :UndirectedMultiGraph, 'plexus/classes/graph_classes'
|
71
|
-
|
72
|
-
# ruby stdlib extensions
|
73
|
-
require 'plexus/ext'
|
74
|
-
# ruby 1.8.x/1.9.x compatibility
|
75
|
-
require 'plexus/ruby_compatibility'
|
76
|
-
end
|
77
|
-
|
78
|
-
# Vendored libraries
|
79
|
-
|
80
|
-
require 'pathname'
|
81
|
-
path = Pathname.new(__FILE__)
|
82
|
-
$LOAD_PATH.unshift(path + '../../vendor') # http://ruby.brian-amberg.de/priority-queue/
|
83
|
-
$LOAD_PATH.unshift(path + '../../vendor/priority-queue/lib')
|
84
|
-
|
85
|
-
require 'rdot'
|
86
|
-
require 'facets/hash'
|
87
|
-
|
88
|
-
require 'priority_queue/ruby_priority_queue'
|
89
|
-
PriorityQueue = RubyPriorityQueue
|
90
|
-
|
@@ -1,224 +0,0 @@
|
|
1
|
-
module Plexus
|
2
|
-
|
3
|
-
# This module provides the basic routines needed to implement the specialized builders:
|
4
|
-
# {DigraphBuilder}, {UndirectedGraphBuilder}, {DirectedPseudoGraphBuilder},
|
5
|
-
# {UndirectedPseudoGraphBuilder}, {DirectedMultiGraphBuilder} and {UndirectedMultiGraphBuilder}
|
6
|
-
# modules, each of them streamlining {AdjacencyGraphBuilder}'s behavior. Those
|
7
|
-
# implementations rely on the {GraphBuilder}, under the control of the {GraphAPI}.
|
8
|
-
module AdjacencyGraphBuilder
|
9
|
-
|
10
|
-
# Defines a useful `push` -> `add` alias for arrays.
|
11
|
-
class ArrayWithAdd < Array
|
12
|
-
alias add push
|
13
|
-
end
|
14
|
-
|
15
|
-
# This method is called by the specialized implementations
|
16
|
-
# upon graph creation.
|
17
|
-
#
|
18
|
-
# Initialization parameters can include:
|
19
|
-
#
|
20
|
-
# * an array of edges to add
|
21
|
-
# * one or several graphs to copy (will be merged if multiple)
|
22
|
-
# * `:parallel_edges` denotes that duplicate edges are allowed
|
23
|
-
# * `:loops denotes` that loops are allowed
|
24
|
-
#
|
25
|
-
# @param *params [Hash] the initialization parameters
|
26
|
-
def implementation_initialize(*params)
|
27
|
-
@vertex_dict = Hash.new
|
28
|
-
clear_all_labels
|
29
|
-
|
30
|
-
# FIXME: could definitely make use of the activesupport helper
|
31
|
-
# extract_options! and facets' reverse_merge! technique
|
32
|
-
# to handle parameters
|
33
|
-
args = (params.pop if params.last.is_a? Hash) || {}
|
34
|
-
|
35
|
-
# Basic configuration of adjacency.
|
36
|
-
@allow_loops = args[:loops] || false
|
37
|
-
@parallel_edges = args[:parallel_edges] || false
|
38
|
-
@edgelist_class = @parallel_edges ? ArrayWithAdd : Set
|
39
|
-
if @parallel_edges
|
40
|
-
@edge_number = Hash.new
|
41
|
-
@next_edge_number = 0
|
42
|
-
end
|
43
|
-
|
44
|
-
# Copy any given graph into this graph.
|
45
|
-
params.select { |p| p.is_a? Plexus::GraphBuilder }.each do |g|
|
46
|
-
g.edges.each do |e|
|
47
|
-
add_edge!(e)
|
48
|
-
edge_label_set(e, edge_label(e)) if edge_label(e)
|
49
|
-
end
|
50
|
-
g.vertices.each do |v|
|
51
|
-
add_vertex!(v)
|
52
|
-
vertex_label_set(v, vertex_label(v)) if vertex_label(v)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# Add all array edges specified.
|
57
|
-
params.select { |p| p.is_a? Array }.each do |a|
|
58
|
-
0.step(a.size-1, 2) { |i| add_edge!(a[i], a[i+1]) }
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
# Returns true if v is a vertex of this Graph
|
63
|
-
# (an "O(1)" implementation of `vertex?`).
|
64
|
-
#
|
65
|
-
# @param [vertex] v
|
66
|
-
# @return [Boolean]
|
67
|
-
def vertex?(v)
|
68
|
-
@vertex_dict.has_key?(v)
|
69
|
-
end
|
70
|
-
|
71
|
-
# Returns true if [u,v] or u is an {Arc}
|
72
|
-
# (an "O(1)" implementation of `edge?`).
|
73
|
-
#
|
74
|
-
# @param [vertex] u
|
75
|
-
# @param [vertex] v (nil)
|
76
|
-
# @return [Boolean]
|
77
|
-
def edge?(u, v = nil)
|
78
|
-
u, v = u.source, u.target if u.is_a? Plexus::Arc
|
79
|
-
vertex?(u) and @vertex_dict[u].include?(v)
|
80
|
-
end
|
81
|
-
|
82
|
-
# Adds a vertex to the graph with an optional label.
|
83
|
-
#
|
84
|
-
# @param [vertex(Object)] vertex any kind of Object can act as a vertex
|
85
|
-
# @param [#to_s] label (nil)
|
86
|
-
def add_vertex!(vertex, label = nil)
|
87
|
-
@vertex_dict[vertex] ||= @edgelist_class.new
|
88
|
-
self[vertex] = label if label
|
89
|
-
self
|
90
|
-
end
|
91
|
-
|
92
|
-
# Adds an edge to the graph.
|
93
|
-
#
|
94
|
-
# Can be called in two basic ways, label is optional:
|
95
|
-
# @overload add_edge!(arc)
|
96
|
-
# Using an explicit {Arc}
|
97
|
-
# @param [Arc] arc an {Arc}[source, target, label = nil] object
|
98
|
-
# @return [AdjacencyGraph] `self`
|
99
|
-
# @overload add_edge!(source, target, label = nil)
|
100
|
-
# Using vertices to define an arc implicitly
|
101
|
-
# @param [vertex] u
|
102
|
-
# @param [vertex] v (nil)
|
103
|
-
# @param [Label] l (nil)
|
104
|
-
# @param [Integer] n (nil) {Arc arc} number of `(u, v)` (if `nil` and if `u`
|
105
|
-
# has an {ArcNumber}, then it will be used)
|
106
|
-
# @return [AdjacencyGraph] `self`
|
107
|
-
def add_edge!(u, v = nil, l = nil, n = nil)
|
108
|
-
n = u.number if u.class.include? ArcNumber and n.nil?
|
109
|
-
u, v, l = u.source, u.target, u.label if u.is_a? Plexus::Arc
|
110
|
-
|
111
|
-
return self if not @allow_loops and u == v
|
112
|
-
|
113
|
-
n = (@next_edge_number += 1) unless n if @parallel_edges
|
114
|
-
add_vertex!(u)
|
115
|
-
add_vertex!(v)
|
116
|
-
@vertex_dict[u].add(v)
|
117
|
-
(@edge_number[u] ||= @edgelist_class.new).add(n) if @parallel_edges
|
118
|
-
|
119
|
-
unless directed?
|
120
|
-
@vertex_dict[v].add(u)
|
121
|
-
(@edge_number[v] ||= @edgelist_class.new).add(n) if @parallel_edges
|
122
|
-
end
|
123
|
-
|
124
|
-
self[n ? edge_class[u,v,n] : edge_class[u,v]] = l if l
|
125
|
-
self
|
126
|
-
end
|
127
|
-
|
128
|
-
# Removes a given vertex from the graph.
|
129
|
-
#
|
130
|
-
# @param [vertex] v
|
131
|
-
# @return [AdjacencyGraph] `self`
|
132
|
-
def remove_vertex!(v)
|
133
|
-
# FIXME This is broken for multi graphs
|
134
|
-
@vertex_dict.delete(v)
|
135
|
-
@vertex_dict.each_value { |adjList| adjList.delete(v) }
|
136
|
-
@vertex_dict.keys.each do |u|
|
137
|
-
delete_label(edge_class[u,v])
|
138
|
-
delete_label(edge_class[v,u])
|
139
|
-
end
|
140
|
-
delete_label(v)
|
141
|
-
self
|
142
|
-
end
|
143
|
-
|
144
|
-
# Removes an edge from the graph.
|
145
|
-
#
|
146
|
-
# Can be called with both source and target as vertex,
|
147
|
-
# or with source and object of {Plexus::Arc} derivation.
|
148
|
-
#
|
149
|
-
# @overload remove_edge!(a)
|
150
|
-
# @param [Plexus::Arc] a
|
151
|
-
# @return [AdjacencyGraph] `self`
|
152
|
-
# @raise [ArgumentError] if parallel edges are enabled
|
153
|
-
# @overload remove_edge!(u, v)
|
154
|
-
# @param [vertex] u
|
155
|
-
# @param [vertex] v
|
156
|
-
# @return [AdjacencyGraph] `self`
|
157
|
-
# @raise [ArgumentError] if parallel edges are enabled and the {ArcNumber} of `u` is zero
|
158
|
-
def remove_edge!(u, v = nil)
|
159
|
-
unless u.is_a? Plexus::Arc
|
160
|
-
raise ArgumentError if @parallel_edges
|
161
|
-
u = edge_class[u,v]
|
162
|
-
end
|
163
|
-
raise ArgumentError if @parallel_edges and (u.number || 0) == 0
|
164
|
-
return self unless @vertex_dict[u.source] # It doesn't exist
|
165
|
-
delete_label(u) # Get rid of label
|
166
|
-
if @parallel_edges
|
167
|
-
index = @edge_number[u.source].index(u.number)
|
168
|
-
raise NoArcError unless index
|
169
|
-
@vertex_dict[u.source].delete_at(index)
|
170
|
-
@edge_number[u.source].delete_at(index)
|
171
|
-
else
|
172
|
-
@vertex_dict[u.source].delete(u.target)
|
173
|
-
end
|
174
|
-
self
|
175
|
-
end
|
176
|
-
|
177
|
-
# Returns an array of vertices that the graph has.
|
178
|
-
#
|
179
|
-
# @return [Array] graph's vertices
|
180
|
-
def vertices
|
181
|
-
@vertex_dict.keys
|
182
|
-
end
|
183
|
-
|
184
|
-
# Returns an array of edges, most likely of class {Arc} or {Edge} depending
|
185
|
-
# upon the type of graph.
|
186
|
-
#
|
187
|
-
# @return [Array]
|
188
|
-
def edges
|
189
|
-
@vertex_dict.keys.inject(Set.new) do |a,v|
|
190
|
-
if @parallel_edges and @edge_number[v]
|
191
|
-
@vertex_dict[v].zip(@edge_number[v]).each do |w|
|
192
|
-
s, t, n = v, w[0], w[1]
|
193
|
-
a.add(edge_class[s, t, n, edge_label(s, t, n)])
|
194
|
-
end
|
195
|
-
else
|
196
|
-
@vertex_dict[v].each do |w|
|
197
|
-
a.add(edge_class[v, w, edge_label(v, w)])
|
198
|
-
end
|
199
|
-
end
|
200
|
-
a
|
201
|
-
end.to_a
|
202
|
-
end
|
203
|
-
|
204
|
-
# FIXME, EFFED UP (but why?)
|
205
|
-
#
|
206
|
-
# @fixme
|
207
|
-
def adjacent(x, options = {})
|
208
|
-
options[:direction] ||= :out
|
209
|
-
if !x.is_a?(Plexus::Arc) and (options[:direction] == :out || !directed?)
|
210
|
-
if options[:type] == :edges
|
211
|
-
i = -1
|
212
|
-
@parallel_edges ?
|
213
|
-
@vertex_dict[x].map { |v| e = edge_class[x, v, @edge_number[x][i+=1]]; e.label = self[e]; e } :
|
214
|
-
@vertex_dict[x].map { |v| e = edge_class[x, v]; e.label = self[e]; e }
|
215
|
-
else
|
216
|
-
@vertex_dict[x].to_a
|
217
|
-
end
|
218
|
-
else
|
219
|
-
graph_adjacent(x,options)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
end # Adjacency Graph
|
224
|
-
end # Plexus
|