使用IDE:VS 2022
有向无环图(Directed Acyclic Graph,简称DAG)是一种特殊的图,它由一组顶点和一组有向边组成,其中边具有方向,并且图中不存在环。在有向无环图中,拓扑排序是一种重要的排序方法,它将图中的所有顶点排成一个线性序列,满足以下条件:
1、图中的每个顶点在序列中只出现一次。
2、对于图中的任意一条有向边 𝑢→𝑣,顶点 𝑢 在序列中出现在顶点 𝑣 之前。
本代码中使用递归的思想输出有向无环图拓扑序列并加以判断图结构是否为无环图,并附有图的基本构造函数,增删算法和深度(DFS算法)优先搜索,求图的节点的度的算法,希望对大家有所帮助。
Graph.h
#pragma once
#include <iostream>
#include <list>
#include <vector>
#include<queue>
using namespace std;
class Graph
{
public:
Graph(bool is_directed)
{
this->is_directed = is_directed;
}
void insert_node(int num)
{
dot.push_back(list<int>());
ind.push_back(0);
dot[num_dot].push_back(num);
visited.push_back(false);
num_dot++;
}
void insert_line(int num1, int num2)
{
if (num1 <= 0 || num1 > num_dot || num2 <= 0 || num2 > num_dot)
{
cout << "插入边失败" << endl;
return;
}
dot[num1 - 1].push_back(num2);
ind.at(num2-1)= ind.at(num2 - 1)+1;
if (!is_directed)
{
dot[num2 - 1].push_back(num1);
ind.at(num1-1)= ind.at(num1 - 1)+1;
}
}
void del_node(int num)
{
for (auto i = dot.begin(); i != dot.end();)
{
if ((*i).front() == num)
{
dot.erase(i);
break;
}
else
{
auto a = (*i).begin();
while (a != (*i).end())
{
if ((*a) == num)
{
(*i).erase(a);
ind[num - 1]--;
break;
}
else
{
a++;
}
}
i++;
}
}
}
void del_line(int num1, int num2)
{
for (auto i = dot.begin(); i != dot.end(); i++)
{
if ((*i).front() == num1)
{
auto b = find((*i).begin(), (*i).end(), num2);
if (b != (*i).end())
{
(*i).erase(b);
ind[num2 - 1]--;
break;
}
}
}
if (!is_directed)
{
for (auto i = dot.begin(); i != dot.end(); i++)
{
if ((*i).front() == num2)
{
auto b = find((*i).begin(), (*i).end(), num1);
if (b != (*i).end())
{
(*i).erase(b);
ind[num1 - 1]--;
break;
}
}
}
}
}
void DFS(int num)
{
if (visited[num-1])
{
return;
}
visited[num-1] = true;
cout << num << " ";
int w;
w = firstadj(num);
while (w != -1)
{
if (!visited[w-1])
{
DFS(w);
}
w = nextadj(num, w);
}
}
int degree(int num)
{
for (auto i = dot.begin(); i != dot.end(); i++)
{
if ((*i).front() == num)
{
return (*i).size() - 1;
}
}
return -1;
}
void topologicalSort()
{
vector<int> result;
vector<vector<int>> results;
int count = 0;
topological_sort(result, results);
if (results.empty())
{
cout << "图中存在环,拓扑排序失败" << endl;
return;
}
cout << "拓扑排序结果如下:" << endl;
for (const auto& e : results)
{
for (int num : e)
{
cout << num << " ";
}
cout << endl;
count++;
}
cout << "拓扑排序共" << count << "种可能的输出结果" << endl;
}
private:
vector<list<int>> dot;
vector<bool> visited;
vector<int> ind;
bool is_directed;
int num_dot = 0;
int firstadj(int num)
{
for (auto i = dot.begin(); i != dot.end(); i++)
{
if ((*i).front() == num && (*i).size() > 1)
{
return *(++(*i).begin());
}
}
return -1;
}
int nextadj(int num1, int num2)
{
for (auto i = dot.begin(); i != dot.end(); i++)
{
if ((*i).front() == num1)
{
auto b = find((*i).begin(), (*i).end(), num2);
if (b != --(*i).end())
{
return *(++b);
}
}
}
return -1;
}
void topological_sort(vector<int>& result, vector<vector<int>>& results)
{
bool flag = false;
for (int i = 0; i < num_dot; ++i)
{
if (ind[i] == 0 && !visited[i])
{
for (int a : dot[i])
{
ind[a - 1]--;
}
result.push_back(i + 1);
visited[i] = true;
topological_sort(result, results);
visited[i] = false;
result.pop_back();
for (int b : dot[i])
{
ind[b - 1]++;
}
flag = true;
}
}
if (!flag)
{
if (result.size() == num_dot)
{
results.push_back(result);
}
}
}
};
Main.cpp
#include<iostream>
using namespace std;
#include"Graph.h"
int main()
{
Graph g(true);
g.insert_node(1);
g.insert_node(2);
g.insert_node(3);
g.insert_node(4);
g.insert_node(5);
g.insert_node(6);
g.insert_node(7);
g.insert_node(8);
g.insert_node(9);
g.insert_node(10);
g.insert_line(1, 3);
g.insert_line(1, 4);
g.insert_line(2, 4);
g.insert_line(2, 5);
g.insert_line(3, 6);
g.insert_line(4, 6);
g.insert_line(4, 7);
g.insert_line(5, 7);
g.insert_line(6, 8);
g.insert_line(6, 9);
g.insert_line(7, 9);
g.insert_line(7, 10);
g.insert_line(8, 9);
g.insert_line(9, 10);
g.topologicalSort();
}
图结构
运行结果
拓扑排序结果如下:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 8 7 9 10
1 2 3 4 5 7 6 8 9 10
1 2 3 4 6 5 7 8 9 10
1 2 3 4 6 5 8 7 9 10
1 2 3 4 6 8 5 7 9 10
1 2 3 5 4 6 7 8 9 10
1 2 3 5 4 6 8 7 9 10
1 2 3 5 4 7 6 8 9 10
1 2 4 3 5 6 7 8 9 10
1 2 4 3 5 6 8 7 9 10
1 2 4 3 5 7 6 8 9 10
1 2 4 3 6 5 7 8 9 10
1 2 4 3 6 5 8 7 9 10
1 2 4 3 6 8 5 7 9 10
1 2 4 5 3 6 7 8 9 10
1 2 4 5 3 6 8 7 9 10
1 2 4 5 3 7 6 8 9 10
1 2 4 5 7 3 6 8 9 10
1 2 5 3 4 6 7 8 9 10
1 2 5 3 4 6 8 7 9 10
1 2 5 3 4 7 6 8 9 10
1 2 5 4 3 6 7 8 9 10
1 2 5 4 3 6 8 7 9 10
1 2 5 4 3 7 6 8 9 10
1 2 5 4 7 3 6 8 9 10
1 3 2 4 5 6 7 8 9 10
1 3 2 4 5 6 8 7 9 10
1 3 2 4 5 7 6 8 9 10
1 3 2 4 6 5 7 8 9 10
1 3 2 4 6 5 8 7 9 10
1 3 2 4 6 8 5 7 9 10
1 3 2 5 4 6 7 8 9 10
1 3 2 5 4 6 8 7 9 10
1 3 2 5 4 7 6 8 9 10
2 1 3 4 5 6 7 8 9 10
2 1 3 4 5 6 8 7 9 10
2 1 3 4 5 7 6 8 9 10
2 1 3 4 6 5 7 8 9 10
2 1 3 4 6 5 8 7 9 10
2 1 3 4 6 8 5 7 9 10
2 1 3 5 4 6 7 8 9 10
2 1 3 5 4 6 8 7 9 10
2 1 3 5 4 7 6 8 9 10
2 1 4 3 5 6 7 8 9 10
2 1 4 3 5 6 8 7 9 10
2 1 4 3 5 7 6 8 9 10
2 1 4 3 6 5 7 8 9 10
2 1 4 3 6 5 8 7 9 10
2 1 4 3 6 8 5 7 9 10
2 1 4 5 3 6 7 8 9 10
2 1 4 5 3 6 8 7 9 10
2 1 4 5 3 7 6 8 9 10
2 1 4 5 7 3 6 8 9 10
2 1 5 3 4 6 7 8 9 10
2 1 5 3 4 6 8 7 9 10
2 1 5 3 4 7 6 8 9 10
2 1 5 4 3 6 7 8 9 10
2 1 5 4 3 6 8 7 9 10
2 1 5 4 3 7 6 8 9 10
2 1 5 4 7 3 6 8 9 10
2 5 1 3 4 6 7 8 9 10
2 5 1 3 4 6 8 7 9 10
2 5 1 3 4 7 6 8 9 10
2 5 1 4 3 6 7 8 9 10
2 5 1 4 3 6 8 7 9 10
2 5 1 4 3 7 6 8 9 10
2 5 1 4 7 3 6 8 9 10
拓扑排序共68种可能的输出结果
路漫漫其修远兮,吾将上下而求索,加油!