Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. Write a C++ (or Java) program for hw3_3 that implements the Depth -First Search ( D FS) algorithm using a stack and a mark

3. Write a C++ (or Java) program for hw3_3 that implements the Depth-First Search (DFS) algorithm using a stack and a mark array as you learned in the class. In Java please

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.

0

2

1

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] 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

More Books

Students also viewed these Databases questions

Question

1. Define mass and mediated communication

Answered: 1 week ago