Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FloydWarshall_Paralel;
- /**
- * @author cristian.chilipirea
- *
- */
- class MyThread extends Thread{
- public int threadId;
- public int N, P;
- public int graph[][];
- public MyThread(int t, int N, int P, int graph[][]) {
- super();
- threadId = t;
- this.N = N;
- this.P = P;
- this.graph = graph;
- }
- @Override
- public void run() {
- int start = threadId * (int)Math.ceil((double)N/P);
- int end;
- if(N > (threadId + 1) * (int)Math.ceil((double)N/P))
- end = (threadId + 1) * (int)Math.ceil((double)N/P);
- else
- end = N;
- for (int k = start; k < end; k++) {
- for (int i = 0; i < 5; i++) {
- for (int j = 0; j < 5; j++) {
- graph[i][j] = Math.min(graph[i][k] + graph[k][j], graph[i][j]);
- }
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- int M = 9;
- int P = 4;
- int graph[][] = { { 0, 1, M, M, M },
- { 1, 0, 1, M, M },
- { M, 1, 0, 1, 1 },
- { M, M, 1, 0, M },
- { M, M, 1, M, 0 } };
- MyThread threads[] = new MyThread[P];
- for(int i = 0; i < P; i++)
- threads[i] = new MyThread(i,5, P, graph);
- for(int i = 0; i < P; i++)
- {
- threads[i].start();
- }
- for(int i = 0; i < P; i++)
- {
- try {
- threads[i].join();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- for (int i = 0; i < 5; i++) {
- for (int j = 0; j < 5; j++) {
- System.out.print(graph[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement