narendraj9 / digsep

Degree of Separation on Twitter
5 stars 0 forks source link

Question: regarding the Frontier object #2

Open BradKML opened 3 years ago

BradKML commented 3 years ago
  1. How are the "internal" and "perimeter" used?
  2. How does one do a BFS with multiple users as the source nodes, but no end nodes to speak of? How does one prevent getting the same data of user Z twice from Twitter when users X and Y both follow user Z?
  3. Tweepy's Authorization does not have to be this complex https://docs.tweepy.org/en/latest/auth_tutorial.html
BradKML commented 3 years ago

Requesting code checks for this object

import typing
from typing import List # can't do List[UserType] ATM

UserType = [int, str]

class MultiFront():

    def __init__(self, sources:List[int], expander):

        self.sources = sources # list of nodes
        self.expander = expander # expansion function, based on API

        self.frontlines = {user: Frontier(user, lambda n: self.sus_expander(n)) for user in sources}
        self.node_table = {} # needed to prevent repeated API calls

        self.counter = 0

    def sus_expander(self,user:UserType):
        if user in self.node_table:
            return self.node_table[user] # dump to the saved API data
        else:
            expansion_result = self.expander(user) # get the API data
            self.node_table[user] = expansion_result # save the downloaded API call
            return expansion_result

    def expand_perimeter(self):
        # expand for each frontier
        self.counter += 1 # needed for the addition of new users
        return set().union(*[self.frontlines[user].expand_perimeter() for user in self.frontlines])

    def is_on_perimeter(self, user_id:UserType):
        # checks all perimeters in each frontier
        return set().union(*[self.frontlines[user].is_on_perimeter(user_id) for user in self.frontlines])

    def covered_all(self):
        # will return 0 if all nodes are covered
        return sum([self.frontlines[user].covered_all() for user in self.frontlines])

    def get_distance(self, user_id:UserType):
        # gets the shortest distance between n and the source node
        return min([self.frontlines[user].get_parent(user_id) for user in self.frontlines])

    def add_user(self, source):
        # needed for adding new BFS items
        self.sources += [source]
        self.frontliners[source] = Frontier(source, self.sus_expander)
        for i in range(counter):
            self.frontliners[source].expand_perimeter()