4-1 Strongly Connected Components (17分)

来源:互联网 发布:C语言迭代法求立方根 编辑:程序博客网 时间:2024/05/16 15:10

Write a program to find the strongly connected components in a digraph.

Format of functions:

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

where Graph is defined as the following:

typedef struct VNode *PtrToVNode;struct VNode {    Vertex Vert;    PtrToVNode Next;};typedef struct GNode *Graph;struct GNode {    int NumOfVertices;    int NumOfEdges;    PtrToVNode *Array;};

Here void (*visit)(Vertex V) is a function parameter that is passed intoStronglyConnectedComponents to handle (print with a certain format) each vertex that is visited. The functionStronglyConnectedComponents is supposed to print a return after each component is found.

Sample program of judge:

#include <stdio.h>#include <stdlib.h>#define MaxVertices 10  /* maximum number of vertices */typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */typedef struct VNode *PtrToVNode;struct VNode {    Vertex Vert;    PtrToVNode Next;};typedef struct GNode *Graph;struct GNode {    int NumOfVertices;    int NumOfEdges;    PtrToVNode *Array;};Graph ReadG(); /* details omitted */void PrintV( Vertex V ){   printf("%d ", V);}void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );int main(){    Graph G = ReadG();    StronglyConnectedComponents( G, PrintV );    return 0;}/* Your function will be put here */

Sample Input (for the graph shown in the figure):

4 50 11 22 03 13 2

Sample Output:

3 1 2 0

Note: The output order does not matter. That is, a solution like

0 1 2 3

is also considered correct.

#include <stdio.h>#include <stdlib.h>#define MaxVertices 10  /* maximum number of vertices */typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */typedef struct VNode *PtrToVNode;struct VNode {    Vertex Vert;    PtrToVNode Next;};typedef struct GNode *Graph;struct GNode {    int NumOfVertices;    int NumOfEdges;    PtrToVNode *Array;};Graph ReadG(); /* details omitted */void PrintV( Vertex V ){    printf("%d ", V);}void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );int main(){    Graph G = ReadG();    StronglyConnectedComponents( G, PrintV );    return 0;}/* Your function will be put here */PtrToVNode insert(PtrToVNode p,int x)  {      p->Next=(PtrToVNode)malloc(sizeof(struct VNode));      p=p->Next;      p->Vert=x;      p->Next=NULL;      return p;  }  Graph ReadG(){int i,j,m,n,x,y;      scanf("%d%d",&m,&n);       Graph p=(Graph)malloc(sizeof(struct GNode));      PtrToVNode *rear=(PtrToVNode*)malloc(m*sizeof(struct VNode));      p->Array=(PtrToVNode*)malloc(m*sizeof(struct VNode));      p->NumOfVertices=m;      p->NumOfEdges=n;      for(i=0;i<m;i++){          rear[i]=p->Array[i]=(PtrToVNode)malloc(sizeof(struct VNode));          p->Array[i]->Next=NULL;      }      for(i=0;i<n;i++){          scanf("%d%d",&x,&y);          rear[x]=insert(rear[x],y);      }      for(i=0;i<m;i++){          PtrToVNode tmp=p->Array[i];          p->Array[i]=p->Array[i]->Next;          tmp->Next=NULL;          free(tmp);      }       return p; }int index=0;int top=0;int Stack[MaxVertices];int DFN[MaxVertices];int LOW[MaxVertices];int vi[MaxVertices];int whether[MaxVertices];int min(int x, int y){if( x >= y ){return y;}else if( x < y ){return x;}}void Tarjan(int u, Graph G){    int t, v;    DFN[u]=LOW[u]=index++;    Stack[top++]=u;    vi[u]=1;    struct VNode *temp;    temp=G->Array[u];    while(temp!=NULL)    {    v=temp->Vert;    if(DFN[v]==-1)    {    Tarjan(v, G);    LOW[u]=min(LOW[u],LOW[v]);}else if(whether[v]==0){LOW[u]=min(LOW[u],DFN[v]);}temp=temp->Next;}if(DFN[u]==LOW[u]){     do    {    t=Stack[--top];    printf("%d ", t);     whether[t]=1;    }while(u!=t);    printf("\n");    }}void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) ){for(int i=0; i<G->NumOfVertices; i++){vi[i]=0;whether[i]=0;DFN[i]=LOW[i]=-1;}for(int i=0; i<G->NumOfVertices; i++){if(vi[i]==0){Tarjan(i, G);}}}
贵校PTA作业,请自行绕道,以免不必要的麻烦

1 0
原创粉丝点击