-
-
Save anonymous/328798e25b3217dfb06f to your computer and use it in GitHub Desktop.
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 Connection < ActiveRecord::Base | |
def self.dfs(poi, target, dos, mas, gas) | |
# dfs & poi = depth first search for person of interest | |
# dos = degrees of separation | |
paths_between_poi_and_target = [] | |
members_already_searched = mas | |
groups_already_searched = gas | |
# members = [] | |
if dos <= 6 | |
# Iterate over all the groups of the group member | |
poi.groups.each do |group| | |
# Check to see if that group has already been searched | |
if groups_already_searched[group.name] | |
next | |
else | |
groups_already_searched[group.name] = true | |
group.members.each do |member| | |
# dos = degrees of separation | |
if members_already_searched[member.name] | |
next | |
else | |
members_already_searched[member.name] = true | |
if member == target && dos != 1 | |
return [group.name, member.name] | |
else | |
dos += 1 | |
response = dfs(member, target, dos, members_already_searched, groups_already_searched) | |
if dos == 1 && response != nil | |
paths_between_poi_and_target << response | |
elsif dos == 1 && response == nil | |
next | |
elsif response != nil | |
return response.unshift({group: member.name}) | |
else | |
return nil | |
end | |
end | |
end | |
end | |
end | |
end | |
else | |
return nil | |
end | |
return paths_between_poi_and_target | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment