数据结构图之五(拓扑排序)

原文链接:http://www.cnblogs.com/Braveliu/p/3460232.html

【1】拓扑排序

在一个表示工程的有向图中,有顶点表示活动,用弧表示活动之间的优先关系,这样的有向图为顶点表示活动的网,我们称为AOV网。

AOV网中的弧表示活动之间存在的某种制约关系。

所谓拓扑排序,其实就是对一个有向图构造拓扑序列的过程。

【2】拓扑排序算法

对AOV网进行拓扑排序的基本思路:

从AOV网中选择一个入度为0的顶点输出;

然后删除此顶点,并删除以次顶点为尾的弧;

继续重复此操作.....

直到输出全部顶点或AOV网中不存在入度为0的顶点为止。

由于拓扑排序过程中,需要删除顶点,显然用邻接表更加方便。

因此我们需要为AOV网建立一个邻接表。

另外,考虑到算法过程中始终需要查找入度为0的顶点?

需要在原顶点表节点结构中,增加一个入度域in,in就是入度数字。

所以结构如下图:

第一幅图AOV网如下:

第二幅图的邻接表逻辑结构

第三幅图邻接表数据结构:

【3】算法及其详解

(1)算法如下图所示:

(2)详解如下:

7. 其它的处理方式类似,直至全部打印删除。

8. 最终的拓扑排序打印结果为:

当然,从整个过程来分析,这个结果并不是唯一的一种拓扑排序方案。

分析整个算法,对一个具有n各顶点e条弧的AOV网来说:

第8-10行扫描顶点表,将入度为0的顶点入栈的时间复杂度为O(n);

而之后的while循环中,每个顶点进一次栈,出一次栈,入度减1的操作执行了e次。

所以整个算法的时间复杂度为O(n+e)。

【4】本示例代码实现

实现代码如下:

#include <iostream>
#include "Stack.h"
#include <malloc.h>
using namespace std;

#define  MAXVEX   14
#define  MAXEDGE  20

typedef struct EdgeNode
{
    int adjvex;    // 邻接点域,存储该顶点对应的下标
    struct EdgeNode* next; // 链域
} EdgeNode;

typedef struct VertexNode
{
    int inNum;    // 顶点入度值
    int data;    // 顶点数值欲
    EdgeNode* firstedge; // 边表头指针
} VertexNode, AdjList[MAXVEX];

typedef struct
{
    AdjList adjList;
    int numVertexes, numEdges; // 图中当前顶点数和边数(对于本案例,已经存在宏定义)
} graphAdjList, *GraphAdjList;

// 构建节点
EdgeNode* BuyNode()
{
    EdgeNode* p = (EdgeNode*)malloc(sizeof(EdgeNode));
    p->adjvex = -1;
    p->next = NULL;
    return p;
}
// 初始化图
void InitGraph(graphAdjList& g)
{
    for (int i = 0; i < MAXVEX; ++i)
    {
        g.adjList[i].firstedge = NULL;
    }
}
// 创建图
void CreateGraph(graphAdjList& g)
{
    int i = 0, begin = 0, end = 0;
    EdgeNode *pNode = NULL;
    cout << "输入14个顶点信息(顶点 入度):" << endl;
    for (i = 0; i < MAXVEX; ++i)
    {
        cin >> g.adjList[i].data >> g.adjList[i].inNum;
    }
    cout << "输入20条边的信息:" << endl;
    for (i = 0; i < MAXEDGE; ++i)
    {
        cin >> begin >> end;
        pNode = BuyNode();
        pNode->adjvex = end;
        pNode->next = g.adjList[begin].firstedge;
        g.adjList[begin].firstedge = pNode;
    }
}
// 打印输入信息的逻辑图
void PrintGraph(graphAdjList &g)
{
    cout << "打印邻接表的逻辑图:" << endl;
    for (int i = 0; i < MAXVEX; ++i)
    {
        cout << " " << g.adjList[i].inNum << " " << g.adjList[i].data << " ";
        EdgeNode* p = g.adjList[i].firstedge;
        cout << ": ";
        while (p != NULL)
        {
            int index = p->adjvex;
            cout << g.adjList[index].data << "  ";
            p = p->next;
        }
        cout << endl;
    }
}
bool TopologicalSort(graphAdjList g)
{
    EdgeNode* pNode = NULL;
    int i = 0, k = 0, gettop = 0;
    int nCnt = 0;
    SeqStack<int> sQ;
    for (i = 0; i < MAXVEX; ++i)
    {
        if (0 == g.adjList[i].inNum)
            sQ.Push(i);
    }
    while (!sQ.IsEmpty())
    {
        sQ.Pop(gettop);
        ++nCnt;
        if (MAXVEX == nCnt)
        {    //去掉拓扑路径后面的-->
            cout << g.adjList[gettop].data; 
            break;
        }
        cout << g.adjList[gettop].data << "-->";
        pNode = g.adjList[gettop].firstedge;
        while (pNode != NULL)
        {
            k = pNode->adjvex;
            --g.adjList[k].inNum;
            if (0 == g.adjList[k].inNum)
                sQ.Push(k);
            pNode = pNode->next;
        }
    }
    return nCnt != MAXVEX;
}
void main()
{
    graphAdjList myg;
    InitGraph(myg);
    cout << "创建图:" << endl;
    CreateGraph(myg);
    cout << "打印图的邻接表逻辑结构:" << endl;
    PrintGraph(myg);
    cout << "拓扑排序路径:" << endl;
    bool bAcire = TopologicalSort(myg);
    cout << endl;
    cout << "存在回环? " << bAcire << endl;
}
/*
创建图:
输入14个顶点信息(顶点 入度):
0 0
1 0
2 2
3 0
4 2
5 3
6 1
7 2
8 2
9 2
10 1
11 2
12 1
13 2
输入20条边的信息:
0 5
0 4
0 11
1 4
1 8
1 2
2 5
2 6
2 9
3 2
3 13
4 7
5 8
5 12
6 5
8 7
9 10
9 11
10 13
12 9
打印图的邻接表逻辑结构:
打印邻接表的逻辑图:
0 0 : 11  4  5
0 1 : 2  8  4
2 2 : 9  6  5
0 3 : 13  2
2 4 : 7
3 5 : 12  8
1 6 : 5
2 7 :
2 8 : 7
2 9 : 11  10
1 10 : 13
2 11 :
1 12 : 9
2 13 :
拓扑排序路径:
3-->1-->2-->6-->0-->5-->8-->12-->9-->10-->13-->11-->4-->7
存在回环? 0
 */


你可能感兴趣的:(工程,结构图)