Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. package se.kth.graph;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.NoSuchElementException;
  8.  
  9. /**
  10. * A graph with a fixed number of vertices implemented using adjacency maps.
  11. * Space complexity is Θ(n + m) where n is the number of vertices and m
  12. * the number of edges.
  13. *
  14. * @author [Name]
  15. * @version [Date]
  16. */
  17. public class HashGraph implements Graph {
  18. /**
  19. * The map edges[v] contains the key-value pair (w, c) if there is an edge
  20. * from v to w; c is the cost assigned to this edge. The maps may be null
  21. * and are allocated only when needed.
  22. */
  23. private final Map<Integer, Integer>[] edges;
  24. private final static int INITIAL_MAP_SIZE = 4;
  25.  
  26. /** Number of edges in the graph. */
  27. private int numEdges;
  28.  
  29. /**
  30. * Constructs a HashGraph with n vertices and no edges. Time complexity:
  31. * O(n)
  32. *
  33. * @throws IllegalArgumentException
  34. * if n < 0
  35. */
  36. public HashGraph(int n) {
  37. if (n < 0)
  38. throw new IllegalArgumentException("n = " + n);
  39.  
  40. // The array will contain only Map<Integer, Integer> instances created
  41. // in addEdge(). This is sufficient to ensure type safety.
  42. @SuppressWarnings("unchecked") Map<Integer, Integer>[] a = new HashMap[n];
  43. edges = a;
  44. }
  45.  
  46. /**
  47. * Add an edge without checking parameters.
  48. */
  49. private void addEdge(int from, int to, int cost) {
  50. if (edges[from] == null)
  51. edges[from] = new HashMap<Integer, Integer>(INITIAL_MAP_SIZE);
  52. if (edges[from].put(to, cost) == null)
  53. numEdges++;
  54. }
  55.  
  56. /**
  57. * {@inheritDoc Graph} Time complexity: O(1).
  58. */
  59. @Override
  60. public int numVertices() {
  61. return edges.length;
  62. }
  63.  
  64. /**
  65. * {@inheritDoc Graph} Time complexity: O(1).
  66. */
  67. @Override
  68. public int numEdges() {
  69. return numEdges;
  70. }
  71.  
  72. /**
  73. * {@inheritDoc Graph}
  74. */
  75. @Override
  76. public int degree(int v) throws IllegalArgumentException {
  77. if(v < 0 || v >= edges.length){
  78. throw new IllegalArgumentException();
  79. }
  80. Map<Integer, Integer> gren
  81. gren = edges[v];
  82. if(edges[v] != null) {
  83. return gren.length;
  84. }
  85. return 0;
  86. }
  87.  
  88. /**
  89. * {@inheritDoc Graph}
  90. */
  91. @Override
  92. public VertexIterator neighbors(int v) {
  93. if(v < 0 && v >= edges.length) {
  94. throw new IllegalArgumentException();
  95. }
  96. return new HashVertexIterator(v);
  97. }
  98.  
  99. private class HashVertexIterator implements VertexIterator {
  100. iterator<Integer> iterator;
  101.  
  102. public HashVertexIterator(int v){
  103. if(edges[v] == null) {
  104. iterator = new HashMap<Integer, Integer>().keySet().iterator();
  105. }
  106. else{iterator = edges[v].keyset().iterator();
  107. }
  108. }
  109. }
  110.  
  111. /**
  112. * {@inheritDoc Graph}
  113. */
  114. @Override
  115. public boolean hasEdge(int from, int to) {
  116. if(edges[from] != null) {
  117. if(edges[from].containsKey(to))
  118. return true;
  119. }
  120. return false;
  121. }
  122.  
  123. /**
  124. * {@inheritDoc Graph}
  125. */
  126. @Override
  127. public int cost(int from, int to) throws IllegalArgumentException {
  128. if(from >= 0 && to >= 0 && edges[to].get(w) != null) {
  129. cost = edges[from].get(w);
  130. return cost;
  131. }
  132. return NO_COST;
  133. }
  134.  
  135. /**
  136. * {@inheritDoc Graph}
  137. */
  138. @Override
  139. public void add(int from, int to) {
  140. addEdge(from, to, NO_COST);
  141. }
  142.  
  143. /**
  144. * {@inheritDoc Graph}
  145. */
  146. @Override
  147. public void add(int from, int to, int c) {
  148. addEdge(from, to, c);
  149. }
  150.  
  151. /**
  152. * {@inheritDoc Graph}
  153. */
  154. @Override
  155. public void addBi(int v, int w) {
  156. addEdge(v, w, NO_COST);
  157. if(v == w){
  158. return;
  159. }
  160. addEdge(w, v, NO_COST);
  161. }
  162.  
  163. /**
  164. * {@inheritDoc Graph}
  165. */
  166. @Override
  167. public void addBi(int v, int w, int c) {
  168. addEdge(v, w, c)
  169. if(v == w){
  170. return;
  171. }
  172. addEdge(w, v, c);
  173. }
  174.  
  175. /**
  176. * {@inheritDoc Graph}
  177. */
  178. @Override
  179. public void remove(int from, int to) {
  180. if(hasEdge(from, to)) {
  181. edges[from].remove(to);
  182. numEdges--;
  183. }
  184. }
  185.  
  186. /**
  187. * {@inheritDoc Graph}
  188. */
  189. @Override
  190. public void removeBi(int v, int w) {
  191. if(v >= 0 && w >= 0) {
  192. if(edes[v] != null) {
  193. remove(v, w);
  194. numEdges--;
  195. }
  196. if(edges[w] != null) {
  197. remove(w, v);
  198. numEdges--;
  199. }
  200. }
  201. }
  202.  
  203. /**
  204. * Returns a string representation of this graph.
  205. *
  206. * Should represent the graph in terms of edges (order does not matter).
  207. * For example, if a graph has edges (2,3) and (1,0) with NO_COST, the
  208. * representaiton should be:
  209. *
  210. * "{(1,0), (2,3)}" or "{(2,3), (1,0)}"
  211. *
  212. * If an edge has a cost (say, 5), it is added as a third value, like so:
  213. *
  214. * "{(1,0,5)}"
  215. *
  216. * An empty graph is just braces:
  217. *
  218. * "{}"
  219. *
  220. * @return a String representation of this graph
  221. */
  222. @Override
  223. public String toString() {
  224. if(edges.length != 0){
  225. for(int i = edges.length; i <= 0; i++){
  226. if(edges[i] != null){
  227. StringBuilder sb = new StringBuilder();
  228. String S = "";
  229. sb.append(edges[i].keySet().toString());
  230. System.out.println(s);
  231. }
  232. }
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement