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.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;
public class GraphStructuredStack
{
private ArrayList<Stack<Integer>> stackList;
private Stack<Integer> stack;
private int numberOfNodes;
private int adjacencyMatrix[][];
private int[] parent;
public GraphStructuredStack()
{
stackList = new ArrayList<Stack<Integer>>();
stack = new Stack<Integer>();
}
public void graphStructuredStack(int adjacencyMatrix[][],int source,int bottomNode)
{
boolean stackFound = false;
this.numberOfNodes = adjacencyMatrix.length - 1;
this.adjacencyMatrix = new int[numberOfNodes + 1][numberOfNodes +1];
this.parent = new int[numberOfNodes+ 1];
for (int sourceVertex = 1; sourceVertex <= numberOfNodes; sourceVertex++)
{
for (int destinationVertex = 1; destinationVertex <= numberOfNodes; destinationVertex++)
{
this.adjacencyMatrix[sourceVertex][destinationVertex]
= adjacencyMatrix[sourceVertex][destinationVertex];
}
}
stack.push(source);
int element, destination;
while (!stack.isEmpty())
{
element = stack.peek();
destination = 1;
while (destination <= numberOfNodes)
{
if (this.adjacencyMatrix[element][destination] == 1)
{
stack.push(destination);
parent[destination] = element;
this.adjacencyMatrix[element][destination] = 0;
if (destination == bottomNode)
{
stackFound = true;
break;
}
element = destination;
destination = 1;
continue;
}
destination++;
}
if (stackFound)
{
Stack<Integer> istack = new Stack<Integer>();
for (int node = bottomNode; node != source; node = parent[node])
{
istack.push(node);
}
istack.push(source);
stackList.add(istack);
stackFound = false;
}
stack.pop();
}
Iterator<Stack<Integer>> iterator = stackList.iterator();
while (iterator.hasNext())
{
Stack<Integer> stack = iterator.next();
Iterator<Integer> stckitr = stack.iterator();
while (stckitr.hasNext())
{
System.out.print(stckitr.next() +"\t");
}
System.out.println();
}
}
public static void main(String...arg)
{
int adjacencyMatrix[][];
int numberofnodes;
int source, bottom;
Scanner scanner = new Scanner(System.in);
System.out.println("enter the number of nodes");
numberofnodes = scanner.nextInt();
adjacencyMatrix = new int[numberofnodes + 1] [numberofnodes + 1];
System.out.println("enter the graph matrix");
for (int sourceVertex = 1; sourceVertex <= numberofnodes; sourceVertex++)
{
for (int destinationVertex = 1; destinationVertex <= numberofnodes; destinationVertex++)
{
adjacencyMatrix[sourceVertex][destinationVertex] = scanner.nextInt();
}
}
System.out.println("enter the source node");
source = scanner.nextInt();
System.out.println("enter the bottom node");
bottom = scanner.nextInt();
System.out.println("the stacks are");
GraphStructuredStack graphStructuredStack = new GraphStructuredStack();
graphStructuredStack.graphStructuredStack(adjacencyMatrix, source, bottom);
scanner.close();
}
}
$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
Related posts:
Mockito and JUnit 5 – Using ExtendWith
A Guide to Concurrent Queues in Java
Java Program to Implement Selection Sort
Spring Boot - Creating Docker Image
Using Spring @ResponseStatus to Set HTTP Status Code
Introduction to Apache Commons Text
Hướng dẫn Java Design Pattern – Composite
Java Program to Describe the Representation of Graph using Adjacency Matrix
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Find Strongly Connected Components in Graphs
Introduction to Spring Cloud OpenFeign
Tính kế thừa (Inheritance) trong java
Spring Boot - File Handling
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Implement Gauss Jordan Elimination
Java Program to Implement Fermat Primality Test Algorithm
Spring Boot - Service Components
Compare Two JSON Objects with Jackson
Reversing a Linked List in Java
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Construct an Expression Tree for an Prefix Expression
Updating your Password
Java Program to Implement Double Order Traversal of a Binary Tree
Guide to Character Encoding
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement Fermat Factorization Algorithm
HandlerAdapters in Spring MVC
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to implement Circular Buffer