Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6.  
  7. /**
  8. * Computes shortest paths using Dijksta's algorithm modified for MapReduce.
  9. */
  10. public class Dijkstra {
  11.  
  12.  
  13. /**
  14. * Finds the shortest paths from the specified node to all other nodes.
  15. *
  16. * @param adjacencyMatrix the graph represented as an adjacency matrix
  17. * @param startNode the start node
  18. * @return an array containing the lengths of the shortest paths from
  19. * startNode to the node corresponding to the index
  20. */
  21. public static int[] findShortestPaths(final int[][] adjacencyMatrix, final int startNode) {
  22. Mapper mapper = new Mapper() {
  23. @Override
  24. public List<Pair> map(Object node, Object shortestPath) {
  25. List<Pair> list = new ArrayList<>();
  26. for (int i = 0; i <adjacencyMatrix.length ; i++) {
  27. int j = (int) node;
  28. list.add(new Pair(i,(int) shortestPath + adjacencyMatrix[i][j]));
  29. }
  30. return list;
  31. }
  32. };
  33.  
  34. Reducer reducer = new Reducer() {
  35. @Override
  36. public List<Pair> reduce(Object o, List list) {
  37. int tmp = (int) list.get(0);
  38. for (int i = 1; i < list.size(); i++) {
  39. if(tmp > (int) list.get(i)){
  40. tmp = (int) list.get(i);
  41. }
  42. }
  43. List res = new ArrayList();
  44. res.add(new Pair(o,tmp));
  45. return res;
  46. }
  47. };
  48.  
  49. //init
  50. SequentialMapReduce seq = new SequentialMapReduce(mapper,reducer);
  51. // ParallelMapReduce par = new ParallelMapReduce(mapper,reducer,4);
  52. List<Pair> list = new ArrayList<>();
  53. int shortestDistance = 99999;
  54. int shortestNode = startNode;
  55. int key = startNode;
  56.  
  57. //init startlist
  58. for (int i = 0; i < adjacencyMatrix.length; i++) {
  59. list.add(new Pair(i,adjacencyMatrix[startNode][i]));
  60. }
  61.  
  62.  
  63. while(shortestNode != key || shortestNode == startNode ){
  64. shortestNode = key;
  65. //Compute the distance from startNode
  66. Iterable seqIt = seq.submit(list);
  67. Iterator it = seqIt.iterator();
  68. // Iterable parIt = par.submit(list);
  69. // Iterator it = parIt.iterator();
  70.  
  71. //Retrieve node with shortest distance to startNode
  72. while (it.hasNext()) {
  73. Pair<Integer, Integer> pair = (Pair) it.next();
  74. if (pair.getValue() < shortestDistance) {
  75. shortestDistance = pair.getValue();
  76. key = pair.getKey();
  77. }
  78. }
  79. list.clear();
  80. for (int i = 0; i < adjacencyMatrix.length; i++) {
  81. list.add(new Pair(i, adjacencyMatrix[key][i]));
  82. }
  83. }
  84.  
  85. int[] res = new int[list.size()];
  86. for (int r = 0; r < list.size(); r++) {
  87. res[r] = (int) list.get(r).getValue();
  88. }
  89.  
  90. return res;
  91. }
  92.  
  93.  
  94. public static void main(final String[] args) {
  95.  
  96. final int[][] adjacencyMatrix = {
  97. {0, 10, 1, 8, -1, -1},
  98. {10, 0, 7, -1, -1, 2},
  99. {1, 7, 0, 4, -1, 6},
  100. {8, -1, 4, 0, 6, -1},
  101. {-1, -1, -1, 6, 0, 3},
  102. {-1, 2, 6, -1, 3, 0}
  103. };
  104.  
  105. final int[] shortest = findShortestPaths(adjacencyMatrix, 0);
  106. System.out.println(Arrays.toString(shortest));
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement