comparison src/pagerank/LinkToVertex.java @ 7:c7b139ff27e2 draft

compute PageRank. initial value is 1/ AllVertexNumber
author one
date Wed, 05 Sep 2012 16:34:47 +0900
parents 140272228818
children 4d1885a2fa36
comparison
equal deleted inserted replaced
6:8ea2212eaee0 7:c7b139ff27e2
12 public class LinkToVertex { 12 public class LinkToVertex {
13 13
14 Graph graph; 14 Graph graph;
15 public final static String PAGE_TITLE = "pageTitle"; 15 public final static String PAGE_TITLE = "pageTitle";
16 public final static String PAGE_RANK = "pageRank"; 16 public final static String PAGE_RANK = "pageRank";
17 // pageIdTable
18 // key: pageTitle value: Vertex ID
17 private HashMap<String, Object> pageIdTable = new HashMap<String, Object>(); 19 private HashMap<String, Object> pageIdTable = new HashMap<String, Object>();
18 20
21 // wikiPageHash
22 // key: pageTitle value: wikiPage(class)
19 private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>(); 23 private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>();
20 private long AllVertexNumber; 24 private long AllVertexNumber;
21 25
22 private final double weight1 = 0.85; 26 private final double weight1 = 0.85;
23 private final double weight2 = 0.15; 27 private final double weight2 = 0.15;
84 Vertex getVertex(String name) { 88 Vertex getVertex(String name) {
85 Object id = pageIdTable.get(name); 89 Object id = pageIdTable.get(name);
86 return graph.getVertex(id); 90 return graph.getVertex(id);
87 } 91 }
88 92
89 Vertex getNode(String nodeId) { 93 Vertex getVertexById(String id) {
90 return graph.getVertex(nodeId); 94 return graph.getVertex(id);
91 } 95 }
92 96
93 Edge setRelationship(Vertex v1, Vertex v2, String label) { 97 Edge setRelationship(Vertex v1, Vertex v2, String label) {
94 Edge e = graph.addEdge(null, v1, v2, label); 98 Edge e = graph.addEdge(null, v1, v2, label);
95 return e; 99 return e;
199 String numOutput = "Number of inHaslink pages: " + numberOfLinkPages + "\n"; 203 String numOutput = "Number of inHaslink pages: " + numberOfLinkPages + "\n";
200 System.out.println(numOutput); 204 System.out.println(numOutput);
201 } 205 }
202 206
203 public double computePageRank(Vertex v) { 207 public double computePageRank(Vertex v) {
204 double sum = 0; 208 double sum = 0.0;
205 double pageRank = 0; 209 double pageRank = 0.0;
206 String title = getPageTitle(v); 210 String title = getPageTitle(v);
207 WikiPage wiki = wikiPageHash.get(title); 211 WikiPage wiki = wikiPageHash.get(title);
208 212
209 for (Edge edge : v.getEdges(Direction.IN, HAS_LINK) ) { 213 for (Edge edge : v.getEdges(Direction.IN, HAS_LINK) ) {
210 Vertex linkV = edge.getVertex(Direction.OUT); 214 Vertex linkV = edge.getVertex(Direction.OUT);
211 sum += (double) ((Double) linkV.getProperty(PAGE_RANK)) / computeInHasLink(linkV) ; 215 if (computeInHasLink(linkV) == 0) {
212 } 216 sum += (Double) linkV.getProperty(PAGE_RANK);
213 217 } else {
214 if (computeOutHasLink(v) == 0) { 218 sum += ((Double) linkV.getProperty(PAGE_RANK)) / computeInHasLink(linkV) ;
215 pageRank = (double) sum * weight1 219 }
216 + (double) ((double) 1 / AllVertexNumber * weight2); 220 }
217 } else { 221
218 pageRank = (double) ((double)sum / computeOutHasLink(v) * weight1) 222 pageRank = (double) sum * weight1
219 + (double) ((double) 1 / AllVertexNumber * weight2); 223 + (double) ((double) 1 / AllVertexNumber * weight2);
220 } 224
221 wiki.setRank(pageRank); 225 wiki.setRank(pageRank);
222 v.setProperty(PAGE_RANK, pageRank); 226 v.setProperty(PAGE_RANK, pageRank);
223 return pageRank; 227 return pageRank;
224 } 228 }
225 229