Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Created a new version which supposedly works but takes 5EVER so we do…
…n't know for sure yet
- Loading branch information
Showing
3 changed files
with
107 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#CSE 3504 Project 2 | ||
#Siena Biales, Vincent Chov | ||
#Google PageRank Algorithm Implementation | ||
|
||
import csv | ||
import operator | ||
import numpy as np | ||
|
||
with open("hollins.dat", "r") as data: | ||
reader = csv.reader(data, delimiter = ' ', skipinitialspace=True) | ||
|
||
cols = next(reader) | ||
|
||
#Extract the number of nodes (V) and edges (E) from the first line of the file | ||
V = int(cols[0]) | ||
E = int(cols[1]) | ||
|
||
#create a dictionary of the index : url | ||
urls = {} | ||
|
||
#create a dictionary of the source nodes (i) : all destination nodes (j) | ||
outgoing = [[] for i in range(V)] | ||
|
||
#add every node to the dictionary | ||
for n in range(0,V) : | ||
line = next(reader) #read the next line from the file | ||
index = int(line[0]) #cast the index to an integer | ||
urls[index] = line[1] #add the data to the dictionary | ||
|
||
for n in range(0, E) : | ||
line = next(reader) #read the next line from the file | ||
src = int(line[0]) | ||
dst = int(line[1]) | ||
#if the key has no value, set the value to an empty list | ||
#then append the destination node to the list | ||
outgoing[src-1].append(dst-1) | ||
|
||
#create initial state vector p(0) | ||
initialVector = [] | ||
for n in range(0,V) : | ||
initialVector.append(1/V) #initialize the vector | ||
|
||
|
||
#Initialize an array/matrix P | ||
P = np.zeros((V,V)) | ||
|
||
#populate the matrix | ||
for i in range(0,V) : | ||
for j in range(0,V) : | ||
if i in outgoing[j] : | ||
P[i][j] = 1/len(outgoing[j]) | ||
|
||
#PR(P, initialVector, damp) | ||
def PageRank(trans, initVec, damp, n=0) : | ||
|
||
nextVector = [] | ||
|
||
for i in range(len(initVec)) : | ||
total = 0 | ||
for j in range(len(initVec)) : | ||
total += initVec[j]*trans[i][j] | ||
nextVector.append((1-damp) + damp*total) | ||
|
||
if (initVec != nextVector and n<100) : | ||
return PageRank(trans, nextVector, damp, n+1) | ||
else : | ||
print("success!") | ||
return nextVector | ||
|
||
finalRank = PageRank(P, initialVector, .85) | ||
print(finalRank) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
4 6 | ||
1 blah | ||
2 blahhh | ||
3 blerhghgh | ||
4 help | ||
1 2 | ||
1 3 | ||
2 1 | ||
2 3 | ||
2 4 | ||
4 3 |