Advertisement
Guest User

Untitled

a guest
May 25th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. package graphs.freshcode;
  2.  
  3. //exported project
  4.  
  5. import static org.junit.Assert.assertEquals;
  6. import static org.junit.Assert.assertTrue;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11. import java.security.KeyStore.Entry;
  12. import java.util.*;
  13.  
  14. import org.jgrapht.Graphs;
  15. import org.jgrapht.graph.*;
  16.  
  17. public class PageRankGraph extends BaseGraph<Integer, DefaultWeightedEdge>
  18. {
  19.  
  20. private static final long serialVersionUID = 1L;
  21.  
  22.  
  23. public PageRankGraph()
  24. {
  25. super(DefaultWeightedEdge.class);
  26. } // ensure non-instantiability.
  27.  
  28.  
  29. // // START OF PAGERANK CODE
  30. //
  31.  
  32. public Integer getRandomLink(Integer v) {
  33.  
  34.  
  35.  
  36. Random r = new Random();
  37. //iterators for weight and outward vert
  38. Iterator iterWeight = outgoingEdgesOf(v).iterator();
  39. Iterator iterVertex = outgoingEdgesOf(v).iterator();
  40.  
  41. int totalWeight = 0;
  42. int x = 0;
  43. int i = 0;
  44. while(iterWeight.hasNext()) {
  45. totalWeight+=(int) this.getEdgeWeight((DefaultWeightedEdge) iterWeight.next());
  46. x+=getWeight(i);
  47.  
  48. }
  49. int count = 0;
  50.  
  51. Integer vertRandom = r.nextInt(totalWeight)+1; //random generator
  52.  
  53. //redeclare iters
  54. iterWeight = outgoingEdgesOf(v).iterator();
  55. iterVertex = outgoingEdgesOf(v).iterator();
  56.  
  57. int greaterComparator = 1;
  58. int lessComparator = 0;
  59.  
  60. Integer vertID = -1; //defalt value
  61. while(iterWeight.hasNext()) {
  62.  
  63. lessComparator = greaterComparator + (int) this.getEdgeWeight((DefaultWeightedEdge) iterWeight.next());
  64.  
  65. if(vertRandom >= greaterComparator && vertRandom < lessComparator) {
  66. vertID = this.getEdgeTarget((DefaultWeightedEdge) iterVertex.next());
  67. return vertID;
  68. }
  69.  
  70. iterVertex.next();
  71. greaterComparator = lessComparator;
  72. count++;
  73. }
  74. System.out.println("count from randomLink: "+count);
  75. return vertID; // TODO: REPLACE THIS WITH AN APPROPRIATE RETURN VALUE
  76. }
  77.  
  78.  
  79. public Integer surfWithJump(Integer v, Integer n, Double jumpThreshold) {
  80. // PRE: v is vertex to start surf; n >= 0; 0 <= jumpThreshold <= 1.0
  81. // POST: surfs the graph randomly for n moves,
  82. // choosing adjacent vertex according to distribution of edge weights if random number is below jumpThreshold,
  83. // choosing any vertex uniformly randomly otherwise;
  84. // modifies # visits in vertices
  85. // returns last visited vertex at end of surfing
  86.  
  87.  
  88. for(int i = 0; i < n; i++) {
  89. Random r = new Random();
  90. float feelingLucky = r.nextFloat();
  91. this.visitMap.put(v, getVisits(v)+1);
  92.  
  93.  
  94. if(jumpThreshold < feelingLucky|| this.outgoingEdgesOf(v).size() == 0) {
  95.  
  96. v = r.nextInt(this.marked.size());//random number between 1-4
  97.  
  98. }
  99. else {
  100. v = getRandomLink(v);
  101. }
  102. }
  103. return v;
  104. }
  105.  
  106.  
  107. // TODO: REPLACE THIS WITH AN APPROPRIATE RETURN VALUE
  108.  
  109.  
  110. public ArrayList<Integer> topN(Integer N) {
  111. // PRE: none
  112. // POST: returns N vertices with highest number of visits, in order;
  113. // returns all vertices if <N in the graph;
  114. // returns vertices ranked 1,..,N,N+1,..,N+k if these k have the
  115. // same number of visits as vertex N
  116.  
  117. // TODO
  118. if(N == 0) {
  119. return null;
  120. }
  121. ArrayList<Integer> arr = new ArrayList<Integer>();
  122.  
  123. int listSize = this.marked.size();
  124.  
  125.  
  126. for(Integer i = 0; i < listSize; i++) {//putting in array unsorted
  127. arr.add(i);
  128. }
  129.  
  130. ArrayList<Integer> rankedList = new ArrayList<Integer>();
  131. Integer smallest = 0; //holds smallest V
  132. for(Integer i = 0; i < arr.size()-1 && rankedList.size() < N; i++) {
  133. smallest = i;
  134.  
  135. for(Integer j = i+1; j < arr.size(); j++) {
  136. if(this.getVisits(arr.get(smallest)) < this.getVisits(arr.get(j))) {
  137. smallest = j;
  138. }
  139. }
  140. Integer temp = arr.get(i);
  141. arr.set(i, smallest);
  142. arr.set(smallest, temp);
  143. rankedList.add(i, smallest);
  144. }
  145.  
  146. return rankedList;
  147. }
  148.  
  149. public void setVertexWeights () {
  150. // PRE: -
  151. // POST: set weights of each vertex v to be v.visits divided by
  152. // the total of visits for all vertices
  153. int totalWeight = 0;
  154. for(int i = 0; i < this.marked.size(); i++) {//getting net visit
  155. totalWeight+=this.getVisits(i);
  156. }
  157. double vertexWeight;
  158. for(int i = 0; i < this.marked.size(); i++) {
  159. vertexWeight = (double)this.getVisits(i)/totalWeight;
  160. weightMap.put(i, vertexWeight);
  161. }
  162. }
  163.  
  164. public void convergenceWeights(Integer dp, Double jumpThreshold) {
  165. // PRE: dp >= 0 representing number of decimal places
  166. // POST: web is surfed until all weights are constant to dp decimal places,
  167. // for at least one iteration
  168. // TODO
  169. }
  170.  
  171. public Integer surfWithJumpUntilHit(Integer v, Integer n, Double jumpThreshold) {
  172. // PRE: v is vertex to start surf; n >= 0; 0 <= jumpThreshold <= 1.0
  173. // POST: surfs the graph randomly until visit v for second time (maximum n moves),
  174. // choosing adjacent vertex according to distribution of edge weights if random number is below jumpThreshold,
  175. // choosing any vertex uniformly randomly otherwise;
  176. // modifies # visits in vertices
  177. // returns number of vertices visited
  178. Random r = new Random();
  179. int count = 0;
  180. this.marked.put(v, false);
  181.  
  182. for(int i = 0; i < n && !isMarked(v); i++) {
  183.  
  184. float feelingLucky = r.nextFloat();
  185. this.visitMap.put(v, getVisits(v)+1);
  186. this.setVertexWeights();
  187. if(i == 0) {
  188. this.setMarked(v);
  189. }
  190. if(feelingLucky < jumpThreshold) {
  191. v = getRandomLink(v);
  192. }
  193. else {
  194. v = r.nextInt(this.marked.size());//random number between 0-4
  195. }
  196. count++;
  197. }
  198. System.out.println("count from surfWithJump: "+count);
  199. return count;
  200.  
  201. }
  202.  
  203. public Integer surfWithJumpUntilCover(Integer v, Integer n, Double jumpThreshold) {
  204. // PRE: v is vertex to start surf; n >= 0; 0 <= jumpThreshold <= 1.0
  205. // POST: surfs the graph randomly until all vertices visited (with maximum n vertices visited),
  206. // choosing adjacent vertex according to distribution of edge weights if random number is below jumpThreshold,
  207. // choosing any vertex uniformly randomly otherwise;
  208. // modifies # visits in vertices
  209. // returns number of vertices visited
  210. int verticesVisited = 0;
  211. Random r = new Random();
  212. int count = 0;
  213. int coverTime = 0;
  214. float feelingLucky = 0;
  215.  
  216. for(int i = 0; i < n; i++) {
  217.  
  218. feelingLucky = r.nextFloat();
  219. verticesVisited++;
  220. if(jumpThreshold < feelingLucky) {
  221.  
  222. v = r.nextInt(this.marked.size());//random number between 1-4
  223.  
  224. }
  225. else {
  226. v = getRandomLink(v);
  227. }
  228. visitMap.put(v, getVisits(v)+1);
  229.  
  230. count = 0; //resets count
  231. setMarked(v);
  232.  
  233. for(int j = 0; j < marked.size(); j++) {//goes to all vertices
  234.  
  235. if(isMarked(j)) {//checks if all has been visited once
  236. count++;
  237. }
  238.  
  239. }
  240.  
  241. if(count == marked.size()) {
  242. coverTime+= Double.valueOf(verticesVisited);
  243.  
  244. unsetAllMarked();
  245. break;
  246. }
  247.  
  248. }
  249.  
  250. return verticesVisited;
  251. }
  252.  
  253.  
  254.  
  255.  
  256. public Double averageHittingTime(Integer v, Integer n, Integer maxHits, Double jumpThreshold) {
  257. // PRE: n >= 1 is number of iterations for averaging; maxHits >= 0; 0 <= jumpThreshold <= 1.0
  258. // POST: returns average number of vertices visited until hitting, across n iterations,
  259. // (not including those which stopped because they reached maxHits)
  260. // returns 0 if all iterations reached maxVisits
  261. // TODO
  262.  
  263. double avrg = 0;
  264. double x = 0;
  265. for(int i = 1; i <= n; i++) {
  266. x = surfWithJumpUntilHit(v, maxHits, jumpThreshold);
  267. if(x == maxHits) {
  268. n--;
  269. }
  270. else {
  271. avrg+=x;
  272. }
  273. }
  274. System.out.println(Integer.valueOf(maxHits*n)+" "+Integer.valueOf((int)avrg));
  275. if(Integer.valueOf(maxHits*n) - Integer.valueOf((int)avrg) == 0) {
  276. return 0.0;
  277. }
  278.  
  279. return Double.valueOf(avrg/n);
  280.  
  281. }
  282.  
  283.  
  284. public Double averageCoverTime(Integer n, Integer maxVisits, Double jumpThreshold) {
  285. // PRE: n >= 1 is number of iterations for averaging; maxVisits >= 0; 0 <= jumpThreshold <= 1.0
  286. // POST: returns average number of vertices visited until cover, across n iterations,
  287. // (not including those which stopped because they reached maxVisits)
  288. // randomly selecting start vertex each iteration
  289. // returns 0 if all iterations reached maxVisits
  290.  
  291. // TODO
  292. Random r = new Random();
  293.  
  294.  
  295. double coverTime = 0.0;
  296. Integer v = r.nextInt(marked.size());
  297.  
  298. for(int i = 1; i <= n; i++) {
  299. coverTime+=surfWithJumpUntilCover(v, maxVisits, jumpThreshold);
  300. v = r.nextInt(marked.size());
  301. }
  302.  
  303. if(Integer.valueOf(maxVisits*n) == Integer.valueOf((int)coverTime)) {
  304. return Double.valueOf(0);
  305. }
  306.  
  307. return Double.valueOf(coverTime/n);
  308. }
  309.  
  310. public Integer boostVertex(Integer v, Integer dp) {
  311. // PRE: v is a vertex in the graph
  312. // POST: returns a vertex v2 (!= v) such that when edge (v,v2,1.0) is added to the graph,
  313. // the weight of v is larger than when edge (v,v3,1.0) is added to the graph
  314. // (for any v3), when the graph is surfed to convergence
  315. // if there is no such vertex v2 (i.e. v is already connected to all other vertices),
  316. // return v
  317. // edges are only added if they do not already exist in the graph
  318.  
  319. // TODO
  320.  
  321. return -1; // TODO: REPLACE THIS WITH AN APPROPRIATE RETURN VALUE
  322.  
  323. }
  324.  
  325.  
  326. public void setWeightedIntFromFile(String fInName) throws IOException {
  327. // Reads list of edges from a file, one pair of integers plus weight per line
  328. BufferedReader fIn = new BufferedReader(
  329. new FileReader(fInName));
  330. String s;
  331. Integer x, y;
  332. Double w;
  333.  
  334. while ((s = fIn.readLine()) != null) {
  335. java.util.StringTokenizer line = new java.util.StringTokenizer(s);
  336. while (line.hasMoreTokens()) {
  337. x = Integer.parseInt(line.nextToken());
  338. y = Integer.parseInt(line.nextToken());
  339. w = Double.parseDouble(line.nextToken());
  340. this.addVertex(x);
  341. this.addVertex(y);
  342. DefaultWeightedEdge e = this.addEdge(x,y);
  343. this.setEdgeWeight(e, w);
  344. }
  345. }
  346. fIn.close();
  347.  
  348. unsetAllMarked();
  349. }
  350.  
  351. public static void main(String[] args) {
  352. PageRankGraph g = new PageRankGraph();
  353.  
  354. try {
  355. String PATH = "/home/evan/Desktop/assignment-sample-graphs/";
  356. g.setWeightedIntFromFile(PATH+"medium-weight.txt");
  357. }
  358. catch (IOException e) {
  359. System.out.println("in exception: " + e);
  360. }
  361. System.out.println(g.toString());
  362. g.print();
  363.  
  364.  
  365. }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement