示例#1
0
 def test_source2(self):
     edges = [
         ["A", "B"],
         ["A", "C"],
         # ["A", "F"],
         ["B", "A"],
         ["B", "C"],
         ["B", "D"],
         # ["C", "F"],
         ["D", "A"],
         ["D", "C"],
         ["D", "E"],
         ["E", "A"],
         ["E", "C"],
         ["F", "C"],
         ["F", "D"],
         ["F", "E"],
     ]
     g = Graph(edges)
     # scores se espera que sea {nodo1: score1, nodo2: score2, ...}
     scores = g.page_rank(damping=0.85, limit=1.0e-8)
     sorted_nodes = [
         node for node, _ in sorted(
             scores.items(), key=lambda x: x[1], reverse=True)
     ]
     self.assertSequenceEqual(sorted_nodes, ["C", "A", "B", "D", "E", "F"])
示例#2
0
def main(args):
    with io.open(args.file) as f:
        edges = list(parse_graph(f))
    g = Graph(edges)
    scores = g.page_rank()
    print(">" * 80)
    for node, score in  sorted(scores.items(), key=lambda x: x[1], reverse=True):
        print("{} ({})".format(node, score))
    print("<" * 80)