Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package FloydWarshall_Paralel;
  2. /**
  3. * @author cristian.chilipirea
  4. *
  5. */
  6.  
  7. class MyThread extends Thread{
  8. public int threadId;
  9. public int N, P;
  10. public int graph[][];
  11.  
  12. public MyThread(int t, int N, int P, int graph[][]) {
  13. super();
  14. threadId = t;
  15. this.N = N;
  16. this.P = P;
  17. this.graph = graph;
  18. }
  19.  
  20. @Override
  21. public void run() {
  22. int start = threadId * (int)Math.ceil((double)N/P);
  23. int end;
  24. if(N > (threadId + 1) * (int)Math.ceil((double)N/P))
  25. end = (threadId + 1) * (int)Math.ceil((double)N/P);
  26. else
  27. end = N;
  28.  
  29. for (int k = start; k < end; k++) {
  30. for (int i = 0; i < 5; i++) {
  31. for (int j = 0; j < 5; j++) {
  32. graph[i][j] = Math.min(graph[i][k] + graph[k][j], graph[i][j]);
  33. }
  34. }
  35. }
  36. }
  37. }
  38.  
  39.  
  40. public class Main {
  41.  
  42. public static void main(String[] args) {
  43. int M = 9;
  44. int P = 4;
  45. int graph[][] = { { 0, 1, M, M, M },
  46. { 1, 0, 1, M, M },
  47. { M, 1, 0, 1, 1 },
  48. { M, M, 1, 0, M },
  49. { M, M, 1, M, 0 } };
  50.  
  51. MyThread threads[] = new MyThread[P];
  52.  
  53. for(int i = 0; i < P; i++)
  54. threads[i] = new MyThread(i,5, P, graph);
  55.  
  56. for(int i = 0; i < P; i++)
  57. {
  58. threads[i].start();
  59. }
  60.  
  61.  
  62. for(int i = 0; i < P; i++)
  63. {
  64. try {
  65. threads[i].join();
  66. } catch(InterruptedException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70.  
  71. for (int i = 0; i < 5; i++) {
  72. for (int j = 0; j < 5; j++) {
  73. System.out.print(graph[i][j] + " ");
  74. }
  75. System.out.println();
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement