Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /**
  2.  
  3. * @brief Calcola le componenti fortemente connesse di un grafo graph con l'algoritmo di Kosaraju
  4.  
  5. * Nota: per comodita', restituiamo la foresta delle cfc. Quando la stampate pero' fate capire che e' un multiinsieme e non una foresta
  6.  
  7. *
  8.  
  9. * @param graph il grafo da esaminare
  10.  
  11. * @return la foresta delle componenti fortemente connesse restituita dall'algoritmo di Kosaraju
  12.  
  13. *
  14.  
  15. */
  16.  
  17. upo_list_t ordine_dec(upo_list_t f)
  18.  
  19. {
  20.  
  21. upo_list_t f_ord=upo_create_list(sizeof(struct finevertice), NULL);
  22.  
  23. struct finevertice *tmp;
  24.  
  25. int max, i, c=-1;
  26.  
  27. while(upo_list_size(f)!=0)
  28.  
  29. {
  30.  
  31. max=-1;
  32.  
  33. for(i=0; i<upo_list_size(f); i++)
  34.  
  35. {
  36.  
  37. if(((struct finevertice *)upo_get(f, i))->tempofine>max)
  38.  
  39. {
  40.  
  41. max=((struct finevertice *)upo_get(f, i))->tempofine;
  42.  
  43. tmp=(struct finevertice *)upo_get(f, i);
  44.  
  45. c=i;
  46.  
  47. }
  48.  
  49. }
  50.  
  51. upo_add_last(f_ord, tmp);
  52.  
  53. upo_remove(f, c);
  54.  
  55. }
  56.  
  57. return f_ord;
  58.  
  59. }
  60.  
  61.  
  62.  
  63. void DFS_ric_cfc(upo_dirgraph_t graph, upo_list_t f, int* cfc, int* color, int u, int* t, int* k)
  64.  
  65. {
  66.  
  67. int i;
  68.  
  69. (*t)++;
  70.  
  71. cfc[*k]=u;
  72.  
  73. (*k)++;
  74.  
  75. color[u]=GRAY;
  76.  
  77. for(i=0; i<graph->n; i++)
  78.  
  79. {
  80.  
  81. if(graph->adj[u][i]!=0)
  82.  
  83. {
  84.  
  85. if(color[i]==WHITE)
  86.  
  87. {
  88.  
  89. DFS_ric_cfc(graph, f, cfc, color, i, t, k);
  90.  
  91. }
  92.  
  93. }
  94.  
  95. }
  96.  
  97. color[u]=BLACK;
  98.  
  99. struct finevertice *tmp;
  100.  
  101. tmp=upo_get(f, u);
  102.  
  103. tmp->tempofine=*t;
  104.  
  105. }
  106.  
  107.  
  108.  
  109. int** upo_DFS_tot_cfc(upo_dirgraph_t graph, upo_list_t f) {
  110.  
  111. int color[graph->n];
  112.  
  113. int i, j, *t=malloc(sizeof(int)), *k=malloc(sizeof(int));
  114.  
  115.  
  116.  
  117. /*ho usato il metodo come per graph->adj*/
  118.  
  119. int **cfc=malloc(graph->n*sizeof(int**));
  120.  
  121. if(cfc==NULL)
  122.  
  123. {
  124.  
  125. perror("Matrice di cfc non creata");
  126.  
  127. abort();
  128.  
  129. }
  130.  
  131. for(i=0; i<graph->n; i++)
  132.  
  133. cfc[i]=malloc(graph->n*sizeof(int*));
  134.  
  135. for(i=0; i<graph->n; i++)
  136.  
  137. for(j=0; j<graph->n; j++)
  138.  
  139. cfc[i][j]=-1;
  140.  
  141.  
  142.  
  143. /*visita dfs. l'inizializza l'ho dovuta mettere ad ogni vertice o non ottenevo le cfc complete ma temo sia sbagliato*/
  144.  
  145. *t=0;
  146.  
  147. for(i=0; i<graph->n; i++)
  148.  
  149. {
  150.  
  151. *k=0;
  152.  
  153. inizializza(color, NULL, graph->n);
  154.  
  155. DFS_ric_cfc(graph, f, cfc[i], color, ((struct finevertice *)upo_get(f, i))->vertice, t, k);
  156.  
  157. }
  158.  
  159. return cfc;
  160.  
  161. }
  162.  
  163.  
  164.  
  165. int* insiemecfc(int** matrix, int n)
  166.  
  167. {
  168.  
  169. int i, j, d=1;
  170.  
  171. int* cfc=malloc(sizeof(int));
  172.  
  173. if(cfc==NULL)
  174.  
  175. {
  176.  
  177. perror("Insieme di cfc non creata");
  178.  
  179. abort();
  180.  
  181. }
  182.  
  183. for(i=0; i<n; i++)
  184.  
  185. {
  186.  
  187. for(j=0; j<n; j++)
  188.  
  189. {
  190.  
  191. if(matrix[i][j]!=-1)
  192.  
  193. {
  194.  
  195. cfc[d-1]=matrix[i][j];
  196.  
  197. d++;
  198.  
  199. cfc=realloc(cfc, d*sizeof(int));
  200.  
  201. }
  202.  
  203. }
  204.  
  205. cfc[d-1]=-1;
  206.  
  207. d++;
  208.  
  209. cfc=realloc(cfc, d*sizeof(int));
  210.  
  211. }
  212.  
  213. cfc[d-1]=-2;
  214.  
  215. return cfc;
  216.  
  217. }
  218.  
  219.  
  220.  
  221. int* upo_strongly_connected_components(upo_dirgraph_t graph) {
  222.  
  223. if(graph==NULL)
  224.  
  225. return NULL;
  226.  
  227. int* cfc;
  228.  
  229. int** matrix, matrix2;
  230.  
  231. upo_list_t f=upo_create_list(sizeof(struct finevertice), NULL);
  232.  
  233. int i, j;
  234.  
  235.  
  236.  
  237. /*vettore delle struct*/
  238.  
  239. struct finevertice *tmp;
  240.  
  241. for(i=0; i<graph->n; i++)
  242.  
  243. {
  244.  
  245. tmp=malloc(sizeof(struct finevertice));
  246.  
  247. tmp->tempofine=-1;
  248.  
  249. tmp->vertice=i;
  250.  
  251. upo_add_last(f, tmp);
  252.  
  253. }
  254.  
  255.  
  256.  
  257. /*dfs sulla prima matrice con ritorno di cfc per ogni vertice*/
  258.  
  259. matrix=upo_DFS_tot_cfc(graph, f);
  260.  
  261.  
  262. upo_list_t f_ord=ordine_dec(f);
  263.  
  264.  
  265.  
  266. /*grafo trasposto*/
  267.  
  268. upo_dirgraph_t grapht=upo_dirgraph_create(graph->n);
  269.  
  270. for(i=0; i<graph->n; i++)
  271.  
  272. for(j=0; j<graph->n; j++)
  273.  
  274. grapht->adj[i][j]=graph->adj[j][i];
  275.  
  276.  
  277.  
  278. /*dfs sulla seconda matrice con ritorno di cfc per ogni vertice*/
  279.  
  280. matrix2=upo_DFS_tot_cfc(grapht, f_ord);
  281.  
  282. for(i=0; i<graph->n; i++)
  283.  
  284. {
  285.  
  286. printf("\n");
  287.  
  288. for(j=0; j<graph->n; j++)
  289.  
  290. printf(" %d", matrix[i][j]);
  291.  
  292. }
  293.  
  294.  
  295.  
  296. cfc=insiemecfc(matrix2, graph->n);
  297.  
  298.  
  299.  
  300. /*implementare l'intersezione tra le matrici e inserirle nel vettore da ritornare*/
  301.  
  302. return cfc;
  303.  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement