Advertisement
strikero

warshall

Mar 13th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package placeholder;
  7.  
  8.  
  9.  
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTextArea;
  18. import javax.swing.JTextField;
  19. import javax.swing.ScrollPaneConstants;
  20.  
  21. public class WarshallProgram extends JFrame implements ActionListener {
  22.  
  23. JLabel titleLabel;
  24. JTextField input[][] = new JTextField[4][4];
  25. JButton computeBtn;
  26. JButton clearBtn;
  27. JScrollPane scroll;
  28. JTextArea resultArea;
  29. String result = "";
  30.  
  31. public WarshallProgram() {
  32. super("Warshall Program");
  33. setLayout(null);
  34. int r = 20;
  35. titleLabel = new JLabel("Warshall Algorithm");
  36. computeBtn = new JButton("Calculate");
  37. clearBtn = new JButton("Clear");
  38. resultArea = new JTextArea("");
  39. scroll = new JScrollPane(resultArea);
  40. scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  41.  
  42. titleLabel.setBounds(20, 20, 500, 20);
  43. computeBtn.setBounds(300, 75, 100, 40);
  44. clearBtn.setBounds(300, 150, 100, 40);
  45. scroll.setBounds(20, 225, 400, 150);
  46. resultArea.setLineWrap(true);
  47. resultArea.setWrapStyleWord(true);
  48. resultArea.setEditable(false);
  49.  
  50. for (int i = 0; i < 4; i++) {
  51. int c = 0;
  52. r += 40;
  53. for (int j = 0; j < 4; j++) {
  54. c += 60;
  55. input[i][j] = new JTextField("");
  56. input[i][j].setBounds(c, r, 30, 25);
  57. add(input[i][j]);
  58. if(i==j){
  59. input[i][j].setText("0");
  60. input[i][j].setEditable(false);
  61. }
  62. }
  63. }
  64.  
  65. add(clearBtn);
  66. add(titleLabel);
  67. add(computeBtn);
  68. add(scroll);
  69.  
  70. clearBtn.addActionListener(this);
  71. computeBtn.addActionListener(this);
  72. setVisible(true);
  73. setSize(475, 450);
  74. setLocation(300, 100);
  75. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76. }
  77.  
  78. @Override
  79. public void actionPerformed(ActionEvent e) {
  80. if (e.getSource() == computeBtn) {
  81. int n=0;
  82. for(int i=0; i<4; i++){
  83. for(int j=0; j<4; j++){
  84. if ((input[i][j].getText().equals("1")) || (input[i][j].getText().equals("0"))){
  85. n+=1;
  86. }
  87. }
  88. }
  89.  
  90. if(n!=16){
  91. JOptionPane.showMessageDialog(this, "Invalid input. Please enter 1 or 0 only.",
  92. "Error", JOptionPane.ERROR_MESSAGE);
  93. }
  94. else{
  95. result = "Result: \n";
  96. int inputs[][] = new int[4][4];
  97. for(int i=0; i<4; i++){
  98. for(int j=0; j<4; j++){
  99. inputs[i][j] = Integer.parseInt(input[i][j].getText());
  100. }
  101. }
  102.  
  103. //for testing
  104. //int sample[][] = {{0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 1, 0, 0}};
  105. WarshallSolution(inputs, 4);
  106. resultArea.setText(result);
  107. }
  108. }
  109. if (e.getSource() == clearBtn) {
  110. for (int i = 0; i < 4; i++) {
  111. for (int j = 0; j < 4; j++) {
  112. input[i][j].setText("");
  113. }
  114. }
  115. input[0][0].setText("0");
  116. input[1][1].setText("0");
  117. input[2][2].setText("0");
  118. input[3][3].setText("0");
  119. result = "";
  120. resultArea.setText(result);
  121. }
  122. }
  123.  
  124. public static void main(String[] args) {
  125. Placeholder wp = new Placeholder();
  126. wp.setVisible(true);
  127. }
  128.  
  129. /**
  130. * This method holds the process for the Warshall Algorithm.
  131. * @param inputs array - user input of matrix
  132. * @param size size of the matrix
  133. */
  134. public void WarshallSolution(int[][] inputs, int size) {
  135.  
  136. //displaying the original input
  137. result += "R(" + 0 + "):\n";
  138. for (int j = 0; j < 4; j++) {
  139. for (int k = 0; k < 4; k++) {
  140. result += inputs[j][k] + "\t";
  141. }
  142. result+="\n";
  143. }
  144. result+="\n\n";
  145.  
  146. //warshall algo process
  147. warshall(0,inputs,size);
  148.  
  149. }
  150. public void warshall(int n, int[][] inputs,int size){
  151. if(n==size){
  152.  
  153. return;
  154. }
  155. for (int r = 0; r < size; r++) {
  156. if (r == n) {
  157. continue;
  158. }
  159. for (int c = 0; c < size; c++) {
  160. if (c == n) {
  161. continue;
  162. }
  163. int prod = inputs[r][n] * inputs[n][c];
  164. if ((prod == 1 || inputs[r][c] == 1)) {
  165. inputs[r][c] = 1;
  166. }
  167. }
  168. }
  169. result += "R(" + (n + 1) + "):\n";
  170. for (int j = 0; j < 4; j++) {
  171. for (int k = 0; k < 4; k++) {
  172. result += inputs[j][k] + "\t";
  173. }
  174. result += "\n";
  175. }
  176. result += "\n\n";
  177. warshall(n+1,inputs,size);
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement