拓扑排序

//Stack.h
#ifndef STACK_H
#define STACK_H

class Stack{
public:

    Stack(int n = 10);
    bool push(int a);
    int pop();
    bool isfull();//判断是否满
    bool isempty();
private:
    int size;//表示最大容量
    int *base, *top;//top指向栈顶前一个元素
};
#endif
//Topo.h
#ifndef TUOPU_H
#define TUOPU_H

class Node{
public:
    int vexNum;
    Node* next;

    Node(int vex = 0, Node* a=NULL);
};

class MGraph{
//图的储存用邻接表,要求入度
public:
    int Vex;
    Node* vertices;

    MGraph(int num = 0);
    bool Inition();

};

class ToPo{
    /*算法:
        1.计算每个顶点的入度(初始化)
          初始化栈
        2.将入度为0的点压栈
        3.弹栈,并将以该顶点为弧尾的顶点入度-1
        4.重复2,3,直到栈空(有可能顶点都遍历,有可能存在环)
    */
public:
    bool TopologicalSort(const MGraph g);
};

#endif
//Stack.cpp
#include"Stack.h"

#include<iostream>
using namespace std;

Stack::Stack(int n){
    size = n;
    top = base = new int[size];
}

bool Stack::push(int a){
    *top = a;
    top++;
    return true;
}

int Stack::pop(){
    /*if (top == base){
    exit(0);
    }*/
    return *(--top);
}

bool Stack::isfull(){
    if (top - base >= size)
        return true;
    else
        return false;
}

bool Stack::isempty(){
    if (top == base)
        return true;
    else
        return false;
}
//ToPo.cpp
#include<iostream>
#include"Topo.h"
#include"Stack.h"
using  namespace std;

//Node
Node::Node(int vex , Node* a ){
    vexNum = vex;
    next = a;
}


//MGraph
MGraph::MGraph(int num){
    Vex = num;
    vertices = new Node[Vex]();
    /*for (int i = 0; i < Vex; i++){
        cout << vertices[i].vexNum;
    }
    cout << endl;*/
}


bool MGraph::Inition(){
    if (!Vex) return false;
    int b, e, value;
    while (cin >> b&&b){
        cin >> e ;
        Node* p = vertices[b-1].next;
        vertices[b-1].next = new Node(e-1,p);//前插
        vertices[e-1].vexNum++;//记录入度
    }
    /*for (int i = 0; i < Vex; i++){
        for (Node* p = vertices[i].next; p; p = p->next){
            cout << p->vexNum << "  ";
        }
        cout << endl;
    }*/
    return true;
}

//Topo
    /*算法:
    1.计算每个顶点的入度(初始化)
    初始化栈
    2.将入度为0的点压栈
    3.弹栈,并将以该顶点为弧尾的顶点入度-1
    4.重复2,3,直到栈空(有可能顶点都遍历,有可能存在环)
    */
bool ToPo::TopologicalSort(const MGraph g){
    //如果最后有环,输出false,如果全部顶点都输出,则返回true
    Stack S(g.Vex);
    int *count = new int[g.Vex];
    for (int i = 0; i < g.Vex; i++){
        count[i] = g.vertices[i].vexNum;
        if (!count[i])
            S.push(i);
    }
    int flag = 0;
    while (!S.isempty()){
        int i = S.pop(); flag++;
        for (Node* p = g.vertices[i].next; p; p = p->next){
            if (--count[p->vexNum] == 0){
                S.push(p->vexNum);
            }
        }
        cout << i+1<< "  ";
    }
    if (flag == g.Vex)
        return true;
    else
        return false;

}
//main.cpp
#include<iostream>
#include"Topo.h"
using namespace std;


int main(){
    int n;
    cout << "Please input the Vex-Number...." << endl;
    cin >> n;
    MGraph g(n);
    g.Inition();
    ToPo p;
    if(!p.TopologicalSort(g))
        cout<<"with cycle....."<<endl;
    system("pause");
    return 0;
}


/*测试数据
13
1 2
1 6
1 7
3 1
3 4
4 6
6 5
7 5
7 10
8 7
9 8
10 11
10 12
10 13
12 13
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值