Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2. Write a C++ program called hw6_2 .cpp that implements the Depth -First Search ( D FS) algorithm using a stack and a mark array

2. Write a C++ program called hw6_2.cpp that implements the Depth-First Search (DFS) algorithmusing a stack and a mark array as you learned in the class.

Input format: This is a sample input from a user.

3

2

0 1

1 2

The first line (= 3 in the example) indicates that there are three vertices in the graph. For the homework, you can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the graph with the input information.

Sample Run 0: Assume that the user typed the following lines

3

2

0 1

1 2

This is the correct output. Your program should display the mark array of DFS. For the problem, you can assume that the starting vertex is always 0. And also, you can assume that the graph is connected.

Mark[0]:1

Mark[1]:2

Mark[2]:3

Sample Run 1: Assume that the user typed the following lines

5

6

0 1

0 2

0 3

1 3

2 3

3 4

This is the correct output.

Mark[0]:1

Mark[1]:2

Mark[2]:5

Mark[3]:3

Mark[4]:4

Sample Run 2: Assume that the user typed the following lines

5

6

0 1

0 2

0 3

1 4

2 3

3 4

This is the correct output.

Mark[0]:1

Mark[1]:2

Mark[2]:4

Mark[3]:5

Mark[4]:3

[Hint] In the lecture, we used a stack for the operation of DFS algorithm. So, you can use an explicit stack to implement it. However, you can use a recursive function which uses an implicit stack. The following is the pseudocode with a recursive function and it may make your implementation easier.

// This is the main function named DFS().

// This is the recursive function named dfs().

// Dont be confused with the main function DFS().

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

How to reverse a Armstrong number by using double linked list ?

Answered: 1 week ago

Question

=+ Are they breakable for any reason?

Answered: 1 week ago

Question

=+When and under what circumstances are contracts renegotiated?

Answered: 1 week ago