Guest User

Untitled

a guest
May 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. #include <iostream>
  2. //#include "Stack.cpp"
  3. //#include "Queue.cpp"
  4.  
  5. #ifndef NULL_VALUE
  6. #define NULL_VALUE -999999
  7. #endif
  8.  
  9. #ifndef INFINITY
  10. #define INFINITY 999999
  11. #endif
  12.  
  13. template <class T>
  14. class Stack
  15. {
  16. public:
  17. Stack (int); // create a stack with given capacity
  18. ~Stack();
  19. void push(T); // push at the end
  20. void pop(); // pop from the end
  21. T peek(); // return the last item
  22. int size() { return top+1; }
  23.  
  24. private:
  25. T * arr;
  26. int capacity;
  27. int top;
  28. };
  29.  
  30. template <class T>
  31. Stack<T>::Stack(int capacity) {
  32. this->capacity = capacity;
  33. top = -1;
  34. arr = new T[capacity];
  35. }
  36.  
  37. template <class T>
  38. Stack<T>::~Stack() {
  39. if (arr) delete[] arr;
  40. arr = NULL;
  41. }
  42.  
  43. template <class T>
  44. void Stack<T>::push(T value) {
  45. if (size() >= capacity) {
  46. std::cout << "Stack is full." << std::endl;
  47. return ;
  48. }
  49.  
  50. top++;
  51. arr[top] = value;
  52. }
  53.  
  54. template <class T>
  55. void Stack<T>::pop() {
  56. if (size() <= 0) {
  57. std::cout << "Stack is empty." << std::endl;
  58. return ;
  59. }
  60. top--;
  61. }
  62.  
  63. template <class T>
  64. T Stack<T>::peek() {
  65. if (size() <= 0)
  66. return NULL_VALUE;
  67. return arr[top];
  68. }
  69.  
  70.  
  71. template <class T>
  72. class Queue
  73. {
  74. public:
  75. Queue (int); // create queue with given capacity
  76. ~Queue();
  77. void enqueue(T); // push item at the last
  78. void dequeue(); // pop item from the beginning
  79. T first(); // return item at the beginning
  80. T last(); // return item at the end
  81. int size(); // return the size
  82.  
  83. private:
  84. T * arr;
  85. int capacity;
  86. int head;
  87. int tail;
  88. int length;
  89. };
  90.  
  91. template <class T>
  92. Queue<T>::Queue(int capacity) {
  93. this->capacity = capacity;
  94. head = 0;
  95. tail = -1;
  96. length = 0;
  97. arr = new T[capacity];
  98. }
  99.  
  100. template <class T>
  101. Queue<T>::~Queue() {
  102. if (arr) delete[] arr;
  103. arr = NULL;
  104. }
  105.  
  106. template <class T>
  107. void Queue<T>::enqueue(T value) {
  108. if (size() >= capacity) {
  109. std::cout << "Queue is full." << std::endl;
  110. return ;
  111. }
  112.  
  113. tail = ( tail + 1 ) % capacity;
  114. arr[tail] = value;
  115. length++;
  116. }
  117.  
  118. template <class T>
  119. void Queue<T>::dequeue() {
  120. if (size() <= 0) {
  121. std::cout << "Queue is empty." << std::endl;
  122. return ;
  123. }
  124.  
  125. head = ( head + 1 ) % capacity;
  126. length--;
  127. }
  128.  
  129. template <class T>
  130. T Queue<T>::first() {
  131. if (size() <= 0) return NULL_VALUE;
  132. return arr[head];
  133. }
  134.  
  135. template <class T>
  136. T Queue<T>::last() {
  137. if (size() <= 0) return NULL_VALUE;
  138. return arr[tail];
  139. }
  140.  
  141. template <class T>
  142. int Queue<T>::size() {
  143. return length;
  144. }
  145.  
  146.  
  147. class Graph {
  148. public:
  149. Graph(bool, int, int);
  150. ~Graph();
  151. bool isEdge(int, int);
  152. void addEdge(int u, int v);
  153. void removeEdge(int u, int v);
  154. int getDegree(int u);
  155. void dfs(int);
  156. void bfs(int);
  157. void topSort(int);
  158. private:
  159. bool isDirected;
  160. int ** adjMat;
  161. int noOfVertices;
  162. int noOfEdges;
  163. Stack<int> * tmpStack;
  164. };
  165.  
  166. Graph::Graph(bool isDirected, int noOfVertices, int noOfEdges) {
  167. this->isDirected = isDirected;
  168. this->noOfVertices = noOfVertices;
  169. this->noOfEdges = noOfEdges;
  170. adjMat = new int * [noOfVertices];
  171. for (int i=0; i<noOfVertices; i++) {
  172. adjMat[i] = new int[noOfVertices];
  173. for (int j=0; j<noOfVertices; ++j)
  174. adjMat[i][j] = 0;
  175. }
  176.  
  177. tmpStack = new Stack<int>(noOfVertices);
  178. }
  179.  
  180. Graph::~Graph() {
  181. for (int i=0; i<noOfVertices; ++i)
  182. if (adjMat[i])
  183. delete[] adjMat[i];
  184. if (adjMat) delete[] adjMat;
  185. adjMat = NULL;
  186. }
  187.  
  188. bool Graph::isEdge(int u, int v) {
  189. return adjMat[u][v];
  190. }
  191.  
  192. void Graph::addEdge(int u, int v)
  193. {
  194. if(u<0 || u>=noOfVertices || v<0 || v>=noOfVertices) return;
  195. adjMat[u][v] = 1;
  196. if(!isDirected) adjMat[v][u] = 1;
  197. }
  198.  
  199. void Graph::removeEdge(int u, int v)
  200. {
  201. if (u<0 || u>=noOfVertices || v<0 || v>=noOfVertices) return;
  202. adjMat[u][v] = 0;
  203. if (!isDirected) adjMat[v][u] = 0;
  204. }
  205.  
  206. void Graph::dfs(int source) {
  207. if (source <0 || source >= noOfVertices) return;
  208.  
  209. bool * visited = new bool[noOfVertices];
  210. bool * adjVertex = new bool[noOfVertices];
  211. for (int i=0; i<noOfVertices; ++i)
  212. visited[i] = false;
  213.  
  214. Stack<int> st(noOfVertices);
  215. st.push(source);
  216.  
  217. while (st.size() > 0) {
  218. source = st.peek();
  219. st.pop();
  220. visited[source] = true;
  221. tmpStack->push(source);
  222. std::cout << source << " " ;
  223.  
  224. int noOfAdjVertices = 0;
  225. for (int i=0; i<noOfVertices; ++i) {
  226. if (isEdge(source, i)) {
  227. noOfAdjVertices++;
  228. adjVertex[i] = true;
  229. } else
  230. adjVertex[i] = false;
  231. }
  232.  
  233. for (int i=0; i<noOfVertices; ++i) {
  234. if (!adjVertex[i]) continue;
  235. else if (!visited[i]) {
  236. st.push(i);
  237. }
  238. }
  239. }
  240. std::cout << std::endl;
  241. }
  242.  
  243. void Graph::bfs(int source) {
  244. if (source <0 || source >= noOfVertices) return;
  245.  
  246. bool * adjVertex = new bool[noOfVertices];
  247. bool * visited = new bool[noOfVertices];
  248. for (int i=0; i<noOfVertices; ++i) {
  249. visited[i] = false;
  250. }
  251.  
  252. Queue<int> q(noOfVertices);
  253. q.enqueue(source);
  254. visited[source] = true;
  255. std::cout << source << " ";
  256.  
  257. while (q.size() > 0) {
  258. source = q.first();
  259. q.dequeue();
  260.  
  261. int noOfAdjVertices = 0;
  262. for (int i=0; i<noOfVertices; ++i) {
  263. if (isEdge(source, i)) {
  264. noOfAdjVertices++;
  265. adjVertex[i] = true;
  266. } else
  267. adjVertex[i] = false;
  268. }
  269. for (int i=0; i<noOfVertices; ++i) {
  270. if (!adjVertex[i]) continue;
  271. else if (!visited[i]) {
  272. q.enqueue(i);
  273. visited[i] = true;
  274. std::cout << i << " ";
  275. }
  276. }
  277. }
  278. std::cout << std::endl;
  279. }
  280.  
  281. void Graph::topSort(int source) {
  282. dfs(source);
  283. while(tmpStack->size() > 0) {
  284. std::cout << tmpStack->peek() ;
  285. tmpStack->pop();
  286. }
  287. }
  288.  
  289. int main() {
  290. Graph g(true, 5, 6);
  291. g.addEdge(0,1);
  292. g.addEdge(0,3);
  293. g.addEdge(1,2);
  294. g.addEdge(1,3);
  295. g.addEdge(2,4);
  296. g.addEdge(3,2);
  297. g.topSort(0);
  298.  
  299. return 0;
  300. }
Add Comment
Please, Sign In to add comment