graphgem 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0577065307d342acdc8a4f20d2512bda58f713b
4
+ data.tar.gz: 1d7cef0581768bd4083bfb735c9f8024be4930aa
5
+ SHA512:
6
+ metadata.gz: 49f7217242f5435a5f20d6c6afa53c36187a0d143d50d06a0b31e0d53b961249465dc7442361b722acc1fae8dfc22054902ebd1912e4f508a206526032c7070a
7
+ data.tar.gz: 287b432471392b775c15c7e26b996e8d792fb36302b5a17ce204232c71eaf02a7fd40478c669693286bbfa42da8642097fa47134c03227e18295214b7c3e476d
data/lib/graph.rb ADDED
@@ -0,0 +1,96 @@
1
+ require_relative 'node/graph_node'
2
+ require_relative 'node/connection'
3
+
4
+ class Graph
5
+ attr_reader :nodes
6
+
7
+ # creates a blank graph
8
+ def initialize
9
+ @nodes = []
10
+ end
11
+
12
+ def addNode name, content
13
+ @nodes.push(GraphNode.new(name, content))
14
+ end
15
+
16
+ # returns true if added
17
+ def addEdge from, to, directed, weight=nil
18
+ return false if !isNode?(to) || !isNode?(from)
19
+ getNode(from).addConnection(to,weight)
20
+ getNode(to).addConnection(from,weight) if(!directed)
21
+ end
22
+
23
+ def isNode? name
24
+ @nodes.each do |n|
25
+ return true if n.name == name
26
+ end
27
+ return false
28
+ end
29
+
30
+ def getNode name
31
+ @nodes.each do |n|
32
+ return n if n.name == name
33
+ end
34
+ return nil
35
+ end
36
+
37
+ def getConnectionWeight from, to
38
+ return nil if !isNode?(from) || !isNode?(to)
39
+ return getNode(from).getConnection(to).weight
40
+ end
41
+
42
+ def isConnected? from, to
43
+ return nil if !isNode?(from) || !isNode?(to)
44
+ return getNode(from).connectedTo?(to)
45
+ end
46
+
47
+ # marks the node mainly so that users can run own algs and know which have been activated
48
+ def markNode name
49
+ getNode(name).marked = true
50
+ end
51
+
52
+ # removes mark from a single node
53
+ def unmarkNode name
54
+ getNode(name).marked = false
55
+ end
56
+
57
+ def refreshNodes
58
+ @nodes.each{|n| n.marked = false}
59
+ end
60
+
61
+ def convertToCSVString
62
+ result = "From,FromContent,To,ToContent,Weight\n"
63
+ @nodes.each do |n|
64
+ n.connections.each do |c|
65
+ result += n.name + ',' + n.content + ',' + c.to.name + ',' + c.to.content + ',' + c.weight + "\n"
66
+ end
67
+ end
68
+ return result
69
+ end
70
+
71
+ def convertFromCSVString input
72
+ lines = input.split("\n")
73
+ lines.delete_if{|l| l=='From,FromContent,To,ToContent,Weight'}
74
+ lines.map!{|l| l.split(',')}
75
+ lines.each do |l|
76
+ if(l.size == 5)
77
+ if(/-{0,1}[1-9]\d*/.match(l[4]))
78
+ weight = l[4].to_f
79
+ else
80
+ weight = l[4]
81
+ end
82
+ else
83
+ weight = nil
84
+ end
85
+ if(!isNode(l[0]))
86
+ addNode(l[0],l[1])
87
+ end
88
+
89
+ if(!isNode(l[2]))
90
+ addNode(l[2],l[3])
91
+ end
92
+
93
+ addEdge(l[0],l[2],weight,true)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,9 @@
1
+ class Connection
2
+ attr_accessor :weight
3
+ attr_accessor :to
4
+
5
+ def initialize to, weight
6
+ @to = to
7
+ @weight = weight
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'connection'
2
+
3
+ class GraphNode
4
+ attr_accessor :name
5
+ attr_accessor :content
6
+ attr_accessor :marked
7
+ attr_reader :connections
8
+
9
+ def initialize name, content
10
+ @name = name
11
+ @content = content
12
+ @connections = []
13
+ @marked = false
14
+ end
15
+
16
+ def addConnection to, weight
17
+ @connections.push(Connection.new(to, weight))
18
+ end
19
+
20
+ def getConnection name
21
+ @connections.each do |c|
22
+ return c if c.to.name == name
23
+ end
24
+ return nil
25
+ end
26
+
27
+ def connectedTo? name
28
+ @connections.each do |c|
29
+ return true if c.to.name == name
30
+ end
31
+ return false
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphgem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Huelsman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem to give a predefined interface for dealing with graphs
14
+ email: michael.huelsman@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/graph.rb
20
+ - lib/node/connection.rb
21
+ - lib/node/graph_node.rb
22
+ homepage: https://github.com/xLeachimx/RGraph
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: An implmentaion of a graph ADT
46
+ test_files: []