PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/DFS/DFS/DFS.cpp

https://bitbucket.org/RazPasca/fundamental-algorithms
C++ | 327 lines | 250 code | 36 blank | 41 comment | 43 complexity | e37e8fdfb25054f5b8e6639aa9c81540 MD5 | raw file
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. //#include "Profiler.h"
  6. using namespace std;
  7. int time = 0;
  8. //Profiler profiler("demo");
  9. typedef struct nodeG nodeG;
  10. typedef struct nodeLista
  11. {
  12. nodeG *node;
  13. struct nodeLista *next;
  14. }nodeLista;
  15. typedef struct lista //folosita pentru lista de adiacenta a fiecarui nod
  16. {
  17. nodeLista *first;
  18. nodeLista *last;
  19. int size;
  20. }lista;
  21. typedef struct queue// coada in care punem nodurile care sunt in curs de vizitare
  22. {
  23. nodeLista *head;
  24. nodeLista *tail;
  25. int size;
  26. }queue;
  27. typedef struct nodeG
  28. {
  29. int key;
  30. int start_time;
  31. int finish_time;
  32. int color;
  33. int dist;
  34. lista *vecini;
  35. }nodeG;
  36. lista* createList()
  37. {
  38. lista *list = (lista*)malloc(sizeof(lista));
  39. list->first = NULL;
  40. list->last = NULL;
  41. list->size = 0;
  42. return list;
  43. }
  44. nodeG* createNodeG(int key)
  45. {
  46. nodeG *node = (nodeG*)malloc(sizeof(nodeG));
  47. node->key = key;
  48. node->vecini = createList();
  49. node->color = 0;
  50. node->start_time = 0;
  51. node->finish_time = 0;
  52. node->dist = 0;
  53. return node;
  54. }
  55. nodeLista* createGraphNodeList(nodeG *node)
  56. {
  57. nodeLista *item = (nodeLista*)malloc(sizeof(nodeLista));
  58. item->node = node;
  59. item->next = NULL;
  60. return item;
  61. }
  62. void addNodeL(lista* list, nodeLista *node)
  63. {
  64. if (list->size == 0)
  65. {
  66. list->first = node;
  67. list->last = node;
  68. list->size = 1;
  69. }
  70. else {
  71. list->last->next = node;
  72. list->last = node;
  73. list->size++;
  74. }
  75. }
  76. queue* createQueue()
  77. {
  78. queue *q = (queue*)malloc(sizeof(queue));
  79. q->head = NULL;
  80. q->tail = NULL;
  81. q->size = 0;
  82. return q;
  83. }
  84. nodeLista* createQueueNode(nodeG *node)
  85. {
  86. nodeLista *elem = (nodeLista*)malloc(sizeof(nodeLista));
  87. elem->node = node;
  88. elem->next = NULL;
  89. return elem;
  90. }
  91. void enqueue(queue* q, nodeLista* element)
  92. {
  93. if (q->size == 0)
  94. {
  95. q->head = element;
  96. q->tail = q->head;
  97. q->size = 1;
  98. }
  99. else {
  100. q->tail->next = element;
  101. q->tail = element;
  102. q->size++;
  103. }
  104. }
  105. nodeLista* dequeue(queue* q)
  106. {
  107. nodeLista *node;
  108. if (q->size == 1)
  109. {
  110. node = q->head;
  111. q->head = NULL;
  112. q->tail = NULL;
  113. q->size = 0;
  114. }
  115. else {
  116. node = q->head;
  117. q->head = q->head->next;
  118. q->size--;
  119. }
  120. return node;
  121. }
  122. nodeG** createGraphNodes(int marime)
  123. {
  124. nodeG **nodes = (nodeG**)malloc(sizeof(nodeG*)*marime);
  125. for (int i = 0; i<marime; i++)
  126. nodes[i] = createNodeG(i);
  127. return nodes;
  128. }
  129. void randomGraphGenerator(nodeG **arr, int verticeNo, int edgeNo)
  130. {
  131. int i, x, y;
  132. nodeLista *iterator;
  133. i = 0;
  134. while (i<edgeNo)
  135. {
  136. do {
  137. x = rand() % verticeNo;
  138. y = rand() % verticeNo;
  139. } while (x == y);
  140. if (x<y)
  141. swap(x, y);
  142. nodeLista *node1 = createGraphNodeList(arr[x]);
  143. nodeLista *node2 = createGraphNodeList(arr[y]);
  144. addNodeL(arr[y]->vecini, node1);
  145. addNodeL(arr[x]->vecini, node2);
  146. i++;
  147. }
  148. for (i = 0; i<verticeNo - 1; i++)
  149. {
  150. if (arr[i]->vecini != NULL && arr[i]->vecini->first != NULL)
  151. {
  152. iterator = arr[i]->vecini->first;
  153. while (iterator != NULL)
  154. {
  155. printf("%d", iterator->node->key);
  156. iterator = iterator->next;
  157. }
  158. printf("\n");
  159. }
  160. }
  161. }
  162. void BFS(nodeG **arr, nodeG *source, int v, int e, char mode)
  163. {
  164. source->color = 1;
  165. source->dist = 0;
  166. // if (mode == 'e')
  167. /// profiler.countOperation("Edge Operations", e, 3);
  168. //else
  169. // profiler.countOperation("Node Operations", v, 3);
  170. queue *q = createQueue();
  171. enqueue(q, createQueueNode(source));
  172. while (q->tail != NULL)
  173. {
  174. nodeG *node = dequeue(q)->node;
  175. nodeLista *vecini = node->vecini->first;
  176. while (vecini != NULL)
  177. {
  178. if (mode == 'e')
  179. profiler.countOperation("Edge Operations", e, 1);
  180. else
  181. profiler.countOperation("Node Operations", v, 1);
  182. if (vecini->node->color == 0)
  183. {
  184. vecini->node->color = 1;
  185. vecini->node->parent = node;
  186. vecini->node->dist = node->dist + 1;
  187. enqueue(q, createQueueNode(vecini->node));
  188. if (mode == 'e')
  189. profiler.countOperation("Edge Operations", e, 3);
  190. else
  191. profiler.countOperation("Node Operations", v, 3);
  192. }
  193. vecini = vecini->next;
  194. }
  195. node->color = 2;
  196. if (mode == 'e')
  197. profiler.countOperation("Edge Operations", e, 1);
  198. else
  199. profiler.countOperation("Node Operations", v, 1);
  200. printf("%d ", node->key);
  201. }
  202. }
  203. int main() {
  204. int i, v, e;
  205. nodeLista *iterator;
  206. //demo
  207. nodeG **demo;
  208. demo = createGraphNodes(7);
  209. nodeLista *node1 = createGraphNodeList(demo[1]);
  210. nodeLista *node2 = createGraphNodeList(demo[2]);
  211. addNodeL(demo[2]->vecini, node1);
  212. addNodeL(demo[1]->vecini, node2);
  213. nodeLista *node3 = createGraphNodeList(demo[1]);
  214. nodeLista *node4 = createGraphNodeList(demo[3]);
  215. addNodeL(demo[3]->vecini, node3);
  216. addNodeL(demo[1]->vecini, node4);
  217. nodeLista *node5 = createGraphNodeList(demo[0]);
  218. nodeLista *node6 = createGraphNodeList(demo[2]);
  219. addNodeL(demo[2]->vecini, node5);
  220. addNodeL(demo[0]->vecini, node6);
  221. nodeLista *node7 = createGraphNodeList(demo[3]);
  222. nodeLista *node8 = createGraphNodeList(demo[4]);
  223. addNodeL(demo[4]->vecini, node7);
  224. addNodeL(demo[3]->vecini, node8);
  225. nodeLista *node9 = createGraphNodeList(demo[4]);
  226. nodeLista *node10 = createGraphNodeList(demo[2]);
  227. addNodeL(demo[2]->vecini, node9);
  228. addNodeL(demo[4]->vecini, node10);
  229. nodeLista *node11 = createGraphNodeList(demo[0]);
  230. nodeLista *node12 = createGraphNodeList(demo[3]);
  231. addNodeL(demo[3]->vecini, node11);
  232. addNodeL(demo[0]->vecini, node12);
  233. nodeLista *node13 = createGraphNodeList(demo[5]);
  234. nodeLista *node14 = createGraphNodeList(demo[6]);
  235. addNodeL(demo[2]->vecini, node13);
  236. addNodeL(demo[3]->vecini, node14);
  237. printf("\nListe de adiacenta:\n");
  238. for (i = 0; i<7; i++)
  239. {
  240. if (demo[i]->vecini != NULL && demo[i]->vecini->first != NULL)
  241. {
  242. printf("Nodul %d ", i);
  243. iterator = demo[i]->vecini->first;
  244. while (iterator != NULL)
  245. {
  246. printf("->%d", iterator->node->key);
  247. iterator = iterator->next;
  248. }
  249. printf("\n");
  250. }
  251. }
  252. printf("\nBFS demo:\n ");
  253. for (i = 0; i<7; i++)
  254. if (demo[i]->color == 0)
  255. BFS(demo, demo[i], 7, 10, '5');
  256. /*
  257. //vertices
  258. nodeG **arr;
  259. srand(time_t(NULL));
  260. v = 100;
  261. for (e=1000; e<=5000; e+=100)
  262. {
  263. arr = createGraphNodes(v);
  264. randomGraphGenerator(arr, v, e);
  265. for (i=0; i<v; i++)
  266. if (arr[i]->color == 0)
  267. {
  268. cout<<endl;
  269. BFS(arr, arr[i], v, e, 'e');
  270. }
  271. }
  272. //edges
  273. e=9000;
  274. for (v=100; v<=200; v+=10)
  275. {
  276. arr = createGraphNodes(v);
  277. randomGraphGenerator(arr, v, e);
  278. for (i=0; i<v; i++)
  279. if (arr[i]->color == 0)
  280. {
  281. cout<<endl;
  282. BFS(arr, arr[i], v, e, 'v');
  283. }
  284. }
  285. profiler.createGroup("Edge Operations", "Operations");
  286. profiler.createGroup("Node Operations", "Operations");
  287. profiler.showReport();*/
  288. return 0;
  289. }