Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ DFS BFS The purpose of this lab is to do a depth-first search and a breadth-first search of a graph. The input to the

    C++ DFS BFS The purpose of this lab is to do a depth-first search and a breadth-first search of a graph. The input to the program will be the number of nodes, the adjacency matrix, and the start node, which are also the parameters to the two functions depth-first and breadth-first. The output from your program will be the list of nodes visited in depth-first order and a list of nodes visited in breadth-first order. You MAY NOT use the standard template library. You MUST use ONLY arrays. There should be exactly 3 functions: main, depth-first, and breadth-first. The largest any graph can be is 10 nodes. The nodes will be numbered beginning with node 1 (keep that in mind when dealing with the C++ arrays). Hint #1: In both algorithms, it is necessary to keep up with whether or not a no de has been visited. This can be done with a one-dimensional int or boolean array that contains 10 slots. If the entry is 0, then the node hasnA????1t been visited. If it is a 1, then the node has been visited.

    Hint #2: In the breadth-first search, it is necessary to manage a queue using an
    array. Remember that an index in an array is an address for that array just like
    a pointer is an address in memory. So you can have a queue array of length
    10 and two int references (indices) head and tail which are used to index into the
    array.

    A sample run is as follows:
    Please input the number of nodes: 5

    Please input the adjacency matrix:
    0 1 0 1 1
    1 0 1 1 0
    0 1 0 1 1
    1 1 1 0 0
    1 0 1 0 0

    Please input the start node: 1

    The depth-first traversal of the graph is: 1 2 3 4 5

    The breadth-first traversal of the graph is: 1 2 4 5 3




Step by Step Solution

3.53 Rating (167 Votes )

There are 3 Steps involved in it

Step: 1

C CODE include using namespace std void dfsint start v... 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

Seeing Through Statistics

Authors: Jessica M.Utts

4th Edition

1285050886, 978-1305176249, 1305176243, 978-1305322394, 978-1285050881

More Books

Students also viewed these Programming questions