Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4.  
  5.  
  6. public class Graph2<V,E> extends Object{
  7.  
  8. public ArrayList<Node> nodeList;
  9. public ArrayList<Edge> edgeList;
  10. /* Default Constructor
  11. *Intializes listV, the list of all the vertices in garph
  12. */
  13. Graph2(){
  14. nodeList = new ArrayList<Node>();
  15. edgeList = new ArrayList<Edge>();
  16.  
  17.  
  18. }
  19.  
  20. //accessor for the nodes list
  21. public ArrayList<Node> getNodeList(){
  22. return nodeList;
  23. }
  24.  
  25. //accessor for the edges list
  26. public ArrayList<Edge> getEdgeList(){
  27. return edgeList;
  28. }
  29.  
  30. /**
  31. * @return the size of the nodes list in Graph
  32. */
  33. public int getSizeNodeList(){
  34. return nodeList.size();
  35. }
  36.  
  37. /**
  38. * @return the size of the edge list in Graph
  39. */
  40. public ArrayList<Edge> getSizeEdgeList(){
  41. return edgeList.size();
  42. }
  43.  
  44.  
  45.  
  46. /**
  47. * @return sizeE The number of edges in Graph21_
  48. */
  49. public int getSizeE() {
  50. return sizeE;
  51. }
  52. /**
  53. * add a new vertex with key k, allows no duplicates.
  54. * @param v Key of the vertex
  55. * @param e Element of the vertex
  56. * @return true If successful; false otherwise(if duplicates found).
  57. */
  58.  
  59.  
  60. //adds a new node and returns the new node to be added to the list of nodes
  61. public Node addNode(V data){
  62. Node newNode = new Node(data);
  63. nodeList.add(newNode);
  64. return newNode;
  65. }
  66.  
  67. //deletes a node from the list of nodes
  68. public void deleteNode(Node newNode){
  69. Edge edge;
  70. do{
  71. edge = newNode.edgeReference().get(0);
  72. deleteEdge(edge);
  73. }
  74. while(newNode.edgeReference().isEmpty() == false);
  75. nodeList.remove(newNode);
  76. }
  77.  
  78. //deletes an edge from the list of edges
  79. public void deleteEdge(Edge edge){
  80. Node tail = edge.returnTail();
  81. Node head = edge.returnHead();
  82. tail.deleteEdgeReference(edge);
  83. head.deleteEdgeReference(edge);
  84. edgeList.remove(edge);
  85. }
  86.  
  87.  
  88.  
  89. //accessor for the edge List
  90. public Edge getEdge(int j){
  91. return edgeList.get(j);
  92. }
  93.  
  94. //accessor for an edge
  95. public Edge edgeReference(Node head, Node tail){
  96. return head.edgeTo(tail);
  97. }
  98.  
  99. //accessor for the nodes List
  100. public Node getNode(int j){
  101. return nodesList.get(j);
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /**
  110. * adds an edge, returns an edge, checks for data head and tail
  111. */
  112. public Edge addEdge(E data, Node head, Node tail) {
  113. Edge edge = new Edge(data, head, tail);
  114. tail.addToEdgeList(edge);
  115. head.addToEdgeList(edge);
  116. edgeList.add(edge);
  117. return edge;
  118. }
  119.  
  120.  
  121.  
  122. //removes the edge with head, tail, and node
  123. public void deleteEdge(Node head, node Tail){
  124. Edge edge = head.edgeTo(tail);
  125. deleteEdge(edge);
  126. }
  127.  
  128.  
  129. //gets the data from the node and edge and prints graph
  130. public void printGraph(){
  131. for(Node newNode: nodeList){
  132. if(newNode.getAdjacentNodes(newNode).isEmpty()){
  133. System.out.println(newNode.returnData());
  134. }
  135. }
  136. for(Edge edge: edgeList){
  137. System.out.println(edge.returnData());
  138. }
  139. }
  140.  
  141. //Edge class that creates an edge
  142. public class Edge{
  143.  
  144. //initializes the value of the head
  145. private Node head;
  146.  
  147. //initializes teh value of the tail
  148. private Node tail;
  149.  
  150. //data for edge
  151. private E data;
  152.  
  153. //accesses the head
  154. public Node returnHead(){
  155. return head;
  156. }
  157.  
  158. //accesses the tail
  159. public Node returnTail(){
  160. return tail;
  161. }
  162.  
  163. //accesses the data of the edge
  164. public E returnData(){
  165. return data;
  166. }
  167.  
  168. //constructor that creates an edge
  169. Edge(E data, Node head, Node tail){
  170. this.tail=tail;
  171. this.head=head;
  172. this.data=data;
  173. }
  174.  
  175. //fixes the data for the edge
  176. public void fixData(E data){
  177. this.data=data;
  178. }
  179.  
  180. public boolean equals(Object b){
  181. if(b.getClass().equals(Edge.class) == false){
  182. System.err.println("should be an edge");
  183. System.exit(-1);
  184. }
  185. Edge edge = (Edge)b;
  186. Node tail0 = this.tail;
  187. Node head0 = this.head;
  188. Node tail1= edge.tail;
  189. Node head1 = edge.head;
  190. if(tail0 == head1 && tail1 == head0){
  191. return true;
  192. }
  193. else if(tail1 == tail0 && head0 == head1){
  194. return true;
  195. }
  196. else{
  197. return false;
  198. }
  199.  
  200. }
  201.  
  202. //puts a node in the graph
  203. public class Node{
  204.  
  205. //list of edges that the nodes are related to
  206. public ArrayList<Edge> edgeReference;
  207.  
  208. // initializes the value of the node
  209. private V data;
  210.  
  211. //accessor for the edge reference
  212. public ArrayList<Edge> returnEdgeReference(){
  213. return edgeReference;
  214. }
  215.  
  216. //accessor for the data
  217. public V returnData(){
  218. return data;
  219. }
  220.  
  221. //fixes for the data for the node
  222. public void fixData(){
  223. this.data = data;
  224. }
  225.  
  226. //adds to the edge list
  227. public void addToEdgeList(Edge edge){
  228. edgeReference.add(edge);
  229. }
  230.  
  231. //deletes an edge reference
  232. public void deleteEdgeReference(Edge edge){
  233. edgeReference.remove(edge);
  234. }
  235.  
  236. //returns an array list of the adjacent nodes
  237. public ArrayList<Node> getAdjacentNodes(Node newNode){
  238. ArrayList<Node> adjacentNodes = new ArrayList<Node>();
  239. for(Edge edge: edgeReference){
  240. if(edge.returnHead() == newNode){
  241. adjacentNodes.add(edge.returnTail());
  242. }
  243. else if(edge.returnTail() == newNode){
  244. adjacentNodes.add(edge.returnHead());
  245. }
  246. }
  247. return adjacentNodes;
  248. }
  249.  
  250. //
  251. public Edge checkEdge(Node adjacentNode){
  252. Edge check = new Edge(null, this, adjacentNode);
  253. for(Edge edge: this.edgeReference){
  254. if(edge.equals(test)){
  255. return edge;
  256. }
  257. }
  258. if(this.isAdjacent(adjacentNode) == false){
  259. return null;
  260. }
  261. return null;
  262. }
  263.  
  264. //returns either true or false depending on whether the node is adjacent to the node we are working with
  265. public boolean isAdjacent(Node newNode){
  266. ArrayList<Node> adjacentNodes = getAdjacentNodes(this);
  267. return adjacentNodes.contains(newNode);
  268. }
  269.  
  270.  
  271. }
  272.  
  273.  
  274.  
  275.  
  276. }
  277. }
  278. /*
  279. public int getEdge(V v1, V v2){
  280. Node<V,E> source,target;
  281. source = getNode(v1);
  282. target = getNode(v2);
  283. for (Edge<V,E> tmp : source.adj) {
  284. if (tmp.target.isDuplicate(target)) {
  285. // return weight if edge found
  286. return tmp.weight;
  287. }
  288. }
  289. //if no edge is found.
  290. return -1;
  291. }
  292. }
  293. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement