module NPGRT::Algorithm

Public Instance Methods

array_to_hash(arr) click to toggle source
# File lib/npgrt/algorithm.rb, line 11
def array_to_hash(arr)
        hsh = {}
        arr.each_with_index{|e, i| hsh[i] = e if e}
        hsh  
end
bfs(*start) { |method(:push), shift| ... } click to toggle source
# File lib/npgrt/algorithm.rb, line 7
def bfs(*start)
        yield(start.method(:push), start.shift)      until start.empty?
end
dfs(start) { |lambda| ... } click to toggle source
# File lib/npgrt/algorithm.rb, line 3
def dfs(start, &b)
        yield(lambda{|v| dfs(v, &b)}, start)
end
graph_buildpath(st, ed, pre) click to toggle source
# File lib/npgrt/algorithm.rb, line 26
def graph_buildpath(st, ed, pre)
        ret = []
        while ed != st && ed != -1
                raise CycleFound, 'path cycled' if ret.include?(ed)
                ret.unshift ed
                ed = pre[ed]
        end  
        if ed == st
                ret.unshift st
        else
                raise NotFound
        end  
        ret          
end
hash_to_array(hsh) click to toggle source
# File lib/npgrt/algorithm.rb, line 17
def hash_to_array(hsh)
        arr = []
        hsh.each{|k, v| arr[k] = v}
        arr
end