Mercurial > hg > Members > nobuyasu > TPPageRank
comparison src/pagerank/LinkToVertex.java @ 13:0ef7268bbbac draft
create descendiangOrder(Graph,FileOutputStream) method.
author | one |
---|---|
date | Sat, 08 Sep 2012 04:12:19 +0900 |
parents | 9787663edb54 |
children | 86567db31710 |
comparison
equal
deleted
inserted
replaced
12:7e38484474f4 | 13:0ef7268bbbac |
---|---|
4 | 4 |
5 import com.tinkerpop.blueprints.Direction; | 5 import com.tinkerpop.blueprints.Direction; |
6 import com.tinkerpop.blueprints.Edge; | 6 import com.tinkerpop.blueprints.Edge; |
7 import com.tinkerpop.blueprints.Graph; | 7 import com.tinkerpop.blueprints.Graph; |
8 import com.tinkerpop.blueprints.Vertex; | 8 import com.tinkerpop.blueprints.Vertex; |
9 import com.tinkerpop.gremlin.java.GremlinPipeline; | |
10 import com.tinkerpop.pipes.util.iterators.SingleIterator; | |
9 | 11 |
10 import pagerank.WikiPage; | 12 import pagerank.WikiPage; |
11 | 13 |
12 public class LinkToVertex { | 14 public class LinkToVertex { |
13 | 15 |
14 Graph graph; | 16 Graph graph; |
15 public final static String PAGE_TITLE = "pageTitle"; | 17 public final static String PAGE_TITLE = "pageTitle"; |
16 public final static String PAGE_RANK = "pageRank"; | 18 public final static String PAGE_RANK = "pageRank"; |
17 // pageIdTable | 19 |
18 // key: pageTitle value: Vertex ID | 20 /* pageIdTable |
21 * key: pageTitle value: Vertex ID | |
22 */ | |
19 private HashMap<String, Object> pageIdTable = new HashMap<String, Object>(); | 23 private HashMap<String, Object> pageIdTable = new HashMap<String, Object>(); |
20 | 24 |
21 // wikiPageHash | 25 /* wikiPageHash |
22 // key: pageTitle value: wikiPage(class) | 26 * key: pageTitle value: wikiPage(class) |
27 */ | |
23 private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>(); | 28 private HashMap<String, WikiPage> wikiPageHash = new HashMap<String, WikiPage>(); |
24 private long AllVertexNumber; | 29 private long AllVertexNumber; |
25 | 30 |
26 private final double weight = 0.85; | 31 private final double weight = 0.85; |
27 | 32 |
106 for (Vertex v : graph.getVertices()) { | 111 for (Vertex v : graph.getVertices()) { |
107 setPageRank(v, 0.0); | 112 setPageRank(v, 0.0); |
108 } | 113 } |
109 } | 114 } |
110 | 115 |
111 long searchAllVertices() { | 116 public long searchAllVertices() { |
112 AllVertexNumber = 0; | 117 AllVertexNumber = 0; |
113 for (Vertex v : graph.getVertices()) { | 118 for (Vertex v : graph.getVertices()) { |
114 if ( (v.getProperty(PAGE_TITLE) != null) && | 119 if ( (v.getProperty(PAGE_TITLE) != null) && |
115 (v.getProperty(PAGE_RANK)) != null ) { | 120 (v.getProperty(PAGE_RANK)) != null ) { |
116 WikiPage wiki = new WikiPage(v); | 121 WikiPage wiki = new WikiPage(v); |
219 Vertex linkV = edge.getVertex(Direction.OUT); | 224 Vertex linkV = edge.getVertex(Direction.OUT); |
220 assert computeOutHasLink(linkV)!=0 ; | 225 assert computeOutHasLink(linkV)!=0 ; |
221 double pr = (Double)linkV.getProperty(PAGE_RANK); | 226 double pr = (Double)linkV.getProperty(PAGE_RANK); |
222 sum += (double) pr / computeOutHasLink(linkV) ; | 227 sum += (double) pr / computeOutHasLink(linkV) ; |
223 } | 228 } |
224 double tmp = (double) 1 - weight; | 229 pageRank = (double) 1 - weight + (double) sum * weight; |
225 pageRank = (double) tmp / AllVertexNumber | 230 |
226 + (double) sum * weight; | 231 wiki.setRank(pageRank); |
227 | 232 v.setProperty(PAGE_RANK, pageRank); |
233 return pageRank; | |
234 } | |
235 | |
236 public double computePageRankUsingPipes(Object id) { | |
237 double sum = 0.0; | |
238 double pageRank = 0.0; | |
239 Vertex v = graph.getVertex(id); | |
240 WikiPage wiki = wikiPageHash.get(v.getProperty(PAGE_TITLE)); | |
241 | |
242 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
243 pipe.start(graph.getVertex(id)).in("HasLink"); | |
244 for (Vertex inVer : pipe) { | |
245 Object inVerId = inVer.getId(); | |
246 GremlinPipeline<Vertex,Vertex> inPipe = new GremlinPipeline<Vertex,Vertex>(); | |
247 inPipe.start(graph.getVertex(inVerId)).out("HasLink"); | |
248 long linkNum = inPipe.count(); | |
249 double pr = (Double) inVer.getProperty(PAGE_RANK); | |
250 sum += (double) pr / linkNum; | |
251 } | |
252 pageRank = (double) 1 - weight + (double) sum * weight; | |
228 wiki.setRank(pageRank); | 253 wiki.setRank(pageRank); |
229 v.setProperty(PAGE_RANK, pageRank); | 254 v.setProperty(PAGE_RANK, pageRank); |
230 return pageRank; | 255 return pageRank; |
231 } | 256 } |
232 | 257 |