-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGraphStructuredStack.cpp
116 lines (112 loc) · 3.85 KB
/
GraphStructuredStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstdlib>
#include <stack>
#include <list>
using namespace std;
class GraphStructuredStack
{
private:
list< stack<int> > stackList;
stack<int> 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];
}
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<int> 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<stack<int> >::iterator iterator;
iterator = stackList.begin();
while (iterator != stackList.end())
{
stack <int> stack = *iterator;
iterator++;
while (!stack.empty())
{
cout<<stack.top()<<"\t";
stack.pop();
}
cout<<endl;
}
}
};
int main()
{
int numberofnodes;
cout<<"Enter number of nodes: ";
cin>>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: "<<endl;
for (int sourceVertex = 1; sourceVertex <= numberofnodes; sourceVertex++)
{
for (int destinationVertex = 1; destinationVertex <= numberofnodes; destinationVertex++)
{
cin>>adjacencyMatrix[sourceVertex][destinationVertex];
}
}
cout<<"Enter the source node: ";
cin>>source;
cout<<"Enter the bottom node: ";
cin>>bottom;
cout<<"The stacks are: "<<endl;
gss.graphStructuredStack(adjacencyMatrix, source, bottom);
return 0;
}