原文:
给定一个,任务是打印一个图的 dfs 遍历,该图包括包括回溯在内的每个步骤。
1st step:- 0 -> 1
2nd step:- 1 -> 5
3rd step:- 5 -> 1 (backtracking step)
4th step:- 1 -> 6...
and so on till all the nodes are visited.
dfs step-wise(including backtracking) is:
0 1 5 1 6 7 8 7 6 1 0 2 4 2 9 3 10
注:在上图中,边缘之间的权重刚刚添加。它在 dfs 遍历中没有任何作用。
进场:t2】dfs 带这里会用到。首先,使用 dfs 同时访问每个节点,并跟踪以前使用的边和父节点。如果一个节点出现在所有相邻节点都被访问过的地方,使用最后使用的边回溯并打印节点。继续这些步骤,在每一步,父节点将成为当前节点。继续上面的步骤,找到图的完整的 dfs 遍历。
下面是上述方法的实现:
c
// c program to print the complete
// dfs-traversal of graph
// using back-tracking
#include
using namespace std;
const int n = 1000;
vector adj[n];
// function to print the complete dfs-traversal
void dfsutil(int u, int node, bool visited[],
vector > road_used, int parent,
int it)
{
int c = 0;
// check if all th node is visited or not
// and count unvisited nodes
for (int i = 0; i < node; i )
if (visited[i])
c ;
// if all the node is visited return;
if (c == node)
return;
// mark not visited node as visited
visited[u] = true;
// track the current edge
road_used.push_back({ parent, u });
// print the node
cout << u << " ";
// check for not visited node and proceed with it.
for (int x : adj[u]) {
// call the dfs function if not visited
if (!visited[x])
dfsutil(x, node, visited, road_used, u, it 1);
}
// backtrack through the last
// visited nodes
for (auto y : road_used)
if (y.second == u)
dfsutil(y.first, node, visited, road_used, u,
it 1);
}
// function to call the dfs function
// which prints the dfs-traversal stepwise
void dfs(int node)
{
// create a array of visited node
bool visited[node];
// vector to track last visited road
vector > road_used;
// initialize all the node with false
for (int i = 0; i < node; i )
visited[i] = false;
// call the function
dfsutil(0, node, visited, road_used, -1, 0);
}
// function to insert edges in graph
void insertedge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
// driver code
int main()
{
// number of nodes and edges in the graph
int node = 11, edge = 13;
// function call to create the graph
insertedge(0, 1);
insertedge(0, 2);
insertedge(1, 5);
insertedge(1, 6);
insertedge(2, 4);
insertedge(2, 9);
insertedge(6, 7);
insertedge(6, 8);
insertedge(7, 8);
insertedge(2, 3);
insertedge(3, 9);
insertedge(3, 10);
insertedge(9, 10);
// call the function to print
dfs(node);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print the complete
// dfs-traversal of graph
// using back-tracking
import java.io.*;
import java.util.*;
class gfg
{
static int n = 1000;
static arraylist> adj =
new arraylist>();
// function to print the complete dfs-traversal
static void dfsutil(int u, int node, boolean visited[],
arraylist> road_used,
int parent, int it)
{
int c = 0;
// check if all th node is visited or not
// and count unvisited nodes
for (int i = 0; i < node; i )
if (visited[i])
c ;
// if all the node is visited return;
if (c == node)
return;
// mark not visited node as visited
visited[u] = true;
// track the current edge
road_used.add(new arraylist(arrays.aslist( parent, u )));
// print the node
system.out.print(u " ");
// check for not visited node and proceed with it.
for (int x : adj.get(u))
{
// call the dfs function if not visited
if (!visited[x])
{
dfsutil(x, node, visited, road_used, u, it 1);
}
}
// backtrack through the last
// visited nodes
for(int y = 0; y < road_used.size(); y )
{
if(road_used.get(y).get(1) == u)
{
dfsutil(road_used.get(y).get(0), node,
visited,road_used, u, it 1);
}
}
}
// function to call the dfs function
// which prints the dfs-traversal stepwise
static void dfs(int node)
{
// create a array of visited node
boolean[] visited = new boolean[node];
// vector to track last visited road
arraylist> road_used =
new arraylist>();
// initialize all the node with false
for (int i = 0; i < node; i )
{
visited[i] = false;
}
// call the function
dfsutil(0, node, visited, road_used, -1, 0);
}
// function to insert edges in graph
static void insertedge(int u, int v)
{
adj.get(u).add(v);
adj.get(v).add(u);
}
// driver code
public static void main (string[] args)
{
// number of nodes and edges in the graph
int node = 11, edge = 13;
for(int i = 0; i < n; i )
{
adj.add(new arraylist());
}
// function call to create the graph
insertedge(0, 1);
insertedge(0, 2);
insertedge(1, 5);
insertedge(1, 6);
insertedge(2, 4);
insertedge(2, 9);
insertedge(6, 7);
insertedge(6, 8);
insertedge(7, 8);
insertedge(2, 3);
insertedge(3, 9);
insertedge(3, 10);
insertedge(9, 10);
// call the function to print
dfs(node);
}
}
// this code is contributed by avanitrachhadiya2155
python 3
# python3 program to print the
# complete dfs-traversal of graph
# using back-tracking
n = 1000
adj = [[] for i in range(n)]
# function to print the complete dfs-traversal
def dfsutil(u, node,visited,
road_used, parent, it):
c = 0
# check if all th node is visited
# or not and count unvisited nodes
for i in range(node):
if (visited[i]):
c = 1
# if all the node is visited return
if (c == node):
return
# mark not visited node as visited
visited[u] = true
# track the current edge
road_used.append([parent, u])
# print the node
print(u, end = " ")
# check for not visited node
# and proceed with it.
for x in adj[u]:
# call the dfs function
# if not visited
if (not visited[x]):
dfsutil(x, node, visited,
road_used, u, it 1)
# backtrack through the last
# visited nodes
for y in road_used:
if (y[1] == u):
dfsutil(y[0], node, visited,
road_used, u, it 1)
# function to call the dfs function
# which prints the dfs-traversal stepwise
def dfs(node):
# create a array of visited node
visited = [false for i in range(node)]
# vector to track last visited road
road_used = []
# initialize all the node with false
for i in range(node):
visited[i] = false
# call the function
dfsutil(0, node, visited,
road_used, -1, 0)
# function to insert edges in graph
def insertedge(u, v):
adj[u].append(v)
adj[v].append(u)
# driver code
if __name__ == '__main__':
# number of nodes and edges in the graph
node = 11
edge = 13
# function call to create the graph
insertedge(0, 1)
insertedge(0, 2)
insertedge(1, 5)
insertedge(1, 6)
insertedge(2, 4)
insertedge(2, 9)
insertedge(6, 7)
insertedge(6, 8)
insertedge(7, 8)
insertedge(2, 3)
insertedge(3, 9)
insertedge(3, 10)
insertedge(9, 10)
# call the function to print
dfs(node)
# this code is contributed by mohit kumar 29
c
// c# program for the above approach
using system;
using system.collections.generic;
public class gfg {
static int n = 1000;
static list > adj = new list >();
// function to print the complete dfs-traversal
static void dfsutil(int u, int node, bool[] visited,
list > road_used,
int parent, int it)
{
int c = 0;
// check if all th node is visited or not
// and count unvisited nodes
for (int i = 0; i < node; i )
if (visited[i])
c ;
// if all the node is visited return;
if (c == node)
return;
// mark not visited node as visited
visited[u] = true;
// track the current edge
road_used.add(new list() { parent, u });
// print the node
console.write(u " ");
// check for not visited node and proceed with it.
foreach(int x in adj[u])
{
// call the dfs function if not visited
if (!visited[x]) {
dfsutil(x, node, visited, road_used, u,
it 1);
}
}
// backtrack through the last
// visited nodes
for (int y = 0; y < road_used.count; y ) {
if (road_used[y][1] == u) {
dfsutil(road_used[y][0], node, visited,
road_used, u, it 1);
}
}
}
// function to call the dfs function
// which prints the dfs-traversal stepwise
static void dfs(int node)
{
// create a array of visited node
bool[] visited = new bool[node];
// vector to track last visited road
list > road_used = new list >();
// initialize all the node with false
for (int i = 0; i < node; i ) {
visited[i] = false;
}
// call the function
dfsutil(0, node, visited, road_used, -1, 0);
}
// function to insert edges in graph
static void insertedge(int u, int v)
{
adj[u].add(v);
adj[v].add(u);
}
static public void main()
{
// number of nodes and edges in the graph
int node = 11;
for (int i = 0; i < n; i ) {
adj.add(new list());
}
// function call to create the graph
insertedge(0, 1);
insertedge(0, 2);
insertedge(1, 5);
insertedge(1, 6);
insertedge(2, 4);
insertedge(2, 9);
insertedge(6, 7);
insertedge(6, 8);
insertedge(7, 8);
insertedge(2, 3);
insertedge(3, 9);
insertedge(3, 10);
insertedge(9, 10);
// call the function to print
dfs(node);
}
}
// this code is contributed by rag2127
java 描述语言
9
output:
0 1 5 1 6 7 8 7 6 1 0 2 4 2 9 3 10
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处