Java Program to Implement Graph Structured Stack

This Java program is to implement graph structured stack. In computer science, a graph-structured stack is a directed acyclic graph where each directed path represents a stack. The graph-structured stack is an essential part of Tomita’s algorithm, where it replaces the usual stack of a pushdown automaton. This allows the algorithm to encode the nondeterministic choices in parsing an ambiguous grammar, sometimes with greater efficiency.

Here is the source code of the Java program to implement graph structured stack. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.Scanner;
  4. import java.util.Stack;
  5.  
  6. public class GraphStructuredStack
  7. {
  8.     private ArrayList<Stack<Integer>> stackList;
  9.     private Stack<Integer> stack;
  10.     private int numberOfNodes;
  11.     private int adjacencyMatrix[][];
  12.     private int[] parent;
  13.  
  14.     public GraphStructuredStack()
  15.     {
  16.         stackList = new ArrayList<Stack<Integer>>();
  17.         stack  = new Stack<Integer>();
  18.     }
  19.  
  20.     public void graphStructuredStack(int adjacencyMatrix[][],int source,int bottomNode)
  21.     {
  22.         boolean stackFound = false;
  23.         this.numberOfNodes = adjacencyMatrix[source].length - 1;
  24.         this.adjacencyMatrix = new int[numberOfNodes + 1][numberOfNodes +1];
  25.         this.parent = new int[numberOfNodes+ 1];
  26.  
  27.         for (int sourceVertex = 1; sourceVertex <= numberOfNodes; sourceVertex++)
  28.         {
  29.             for (int destinationVertex = 1; destinationVertex <= numberOfNodes; destinationVertex++)
  30.             {
  31.                 this.adjacencyMatrix[sourceVertex][destinationVertex] 
  32.                       = adjacencyMatrix[sourceVertex][destinationVertex];
  33.             }
  34.         }
  35.         stack.push(source);
  36.         int element, destination;
  37.         while (!stack.isEmpty())
  38.         {
  39.             element = stack.peek();
  40.             destination = 1;
  41.             while (destination <= numberOfNodes)
  42.             {
  43.                 if (this.adjacencyMatrix[element][destination] == 1)
  44.                 {
  45.                     stack.push(destination);
  46.                     parent[destination] = element;
  47.                     this.adjacencyMatrix[element][destination] = 0;
  48.                     if (destination == bottomNode)
  49.                     {
  50.                         stackFound = true;
  51.                         break;
  52.                     }
  53.                     element = destination;
  54.                     destination = 1;
  55.                     continue;
  56.                 }
  57.                 destination++;
  58.             }
  59.             if (stackFound)
  60.             {
  61.                 Stack<Integer> istack = new Stack<Integer>();
  62.                 for (int node = bottomNode; node != source; node = parent[node])
  63.                 {
  64.                     istack.push(node);
  65.                 }
  66.                 istack.push(source);
  67.                 stackList.add(istack);
  68.                 stackFound = false;
  69.             }
  70.             stack.pop();
  71.         }
  72.  
  73.         Iterator<Stack<Integer>> iterator = stackList.iterator();
  74.         while (iterator.hasNext())
  75.         {
  76.             Stack<Integer> stack = iterator.next();
  77.             Iterator<Integer> stckitr = stack.iterator();
  78.             while (stckitr.hasNext())
  79.             {
  80.                 System.out.print(stckitr.next() +"\t");
  81.             }
  82.             System.out.println();
  83.         }
  84.     }
  85.  
  86.     public static void main(String...arg)
  87.     {
  88.         int adjacencyMatrix[][];
  89.         int numberofnodes;
  90.         int source, bottom;
  91.  
  92.         Scanner scanner = new Scanner(System.in);
  93.         System.out.println("enter the number of nodes");
  94.         numberofnodes = scanner.nextInt();
  95.         adjacencyMatrix = new int[numberofnodes + 1] [numberofnodes + 1];
  96.  
  97.         System.out.println("enter the graph matrix");
  98.         for (int sourceVertex = 1; sourceVertex <= numberofnodes; sourceVertex++)
  99.         {
  100.             for (int destinationVertex = 1; destinationVertex <= numberofnodes; destinationVertex++)
  101.             {
  102.                 adjacencyMatrix[sourceVertex][destinationVertex] = scanner.nextInt();
  103.             }
  104.         }
  105.  
  106.         System.out.println("enter the source node");
  107.         source = scanner.nextInt();
  108.  
  109.         System.out.println("enter the bottom node");
  110.         bottom = scanner.nextInt();
  111.  
  112.         System.out.println("the stacks are");
  113.         GraphStructuredStack graphStructuredStack = new GraphStructuredStack();
  114.         graphStructuredStack.graphStructuredStack(adjacencyMatrix, source, bottom);
  115.         scanner.close();
  116.     }
  117. }


$javac GraphStructuredStack.java
$java GraphStructuredStack
enter the number of nodes
6
enter the graph matrix
0 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
0 1 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
enter the source node
6
enter the bottom node
1
the stacks are
1	2	4	5	6	
1	3	4	5	6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.