Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Graphs.h"
  4. #define MAX_GRAPH_SIZE 50
  5. #define MAX_QUEUE_SIZE 50
  6.  
  7. struct adj_list
  8. {
  9. int item;
  10. struct adj_list *next_adj_vertex;
  11. };
  12.  
  13. AdjList* create_adj_list(int item)
  14. {
  15. AdjList *adj_list = (AdjList*) malloc(sizeof(AdjList));
  16. adj_list->item = item;
  17. adj_list->next_adj_vertex = NULL;
  18.  
  19. return adj_list;
  20. }
  21.  
  22. struct graph
  23. {
  24. AdjList *vertices[MAX_GRAPH_SIZE];
  25. short visited[MAX_GRAPH_SIZE];
  26. };
  27.  
  28. Graph* create_graph()
  29. {
  30. Graph *graph = (Graph*) malloc(sizeof(Graph));
  31. int i;
  32. for (i = 1; i <= MAX_GRAPH_SIZE - 1; i++)
  33. {
  34. graph->vertices[i] = NULL;
  35. graph->visited[i] = 0;
  36. }
  37. return graph;
  38. }
  39.  
  40. //ADICIONANDO ARESTAS - addEdge + AdjList
  41. void add_edge(Graph *graph, int vertexX, int vertexY)
  42. {
  43. AdjList *vertex = create_adj_list(vertexY);
  44. vertex->next_adj_vertex = graph->vertices[vertexX];
  45. graph->vertices[vertexX] = vertex;
  46.  
  47. //Undirected graph. Edge to the other direction as well.
  48. vertex = create_adj_list(vertexX);
  49. vertex->next_adj_vertex = graph->vertices[vertexY];
  50. graph->vertices[vertexY] = vertex;
  51.  
  52. }
  53.  
  54. //DFS - BUSCA EM PROFUNDIDADE (DEPTH FIRST SEARCH)
  55. void depth_first_search(Graph *graph, int source)
  56. {
  57. graph->visited[source] = 1;
  58. printf("%d\n", source);
  59. AdjList *adj_list = graph->vertices[source];
  60.  
  61. while (adj_list != NULL)
  62. {
  63. if (!graph->visited[adj_list->item])
  64. {
  65. depth_first_search(graph, adj_list->item);
  66. }
  67. adj_list = adj_list->next_adj_vertex;
  68. }
  69. }
  70. //BFS - BUSCA EM LARGURA (BREADTH FIRST SEARCH)
  71. void breadth_first_search(Graph *graph, int source)
  72. {
  73. Queue *queue = create_queue();
  74. int dequeued;
  75. AdjList *adj_list = graph->vertices[source];
  76. graph->visited[source] = 1;
  77. enqueue(queue, source);
  78.  
  79. while (!is_empty(queue))
  80. {
  81. dequeued = dequeue(queue);
  82. adj_list = graph->vertices[dequeued];
  83. while (adj_list != NULL)
  84. {
  85. if (graph->visited[adj_list->item] == 0)
  86. {
  87. printf("%d\n", adj_list->item);
  88. graph->visited[adj_list->item] = 1;
  89. enqueue(queue, adj_list->item);
  90. }
  91. adj_list = adj_list->next_adj_vertex;
  92. }
  93. }
  94. }
  95. //FILA
  96. struct queue
  97. {
  98. int current_size;
  99. int first;
  100. int last;
  101. int items[MAX_QUEUE_SIZE];
  102. };
  103.  
  104. Queue* create_queue()
  105. {
  106. Queue *queue = (Queue*)malloc(sizeof(Queue));
  107. queue->current_size = 0;
  108. queue->first = 0;
  109. queue->last = MAX_QUEUE_SIZE-1;
  110. return queue;
  111. }
  112. int is_empty(Queue *queue)
  113. {
  114. return queue->current_size==0;
  115. }
  116. void enqueue(Queue *queue, int item)
  117. {
  118. if(queue->current_size>=MAX_QUEUE_SIZE)
  119. printf("Queue is full!\n");
  120. else
  121. {
  122. queue->last = (queue->last+1)%MAX_QUEUE_SIZE;
  123. queue->items[queue->last] = item;
  124. queue->current_size++;
  125. }
  126. }
  127. int dequeue(Queue *queue)
  128. {
  129. if(is_empty(queue))
  130. {
  131. printf("Queue is empty!\n");
  132. return -1;
  133. }
  134. else
  135. {
  136. int dequeue = queue->items[queue->first];
  137. queue->first = (queue->first+1)%MAX_QUEUE_SIZE;
  138. queue->current_size--;
  139. return dequeue;
  140. }
  141. }
  142.  
  143. int main()
  144. {
  145. Graph *undirectedGraph = create_graph();
  146.  
  147. add_edge(undirectedGraph, 1, 2);
  148. add_edge(undirectedGraph, 1, 5);
  149. add_edge(undirectedGraph, 2, 5);
  150. add_edge(undirectedGraph, 2, 3);
  151. add_edge(undirectedGraph, 2, 4);
  152. add_edge(undirectedGraph, 3, 4);
  153. add_edge(undirectedGraph, 4, 5);
  154.  
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement