Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. package ee.ttu.algoritmid.friends;
  2.  
  3. import java.util.*;
  4. import java.util.AbstractMap.SimpleEntry;
  5.  
  6. public class AL06 {
  7. public UndirectedGraph graph = new UndirectedGraph();
  8.  
  9. public class UndirectedGraph {
  10. private HashMap<Integer, List<Integer>> graph = new HashMap<Integer, List<Integer>>();
  11. private ArrayList<Integer> shortPath = new ArrayList<Integer>();
  12.  
  13. /**
  14. * Add undirected edge to the graph.
  15. *
  16. * @param one one element of the edge
  17. * @param other the other element of edge
  18. */
  19. public void addEdge(Integer one, Integer other) {
  20. if (!graph.containsKey(one)) {
  21. List<Integer> edges = new ArrayList<Integer>();
  22. edges.add(other);
  23. graph.put(one, edges);
  24. } else {
  25. if (!graph.get(one).contains(other)) {
  26. graph.get(one).add(other);
  27. }
  28. }
  29. if (!graph.containsKey(other)) {
  30. List<Integer> edges = new ArrayList<Integer>();
  31. edges.add(one);
  32. graph.put(other, edges);
  33. } else {
  34. if (!graph.get(other).contains(one)) {
  35. graph.get(other).add(one);
  36. }
  37. }
  38. }
  39.  
  40. /**
  41. * Return the graph.
  42. * @return the internal graph structure.
  43. */
  44. public HashMap<Integer, List<Integer>> getGraph() {
  45. return graph;
  46. }
  47.  
  48. /**
  49. * Perform breadth first search.
  50. *
  51. * @param start the vertex to start the search from
  52. * @param goal the goal vertex to find
  53. *
  54. * @return the number of vertices of the path from start to goal including start and goal (e.g.,
  55. * start = A, goal = C, path = A, B, C => 3) and the path itself as a list of integers.
  56. * NB! You can return null as path and only return the number of nodes
  57. * that connect the start and goal vertices for partial credit
  58. * (some tests only check for number of nodes)
  59. */
  60. public SimpleEntry<Integer, List<Integer>> breadthFirstSearch(Integer start, Integer goal) {
  61. shortPath.clear();
  62. HashMap<Integer, List<Integer>> currentGraph = this.getGraph();
  63.  
  64. ArrayList<Integer> path = new ArrayList<>();
  65.  
  66. if (start.equals(goal) && currentGraph.containsKey(start)) {
  67. path.add(start);
  68. return new SimpleEntry<Integer, List<Integer>>(1, path);
  69. }
  70.  
  71. ArrayDeque<Integer> queue = new ArrayDeque<>();
  72.  
  73. ArrayDeque<Integer> visitedVertexes = new ArrayDeque<>();
  74.  
  75. queue.offer(start);
  76.  
  77. while (!queue.isEmpty()) {
  78. Integer vertex = queue.poll();
  79. visitedVertexes.offer(vertex);
  80.  
  81. ArrayList<Integer> neighbours = findNeighbours(vertex);
  82.  
  83. for (int i = 0; i < neighbours.size(); i++) {
  84. Integer neighbour = neighbours.get(i);
  85.  
  86. path.add(neighbour);
  87. path.add(vertex);
  88.  
  89. if (neighbour.equals(goal)) {
  90. ArrayList<Integer> goalPath = findShortPath(start, goal, path);
  91. return new SimpleEntry<Integer, List<Integer>>(goalPath.size(), goalPath);
  92. } else {
  93. if (!visitedVertexes.contains(neighbour)) {
  94. queue.offer(neighbour);
  95. }
  96. }
  97. }
  98.  
  99. }
  100. return null;
  101. }
  102.  
  103. private ArrayList<Integer> findNeighbours(Integer vertex) {
  104. List<Integer> neighbours;
  105. Set<Integer> keys = graph.keySet();
  106. for (Integer key : keys) {
  107. if (key.equals(vertex)) {
  108. neighbours = graph.get(key);
  109. return new ArrayList<Integer>(neighbours);
  110. }
  111. }
  112. return new ArrayList<Integer>();
  113. }
  114.  
  115. private ArrayList<Integer> findShortPath(Integer start, Integer goal, ArrayList<Integer> path) {
  116. Integer s = path.get(path.indexOf(goal) + 1);
  117.  
  118. shortPath.add(0, goal);
  119.  
  120. if (s.equals(start)) {
  121. shortPath.add(0, start);
  122. return shortPath;
  123. } else {
  124. return findShortPath(start, s, path);
  125. }
  126. }
  127. }
  128.  
  129. /**
  130. * Use buildGraphAndFindLink to build a graph using the UndirectedGraph class and then use its breadthFirstSearch to
  131. * return the links.
  132. *
  133. * @param friends the list of friends as pairs (people are denoted as integers)
  134. * (e.g., [[2, 7], [9, 5]] means that 2 is friends with 7 and 9 is friends with 5)
  135. * @param pair the pair to be searched
  136. * @return the number of people that connect the searched pair including the pair itself (e.g., if pair is
  137. * = [1, 4] and path is [1, 2, 3, 4], the number of people is 4) the list of people that connect
  138. * the searched pair (e.g., pair = [1, 4] => result = [1, 7, 11, 3, 2, 4])
  139. */
  140. public SimpleEntry<Integer, List<Integer>> buildGraphAndFindLink(List<SimpleEntry<Integer, Integer>> friends,
  141. SimpleEntry<Integer, Integer> pair) {
  142. UndirectedGraph graph = new UndirectedGraph();
  143.  
  144. for (int i = 0; i < friends.size(); i++) {
  145. SimpleEntry<Integer, Integer> friendsPair = friends.get(i);
  146. graph.addEdge(friendsPair.getKey(), friendsPair.getValue());
  147. }
  148.  
  149. return graph.breadthFirstSearch(pair.getKey(), pair.getValue());
  150. }
  151. }
  152.  
  153.  
  154. package ee.ttu.algoritmid.friends;
  155.  
  156. /**
  157. *
  158. */
  159. public class Main {
  160. public static void main(String[] args) {
  161. AL06 impl = new AL06();
  162. impl.graph.addEdge(1, 2);
  163. impl.graph.addEdge(1, 2);
  164. impl.graph.addEdge(3, 2);
  165. System.out.println(impl.graph.getGraph());
  166. System.out.println(impl.graph.breadthFirstSearch(1, 3));
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement