Topological Sort(25 分)

6-1 Topological Sort(25 分)

Write a program to find the topological order in a digraph.

Format of functions:

bool TopSort( LGraph Graph, Vertex TopOrder[] );

where LGraph is defined as the following:

typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

The topological order is supposed to be stored in TopOrder[] whereTopOrder[i] is thei-th vertex in the resulting sequence. The topological sort cannot be successful if there is a cycle in the graph -- in that caseTopSort must returnfalse; otherwise return true.

Notice that the topological order might not be unique, but the judge's input guarantees the uniqueness of the result.

Sample program of judge:

#include 
#include 

typedef enum {false, true} bool;
#define MaxVertexNum 10  /* maximum number of vertices */
typedef int Vertex;      /* vertices are numbered from 0 to MaxVertexNum-1 */

typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

LGraph ReadG(); /* details omitted */

bool TopSort( LGraph Graph, Vertex TopOrder[] );

int main()
{
    int i;
    Vertex TopOrder[MaxVertexNum];
    LGraph G = ReadG();

    if ( TopSort(G, TopOrder)==true )
        for ( i=0; iNv; i++ )
            printf("%d ", TopOrder[i]);
    else
        printf("ERROR");
    printf("\n");

    return 0;
}

/* Your function will be put here */

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

5 7
1 0
4 3
2 1
2 0
3 2
4 1
4 2

Sample Output 1:

4 3 2 1 0

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

5 8
0 3
1 0
4 3
2 1
2 0
3 2
4 1
4 2

Sample Output 2:

ERROR
/*
   此题中 MaxVertexNum 可能会变 因此最好用MaxVertexNum
   开辟空间
*/

/* 
   LGraph ReadG();已经补充出来
*/


#include 
#include 
#define MaxVertexNum 10
typedef int Vertex;
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};
typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;
LGraph ReadG();
bool TopSort(LGraph Graph, Vertex TopOrder[]);
//void Printf(LGraph G)
//{
//    int i;
//     for(i = 0; i < G->Nv; ++i)
//    {
//      PtrToAdjVNode temp = G->G[i].FirstEdge;
//      printf("%d ╣д╨╒вссп : ", i);
//      while(temp != NULL)
//      {
//          printf("%d ", temp->AdjV);
//          temp = temp->Next;
//      }
//      printf("\n");
//    }
//}
int main()
{
    int i;
    Vertex TopOrder[MaxVertexNum];
    LGraph G = ReadG();
    if ( TopSort(G, TopOrder)==true )
        for ( i=0; iNv; i++ )
            printf("%d ", TopOrder[i]);
    else
        printf("ERROR");
    printf("\n");
    return 0;
}


typedef struct Stack
{
    int *base;
    int *top;
    int stacksize;
} Stack;
int InitStack(Stack *S)
{
    S->base = (int *)malloc(MaxVertexNum * sizeof(int));
    if(!S->base) return 0;
    S->top = S->base;
    S->stacksize = MaxVertexNum;
    return 1;
}
int IsEmpty(Stack *S)
{
    if(S->top == S->base)
        return 1;
    return 0;
}
int Push(Stack *S, int e)
{
    if(S->top - S->base == S->stacksize)
    {
        S->base = (int *)realloc(S->base, (S->stacksize + MaxVertexNum) * sizeof(int));
        if(!S->base) return 0;
        S->top = S->base + S->stacksize;
        S->stacksize += MaxVertexNum;
    }
    *S->top++ = e;
    return 1;
}
int Pop(Stack *S)
{
    int e;
    if(S->base == S->top)
        return 0;
    e = *--S->top;
    return e;
}

LGraph ReadG()
{
    int i;
    LGraph LG = (LGraph)malloc(sizeof(GNode));
    scanf("%d %d", &LG->Nv, &LG->Ne);
    for(i = 0; i < LG->Ne; ++i)
    {
        int p1, p2;
        PtrToAdjVNode adj = (AdjVNode *)malloc(sizeof(AdjVNode));
        scanf("%d %d", &p1, &p2);
        adj->AdjV = p2;
        adj->Next = NULL;
        if(LG->G[p1].FirstEdge == NULL)
        {
            LG->G[p1].FirstEdge = adj;
        }
        else
        {
            adj->Next = LG->G[p1].FirstEdge;
            LG->G[p1].FirstEdge = adj;
        }
    }
    return LG;
}
bool TopSort(LGraph Graph, Vertex TopOrder[])
{
    Stack *S = (Stack *)malloc(sizeof(Stack));
    int i, cnt, indegree[MaxVertexNum] = {0};
    InitStack(S);
    for(i = 0; i < Graph->Nv; ++i)
    {
      PtrToAdjVNode temp = Graph->G[i].FirstEdge;
      while(temp != NULL)
      {
          indegree[temp->AdjV]++;
          temp = temp->Next;
      }
    }
    for(i = 0; i < Graph->Nv; ++i)
    {
        if(indegree[i] == 0)
        {
            Push(S, i);
        }
    }
    cnt = 0;
    while(IsEmpty(S) != 1)
    {
        int e = Pop(S);
        TopOrder[cnt++] = e;
        PtrToAdjVNode temp = Graph->G[e].FirstEdge;
       while(temp != NULL)
       {
          indegree[temp->AdjV]--;
          if(indegree[temp->AdjV] == 0)
            Push(S, temp->AdjV);
          temp = temp->Next;
       }
    }
    if(cnt == Graph->Nv)
        return true;
    else
        return false;

}


你可能感兴趣的:(数据结构-PTA)