From b95f86bbd1c3467e62ab988c5d73ed7291570220 Mon Sep 17 00:00:00 2001 From: Vanshika Jalan Date: Sun, 20 Oct 2019 18:35:29 +0530 Subject: [PATCH] adding code to implement graph structured stack --- graph.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 graph.cpp diff --git a/graph.cpp b/graph.cpp new file mode 100644 index 0000000..23c6284 --- /dev/null +++ b/graph.cpp @@ -0,0 +1,127 @@ +/* + * C++ Program to Implement Graph Structured Stack + */ +#include +#include +#include +#include +using namespace std; + +/* + * Class Graph Structured Stack + */ +class GraphStructuredStack +{ + private: + list< stack > stackList; + stack mystack; + int numberOfNodes; + int **adjacencyMatrix; + int *parent; + public: + GraphStructuredStack(int numberOfNodes) + { + this->numberOfNodes = numberOfNodes; + adjacencyMatrix = new int* [numberOfNodes + 1]; + this->parent = new int [numberOfNodes + 1]; + for (int i = 0; i < numberOfNodes + 1; i++) + adjacencyMatrix[i] = new int [numberOfNodes + 1]; + } + /* + * Implement Graph Structured Stack + */ + void graphStructuredStack(int **adjacencyMatrix, int source,int bottomNode) + { + bool stackFound = false; + for (int sourceVertex = 1; sourceVertex <= numberOfNodes; sourceVertex++) + { + for (int destinationVertex = 1; destinationVertex <= numberOfNodes; destinationVertex++) + { + this->adjacencyMatrix[sourceVertex][destinationVertex] + = adjacencyMatrix[sourceVertex][destinationVertex]; + } + } + + mystack.push(source); + int element, destination; + while (!mystack.empty()) + { + element = mystack.top(); + destination = 1; + while (destination <= numberOfNodes) + { + if (this->adjacencyMatrix[element][destination] == 1) + { + mystack.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 istack; + for (int node = bottomNode; node != source; node = parent[node]) + { + istack.push(node); + } + istack.push(source); + stackList.push_back(istack); + stackFound = false; + } + mystack.pop(); + } + list >::iterator iterator; + iterator = stackList.begin(); + while (iterator != stackList.end()) + { + + stack stack = *iterator; + iterator++; + while (!stack.empty()) + { + cout<>numberofnodes; + GraphStructuredStack gss(numberofnodes); + int source, bottom; + int **adjacencyMatrix; + adjacencyMatrix = new int* [numberofnodes + 1]; + for (int i = 0; i < numberofnodes + 1; i++) + adjacencyMatrix[i] = new int [numberofnodes + 1]; + cout<<"Enter the graph matrix: "<>adjacencyMatrix[sourceVertex][destinationVertex]; + } + } + cout<<"Enter the source node: "; + cin>>source; + cout<<"Enter the bottom node: "; + cin>>bottom; + cout<<"The stacks are: "<