Advertisement
trihadiahmulia

ASD - Graph - rev - refactored

Dec 12th, 2019
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. package revisedversion;
  2.  
  3. public class Edge<T> {
  4.  
  5. private T destination;
  6. private int weight;
  7.  
  8. public Edge(T destination, int weight) {
  9. this.destination = destination;
  10. this.weight = weight;
  11. }
  12.  
  13. @Override
  14. public boolean equals(Object object) {
  15. return ((Edge) object).destination == this.destination;
  16. }
  17.  
  18. public void setDestination(T destination) {
  19. this.destination = destination;
  20. }
  21.  
  22. public void setWeight(int weight) {
  23. this.weight = weight;
  24. }
  25.  
  26. public T getDestination() {
  27. return destination;
  28. }
  29.  
  30. public int getWeight() {
  31. return weight;
  32. }
  33.  
  34. }
  35.  
  36. >>
  37. import java.util.LinkedList;
  38.  
  39. public class Vertex <T> {
  40. private T item;
  41. private LinkedList<Edge> edgeList;
  42. private int inDegree;
  43. private int outDegree;
  44.  
  45. public Vertex(T item) {
  46. this.item = item;
  47. this.edgeList = new LinkedList<>();
  48. this.inDegree = 0;
  49. this.outDegree = 0;
  50. }
  51.  
  52. @Override
  53. public boolean equals(Object object) {
  54. Vertex<T> vertex2 = (Vertex<T>) object;
  55. return this.item.equals(vertex2.item);
  56. }
  57.  
  58. public T getItem() {
  59. return item;
  60. }
  61.  
  62. public void setItem(T item) {
  63. this.item = item;
  64. }
  65.  
  66. public LinkedList<Edge> getEdgeList() {
  67. return edgeList;
  68. }
  69.  
  70. public int getInDegree() {
  71. return inDegree;
  72. }
  73.  
  74. public void setInDegree(int inDegree) {
  75. this.inDegree = inDegree;
  76. }
  77.  
  78. public int getOutDegree() {
  79. return outDegree;
  80. }
  81.  
  82. public void setOutDegree(int outDegree) {
  83. this.outDegree = outDegree;
  84. }
  85. }
  86.  
  87. >>
  88. import java.util.HashMap;
  89. import java.util.List;
  90. import java.util.Map;
  91. import java.util.Set;
  92. import java.util.TreeSet;
  93.  
  94. public class DiGraph<T> {
  95.  
  96. Map<T, Vertex<T>> vertexMap;
  97.  
  98. public DiGraph() {
  99. vertexMap = new HashMap<>();
  100. }
  101.  
  102. boolean addEdge(T sourceItem, T destinationItem, int weight) {
  103. if (containsVertex(sourceItem) && containsVertex(destinationItem)) {
  104. if (containsEdge(sourceItem, destinationItem)) {
  105. return false;
  106. }
  107. Vertex<T> sourceVertex = vertexMap.get(sourceItem);
  108. Vertex<T> destinationVertex = vertexMap.get(destinationItem);
  109. List sourceEdgeList = sourceVertex.getEdgeList();
  110.  
  111. sourceEdgeList.add(new Edge(destinationItem, weight));
  112. sourceVertex.setOutDegree(sourceVertex.getOutDegree() + 1);
  113. destinationVertex.setInDegree(destinationVertex.getInDegree() + 1);
  114. return true;
  115. }
  116. return false;
  117. }
  118.  
  119. boolean addVertex(T item) {
  120. if (!containsVertex(item)) {
  121. Vertex<T> vertex = new Vertex<>(item);
  122. vertexMap.put(item, vertex);
  123. return true;
  124. }
  125. return false;
  126. }
  127.  
  128. int getNumberOfEdges() {
  129. int numberOfEdges = 0;
  130. for (Vertex<T> vertex : vertexMap.values()) {
  131. numberOfEdges += vertex.getEdgeList().size();
  132. }
  133. return numberOfEdges;
  134. }
  135.  
  136. int getNumberOfVertices() {
  137. return vertexMap.size();
  138. }
  139.  
  140. boolean isEmpty() {
  141. return getNumberOfVertices() == 0;
  142. }
  143.  
  144. boolean containsVertex(T item) {
  145. return vertexMap.containsKey(item);
  146. }
  147.  
  148. public String toString() {
  149. String output = "";
  150. for (Vertex<T> vertex : vertexMap.values()) {
  151. output += vertex.getItem() + ":\tin-degree " + vertex.getInDegree()
  152. + "\tout-degree " + vertex.getOutDegree() + "\n";
  153. output += "\tEdges: ";
  154. for (Edge edge : vertex.getEdgeList()) {
  155. output += edge.getDestination() + "(" + edge.getWeight() + ")\t";
  156. }
  157. output += "\n";
  158. }
  159. return output;
  160. }
  161.  
  162. int getInDegree(T item) {
  163. if (containsVertex(item)) {
  164. Vertex<T> vertex = vertexMap.get(item);
  165. return vertex.getInDegree();
  166. }
  167. return -1;
  168. }
  169.  
  170. int getOutDegree(T item) {
  171. if (containsVertex(item)) {
  172. Vertex<T> vertex = vertexMap.get(item);
  173. return vertex.getOutDegree();
  174. }
  175. return -1;
  176. }
  177.  
  178. private Edge getEdge(T sourceItem, T destinationItem) {
  179. if (containsVertex(sourceItem)) {
  180. Vertex<T> sourceVertex = vertexMap.get(sourceItem);
  181. for (Edge edge : sourceVertex.getEdgeList()) {
  182. if (edge.getDestination().equals(destinationItem)) {
  183. return edge;
  184. }
  185. }
  186. }
  187. return null;
  188. }
  189.  
  190. boolean containsEdge(T sourceItem, T destinationItem) {
  191. return getEdge(sourceItem, destinationItem) != null;
  192. }
  193.  
  194. int getWeight(T sourceItem, T destinationItem) {
  195. Edge edge = getEdge(sourceItem, destinationItem);
  196. if (edge != null) {
  197. return edge.getWeight();
  198. }
  199. return -1;
  200. }
  201.  
  202. boolean setWeight(T sourceItem, T destinationItem, int weight) {
  203. Edge edge = getEdge(sourceItem, destinationItem);
  204. if (edge != null) {
  205. edge.setWeight(weight);
  206. return true;
  207. }
  208. return false;
  209. }
  210.  
  211. Set getNeighbors(T item) {
  212. Set neighborsSet = new TreeSet<>();
  213. if (containsVertex(item)) {
  214. List<Edge> vertexEdgeList = vertexMap.get(item).getEdgeList();
  215. for (Edge edge : vertexEdgeList) {
  216. neighborsSet.add(edge.getDestination());
  217. }
  218. return neighborsSet;
  219. } else {
  220. throw new IllegalArgumentException("Vertex " + item + " is not member of graph");
  221. }
  222. // exception belum berjalan
  223. }
  224.  
  225. boolean removeEdge(T sourceItem, T destinationItem) {
  226. Edge removedEdge = getEdge(sourceItem, destinationItem);
  227. if (removedEdge != null) {
  228. Vertex<T> sourceVertex = vertexMap.get(sourceItem);
  229. Vertex<T> destinationVertex = vertexMap.get(destinationItem);
  230. sourceVertex.getEdgeList().remove(removedEdge);
  231. sourceVertex.setOutDegree(sourceVertex.getOutDegree() - 1);
  232. destinationVertex.setInDegree(destinationVertex.getInDegree() - 1);
  233. return true;
  234. }
  235. return false;
  236. }
  237.  
  238. boolean removeVertex(T item) {
  239. if (containsVertex(item)) {
  240. Vertex<T> removedVertex = vertexMap.get(item);
  241. List<Edge> removedVertexEdgeList = removedVertex.getEdgeList();
  242. if (!removedVertexEdgeList.isEmpty()) {
  243. for (Edge edge : removedVertexEdgeList) {
  244. T destination = (T) edge.getDestination();
  245. Vertex<T> destinationVertex = vertexMap.get(destination);
  246. destinationVertex.setInDegree(destinationVertex.getInDegree() - 1);
  247. }
  248. }
  249. for (T key : vertexMap.keySet()) {
  250. Vertex<T> vertex = vertexMap.get(key);
  251. Edge edgeToRemovedVertex = getEdge(key, item);
  252. if (edgeToRemovedVertex != null) {
  253. vertex.getEdgeList().remove(edgeToRemovedVertex);
  254. }
  255. }
  256. vertexMap.remove(item);
  257. return true;
  258. }
  259. return false;
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement