zoukankan      html  css  js  c++  java
  • 有向无环图及其应用(拓扑排序)

    // func7-1.cpp algo7-4.cpp和algo7-5.cpp要调用
    void FindInDegree(ALGraph G,int indegree[])
    { // 求顶点的入度,算法7.12、7.13调用
    	int i;
    	ArcNode *p;
    	for(i=0;i<G.vexnum;i++)
    		indegree[i]=0; // 赋初值
    	for(i=0;i<G.vexnum;i++)
    	{
    		p=G.vertices[i].firstarc;
    		while(p)
    		{
    			indegree[p->data.adjvex]++;
    			p=p->nextarc;
    		}
    	}
    }
    
    // algo7-4.cpp 输出有向图的一个拓扑序列。实现算法7.12的程序
    #include"c1.h"
    #define MAX_NAME 5 // 顶点字符串的最大长度
    typedef int InfoType;
    typedef char VertexType[MAX_NAME]; // 字符串类型
    #include"c7-21.h" // 邻接表存储结构
    #include"bo7-2.cpp" // 邻接表存储结构的基本操作
    #include"func7-1.cpp"
    typedef int SElemType; // 栈元素类型
    #include"c3-1.h" // 顺序栈的存储结构
    #include"bo3-1.cpp" // 顺序栈的基本操作
    Status TopologicalSort(ALGraph G)
    { // 有向图G采用邻接表存储结构。若G无回路,则输出G的顶点的一个拓扑序列并返回OK,
    	// 否则返回ERROR。算法7.12
    	int i,k,count=0; // 已输出顶点数,初值为0
    	int indegree[MAX_VERTEX_NUM]; // 入度数组,存放各顶点当前入度数
    	SqStack S;
    	ArcNode *p;
    	FindInDegree(G,indegree); // 对各顶点求入度indegree[],在func7-1.cpp中
    	InitStack(S); // 初始化零入度顶点栈S
    	for(i=0;i<G.vexnum;++i) // 对所有顶点i
    		if(!indegree[i]) // 若其入度为0
    			Push(S,i); // 将i入零入度顶点栈S
    		while(!StackEmpty(S)) // 当零入度顶点栈S不空
    		{
    			Pop(S,i); // 出栈1个零入度顶点的序号,并将其赋给i
    			printf("%s ",G.vertices[i].data); // 输出i号顶点
    			++count; // 已输出顶点数+1
    			for(p=G.vertices[i].firstarc;p;p=p->nextarc)
    			{ // 对i号顶点的每个邻接顶点
    				k=p->data.adjvex; // 其序号为k
    				if(!(--indegree[k])) // k的入度减1,若减为0,则将k入栈S
    					Push(S,k);
    			}
    		}
    		if(count<G.vexnum) // 零入度顶点栈S已空,图G还有顶点未输出
    		{
    			printf("此有向图有回路
    ");
    			return ERROR;
    		}
    		else
    		{
    			printf("为一个拓扑序列。
    ");
    			return OK;
    		}
    }
    void main()
    {
    	ALGraph f;
    	printf("请选择有向图
    ");
    	CreateGraph(f); // 构造有向图f,在bo7-2.cpp中
    	Display(f); // 输出有向图f,在bo7-2.cpp中
    	TopologicalSort(f); // 输出有向图f的1个拓扑序列
    }
    

    代码的运行结果如下:

    请选择有向图
    请输入G的类型(有向图:0,有向网:1,无向图:2,无向网:3): 0(见图762)
    请输入G的顶点数,边数: 6,8
    请输入6个顶点的值(<5个字符):
    V1 V2 V3 V4 V5 V6
    请顺序输入每条弧(边)的弧尾和弧头(以空格作为间隔):
    V1 V2
    V1 V3
    V1 V4
    V3 V2
    V3 V5
    V4 V5
    V6 V4
    V6 V5
    有向图(见图763)
    6个顶点:
    V1 V2 V3 V4 V5 V6
    8条弧(边):
    V1→V4 V1→V3 V1→V2
    V3→V5 V3→V2
    V4→V5
    V6→V5 V6→V4
    V6 V1 V3 V2 V4 V5 为一个拓扑序列。


    图764 显示了运行algo7-4.cpp 的过程。其中的S 栈也可用队列代替,这样将输出
    一个不同的拓扑序列。




  • 相关阅读:
    css 写一个三角形
    vue 知识汇总,中级阶段的。
    获取url参数封装的
    vue 知识点
    不换行css
    微信小程序的横向滚动
    git提交本地分支到远程分支
    linux shell head tail 用法简介
    PHP服务重启
    MongoDB用户创建
  • 原文地址:https://www.cnblogs.com/KongkOngL/p/4074450.html
Copyright © 2011-2022 走看看