Guest User

Untitled

a guest
Jan 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. package MatrixGraph;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Iterator;
  5.  
  6. /**
  7. * Implementation of graph algorithms for a (undirected) graph structure
  8. * Considering generic vertex V and edge E types
  9. *
  10. * Works on AdjancyMatrixGraph objects
  11. *
  12. * @author DEI-ESINF
  13. *
  14. */
  15. public class GraphAlgorithms {
  16.  
  17. private static <T> LinkedList<T> reverse(LinkedList<T> list) {
  18. LinkedList<T> reversed = new LinkedList<T>();
  19. Iterator<T> it = list.iterator();
  20. while (it.hasNext()) {
  21. reversed.push(it.next());
  22. }
  23. return reversed;
  24. }
  25.  
  26. /**
  27. * Performs depth-first search of the graph starting at vertex. Calls
  28. * package recursive version of the method.
  29. *
  30. * @param graph Graph object
  31. * @param vertex Vertex of graph that will be the source of the search
  32. * @return queue of vertices found by search (including vertex), null if
  33. * vertex does not exist
  34. *
  35. */
  36. public static <V, E> LinkedList<V> DFS(AdjacencyMatrixGraph<V, E> graph, V vertex) {
  37.  
  38. int index = graph.toIndex(vertex);
  39. if (index == -1) {
  40. return null;
  41. }
  42.  
  43. LinkedList<V> resultQueue = new LinkedList<V>();
  44. resultQueue.add(vertex);
  45. boolean[] knownVertices = new boolean[graph.numVertices];
  46. DFS(graph, index, knownVertices, resultQueue);
  47. return resultQueue;
  48. }
  49.  
  50. /**
  51. * Actual depth-first search of the graph starting at vertex. The method
  52. * adds discovered vertices (including vertex) to the queue of vertices
  53. *
  54. * @param graph Graph object
  55. * @param index Index of vertex of graph that will be the source of the
  56. * search
  57. * @param known previously discovered vertices
  58. * @param verticesQueue queue of vertices found by search
  59. *
  60. */
  61. static <V, E> void DFS(AdjacencyMatrixGraph<V, E> graph, int index, boolean[] knownVertices, LinkedList<V> verticesQueue) {
  62. knownVertices[index] = true;
  63. for (int i = 0; i < graph.numVertices; i++) {
  64. if (graph.edgeMatrix[index][i] != null && knownVertices[i] == false) {
  65. verticesQueue.add(graph.vertices.get(i));
  66. DFS(graph, i, knownVertices, verticesQueue);
  67. }
  68. }
  69. }
  70.  
  71. /**
  72. * Performs breath-first search of the graph starting at vertex. The method
  73. * adds discovered vertices (including vertex) to the queue of vertices
  74. *
  75. * @param graph Graph object
  76. * @param vertex Vertex of graph that will be the source of the search
  77. * @return queue of vertices found by search (including vertex), null if
  78. * vertex does not exist
  79. *
  80. */
  81. public static <V, E> LinkedList<V> BFS(AdjacencyMatrixGraph<V, E> graph, V vertex) {
  82. if (graph.checkVertex(vertex)) {
  83.  
  84. LinkedList<V> qbfs = new LinkedList<>();
  85. LinkedList<V> qaux = new LinkedList<>();
  86. boolean[] hasVisited = new boolean[graph.numVertices];
  87. int posVertex = graph.toIndex(vertex);
  88.  
  89. qbfs.add(vertex);
  90. qaux.add(vertex);
  91. hasVisited[posVertex] = true;
  92.  
  93. while (!qaux.isEmpty()) {
  94. vertex = qaux.remove();
  95.  
  96. for (V vert : graph.directConnections(vertex)) {
  97. int index = graph.toIndex(vert);
  98. if (hasVisited[index] == false) {
  99. qbfs.add(vert);
  100. qaux.add(vert);
  101. hasVisited[index] = true;
  102. }
  103. }
  104.  
  105. }
  106. return qbfs;
  107. } else {
  108. return null;
  109. }
  110.  
  111. }
  112.  
  113. /**
  114. * All paths between two vertices Calls recursive version of the method.
  115. *
  116. * @param graph Graph object
  117. * @param source Source vertex of path
  118. * @param dest Destination vertex of path
  119. * @param path LinkedList with paths (queues)
  120. * @return false if vertices not in the graph
  121. *
  122. */
  123. public static <V, E> boolean allPaths(AdjacencyMatrixGraph<V, E> graph, V source, V dest, LinkedList<LinkedList<V>> paths) {
  124. if (graph.checkVertex(source) && graph.checkVertex(dest)) {
  125. int sourceIndex = graph.toIndex(source), destIndex = graph.toIndex(dest);
  126. boolean knownVertices[] = new boolean[graph.numVertices];
  127.  
  128. LinkedList<V> auxList = new LinkedList<>();
  129.  
  130. allPaths(graph, sourceIndex, destIndex, knownVertices, auxList, paths);
  131. return true;
  132.  
  133. }
  134. return false;
  135. }
  136.  
  137. /**
  138. * Actual paths search The method adds vertices to the current path (stack
  139. * of vertices) when destination is found, the current path is saved to the
  140. * list of paths
  141. *
  142. * @param graph Graph object
  143. * @param sourceIdx Index of source vertex
  144. * @param destIdx Index of destination vertex
  145. * @param knownVertices previously discovered vertices
  146. * @param auxStack stack of vertices in the path
  147. * @param path LinkedList with paths (queues)
  148. *
  149. */
  150. static <V, E> void allPaths(AdjacencyMatrixGraph<V, E> graph, int sourceIdx,
  151. int destIdx, boolean[] knownVertices, LinkedList<V> auxStack, LinkedList<LinkedList<V>> paths) {
  152. V vOrig = graph.vertices.get(sourceIdx);
  153. V vDest = graph.vertices.get(destIdx);
  154. knownVertices[sourceIdx] = true;
  155.  
  156. auxStack.add(vOrig);
  157.  
  158. for (V vAdjacent : graph.directConnections(vOrig)) {
  159. int vAdjacentIndex = graph.vertices.indexOf(vAdjacent);
  160. if (vAdjacent == vDest) {
  161. auxStack.add(graph.vertices.get(vAdjacentIndex));
  162. paths.add(auxStack);
  163. auxStack.removeLast();
  164. } else if (knownVertices[vAdjacentIndex] == false) {
  165. allPaths(graph, vAdjacentIndex, destIdx, knownVertices, auxStack, paths);
  166.  
  167. }
  168. }
  169. knownVertices[sourceIdx] = false;
  170. auxStack.removeLast();
  171. }
  172.  
  173. /**
  174. * Transforms a graph into its transitive closure uses the Floyd-Warshall
  175. * algorithm
  176. *
  177. * @param graph Graph object
  178. * @param dummyEdge object to insert in the newly created edges
  179. * @return the new graph
  180. */
  181. public static <V, E> AdjacencyMatrixGraph<V, E> transitiveClosure(AdjacencyMatrixGraph<V, E> graph, E dummyEdge) {
  182. AdjacencyMatrixGraph<V, E> n = (AdjacencyMatrixGraph<V, E>) graph.clone();
  183. for (int k = 0; k < n.numVertices; k++) {
  184. for (int i = 0; i < n.numVertices; i++) {
  185. if (i != k && n.edgeMatrix[i][k] != null) {
  186. for (int j = 0; j < n.numVertices; j++) {
  187. if (i != j && j != k && n.edgeMatrix[k][j] != null) {
  188. n.insertEdge(i, j, dummyEdge);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. return n;
  195. }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment