Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions archive/r/ruby/depth-first-search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

USAGE = 'Usage: please provide a tree in an adjacency matrix form ("0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0") together with a list of vertex values ("1, 3, 5, 2, 4") and the integer to find ("4")'

def usage!
warn USAGE
exit 1
end

def parse_list(str)
str.split(",").map { Integer(it.strip) }
rescue ArgumentError, NoMethodError
usage!
end

def matrix(flat)
n = Integer(Math.sqrt(flat.size))
usage! unless n * n == flat.size
flat.each_slice(n).to_a
end

def dfs(mat, vals, i, target, seen)
return true if vals[i] == target
return false if seen[i]

seen[i] = true

mat[i].each_with_index do |edge, j|
next unless edge == 1
return true if dfs(mat, vals, j, target, seen)
end

false
end

tree_s, vals_s, target_s = ARGV
usage! if tree_s.nil? || vals_s.nil? || target_s.nil?

tree = parse_list(tree_s)
vals = parse_list(vals_s)
target = begin
Integer(target_s)
rescue
usage!
end

usage! unless vals.size * vals.size == tree.size

mat = matrix(tree)

puts dfs(mat, vals, 0, target, {})
Loading