floyd - } // Returns the smaller of a and b. public static...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// Arup Guha // 6/25/02 // Floyd Warshall's algorithm: an example of dynamic programming. public class floyd { public static int[][] shortestpath(int[][] adj) { int n = adj.length; int[][] ans = new int[n][n]; // Implement algorithm on a copy matrix so that the adjacency isn't // destroyed. copy(ans, adj); // Compute successively better paths through vertex k. for (int k=0; k<n;k++) { // Do so between each possible pair of points. for (int i=0; i<n; i++) { for (int j=0; j<n;j++) ans[i][j] = min(ans[i][j], ans[i][k]+ans[k][j]); } } // Return the shortest path matrix. return ans; } // Copies the contents of array b into array a. Assumes that both a and // b are 2D arrays of identical dimensions. public static void copy(int[][] a, int[][] b) { for (int i=0;i<a.length;i++) for (int j=0;j<a[0].length;j++) a[i][j] = b[i][j];
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } // Returns the smaller of a and b. public static int min(int a, int b) { if (a < b) return a; else return b; } public static void main(String args) { // Tests out algorithm with graph shown in class. int m = new int[5][5]; m[0][0] = 0; m[0][1] = 3; m[0][2] = 8; m[0][3] = 10000; m[0][4] = -4; m[1][0] = 10000; m[1][1] = 0; m[1][2] = 10000; m[1][3] = 1; m[1][4]=7; m[2][0] = 10000; m[2][1] = 4; m[2][2] = 0; m[2][3] = 10000; m[2][4] = 10000; m[3][0] = 2; m[3][1] = 10000; m[3][2] = -5; m[3][3] = 0; m[3][4] = 10000; m[4][0] = 10000; m[4][1] = 10000; m[4][2] = 10000; m[4][3] = 6; m[4][4] =0; int shortpath; shortpath = shortestpath(m); for (int i=0; i<5;i++) { for (int j=0; j<5;j++) System.out.print(shortpath[i][j]+" "); System.out.println(); } } }...
View Full Document

This document was uploaded on 11/09/2009.

Page1 / 2

floyd - } // Returns the smaller of a and b. public static...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online