Advertisement
FNSY

Untitled

Apr 13th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. package signalFlowGraph;
  2.  
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Scanner;
  7.  
  8. public class AnalyzeSFG {
  9.  
  10. private int[][] adjMatrix;
  11. private int numberOfNodes;
  12. private String path = new String();
  13. private int forwardPathGain = 1;
  14. private int loopPathGain = 1;
  15. private ArrayList<String> loopsPath = new ArrayList<>();
  16. private ArrayList<String> forwardPath = new ArrayList<>();
  17. private ArrayList<Integer> forwardGains = new ArrayList<>();
  18. private ArrayList<Integer> loopsGains = new ArrayList<>();
  19. private ArrayList<Point> searchLoops = new ArrayList<>();
  20.  
  21. public static void main(String[] args) {
  22. AnalyzeSFG g = new AnalyzeSFG();
  23. g.inputs();
  24. g.printMatrix();
  25. g.traverseForwardPath(0);
  26. // g.loops();
  27. g.print();
  28. }
  29.  
  30. private void makeAllZeros(int[][] matrix) {
  31. for (int i = 0; i < matrix.length; i++)
  32. Arrays.fill(matrix[i], 0);
  33. }
  34.  
  35. private void inputs() {
  36. Scanner scan = new Scanner(System.in);
  37. System.out.print("Enter The Number Of Nodes : ");
  38. numberOfNodes = scan.nextInt();
  39. adjMatrix = new int[numberOfNodes][numberOfNodes];
  40. makeAllZeros(adjMatrix);
  41. System.out.println("To Get Out From Entering Signal Flow Graph Representation");
  42. System.out.println("Put From = 0 ");
  43. while (true) {
  44. int from, to, gain;
  45. System.out.print("From : ");
  46. from = scan.nextInt() - 1;
  47. if (from < 0) {
  48. break;
  49. }
  50. System.out.print("To : ");
  51. to = scan.nextInt() - 1;
  52. System.out.print("gain : ");
  53. gain = scan.nextInt();
  54. adjMatrix[to][from] = gain;
  55. }
  56. addFinalNode();
  57. }
  58.  
  59. private void printMatrix() {
  60. for (int i = 0; i < numberOfNodes; i++) {
  61. for (int j = 0; j < numberOfNodes; j++) {
  62. System.out.print(adjMatrix[i][j] + " ");
  63. }
  64. System.out.println();
  65. }
  66.  
  67. }
  68.  
  69. private void print() {
  70. for (int i = 0; i < this.forwardGains.size(); i++) {
  71. System.out.println(this.forwardGains.get(i));
  72. System.out.println(this.forwardPath.get(i));
  73. }
  74. for (int i = 0; i < this.loopsPath.size(); i++) {
  75. System.out.println(this.loopsPath.get(i));
  76. }
  77. }
  78.  
  79. private int[][] copyMatrix(int[][] copyTo, int[][] copyFrom) {
  80. for (int i = 0; i < copyFrom.length; i++)
  81. for (int j = 0; j < copyFrom.length; j++)
  82. copyTo[i][j] = copyFrom[i][j];
  83. return copyTo;
  84. }
  85.  
  86. private void addFinalNode() {
  87. boolean addColumn = false;
  88. for (int i = 0; i < this.adjMatrix.length; i++)
  89. if (this.adjMatrix[i][this.numberOfNodes - 1] != 0) {
  90. addColumn = true;
  91. break;
  92. }
  93.  
  94. if (addColumn == true) {
  95. int newDimension = this.numberOfNodes + 1;
  96. int[][] copy = new int[newDimension][newDimension];
  97. makeAllZeros(copy);
  98. copy = copyMatrix(copy, adjMatrix);
  99. copy[newDimension - 1][newDimension - 2] = 1;
  100. adjMatrix = copy;
  101. this.numberOfNodes = newDimension;
  102. }
  103. }
  104.  
  105. private void loops() {
  106. // searching for self loops
  107. for (int i = 0; i < numberOfNodes; i++) {
  108. if (adjMatrix[i][i] != 0) {
  109. String selfLoop = "x" + i + "x" + i;
  110. loopsPath.add(selfLoop);
  111. loopsGains.add(adjMatrix[i][i]);
  112. }
  113. }
  114. // finiding possible loops path
  115. for (int i = 1; i < numberOfNodes; i++) {
  116. for (int j = i - 1; j >= 0; j--) {
  117. if (adjMatrix[j][i] != 0) {
  118. // from i to j (reverse path) j <==== i
  119. Point x = new Point(j, i);
  120. searchLoops.add(x);
  121. }
  122. }
  123. }
  124. // search in the forwarc pathes for path from j to i
  125. // if found j ===> i then the is a loop j => i => j
  126. for (int i = 0; i < this.forwardPath.size(); i++) {
  127. String current = this.forwardPath.get(i);
  128. for (int j = 0; j < this.searchLoops.size(); j++) {
  129. int x = this.searchLoops.get(j).x;
  130. String from = "x" + x;
  131. if (current.contains(from)) {
  132. int y = this.searchLoops.get(j).y;
  133. String to = "x" + y;
  134. if (current.contains(to)) {
  135. // this is a loop we must calculate the gain and print
  136. // the loop path
  137. int start = current.lastIndexOf(from) - 1;
  138. int end = current.lastIndexOf(to);
  139. String loop = current.substring(start, end) + current.substring(start, start + 1);
  140. loopsPath.add(loop);
  141. }
  142. }
  143. }
  144. }
  145.  
  146. }
  147.  
  148. private void traverseForwardPath(int columnIndex) {
  149.  
  150. if (isLastNode(columnIndex)) {
  151. addToForwardPath(columnIndex, 1);
  152. forwardPath.add(path);
  153. forwardGains.add(forwardPathGain);
  154. path = path.replace("x"+columnIndex, "");
  155. return;
  156. }
  157.  
  158. int rowIndex = -1;
  159. rowIndex = getNextNode(rowIndex + 1, columnIndex);
  160. while (rowIndex != -1) {
  161. if (rowIndex < columnIndex) {
  162. rowIndex = getNextNode(rowIndex + 1, columnIndex);
  163. continue;
  164. }
  165. addToForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  166. traverseForwardPath(rowIndex);
  167. removeFromForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  168. rowIndex = getNextNode(rowIndex + 1, columnIndex);
  169. }
  170. }
  171.  
  172. private void removeFromForwardPath(int currentNode, int gain) {
  173. String toBeRemoved = "x" + currentNode;
  174. if (path.contains(toBeRemoved))
  175. path = path.replace(toBeRemoved, "");
  176. forwardPathGain /= gain;
  177.  
  178. }
  179.  
  180. private void addToForwardPath(int currentNode, int gain) {
  181. path += "x" + currentNode;
  182. forwardPathGain *= gain;
  183. }
  184.  
  185. private int getNextNode(int rowIndex, int columnIndex) {
  186. for (; rowIndex < this.numberOfNodes; rowIndex++)
  187. if (adjMatrix[rowIndex][columnIndex] != 0)
  188. return rowIndex;
  189. return -1;
  190. }
  191.  
  192. private boolean isLastNode(int columnIndex) {
  193. for (int i = 0; i < this.numberOfNodes; i++)
  194. if (adjMatrix[i][columnIndex] != 0)
  195. return false;
  196. return true;
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement