forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_335.py
49 lines (34 loc) · 1.03 KB
/
problem_335.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from typing import Set, Dict
DAMPING = 0.85
class Graph:
def __init__(self):
self.edges = dict()
def __repr__(self):
return str(self.edges)
def add_edge(self, src, tgt):
if not src in self.edges:
self.edges[src] = set()
if not tgt in self.edges:
self.edges[tgt] = set()
self.edges[src].add(tgt)
def calculate_score(node: str, g: Graph, page_scores: Dict):
agg_score = 0
for other in g.edges:
if node in g.edges[other]:
agg_score += page_scores[other] / len(g.edges[other])
score = ((1 - DAMPING) / len(g.edges)) + (DAMPING * agg_score)
return score
def rank_pages(g: Graph):
page_scores = dict()
start_prob = 1 / len(g.edges)
for node in g.edges.keys():
page_scores[node] = start_prob
for node in g.edges.keys():
page_scores[node] = calculate_score(node, g, page_scores)
return page_scores
# Tests
g = Graph()
g.add_edge('a', 'b')
g.add_edge('a', 'c')
g.add_edge('b', 'c')
print(rank_pages(g))