Skip to content

Commit

Permalink
fixed directed pagerank calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyasp committed Jun 7, 2012
1 parent c0e3ca8 commit 3422fe1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
7 changes: 3 additions & 4 deletions pageRank.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def rank(self):
if self.directed:
neighbors = self.graph.out_edges(key)
for n in neighbors:
outlinks = len(self.graph.out_edges(n[0]))
rank_sum += (1 / float(outlinks)) * self.ranks[n[0]]

outlinks = len(self.graph.out_edges(n[1]))
if outlinks > 0:
rank_sum += (1 / float(outlinks)) * self.ranks[n[1]]
else:
neighbors = self.graph[key]
for n in neighbors:
Expand All @@ -49,7 +49,6 @@ def rank(self):
graph = parse(filename, isDirected)
p = PageRank(graph, isDirected)
p.rank()

for state, rank in p.ranks.iteritems():
print state+": "+str(rank)

Expand Down
21 changes: 16 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys, math, random, csv, types, networkx as nx
import re, sys, math, random, csv, types, networkx as nx
from collections import defaultdict

def parse(filename, isDirected):
Expand Down Expand Up @@ -27,10 +27,11 @@ def parse_directed(data):
DG = nx.DiGraph()

for i, row in enumerate(data):
node_a = row[0].strip()
node_b = row[2].strip()
val_a = int(row[1])
val_b = int(row[3])

node_a = format_key(row[0])
node_b = format_key(row[2])
val_a = digits(row[1])
val_b = digits(row[3])

DG.add_edge(node_a, node_b)
if val_a >= val_b:
Expand All @@ -40,6 +41,16 @@ def parse_directed(data):

return DG

def digits(val):
return int(re.sub("\D", "", val))

def format_key(key):
key = key.strip()
if key.startswith('"') and key.endswith('"'):
key = key[1:-1]
return key


def print_results(f, method, results):
print method

0 comments on commit 3422fe1

Please sign in to comment.