/*
时间:2015/11/22
内容:拓扑排序的实现(可用于判断图中是否有环路)
概述:基于Kahn算法(DFS算法的实现),采用邻接表的方式来存储有向无权图,并实现对图的拓扑排序,在给出拓扑序列的同时,也能判断图中是否存在环路(即没有拓扑序列)
操作:邻接表的创建 邻接表的打印 拓扑排序及其辅助函数
*/
#include<iostream>
#include<vector>
#include<deque>
#include<memory>
#include<algorithm>
#include<utility>
class edgesList//边表
{
public:
int index;//表示与之相连的顶点在顶点表中的编号
edgesList *next;//表示与之相连的其它顶点
edgesList(int index, edgesList* next) :index(index), next(next){}
};
class vertexList//顶点表
{
public:
char vertex;//当前顶点用字符表示
edgesList *edges;//表示与之相连的顶点,用智能指针管理
vertexList(char vertex, edgesList* next) :vertex(vertex), edges(next){}
};
class Graphe
{
public:
std::vector<vertexList> g;//图(邻接表存储方式)
std::vector<char> topologicallysort;//拓扑排序结果
std::vector<std::pair<int,bool>> incoming;
Kahn算法-拓扑排序
最新推荐文章于 2025-06-08 14:48:24 发布
