graphgem 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graph.rb +10 -15
- data/lib/node/graph_node.rb +4 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77d8439a0f689df1237faa891b682d77e47a5424
|
4
|
+
data.tar.gz: 6a2024130d9f5d19ae664389283957eabc04ea70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df2e5fa3017a80b7a3928e9e3e4b463e56b83f595cbd1e711df03c8e0f20465957d9498355860b0e9c8531c98eae25ec74572c4253edb3f98c2817ec8be6b403
|
7
|
+
data.tar.gz: fbe05350a84d67b59c1f876e5372a593b71018392bef12911030c7df03871857054e740d8f6380bca20bc0b669b828cbdab1a918896f4e298e2183ff7de41b03
|
data/lib/graph.rb
CHANGED
@@ -6,36 +6,31 @@ class Graph
|
|
6
6
|
|
7
7
|
# creates a blank graph
|
8
8
|
def initialize
|
9
|
-
@nodes =
|
9
|
+
@nodes = {}
|
10
10
|
end
|
11
11
|
|
12
12
|
def addNode name, content
|
13
|
-
@nodes
|
13
|
+
@nodes[name] = GraphNode.new(name, content)
|
14
14
|
end
|
15
15
|
|
16
16
|
# returns true if added
|
17
17
|
def addEdge from, to, directed, weight=nil
|
18
18
|
return false if !isNode?(to) || !isNode?(from)
|
19
|
-
getNode(from).addConnection(to,weight)
|
20
|
-
getNode(to).addConnection(from,weight) if(!directed)
|
19
|
+
getNode(from).addConnection(getNode(to),weight)
|
20
|
+
getNode(to).addConnection(getNode(from),weight) if(!directed)
|
21
21
|
end
|
22
22
|
|
23
23
|
def isNode? name
|
24
|
-
@nodes
|
25
|
-
return true if n.name == name
|
26
|
-
end
|
27
|
-
return false
|
24
|
+
return @nodes[name] != nil
|
28
25
|
end
|
29
26
|
|
30
27
|
def getNode name
|
31
|
-
@nodes
|
32
|
-
return n if n.name == name
|
33
|
-
end
|
34
|
-
return nil
|
28
|
+
return @nodes[name]
|
35
29
|
end
|
36
30
|
|
37
31
|
def getConnectionWeight from, to
|
38
32
|
return nil if !isNode?(from) || !isNode?(to)
|
33
|
+
return nil if !isConnected?(from,to)
|
39
34
|
return getNode(from).getConnection(to).weight
|
40
35
|
end
|
41
36
|
|
@@ -65,9 +60,9 @@ class Graph
|
|
65
60
|
|
66
61
|
def convertToCSVString
|
67
62
|
result = "From,FromContent,To,ToContent,Weight\n"
|
68
|
-
@nodes.each do |n|
|
69
|
-
n.connections.each do |c|
|
70
|
-
result += n.name + ',' + n.content + ',' + c.to.name + ',' + c.to.content + ',' + c.weight + "\n"
|
63
|
+
@nodes.each do |key, n|
|
64
|
+
n.connections.each do |key, c|
|
65
|
+
result += n.name + ',' + n.content + ',' + c.to.name + ',' + c.to.content + ',' + c.weight.to_s + "\n"
|
71
66
|
end
|
72
67
|
end
|
73
68
|
return result
|
data/lib/node/graph_node.rb
CHANGED
@@ -9,25 +9,19 @@ class GraphNode
|
|
9
9
|
def initialize name, content
|
10
10
|
@name = name
|
11
11
|
@content = content
|
12
|
-
@connections =
|
12
|
+
@connections = {}
|
13
13
|
@marked = false
|
14
14
|
end
|
15
15
|
|
16
16
|
def addConnection to, weight
|
17
|
-
@connections
|
17
|
+
@connections[to] = Connection.new(to,weight)
|
18
18
|
end
|
19
19
|
|
20
20
|
def getConnection name
|
21
|
-
@connections
|
22
|
-
return c if c.to.name == name
|
23
|
-
end
|
24
|
-
return nil
|
21
|
+
return @connections[name]
|
25
22
|
end
|
26
23
|
|
27
24
|
def connectedTo? name
|
28
|
-
|
29
|
-
return true if c.to.name == name
|
30
|
-
end
|
31
|
-
return false
|
25
|
+
return getConnection(name) != nil
|
32
26
|
end
|
33
27
|
end
|