C
Chuck Brotman
I'm confused! I may have bitten off more than I can chew!
This question may be more about OOP ion general than Ruby, but since I'm
implementing in Ruby (trying to, anyway) I thought I'd start here. If I
should be posting elsewhere please let me know.
Here's the problem:
I'm trying to write my own graph theory library. I'm aware of
Adjacency matrices and the like, but I want to do this as OO as
possible, using objects of class node and edge to implement.
Each node has a name and an array of adjacent nodes. How can I add a
reference to a node which is adjacent,if I haven't created that node
yet? Or, do I have to create all the nodes first (without adjacencies
and then back fill them?) Or??
I feel like this should be doable, but I'm having trouble "wrapping my
head around it!". Here's an early cut at my class def for Node:
TYhanks for any help you are able to provide...
Chuck
#Node ----------------------------------.
class Node
attr_accessor :name, :nextnode, :adjacencies
def initialize(name, adjacencies)
@name = name
@adj = adjacencies
self.report "***new node #{@name}, created= [{#{@name} |
#{@adj.inspect}]\n"
end #initialize
def to_s
return "001 [#{@name.inspect}),#{@adj.insert ","}]"
end # to_s
end #node class-----------
#Edge --------------------
class Edge
attr_accessor :name, :N1, :N2
def initialize (name,n1, n2)
@name = name
@n1 = n1
@n2 = n2
print ("\n200:new edge, [#{@name}] created= [#{@n1}-#{@n2}]")
end # initialize
end # edge class ------------------
This question may be more about OOP ion general than Ruby, but since I'm
implementing in Ruby (trying to, anyway) I thought I'd start here. If I
should be posting elsewhere please let me know.
Here's the problem:
I'm trying to write my own graph theory library. I'm aware of
Adjacency matrices and the like, but I want to do this as OO as
possible, using objects of class node and edge to implement.
Each node has a name and an array of adjacent nodes. How can I add a
reference to a node which is adjacent,if I haven't created that node
yet? Or, do I have to create all the nodes first (without adjacencies
and then back fill them?) Or??
I feel like this should be doable, but I'm having trouble "wrapping my
head around it!". Here's an early cut at my class def for Node:
TYhanks for any help you are able to provide...
Chuck
#Node ----------------------------------.
class Node
attr_accessor :name, :nextnode, :adjacencies
def initialize(name, adjacencies)
@name = name
@adj = adjacencies
self.report "***new node #{@name}, created= [{#{@name} |
#{@adj.inspect}]\n"
end #initialize
def to_s
return "001 [#{@name.inspect}),#{@adj.insert ","}]"
end # to_s
end #node class-----------
#Edge --------------------
class Edge
attr_accessor :name, :N1, :N2
def initialize (name,n1, n2)
@name = name
@n1 = n1
@n2 = n2
print ("\n200:new edge, [#{@name}] created= [#{@n1}-#{@n2}]")
end # initialize
end # edge class ------------------