Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package signalFlowGraph;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class AnalyzeSFG {
- private int[][] adjMatrix;
- private int numberOfNodes;
- private String path = new String();
- private int forwardPathGain = 1;
- private ArrayList<String> forwardPath = new ArrayList<>();
- private ArrayList<Integer> forwardGains = new ArrayList<>();
- public static void main(String[] args) {
- AnalyzeSFG g = new AnalyzeSFG();
- g.inputs();
- g.printMatrix();
- g.traverseForwardPath(0);
- g.print();
- }
- private void inputs() {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter The Number Of Nodes : ");
- numberOfNodes = scan.nextInt();
- adjMatrix = new int[numberOfNodes][numberOfNodes];
- for (int i = 0; i < this.numberOfNodes; i++)
- Arrays.fill(adjMatrix[i], 0);
- System.out.println("To Get Out From Entering Signal Flow Graph Representation");
- System.out.println("Put From = 0 ");
- while (true) {
- int from, to, gain;
- System.out.print("From : ");
- from = scan.nextInt() - 1;
- if (from < 0) {
- break;
- }
- System.out.print("To : ");
- to = scan.nextInt() - 1;
- System.out.print("gain : ");
- gain = scan.nextInt();
- adjMatrix[to][from] = gain;
- }
- }
- private void printMatrix() {
- for (int i = 0; i < numberOfNodes; i++) {
- for (int j = 0; j < numberOfNodes; j++) {
- System.out.print(adjMatrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- private void print() {
- for (int i = 0; i < this.forwardGains.size(); i++) {
- System.out.println(this.forwardGains.get(i));
- System.out.println(this.forwardPath.get(i));
- }
- }
- private void traverseForwardPath(int columnIndex) {
- if (isLastNode(columnIndex)) {
- addToForwardPath(columnIndex, 1);
- forwardPath.add(path);
- forwardGains.add(forwardPathGain);
- return;
- }
- int rowIndex = -1;
- // for (int columnIndex = nextNodeInThePath; columnIndex <
- // numberOfNodes; columnIndex++) {
- rowIndex = getNextNode(rowIndex + 1, columnIndex);
- addToForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
- traverseForwardPath(rowIndex);
- removeFromForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
- // }
- }
- private void removeFromForwardPath(int currentNode, int gain) {
- String toBeRemoved = "x" + currentNode;
- if (path.contains(toBeRemoved))
- path = path.replace(toBeRemoved, "");
- forwardPathGain /= gain;
- }
- private void addToForwardPath(int currentNode, int gain) {
- path += "x" + currentNode;
- forwardPathGain *= gain;
- }
- private int getNextNode(int rowIndex, int columnIndex) {
- for (; rowIndex < this.numberOfNodes; rowIndex++)
- if (adjMatrix[rowIndex][columnIndex] != 0)
- return rowIndex;
- return -1;
- }
- private boolean isLastNode(int columnIndex) {
- for (int i = 0; i < this.numberOfNodes; i++)
- if (adjMatrix[i][columnIndex] != 0)
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement