1、有向无环图(DAG图)

  用二叉树表示表达式(空间耗费过多)------->有向无环图表示(可以实现相同子式的共享,节省存储空间)。

  有向图检查环比无向图的复杂许多。

  AOV网络:强调以顶点作为活动的网络图;--->拓扑排序

  AOE网络:强调以作为活动的网络图;  --->关心边上的权值信息--->关键路径

2、拓扑排序

  不关心所花费的代价,只关心当前的工作完成没有。

  由某个集合上的一个偏序得到该集合上的一个全序, (偏序,全序,离散数学上的);

  前提:(1)、有向无环图;(2)、结果不唯一

  思想:(1)、从任何一个没有入度的顶点作为起始;

  (2)、排完该顶点后,删除该顶点,连边一起删了(也就是将其它顶点的入度-1);

  (3)、每次只找没有入度的进行排序(有好几个入度为0时,就造成了拓扑排序不唯一);

模型分析:

拓扑排序_第1张图片

拓扑排序_第2张图片

3、拓扑排序算法

  均由C++实现(用邻接矩阵)

template
void GraphMtx::TopoLogicalSort(){  //拓扑排序
    int n = Graph::getCurVertex();
    int *count = new int[n];
    
    for(int i = 0; i < n; i++){  //count数组记载入度
        count[i] = 0;
    }
    for(i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(edge[i][j] == 1){
                count[j]++;  //计算每个顶点的入度
            }
        }
    }

    int top = -1;
    for(i = 0; i < n; i++){
        if(count[i] == 0){  //模拟入栈push()
            count[i] = top;
            top = i;
        }
    }
    int v, w;
    for(i = 0; i < n; i++){
        if(top == -1){  //有环图,直接返回;
            return;
        }else{
            v = top;  //模拟出栈pop()
            top = count[top];
            printf("%c-->", getValue(v));
            w = getFirstNeighbor(getValue(v));
            while(w != -1){
                if(--count[w] == 0){
                    count[w] = top;  //删除了一个顶点,统计一遍,满足入栈push()
                    top = w;
                }
                w = getNextNeighbor(getValue(v), getValue(w));
            }
        }
    }

    cout<<"Over."< 
  

4、完整代码、测试代码、测试结果

  (1)、完整代码

#ifndef _GRAPH_H_
#define _GRAPH_H_

#include
#include
using namespace std;

#define VERTEX_DEFAULT_SIZE        10
#define MAX_COST                0x7FFFFFFF

template    
class Graph{
public:
    bool isEmpty()const{
        return curVertices == 0;
    }
    bool isFull()const{
        if(curVertices >= maxVertices || curEdges >= curVertices*(curVertices-1)/2)
            return true;  //图满有2种情况:(1)、当前顶点数超过了最大顶点数,存放顶点的空间已满
        return false;     //(2)、当前顶点数并没有满,但是当前顶点所能达到的边数已满
    }
    int getCurVertex()const{
        return curVertices;
    }
    int getCurEdge()const{
        return curEdges;
    }
public:
    virtual bool insertVertex(const Type &v) = 0;  //插入顶点
    virtual bool insertEdge(const Type &v1, const Type &v2, E cost) = 0; //插入边
    virtual bool removeVertex(const Type &v) = 0;  //删除顶点
    virtual bool removeEdge(const Type &v1, const Type &v2) = 0; //删除边
    virtual int getFirstNeighbor(const Type &v) = 0; //得到第一个相邻顶点
    virtual int getNextNeighbor(const Type &v, const Type &w) = 0; //得到下一个相邻顶点
public:
    virtual int getVertexIndex(const Type &v)const = 0; //得到顶点下标
    virtual void showGraph()const = 0;  //显示图
    virtual Type getValue(int index)const = 0; 
public:
    virtual void DFS(const Type &v) = 0;
    virtual void BFS(const Type &v) = 0;
protected:
    int maxVertices;  //最大顶点数
    int curVertices;  //当前顶点数
    int curEdges;  //当前边数
};

template
class GraphMtx : public Graph{ //邻接矩阵继承父类矩阵
#define maxVertices  Graph::maxVertices  //因为是模板,所以用父类的数据或方法都得加上作用域限定符
#define curVertices  Graph::curVertices
#define curEdges     Graph::curEdges
public:
    GraphMtx(int vertexSize = VERTEX_DEFAULT_SIZE){  //初始化邻接矩阵
        maxVertices = vertexSize > VERTEX_DEFAULT_SIZE ? vertexSize : VERTEX_DEFAULT_SIZE;
        vertexList = new Type[maxVertices]; //申请顶点空间
        for(int i = 0; i < maxVertices; i++){  //都初始化为0
            vertexList[i] = 0;
        }
        edge = new int*[maxVertices];  //申请边的行
        for(i = 0; i < maxVertices; i++){ //申请列空间
            edge[i] = new int[maxVertices];
        }
        for(i = 0; i < maxVertices; i++){ //赋初值为0 
            for(int j = 0; j < maxVertices; j++){
                if(i != j){
                    edge[i][j] = MAX_COST; //初始化时都赋为到其它边要花的代价为无穷大。
                }else{
                    edge[i][j] = 0;  //初始化时自己到自己认为花费为0
                }
            }
        }
        curVertices = curEdges = 0; //当前顶点和当前边数
    }
    GraphMtx(Type (*mt)[4], int sz){  //通过已有矩阵的初始化
        int e = 0; //统计边数
        maxVertices = sz > VERTEX_DEFAULT_SIZE ? sz : VERTEX_DEFAULT_SIZE;
        vertexList = new Type[maxVertices]; //申请顶点空间
        for(int i = 0; i < maxVertices; i++){  //都初始化为0
            vertexList[i] = 0;
        }
        edge = new int*[maxVertices];  //申请边的行
        for(i = 0; i < maxVertices; i++){ //申请列空间
            edge[i] = new Type[maxVertices];
        }
        for(i = 0; i < maxVertices; i++){ //赋初值为矩阵当中的值
            for(int j = 0; j < maxVertices; j++){
                edge[i][j] = mt[i][j];
                if(edge[i][j] != 0){
                    e++; //统计列的边数
                }
            }
        }
        curVertices = sz;
        curEdges = e/2;
    }
    ~GraphMtx(){}
public:
    bool insertVertex(const Type &v){
        if(curVertices >= maxVertices){
            return false;
        }
        vertexList[curVertices++] = v;
        return true;
    }
    bool insertEdge(const Type &v1, const Type &v2, E cost){
        int maxEdges = curVertices*(curVertices-1)/2;
        if(curEdges >= maxEdges){
            return false;
        }

        int v = getVertexIndex(v1);
        int w = getVertexIndex(v2);

        if(v==-1 || w==-1){
            cout<<"edge no exit"<::getCurVertex();
        bool *visit = new bool[n];

        for(int i = 0; i < n; i++){
            visit[i] = false;
        }
        DFS(v, visit);
        delete []visit;
    }
    void BFS(const Type &v){
        int n = Graph::getCurVertex();
        bool *visit = new bool[n];
        for(int i = 0; i < n; i++){
            visit[i] = false;
        }
        cout<";
        int index = getVertexIndex(v);
        visit[index] = true;

        queue q;  //队列中存放的是顶点下标;
        q.push(index);
        int w;
        while(!q.empty()){
            index = q.front();
            q.pop();
            w = getFirstNeighbor(getValue(index));
            while(w != -1){
                if(!visit[w]){
                    cout<";
                    visit[w] = true; 
                    q.push(w);
                }
                
                w = getNextNeighbor(getValue(index), getValue(w));
                
            }
        }

        delete []visit;
    }
public:
    void MinSpanTree_Kruskal();
    void MinSpanTree_Prim(const Type &v);
public:
    void TopoLogicalSort();
protected:
    void DFS(const Type &v, bool *visit){
        cout<";
        int index = getVertexIndex(v);
        visit[index] = true;
        int w = getFirstNeighbor(v);
        while(w != -1){
            if(!visit[w]){
                DFS(getValue(w), visit);
            }
            w = getNextNeighbor(v, getValue(w)); 
        }
    }
private:
    Type *vertexList;  //存放顶点的数组
    int **edge;  //存放边关系的矩阵
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct MstEdge{
    int x;  //row
    int y;  //col
    int cost;
}MstEdge;

int cmp(const void *a, const void *b){
    return (*(MstEdge*)a).cost - (*(MstEdge*)b).cost;
}

bool isSame(int *father, int i, int j){
    while(father[i] != i){
        i = father[i];
    }
    while(father[j] != j){
        j = father[j];
    }

    return i == j;
}
void markSame(int *father, int i, int j){
    while(father[i] != i){
        i = father[i];
    }
    while(father[j] != j){
        j = father[j];
    }

    father[j] = i;
}

template
void GraphMtx::MinSpanTree_Kruskal(){ 
    int n = Graph::getCurVertex();  //由于要用到父类的保护数据或方法,有模板的存在,必须加上作用域限定符;
    MstEdge *edge1 = new MstEdge[n*(n-1)/2];
    int k = 0;

    for(int i = 0; i < n; i++){
        for(int j = i+1; j < n; j++){
            if(edge[i][j] != MAX_COST){
                edge1[k].x = i;
                edge1[k].y = j;
                edge1[k].cost = edge[i][j];
                k++;
            }
        }
    }
    qsort(edge1, k, sizeof(MstEdge), cmp);

    int *father = new int[n];
    Type v1, v2;
    for(i = 0; i < n; i++){
        father[i] = i;
    }
    for(i = 0; i < n; i++){
        if(!isSame(father, edge1[i].x, edge1[i].y)){
            v1 = getValue(edge1[i].x);
            v2 = getValue(edge1[i].y);
            printf("%c-->%c : %d\n", v1, v2, edge1[i].cost);
            markSame(father, edge1[i].x, edge1[i].y);
        }
    }
}
template
void GraphMtx::MinSpanTree_Prim(const Type &v){
    int n = Graph::getCurVertex();
    int *lowCost = new int[n];
    int *mst = new int[n];

    int k = getVertexIndex(v);
    for(int i = 0; i < n; i++){
        if(i != k){
            lowCost[i] = edge[k][i];
            mst[i] = k;
        }else{
            lowCost[i] = 0;
        }
    }

    int min;
    int minIndex;
    int begin;
    int end;

    for(i = 0; i < n-1; i++){
        min = MAX_COST;
        minIndex = -1;

        for(int j = 0; j < n; j++){
            if(lowCost[j] != 0 && lowCost[j] < min){
                min = lowCost[j];
                minIndex = j;
            }
        }
        begin = mst[minIndex];
        end = minIndex;
        printf("%c-->%c : %d\n", getValue(begin), getValue(end), min);

        lowCost[minIndex] = 0;

        int cost;
        for(j = 0; j < n; j++){
            cost = edge[minIndex][j];
            if(cost < lowCost[j]){
                lowCost[j] = cost;
                mst[j] = minIndex;
            }
        }
        
    }
}
template
void GraphMtx::TopoLogicalSort(){  //拓扑排序
    int n = Graph::getCurVertex();
    int *count = new int[n];
    
    for(int i = 0; i < n; i++){  //count数组记载入度
        count[i] = 0;
    }
    for(i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(edge[i][j] == 1){
                count[j]++;  //计算每个顶点的入度
            }
        }
    }

    int top = -1;
    for(i = 0; i < n; i++){
        if(count[i] == 0){  //模拟入栈push()
            count[i] = top;
            top = i;
        }
    }
    int v, w;
    for(i = 0; i < n; i++){
        if(top == -1){  //有环图,直接返回;
            return;
        }else{
            v = top;  //模拟出栈pop()
            top = count[top];
            printf("%c-->", getValue(v));
            w = getFirstNeighbor(getValue(v));
            while(w != -1){
                if(--count[w] == 0){
                    count[w] = top;  //删除了一个顶点,统计一遍,满足入栈push()
                    top = w;
                }
                w = getNextNeighbor(getValue(v), getValue(w));
            }
        }
    }

    cout<<"Over."< 
  

  (2)、测试代码

#include"Graph1.h"

int main(void){
    GraphMtx gm;
    gm.insertVertex('A'); //0
    gm.insertVertex('B'); //1
    gm.insertVertex('C'); //2
    gm.insertVertex('D'); //3
    gm.insertVertex('E'); //4
    gm.insertVertex('F'); //5

    gm.insertEdge('A','B',1);
    gm.insertEdge('A','C',1);
    gm.insertEdge('A','D',1);
    gm.insertEdge('C','B',1);
    gm.insertEdge('C','E',1);
    gm.insertEdge('D','E',1);
    gm.insertEdge('F','D',1);
    gm.insertEdge('F','E',1);
   

    gm.showGraph();
//  gm.MinSpanTree_Kruskal();
//    cout<<"---------------------------------------------------------"< 
  

  (3)、测试结果

测试图模型: