Skip to content

Instantly share code, notes, and snippets.

@michaelmcmillan
Last active November 23, 2015 14:00
Show Gist options
  • Save michaelmcmillan/2b874210a5011fc27117 to your computer and use it in GitHub Desktop.
Save michaelmcmillan/2b874210a5011fc27117 to your computer and use it in GitHub Desktop.
class Page(object):
def __init__(self, name):
self.name = name
self.auth = 1
self.hub = 1
self.incoming_neighbors = []
self.outgoing_neighbors = []
def points_to(self, other_pages):
for other_page in other_pages:
other_page.incoming_neighbors.append(self)
self.outgoing_neighbors.append(other_page)
def __str__(self):
return '{0}: Auth = {1}. Hub = {2}'.format(
self.name, self.auth, self.hub
)
class HITS(object):
def __init__(self, pages = []):
self.pages = pages
def calculate(self, iterations = 1):
for iteration in range(0, iterations):
self.update_auth_values()
self.update_hub_values()
def update_auth_values(self):
for page in self.pages:
page.auth = 0
for neighbor in page.incoming_neighbors:
page.auth += neighbor.hub
def update_hub_values(self):
for page in self.pages:
page.hub = 0
for neighbor in page.outgoing_neighbors:
page.hub += neighbor.auth
def __str__(self):
representation = ''
for page in self.pages:
representation += str(page) + '\n'
return representation
A = Page('A')
B = Page('B')
C = Page('C')
D = Page('D')
A.points_to([C, D])
B.points_to([C])
C.points_to([A])
D.points_to([C])
D.points_to([B])
hits = HITS([A, B, C, D])
hits.calculate(iterations = 5)
print hits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment