深度优先搜索(DFS)将图形的所有顶点标记为已访问。因此,为了使DFS有用,还可以存储一些其他信息。例如,运行DFS时顶点的访问顺序。
访问前和访问后号码是在图形上运行DFS时可以存储的额外信息,这些信息实际上非常有用。访问前号指示节点进入递归堆栈的时间,访问后号指示节点从DFS递归堆栈中出来的时间。
例子:
括号内的数字表示[访问前编号,访问后编号]。
Pre and Post numbers are widely used in graph algorithms. For example, they can be used to find out whether a particular node lies in the sub-tree of another node.
To find whether u lies in the sub-tree of v or not we just compare the pre and post number of u and v. If pre[u] > pre[v] and post[u] < post[v] then u lies in the sub-tree of v otherwise not. You can see above example for more clarification.
可以通过简单的DFS来找到访问前和访问后的号码。我们将采用两个数组,一个用于存储前序号,另一个用于存储后序号,并采用一个可跟踪时间的变量。相同的实现如下:
C++
#include
using namespace std;
// Variable to keep track of time
int Time = 1;
// Function to perform DFS starting from node u
void dfs(int u, vector> aList,
vector &pre,
vector &post,
vector &vis)
{
// Storing the pre number whenever
// the node comes into recursion stack
pre[u] = Time;
// Increment time
Time++;
vis[u] = 1;
for(int v : aList[u])
{
if (vis[v] == 0)
dfs(v, aList, pre, post, vis);
}
// Storing the post number whenever
// the node goes out of recursion stack
post[u] = Time;
Time++;
}
// Driver Code
int main()
{
// Number of nodes in graph
int n = 6;
// Adjacency list
vector> aList(n + 1);
vector pre(n + 1);
vector post(n + 1);
// Visited array
vector vis(n + 1);
// Edges
aList[1].push_back(2);
aList[2].push_back(1);
aList[2].push_back(4);
aList[4].push_back(2);
aList[1].push_back(3);
aList[3].push_back(1);
aList[3].push_back(4);
aList[4].push_back(3);
aList[3].push_back(5);
aList[5].push_back(3);
aList[5].push_back(6);
aList[6].push_back(5);
// DFS starting at Node 1
dfs(1, aList, pre, post, vis);
// Number of nodes in graph
for(int i = 1; i <= n; i++)
cout << "Node " << i << " Pre number "
<< pre[i] << " Post number "
<< post[i] << endl;
return 0;
}
// This code is contributed by divyesh072019
Java
import java.util.*;
public class GFG {
// Variable to keep track of time
static int time = 1;
// Function to perform DFS starting from node u
static void dfs(int u, ArrayList > aList,
int pre[], int post[], int vis[])
{
// Storing the pre number whenever
// the node comes into recursion stack
pre[u] = time;
// Increment time
time++;
vis[u] = 1;
for (int v : aList.get(u)) {
if (vis[v] == 0)
dfs(v, aList, pre, post, vis);
}
// Storing the post number whenever
// the node goes out of recursion stack
post[u] = time;
time++;
}
// Driver code
public static void main(String args[])
{
// Number of nodes in graph
int n = 6;
// Adjacency list
ArrayList > aList
= new ArrayList >(n + 1);
for (int i = 1; i <= n; i++) {
ArrayList list = new ArrayList<>();
aList.add(list);
}
aList.add(new ArrayList());
int pre[] = new int[n + 1];
int post[] = new int[n + 1];
// Visited array
int vis[] = new int[n + 1];
// Edges
aList.get(1).add(2);
aList.get(2).add(1);
aList.get(2).add(4);
aList.get(4).add(2);
aList.get(1).add(3);
aList.get(3).add(1);
aList.get(3).add(4);
aList.get(4).add(3);
aList.get(3).add(5);
aList.get(5).add(3);
aList.get(5).add(6);
aList.get(6).add(5);
// DFS starting at Node 1
dfs(1, aList, pre, post, vis);
// Number of nodes in graph
for (int i = 1; i <= n; i++)
System.out.println("Node " + i + " Pre number "
+ pre[i] + " Post number " + post[i]);
}
}
Python3
# Variable to keep track of time
time = 1
# Function to perform DFS starting
# from node u
def dfs(u, aList, pre, post, vis):
global time
# Storing the pre number whenever
# the node comes into recursion stack
pre[u] = time
# Increment time
time += 1
vis[u] = 1
for v in aList[u]:
if (vis[v] == 0):
dfs(v, aList, pre, post, vis)
# Storing the post number whenever
# the node goes out of recursion stack
post[u] = time
time += 1
# Driver code
if __name__=='__main__':
# Number of nodes in graph
n = 6
# Adjacency list
aList = [[] for i in range(n + 1)]
pre = [0 for i in range(n + 1)]
post = [0 for i in range(n + 1)]
# Visited array
vis = [0 for i in range(n + 1)]
# Edges
aList[1].append(2)
aList[2].append(1)
aList[2].append(4)
aList[4].append(2)
aList[1].append(3)
aList[3].append(1)
aList[3].append(4)
aList[4].append(3)
aList[3].append(5)
aList[5].append(3)
aList[5].append(6)
aList[6].append(5)
# DFS starting at Node 1
dfs(1, aList, pre, post, vis)
# Number of nodes in graph
for i in range(1, n + 1):
print("Node " + str(i) +
" Pre number " + str(pre[i]) +
" Post number " + str(post[i]))
# This code is contributed by rutvik_56
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
// Variable to keep track of time
static int time = 1;
// Function to perform DFS starting from node u
static void dfs(int u, ArrayList aList,
int []pre, int []post,
int []vis)
{
// Storing the pre number whenever
// the node comes into recursion stack
pre[u] = time;
// Increment time
time++;
vis[u] = 1;
foreach(int v in (ArrayList)aList[u])
{
if (vis[v] == 0)
dfs(v, aList, pre, post, vis);
}
// Storing the post number whenever
// the node goes out of recursion stack
post[u] = time;
time++;
}
// Driver code
public static void Main(string []args)
{
// Number of nodes in graph
int n = 6;
// Adjacency list
ArrayList aList = new ArrayList(n + 1);
for(int i = 1; i <= n; i++)
{
ArrayList list = new ArrayList();
aList.Add(list);
}
aList.Add(new ArrayList());
int []pre = new int[n + 1];
int []post = new int[n + 1];
// Visited array
int []vis = new int[n + 1];
// Edges
((ArrayList)aList[1]).Add(2);
((ArrayList)aList[2]).Add(1);
((ArrayList)aList[2]).Add(4);
((ArrayList)aList[4]).Add(2);
((ArrayList)aList[1]).Add(3);
((ArrayList)aList[3]).Add(1);
((ArrayList)aList[3]).Add(4);
((ArrayList)aList[4]).Add(3);
((ArrayList)aList[3]).Add(5);
((ArrayList)aList[5]).Add(3);
((ArrayList)aList[5]).Add(6);
((ArrayList)aList[6]).Add(5);
// DFS starting at Node 1
dfs(1, aList, pre, post, vis);
// Number of nodes in graph
for(int i = 1; i <= n; i++)
Console.WriteLine("Node " + i +
" Pre number " + pre[i] +
" Post number " + post[i]);
}
}
// This code is contributed by pratham76
Node 1 Pre number 1 Post number 12
Node 2 Pre number 2 Post number 11
Node 3 Pre number 4 Post number 9
Node 4 Pre number 3 Post number 10
Node 5 Pre number 5 Post number 8
Node 6 Pre number 6 Post number 7
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。