|
| 1 | +#include "graph.h" |
| 2 | +#include "types.h" |
| 3 | +#include "simple.h" |
| 4 | + |
| 5 | + |
| 6 | +int block_cut(V* graph,int nVertices, int s,int *C,int *k) |
| 7 | +/*input: |
| 8 | + s: where to start the DFS |
| 9 | + C: the set of cutting points, will allocat mem for it. |
| 10 | + k: number of cutting points. |
| 11 | +*/ |
| 12 | +{ |
| 13 | + |
| 14 | + int v,w,*S; |
| 15 | + int i,index1,index2,top=-1; /*pointer to stack S's top*/ |
| 16 | + |
| 17 | + nodeT *nr,*p,*L; /*array nr, p, and L*/ |
| 18 | + |
| 19 | + |
| 20 | + nr= malloc(nVertices*sizeof(nodeT)); |
| 21 | + p = malloc(nVertices*sizeof(nodeT)); |
| 22 | + L = malloc(nVertices*sizeof(nodeT)); |
| 23 | + C = malloc (nVertices*sizeof(nodeT)); |
| 24 | + S = malloc (nVertices*sizeof(nodeT)); |
| 25 | + |
| 26 | + if(C == NULL || S==NULL || nr==NULL || p==NULL || L==NULL ) |
| 27 | + { |
| 28 | + if(C) free(C); |
| 29 | + if(S) free(S); |
| 30 | + if(nr) free(nr); |
| 31 | + if(p) free(p); |
| 32 | + if(L) free(L); |
| 33 | + return -1; |
| 34 | + } |
| 35 | + |
| 36 | + for(i=0;i<nVertices;i++) |
| 37 | + { |
| 38 | + nr[i]=-1; |
| 39 | + p[i]=-1; |
| 40 | + } |
| 41 | + |
| 42 | + nr[s]=0; |
| 43 | + v=s; |
| 44 | + *k=0; |
| 45 | + i=0; |
| 46 | + |
| 47 | + S[++top]=v; |
| 48 | + do{ |
| 49 | + while(unvisited_edge(graph, visitedE,v,&w,&index1,&index2)){ |
| 50 | + set_visited(visitedE,v,index1,w,index2); |
| 51 | + if(nr[w]==-1){ |
| 52 | + p[w]=v;i++;nr[w]=i;L[w]=i; |
| 53 | + S[++top]=w; |
| 54 | + v=w; |
| 55 | + } else L[v]=(L[v]<nr[w]) ? L[v]:nr[w] ; |
| 56 | + } /*end_while*/ |
| 57 | + |
| 58 | + if(p[v]!=s) |
| 59 | + { |
| 60 | + if(L[v]<nr[p[v]]) |
| 61 | + L[p[v]]=(L[p[v]]<L[v]) ? L[p[v]]:L[v]; |
| 62 | + else { |
| 63 | + C[(*k)++]=p[v]; |
| 64 | + /*printf("2-connected block found:\n");*/ |
| 65 | + while(S[top]!=v) |
| 66 | + { |
| 67 | + /*printf("%d ",S[top]);*/ |
| 68 | + top--; |
| 69 | + } |
| 70 | + /*printf("%d ",S[top--]); this value should be equal to v*/ |
| 71 | + top--; /*if you uncomment the above line, delet this line:)*/ |
| 72 | + /*printf("%d \n",p[v]);*/ |
| 73 | + } |
| 74 | + } else { |
| 75 | + if(unvisited_edge(graph, visitedE,s,&w,&index1,&index2)) |
| 76 | + { |
| 77 | + set_visited(visitedE,s,index1,w,index2); |
| 78 | + C[(*k)+1]=s; |
| 79 | + } |
| 80 | + (*k)++; |
| 81 | + |
| 82 | + /*printf("2-connected block found:\n");*/ |
| 83 | + while(S[top]!=v) |
| 84 | + { |
| 85 | + /*printf("%d ",S[top]);*/ |
| 86 | + top--; |
| 87 | + } |
| 88 | + /*printf("%d ",S[top--]); this value should be equal to v*/ |
| 89 | + top--;/*If you uncomment the above line, delete this line:)*/ |
| 90 | + /*printf("%d \n",s);*/ |
| 91 | + } |
| 92 | + v=p[v]; |
| 93 | + } while (p[v]!=-1 || unvisited_edge(graph,visitedE,v,&w,&index1,&index2)); |
| 94 | + |
| 95 | + if(nr) free(nr); |
| 96 | + if(p) free(p); |
| 97 | + if(L) free(L); |
| 98 | + |
| 99 | +} |
0 commit comments