Last active
January 4, 2016 05:19
-
-
Save diclophis/8574572 to your computer and use it in GitHub Desktop.
node graph chain detector thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EdgeEmitter | |
include Enumerable | |
attr_accessor :edges | |
def initialize(filename) | |
# you should load the file and save the edges | |
end | |
# implementation of Enumerable is bonus points | |
def length | |
# how do you know how many edges there are? | |
end | |
def each | |
# yield each edge once | |
end | |
end | |
class Graph | |
attr_accessor :nodes | |
def initialize | |
# setup @nodes to prepare for filling in edges | |
end | |
def add_edge(a, b) | |
# create OR find existing Node for a and b and establish correct relations between the nodes | |
end | |
def possible_chains | |
# identify each chain of connected nodes by looping over all nodes and creating a collection of their family_trees | |
end | |
def to_s | |
# filter out smaller chains that are part of a larger chain | |
# uniqify and sort the resulting chains to match desired output | |
end | |
end | |
class Node | |
attr_accessor :children | |
attr_accessor :name | |
attr_accessor :memoized_tree | |
def initialize(name) | |
# prepare node name and children container | |
end | |
def add_child(node) | |
# add node to children | |
endp | |
def family_tree | |
# memoize the result of recursing into all children and appending their family_tree to this nodes family tree | |
# if you don't memoize you will get a infinite loop | |
# also note this should return a FLAT array | |
end | |
def to_s | |
self.name # this will make printing the result easier | |
end | |
end | |
edge_emitter = EdgeEmitter.new("pairs") # "pairs" is a file in the current working directory | |
graph = Graph.new | |
edge_emitter.each { |a, b| # or edge_emitter.edges.each {} if you can't get Enumerable mixin to work | |
graph.add_edge(a, b) | |
} | |
puts graph |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment