class Graph

Attributes

nodes[R]

Public Class Methods

new() click to toggle source

creates a blank graph

# File lib/graph.rb, line 8
def initialize
        @nodes = {}
end

Public Instance Methods

addEdge(from, to, directed, weight=nil) click to toggle source

returns true if added

# File lib/graph.rb, line 17
def addEdge from, to, directed, weight=nil
        return false if !isNode?(to) || !isNode?(from)
        getNode(from).addConnection(getNode(to),weight)
        getNode(to).addConnection(getNode(from),weight) if(!directed)
end
addNode(name, content) click to toggle source
# File lib/graph.rb, line 12
def addNode name, content
        @nodes[name] = GraphNode.new(name, content)
end
convertFromCSVString(input) click to toggle source
# File lib/graph.rb, line 71
def convertFromCSVString input
        lines = input.split("\n")
        lines.delete_if{|l| l=='From,FromContent,To,ToContent,Weight'}
        lines.map!{|l| l.split(',')}
        lines.each do |l|
                if(l.size == 5)
                        if(/-{0,1}[1-9]\d*/.match(l[4]))
                                weight = l[4].to_f
                        else
                                weight = l[4]
                        end
                else
                        weight = nil
                end
                if(!isNode(l[0]))
                        addNode(l[0],l[1])
                end

                if(!isNode(l[2]))
                        addNode(l[2],l[3])
                end

                addEdge(l[0],l[2],weight,true)
        end
end
convertToCSVString() click to toggle source
# File lib/graph.rb, line 61
def convertToCSVString
        result = "From,FromContent,To,ToContent,Weight\n"
        @nodes.each do |key, n|
                n.connections.each do |key, c|
                        result += n.name + ',' + n.content + ',' + c.to.name + ',' + c.to.content + ',' + c.weight.to_s + "\n"
                end
        end
        return result
end
getConnectionWeight(from, to) click to toggle source
# File lib/graph.rb, line 31
def getConnectionWeight from, to
        return nil if !isNode?(from) || !isNode?(to)
        return nil if !isConnected?(from,to)
        return getNode(from).getConnection(to).weight
end
getNode(name) click to toggle source
# File lib/graph.rb, line 27
def getNode name
        return @nodes[name]
end
isConnected?(from, to) click to toggle source
# File lib/graph.rb, line 42
def isConnected? from, to
        return nil if !isNode?(from) || !isNode?(to)
        return getNode(from).connectedTo?(to)
end
isNode?(name) click to toggle source
# File lib/graph.rb, line 23
def isNode? name
        return @nodes[name] != nil
end
markNode(name) click to toggle source

marks the node mainly so that users can run own algs and know which have been activated

# File lib/graph.rb, line 48
def markNode name
        getNode(name).marked = true
end
modifyWeight(from, to, newWeight) click to toggle source
# File lib/graph.rb, line 37
def modifyWeight from, to, newWeight
        return nil if !isNode?(from) || !isNode?(to)
        getNode(from).getConnection(to).weight = newWeight
end
refreshNodes() click to toggle source
# File lib/graph.rb, line 57
def refreshNodes
        @nodes.each{|n| n.marked = false}
end
unmarkNode(name) click to toggle source

removes mark from a single node

# File lib/graph.rb, line 53
def unmarkNode name
        getNode(name).marked = false
end