迭代深度优先算法c语言,图算法-图的迭代深度优先遍历

原标题:图算法-图的迭代深度优先遍历

图的深度优先遍历(或搜索)类似于树的深度优先遍历(DFS)。这里唯一的问题是,与树不同,图形可能包含循环,因此我们可能会再次来到同一节点。为避免多次处理节点,我们使用布尔访问数组。

例如,下图的DFS是“0 3 4 2 1”,其他可能的DFS是“0 2 1 3 4”。

5fb8cc131f3c5e0d0f31223f26453db8.png

我们已经在之前的帖子中讨论了DFS的递归实现。在帖子中,讨论了迭代DFS。递归实现使用函数调用堆栈。在迭代实现中,显式堆栈用于保存访问顶点。

下面是迭代DFS的实现。实现类似于BFS,唯一的区别是queue被stack替换。

C ++

Java的

filter_none

编辑

play_arrow

brightness_4

// An Iterative C++ program to do DFS traversal from

// a given source vertex. DFS(int s) traverses vertices

// reachable from s.

#include

using namespace std;

// This class represents a directed graph using adjacency

// list representation

class Graph

{

int V; // No. of vertices

list *adj; // adjacency lists

public:

Graph(int V); // Constructor

void addEdge(int v, int w); // to add an edge to graph

void DFS(int s); // prints all vertices in DFS manner

// from a given source.

};

Graph::Graph(int V)

{

this->V = V;

adj = new list[V];

}

void Graph::addEdge(int v, int w)

{

adj[v].push_back(w); // Add w to v’s list.

}

// prints all not yet visited vertices reachable from s

void Graph::DFS(int s)

{

// Initially mark all verices as not visited

vector visited(V, false);

// Create a stack for DFS

stack stack;

// Push the current source node.

stack.push(s);

while (!stack.empty())

{

// Pop a vertex from stack and print it

s = stack.top();

stack.pop();

// Stack may contain same vertex twice. So

// we need to print the popped item only

// if it is not visited.

if (!visited[s])

{

cout << s << " ";

visited[s] = true;

}

// Get all adjacent vertices of the popped vertex s

// If a adjacent has not been visited, then puah it

// to the stack.

for (auto i = adj[s].begin(); i != adj[s].end(); ++i)

if (!visited[*i])

stack.push(*i);

}

}

// Driver program to test methods of graph class

int main()

{

Graph g(5); // Total 5 vertices in graph

g.addEdge(1, 0);

g.addEdge(0, 2);

g.addEdge(2, 1);

g.addEdge(0, 3);

g.addEdge(1, 4);

cout << "Following is Depth First Traversal\n";

g.DFS(0);

return 0;

}

输出:

以下是Depth First Traversal

0 3 2 1 4

请注意,上述实现仅打印可从给定顶点到达的顶点。例如,如果我们删除边0-3和0-2,上面的程序只会打印0.要打印图的所有顶点,我们需要为每个顶点调用DFS。以下是相同的实现。

C ++

Java的

Python3

filter_none

编辑

play_arrow

brightness_4

// An Iterative C++ program to do DFS traversal from

// a given source vertex. DFS(int s) traverses vertices

// reachable from s.

#include

using namespace std;

// This class represents a directed graph using adjacency

// list representation

class Graph

{

int V; // No. of vertices

list *adj; // adjacency lists

public:

Graph(int V); // Constructor

void addEdge(int v, int w); // to add an edge to graph

void DFS(); // prints all vertices in DFS manner

// prints all not yet visited vertices reachable from s

void DFSUtil(int s, vector &visited);

};

Graph::Graph(int V)

{

this->V = V;

adj = new list[V];

}

void Graph::addEdge(int v, int w)

{

adj[v].push_back(w); // Add w to v’s list.

}

// prints all not yet visited vertices reachable from s

void Graph::DFSUtil(int s, vector &visited)

{

// Create a stack for DFS

stack stack;

// Puah the current source node.

stack.push(s);

while (!stack.empty())

{

// Pop a vertex from stack and print it

s = stack.top();

stack.pop();

// Stack may contain same vertex twice. So

// we need to print the popped item only

// if it is not visited.

if (!visited[s])

{

cout << s << " ";

visited[s] = true;

}

// Get all adjacent vertices of the popped vertex s

// If a adjacent has not been visited, then puah it

// to the stack.

for (auto i = adj[s].begin(); i != adj[s].end(); ++i)

if (!visited[*i])

stack.push(*i);

}

}

// prints all vertices in DFS manner

void Graph::DFS()

{

// Mark all the vertices as not visited

vector visited(V, false);

for (int i = 0; i < V; i++)

if (!visited[i])

DFSUtil(i, visited);

}

// Driver program to test methods of graph class

int main()

{

Graph g(5); // Total 5 vertices in graph

g.addEdge(1, 0);

g.addEdge(2, 1);

g.addEdge(3, 4);

g.addEdge(4, 0);

cout << "Following is Depth First Traversal\n";

g.DFS();

return 0;

}

输出:以下是Depth First Traversal

0 1 2 3 4返回搜狐,查看更多

责任编辑:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值