Advertisement
Guest User

Untitled

a guest
May 25th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. package graphs.freshcode;
  2.  
  3. // exported project
  4.  
  5. import org.jgrapht.*;
  6. import org.jgrapht.graph.*;
  7. import org.jgrapht.traverse.*;
  8. import java.util.*;
  9.  
  10. public class BaseGraph<V,E> extends SimpleDirectedWeightedGraph<V,E> {
  11.  
  12. // for mapping vertices to boolean values to indicate whether marked
  13. HashMap<V, Boolean> marked = new HashMap<V, Boolean>();
  14.  
  15. // for mapping vertices to int values to indicate visits
  16. HashMap<V, Integer> visitMap = new HashMap<V, Integer>();
  17.  
  18. // for mapping vertices to Double values to indicate weight
  19. HashMap<V, Double> weightMap = new HashMap<V, Double>();
  20.  
  21.  
  22. private static final long serialVersionUID = 3545796589454112304L;
  23.  
  24. public BaseGraph (EdgeFactory<V, E> ef) {
  25. super(ef);
  26. }
  27.  
  28. public BaseGraph (Class<? extends E> edgeClass) {
  29. super(edgeClass);
  30. }
  31.  
  32. @Override
  33. public boolean addVertex (V v) {
  34. marked.put(v, false);
  35. return super.addVertex(v);
  36. }
  37.  
  38.  
  39. public void print() {
  40. TreeSet<V> ts = new TreeSet<V>(vertexSet());
  41. Iterator<V> it = ts.iterator();
  42. System.out.println("Number of vertices is " + vertexSet().size());
  43. System.out.println("Number of edges is " + edgeSet().size());
  44. while (it.hasNext()) {
  45. V v = it.next();
  46. System.out.print(v+": ");
  47. System.out.println(Graphs.neighborListOf(this, v));
  48. }
  49. }
  50.  
  51.  
  52.  
  53. // get access to lowest rank vertex of graph
  54.  
  55. public V getFirstVertex() {
  56. Set<V> vs = vertexSet();
  57. TreeSet<V> ts = new TreeSet<V>(vs);
  58. return ts.first();
  59. }
  60.  
  61.  
  62. // below all for marking vertices
  63.  
  64. public void unsetAllMarked() {
  65. Iterator<V> it = this.vertexSet().iterator();
  66. while (it.hasNext())
  67. marked.put(it.next(), false);
  68. }
  69.  
  70. public void setMarked(V v) {
  71. marked.put(v, true);
  72. }
  73.  
  74. public void unsetMarked(V v) {
  75. marked.put(v, false);
  76. }
  77.  
  78. public Boolean isMarked(V v) {
  79. return marked.get(v);
  80. }
  81.  
  82. // ADDED
  83.  
  84. public void setVisits(){
  85. Iterator<V> it = this.vertexSet().iterator();
  86. while (it.hasNext())
  87. visitMap.put(it.next(), 0);
  88. }
  89.  
  90.  
  91. public Integer getVisits(V v) {
  92. // PRE: vertex V exists
  93. // POST: returns the value of attribute visits for vertex v
  94.  
  95. if(!visitMap.containsKey(v)){
  96. setVisits();
  97. }
  98. return visitMap.get(v);
  99. }
  100.  
  101. public void setWeights(){
  102. Iterator<V> it = vertexSet().iterator();
  103. while (it.hasNext()) {
  104. weightMap.put(it.next(), Double.valueOf(0));
  105. }
  106.  
  107. }
  108.  
  109. public Double getWeight(V v) {
  110. // PRE: vertex V exists
  111. // POST: returns the value of attribute weight for vertex v
  112.  
  113. // TODO
  114.  
  115. if(!weightMap.containsKey(v)){
  116. setWeights();
  117. }
  118. return weightMap.get(v);
  119. // TODO: REPLACE THIS WITH AN APPROPRIATE RETURN VALUE
  120. }
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement