5.5拓扑排序

#include "stdafx.h"
#include 
<iostream.h>
#include 
<malloc.h>

int const vnum=6;
//单链表定义
typedef struct arcnode
{
    
int adjvex;                                            //与其相连的下个结点的编号
    struct arcnode * nextarc;                            //指向下一个结点的指针
}
ArcNodeTp;

//顶点定义
typedef struct vexnode
{
    
int vertex;                                            //顶点的值
    int in;                                                //入度数量
    ArcNodeTp * firstarc;                                //指向的出度的单链表
}
AdjList[vnum];

//图的定义
typedef struct graph
{
    AdjList adjlist;
    
int vexnum,arcnum;
}
GraphTp;

//建立一个新的结点
ArcNodeTp * CreateNewNode(int adjvex)
{
    ArcNodeTp 
* node=(ArcNodeTp *)malloc(sizeof(ArcNodeTp));
    node
->adjvex=adjvex;
    node
->nextarc=NULL;

    
return node;
}


//建立P122的图5-17(a)
void CreateGraph(GraphTp & graph)
{
    graph.arcnum
=7;
    graph.vexnum
=vnum;

    ArcNodeTp 
* p=NULL;
    
int index=0;
    
//建立结点
    index=0;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(1);
        
//修改头结点
    graph.adjlist[index].firstarc=p;
        
//指向下一个结点
    p->nextarc=CreateNewNode(2);
    graph.adjlist[index].
in=0;

    index
=1;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(5);
    graph.adjlist[index].firstarc
=p;
    graph.adjlist[index].
in=1;

    index
=2;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(5);
    graph.adjlist[index].firstarc
=p;
    graph.adjlist[index].
in=2;

    index
=3;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(2);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(4);
    graph.adjlist[index].
in=0;

    index
=4;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(5);
    graph.adjlist[index].firstarc
=p;
    graph.adjlist[index].
in=1;

    index
=5;
    graph.adjlist[index].vertex
=index;
    graph.adjlist[index].firstarc
=NULL;
    graph.adjlist[index].
in=3;
    
}


void Top_Sort(GraphTp g)
{
    
//用数组和一个变量模拟栈
    int stack[vnum];
    
int top=0;
    
    
int i=0;
    
for(i=0;i<g.vexnum;i++)
    
{
        
if (g.adjlist[i].in==0)
        
{
            stack[
++top]=i;
        }

    }

    
int count=0;
    
while(top!=0)
    
{
        
int v=stack[top--];
        count
++;
        cout
<<"输出顶点:"<<g.adjlist[v].vertex<<endl;
        ArcNodeTp 
* p=g.adjlist[v].firstarc;
        
//此循环目的是去掉此弧尾对应的顶点的边,实现的方法并非删除,则是将出度的对应的顶点的In-1
        while(p!=NULL)
        
{
            
//将弧头的入度数减一后,判断是否入度为0,如果为0则入栈
            if (--g.adjlist[p->adjvex].in==0)
            
{
                stack[
++top]=p->adjvex;
            }

            p
=p->nextarc;
        }

    }

    
if (count<g.vexnum)
        cout
<<"图中存在环"<<endl;
    
else
        cout
<<"拓扑排序完成"<<endl;
}


int main(int argc, char* argv[])
{
    GraphTp g;
    CreateGraph(g);
    Top_Sort(g);
    
return 0;
}

    

另外,书在首次将入度为0的顶点编号入栈的地方没有写"{",这样的结果是错误的,估计是排版的人弄错的.可以参考我的代码,填上相应的"{}"
posted @ 2007-07-30 13:46  吴东雷  阅读(270)  评论(0编辑  收藏  举报