tangle 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 745b0e3e2a81c5d3f7c79889774a90f0c6ec417cc9e1b55f9f716ad47a82a95e
4
- data.tar.gz: b4a88c9eab9cfe842685510fae6a39fc48f39eda0a6e70c2325fa652b4a20039
3
+ metadata.gz: 68b534152678b108e1d5ad38d3947ac056fc1218a01f7bab10f73dcb0f6868bc
4
+ data.tar.gz: 9ebf86f0191780d2b49f69f8b3e2cf8a0b57ac9117ff7d5dff57a0bfd52fa346
5
5
  SHA512:
6
- metadata.gz: 349814a3c91df5236c46aade369f5f1b5c6b90d3a555f91366986b465592492b00037f9b9e0ac39f8af2a369d78cf473d6582bfbdc9b79935280ac15c2969088
7
- data.tar.gz: b8182efcff132d3202e291787db87418ed93982dac257d42f5f640187fe456fb6ab01c0ffd570157910dbdd1aae70a4d755286e218b1d0faf6c00fc3a342fe7e
6
+ metadata.gz: cdb0d5204e4e6fc4de0d1a54f92fb73d3808322b7478de49fb9c571998fb18c49f400cbff042c4bd38218d2c966a9e7ca5ac697b7120d26bd46193e157bca4a6
7
+ data.tar.gz: 2eacf5cfb13389a359566563d6d72fae9ee86d1d373030b8677f6674d38cdec2663b5d5dfbb66a75aac6018ea41539691c75fcbee93deb4cefe48bde7cf7b591
data/lib/tangle/graph.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'tangle/mixin'
1
2
  require 'tangle/vertex'
2
3
  require 'tangle/edge'
3
4
 
@@ -8,6 +9,8 @@ module Tangle
8
9
  class Graph
9
10
  Edge = Tangle::Edge
10
11
 
12
+ include Tangle::Mixin::Connectedness::Graph
13
+
11
14
  # Initialize a new graph, optionally preloading it with vertices and edges
12
15
  #
13
16
  # Graph.new() => Graph
@@ -0,0 +1,72 @@
1
+ module Tangle
2
+ module Mixin
3
+ module Connectedness
4
+ #
5
+ # Mixin for adding connectedness to a graph
6
+ #
7
+ module Graph
8
+ # Get the largest connected subgraph for a vertex.
9
+ # Also aliased as :component and :connected_component
10
+ #
11
+ # connected_subgraph(vertex) => Graph
12
+ #
13
+ def connected_subgraph(vertex)
14
+ subgraph { |other| vertex.connected?(other) }
15
+ end
16
+ alias component connected_subgraph
17
+ alias connected_component connected_subgraph
18
+
19
+ # Get the largest subgraph that is not connected to a vertex, or what's
20
+ # left after removing the connected subgraph.
21
+ #
22
+ def disconnected_subgraph(vertex)
23
+ subgraph { |other| !vertex.connected?(other) }
24
+ end
25
+
26
+ # A graph is connected if all vertices are connected to all vertices
27
+ # An empty graph is disconnected.
28
+ #
29
+ def connected?
30
+ return false if vertices.empty?
31
+
32
+ vertices.combination(2).all? do |pair|
33
+ this, that = pair.to_a
34
+ this.connected?(that)
35
+ end
36
+ end
37
+
38
+ # A graph is disconnected if any vertex is not connected to all other.
39
+ # An empty graph is disconnected.
40
+ #
41
+ def disconnected?
42
+ !connected?
43
+ end
44
+ end
45
+
46
+ #
47
+ # Mixin for adding connectedness to a vertex
48
+ #
49
+ module Vertex
50
+ # Two vertices are connected if there is a path between them,
51
+ # and a vertex is connected to itself.
52
+ #
53
+ def connected?(other)
54
+ raise GraphError unless @graph == other.graph
55
+ return true if self == other
56
+
57
+ connected_excluding?(other, Set[self])
58
+ end
59
+
60
+ protected
61
+
62
+ def connected_excluding?(other, history)
63
+ return true if adjacent?(other)
64
+
65
+ (neighbours - history).any? do |vertex|
66
+ vertex.connected_excluding?(other, history << self)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1 @@
1
+ require 'tangle/mixin/connectedness'
data/lib/tangle/vertex.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'delegate'
2
2
  require 'pp'
3
+ require 'tangle/mixin'
3
4
 
4
5
  module Tangle
5
6
  #
@@ -7,6 +8,7 @@ module Tangle
7
8
  #
8
9
  class Vertex < SimpleDelegator
9
10
  include PP::ObjectMixin
11
+ include Tangle::Mixin::Connectedness::Vertex
10
12
 
11
13
  # Create a new vertex
12
14
  #
@@ -55,6 +57,12 @@ module Tangle
55
57
  @graph.edges { |edge| edge.include? self }
56
58
  end
57
59
 
60
+ # Return the set of adjacent vertices
61
+ #
62
+ def neighbours
63
+ Set.new(edges.map { |edge| edge.walk(self) })
64
+ end
65
+
58
66
  # If two vertices have the same vertex_id, they have the same value
59
67
  #
60
68
  def ==(other)
@@ -73,6 +81,13 @@ module Tangle
73
81
  @object_id == other.object_id
74
82
  end
75
83
 
84
+ # Two vertices are adjacent if there is an edge between them
85
+ #
86
+ def adjacent?(other)
87
+ raise GraphError unless @graph == other.graph
88
+ edges.any? { |edge| edge.walk(self) == other }
89
+ end
90
+
76
91
  attr_reader :graph
77
92
  attr_reader :name
78
93
  attr_reader :delegate
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tangle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calle Englund
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-04 00:00:00.000000000 Z
11
+ date: 2018-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git-version-bump
@@ -102,6 +102,8 @@ files:
102
102
  - lib/tangle/edge.rb
103
103
  - lib/tangle/errors.rb
104
104
  - lib/tangle/graph.rb
105
+ - lib/tangle/mixin.rb
106
+ - lib/tangle/mixin/connectedness.rb
105
107
  - lib/tangle/simple/edge.rb
106
108
  - lib/tangle/simple/graph.rb
107
109
  - lib/tangle/version.rb