mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-16 03:43:19 +01:00
Merge pull request #49 from dandynaufaldi/search-dfs
Add depth first search
This commit is contained in:
commit
d6d2d67fe8
1 changed files with 51 additions and 0 deletions
51
Searches/depth_first_search.rb
Normal file
51
Searches/depth_first_search.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
# @param [Integer] start
|
||||
# @param [Integer] target
|
||||
# @param [Array] adjacency_list
|
||||
# @return [Array] routes
|
||||
def dfs(start, target, adjacency_list)
|
||||
is_visited = Hash.new(false)
|
||||
parent = {}
|
||||
stack = [start]
|
||||
loop do
|
||||
break if stack.empty?
|
||||
current_node = stack.pop
|
||||
is_visited[current_node] = true
|
||||
|
||||
return get_path(parent, target) if current_node == target
|
||||
|
||||
adjacency_list[current_node].each do |neighbor|
|
||||
next if is_visited[neighbor]
|
||||
stack << neighbor
|
||||
is_visited[neighbor] = true
|
||||
parent[neighbor] = current_node
|
||||
end
|
||||
end
|
||||
[]
|
||||
end
|
||||
|
||||
# @param [Hash] parent
|
||||
# @param [Integer] dest
|
||||
# @return [Array] path
|
||||
def get_path(parent, dest)
|
||||
iterator = dest
|
||||
path = [dest]
|
||||
while parent.has_key?(iterator)
|
||||
path << parent[iterator]
|
||||
iterator = parent[iterator]
|
||||
end
|
||||
path.reverse
|
||||
end
|
||||
|
||||
def main
|
||||
adjacency_list = [
|
||||
[1, 2], #0
|
||||
[0, 3], #1
|
||||
[0, 3], #2
|
||||
[1, 2, 4], #3
|
||||
[3, 5], #4
|
||||
[4] #5
|
||||
]
|
||||
p dfs(0, 5, adjacency_list)
|
||||
end
|
||||
|
||||
main
|
Loading…
Reference in a new issue