Modify the DFS algorithm f Procedure explore(G,v) Visited[v]=true For each edge
ID: 3564477 • Letter: M
Question
Modify the DFS algorithm f
Procedure explore(G,v)
Visited[v]=true
For each edge (v,u) in E:
If not visited[u]
Explore(G,u)
Produce dfs(G)
For all v in V:
Visited(v)= false
For all v in V:
If not visited(v)
Explore(G,v)
Procedure explore(G,v)
Visited[v]=true
For each edge (v,u) in E:
If not visited[u]
Explore(G,u)
Produce dfs(G)
For all v in V:
Visited(v)= false
For all v in V:
If not visited(v)
Explore(G,v)
Explanation / Answer
The algorithm is roughly:
The result is a set of "component" data structures (std::vectors in my implementation), each containing a set of exclusively inter-connected nodes.
Considerations:
Here is the working code. Note that some C++11 features were used, but they should be easy to replace if older compiler is used. The error handling is left as exercise for the reader.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.