# Feb/2/2010 # A script to find out the pagerank of websites # Author: Niyaz PK (http://www.diovo.com/2010/02/the-flow-of-pagerank/) # Graph representation of the links # Lists the pages each page links to graph = {'A': ['C', 'D'], 'B': ['A'], 'C': ['D'], 'D': ['B', 'E'], 'E': ['B','D', 'G'], 'F': ['C', 'D', 'G'], 'G': [], } # Initialize ranks ranks = [1.0 for x in graph.keys()] S = sum(v for v in ranks) print [ int(ranks[graph.keys().index(v)]/S * 100) for v in sorted(graph.keys())] # Iteratively refine the pagerank value for i in range(20): new_ranks = [0.0 for x in graph.keys()] for x in graph: n = graph.keys().index(x) num = len(graph[x]) for y in graph[x]: m = graph.keys().index(y) new_ranks[m] += ranks[n]/num ranks = new_ranks S = sum(v for v in ranks) print [ int(ranks[graph.keys().index(v)]/S * 100) for v in sorted(graph.keys())]