Commit 1d1d9583 authored by Olli's avatar Olli
Browse files

Created a simple PageRank Implementation

parent 6bfa0d2e
......@@ -47,6 +47,9 @@ class Vertex:
self.outgoing_edges = {}
self.approx_to_goal = 0
self.goal_id = -1
self.pr = 1
self.pr_temp = 0
self.number_of_incoming_links = 0
def __str__(self):
return "Vertex." + str(self.id)
......@@ -62,6 +65,8 @@ class Vertex:
self.set = []
self.set.append(self.id)
class Edge:
'''
Edge objects are used to save link data before it is embedded to Vertex objects.
......
'''
TSGS is a package containing graph analyzing scripts. Copyright (C) 2021 Leinonen Jenni, Rantanen Katariina, Tuhkanen Olli
TSGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TSGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TSGS. If not, see <https://www.gnu.org/licenses/>.
'''
from Settings_reader import TSGS_settings
from Graph import read_matrix
def calc_pagerank(graph, dampening_factor = 0.85):
#initialize pr_temp
for v in graph.vertices:
graph.vertices[v].pr_temp = graph.vertices[v].pr
graph.vertices[v].pr_temp = 0
for v in graph.vertices:
for v2 in graph.vertices[v].outgoing_edges:
#graph.vertices[v2].number_of_incoming_links += 1
increase = graph.vertices[v].pr / len(graph.vertices[v].outgoing_edges)
#print(increase)
graph.vertices[v2].pr_temp += increase
'''
if graph.vertices[v].pr_temp > 0:
print(graph.vertices[v].id)
print(graph.vertices[v].pr_temp)
print(graph.vertices[v].pr)
raise Exception()
'''
for v in graph.vertices:
graph.vertices[v].pr = (1 - dampening_factor) + (dampening_factor * graph.vertices[v].pr_temp)
def get_n_best(graph, n):
list_of_vertices = []
for v in graph.vertices:
list_of_vertices.append(graph.vertices[v])
s_list = sorted(list_of_vertices, key=lambda vertex: vertex.pr, reverse=True)
#GEOMETRYCOLLECTION(POINT(23.31298828125 60.73231517201879),POINT(23.53271484375 61.01040072727079))
print("GEOMETRYCOLLECTION(", end="")
for i in range(n):
print("POINT(" + str(s_list[i].x) + " " + str(s_list[i].y) + ")", end="")
if i < n-1:
print(",", end="")
#print(str(s_list[i].id) + " " + str(s_list[i].pr) + " " + str(s_list[i].x) + " " + str(s_list[i].y))
print(")")
if __name__ == "__main__":
SETTINGS = TSGS_settings()
nodes = SETTINGS.NODES_FILE
arcs = SETTINGS.ARCS_FILE
g = read_matrix(nodes, arcs, printing = True)
for i in range(100):
print(i)
calc_pagerank(g)
print("READY")
'''
for v in g.vertices:
print(str(g.vertices[v].id) + " " + str(g.vertices[v].x) + " " + str(g.vertices[v].y) + " " + str(g.vertices[v].pr))
'''
get_n_best(g, 100)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment