Advertisement
Guest User

aaa

a guest
Feb 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. package sample;
  2.  
  3. // A naive recursive implementation of optimal binary
  4. // search tree problem
  5. public class Algo
  6. {
  7. /* A Dynamic Programming based function that calculates
  8. minimum cost of a Binary Search Tree. */
  9. static int optimalSearchTree(int keys[], int freq[], int n) {
  10.  
  11. /* Create an auxiliary 2D matrix to store results of
  12. subproblems */
  13. int cost[][] = new int[n + 1][n + 1];
  14. int root[][] = new int[n + 1][n + 1];
  15.  
  16.  
  17. /* cost[i][j] = Optimal cost of binary search tree that
  18. can be formed from keys[i] to keys[j]. cost[0][n-1]
  19. will store the resultant cost */
  20.  
  21. // For a single key, cost is equal to frequency of the key
  22. for (int i = 0; i < n; i++)
  23. cost[i][i] = freq[i];
  24.  
  25. printTable(cost,n);
  26.  
  27.  
  28. // Now we need to consider chains of length 2, 3, ... .
  29. // L is chain length.
  30. for (int L = 2; L <= n; L++) {
  31.  
  32. // i is row number in cost[][]
  33. for (int i = 0; i <= n - L + 1; i++) {
  34.  
  35. // Get column number j from row number i and
  36. // chain length L
  37. int j = i + L - 1;
  38. cost[i][j] = Integer.MAX_VALUE;
  39.  
  40. // Try making all keys in interval keys[i..j] as root
  41. for (int r = i; r <= j; r++) {
  42.  
  43. // c = cost when keys[r] becomes root of this subtree
  44. int ll = 0;
  45. int rr = 0;
  46. int sm = sum(freq, i, j);
  47. int c = 0;
  48. if (r > i)
  49. ll = cost[i][r - 1];
  50. else
  51. ll = 0;
  52. //
  53. if (r < j)
  54. rr += cost[r + 1][j];
  55. else
  56. rr += 0;
  57. //
  58. c = ll + rr + sm;
  59. if (c < cost[i][j]) {
  60. cost[i][j] = c;
  61. System.out.println("ll:"+ll+"\trr:"+rr+"\tsum:"+sm);
  62. }
  63. }
  64. System.out.println("cost["+i+"]["+j+"] = "+cost[i][j]);
  65. }
  66. printTable(cost,n);
  67.  
  68. }
  69. printTable(cost,n);
  70. return cost[0][n - 1];
  71. }
  72.  
  73. static void printTable(int cost[][],int n){
  74. for (int i=0;i<n;i++){
  75. for (int i2=0;i2<n;i2++)
  76. System.out.print(cost[i][i2]+"\t");
  77. System.out.println("");
  78. }
  79. System.out.println("");
  80. }
  81.  
  82. // A utility function to get sum of array elements
  83. // freq[i] to freq[j]
  84. static int sum(int freq[], int i, int j) {
  85. int s = 0;
  86. for (int k = i; k <= j; k++) {
  87. if (k >= freq.length)
  88. continue;
  89. s += freq[k];
  90. }
  91. return s;
  92. }
  93.  
  94. public static void main(String[] args) {
  95.  
  96. //int keys[] = { 10, 12, 20};
  97. //int freq[] = { 34, 8, 50 };
  98. int keys[] = {10,12,16,21};
  99. int freq[] = {4,2,6,3};
  100. int n = keys.length;
  101.  
  102. for (int i = 0;i<n;i++)
  103. System.out.print(i+"\t");
  104. System.out.println("");
  105. for (int i = 0;i<n;i++)
  106. System.out.print(keys[i]+"\t");
  107. System.out.println("");
  108. for (int i = 0;i<n;i++)
  109. System.out.print(freq[i]+"\t");
  110. System.out.println("\n\n");
  111.  
  112. System.out.println("Cost of Optimal BST is "
  113. + optimalSearchTree(keys, freq, n));
  114. }
  115.  
  116. }
  117. //This code is contributed by Sumit Ghosh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement