写在前面
最近做的几道题目的解法都是拓扑排序,网易游戏今年实习生招聘有一题也是用拓扑排序来解的,有必要完整了解拓扑排序过程。
什么是拓扑排序
拓扑排序本质上就是一个排序过程,与普通排序不同的是,所谓的拓扑序,指的是在有向无环图中任意两个结点的关系。这个关系,非常类似所谓的偏序关系,假设有向图中存在边(u,v),那么我们称 u,v构成一个偏序,若用小于号表示偏序,即 u < v。所谓的拓扑排序,就是通过结点之间的偏序关系得到该有向图的一个全序关系。这里借助geeksforgeeks上的一张图来说明:
从这张图来看,每一个有向边都代表着一种偏序关系,对图中的结点来说,存在入度即表示前向结点的序小于当前结点,即存在a < b.必须说明的是,拓扑序并不一定是唯一的,关于这一点,在DFS的拓扑实现中我们会提到。在这里,我们先用一种比较常见的方法获得本图的第一个拓扑序:
首先,选择一个入度为0的结点作为起始结点;
其后,我们删除该结点及其有向边(这样做的原因是,前向结点已经被标记为有序,因此删去与其相关的有向边)。
重复上述过程,直到图为空或图中所有结点的入度都不为0(存在环路),则结束。
在本图中,显然,5和4的入度都为0,它们都可以作为拓扑序的起点位置,在这里我们选择5作为序的起点,删除有向边,我们发现4和2的入度为0,我们接着选择4(当然2也是正确的),按照这种思路,我们可能得到的一种结果是: 5 4 2 3 1 0,我们发现,这种结果并不唯一。但是能够得到的拓扑序都是合法的序。
上述解法的主要思想是维护一个入度为0的序列,以此得到顺序的拓扑序。而DFS解法的拓扑序列则完全相反。DFS方法考虑的是拓扑序的终点位置。我们知道,拓扑序中的最大值一定是出度为0的结点位置。反馈到DFS上,我们的思路就是,DFS当前路径的末尾一定是拓扑序的终点位置。该结点被标记为已访问,则也相当于删除了与其相关连的边。递归操作会不断寻找当前路径上的的拓扑序终点,遍历所有结点(因为有可能原有向图中入度为0的结点不唯一,这样就必须对所有结点DFS),得到序的倒序即是一个合法的拓扑序。我们通常使用栈来解这一过程,这样最后pop出所有栈内元素得到的就是要求的序。此外,DFS并不要求遍历的结点的具体位置,换句话说,从任意结点开始的遍历都可得到正确的拓扑序(但是必须遍历完所有结点)。下面会用DFS来解拓扑序(来自geeksforgeeks):
DFS的拓扑序解
// A C++ program to print topological sorting of a DAG
#include<iostream>
#include <list>
#include <stack>
using namespace std;
// Class to represent a graph
class Graph
{
int V; // No. of vertices'
// Pointer to an array containing adjacency listsList
list<int> *adj;
// A function used by topologicalSort
void topologicalSortUtil(int v, bool visited[], stack<int> &Stack);
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int v, int w);
// prints a Topological Sort of the complete graph
void topologicalSort();
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
// A recursive function used by topologicalSort
void Graph::topologicalSortUtil(int v, bool visited[],
stack<int> &Stack)
{
// Mark the current node as visited.
visited[v] = true;
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
topologicalSortUtil(*i, visited, Stack);
// Push current vertex to stack which stores result
Stack.push(v);
}
// The function to do Topological Sort. It uses recursive
// topologicalSortUtil()
void Graph::topologicalSort()
{
stack<int> Stack;
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to store Topological
// Sort starting from all vertices one by one
for (int i = 0; i < V; i++)
if (visited[i] == false)
topologicalSortUtil(i, visited, Stack);
// Print contents of stack
while (Stack.empty() == false)
{
cout << Stack.top() << " ";
Stack.pop();
}
}
// Driver program to test above functions
int main()
{
// Create a graph given in the above diagram
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
cout << "Following is a Topological Sort of the given graph n";
g.topologicalSort();
return 0;
}
上述代码从0开始遍历,事实上任意位置都是OK的。