(C++)Below are depthFirstTraversal, dft, and breadthFirstTraversal methods. Help
ID: 3846318 • Letter: #
Question
(C++)Below are depthFirstTraversal, dft, and breadthFirstTraversal methods. Help create(implement) method named cycleFinder by follow this instruction: traverse the graph and determine the node(s) that start the cycle(s) in the graph. Use the unordered_map data structure in the STL to keep track of the nodes that have been visited.
void graphType::depthFirstTraversal()
{
int index;
bool *visited;
visited=new bool[gSize];
for(index = 0; index < gSize; index++)
{
visited[index]=false;
}
for(index = 0; index < gSize; index++)
if(!visited[index])
dft(index, visited);
delete[] visited;
} //end depthFirstTraversal
void graphType::dft(int v, bool visited[])
{
visited[v] = true;
cout << " " << v << " ";
linkedListIterator mytp;
for(mytp=graph[v].begin(); mytp!=graph[v].end(); ++mytp)
{
int x = *mytp;
if(!visited[x])
dft(x,visited);
}
} //end dft
void graphType::breadthFirstTraversal()
{
int index;
linkedQueueType tp;
bool *visited;
visited = new bool[gSize];
for(index=0;index visited[index]=false;
linkedListIterator tt;
for(index=0; index
if(!visited[index])
{
tp.addQueue(index);
visited[index]=true;
cout << " " << index << " ";
while(!tp.isEmptyQueue())
{
int x = tp.front();
tp.deleteQueue();
for(tt=graph[x].begin();tt!=graph[x].end();++tt)
{
int y = *tt;
if(!visited[y])
{
tp.addQueue(y);
visited[y]=true;
cout<<" "<< y << " " << endl;
delete [] visited;
} //end breadthFirstTraversal
Explanation / Answer
void graphType::depthFirstTraversal()
{
int index;
bool *visited;
visited=new bool[gSize];
for(index = 0; index < gSize; index++)
{
visited[index]=false;
}
for(index = 0; index < gSize; index++)
if(!visited[index])
dft(index, visited);
delete[] visited;
} //end depthFirstTraversal
void graphType::dft(int v, bool visited[])
{
visited[v] = true;
cout << " " << v << " ";
linkedListIterator mytp;
for(mytp=graph[v].begin(); mytp!=graph[v].end(); ++mytp)
{
int x = *mytp;
if(!visited[x])
dft(x,visited);
}
} //end dft
void graphType::breadthFirstTraversal()
{
int index;
linkedQueueType tp;
bool *visited;
visited = new bool[gSize];
for(index=0;index visited[index]=false;
linkedListIterator tt;
for(index=0; index
if(!visited[index])
{
tp.addQueue(index);
visited[index]=true;
cout << " " << index << " ";
while(!tp.isEmptyQueue())
{
int x = tp.front();
tp.deleteQueue();
for(tt=graph[x].begin();tt!=graph[x].end();++tt)
{
int y = *tt;
if(!visited[y])
{
tp.addQueue(y);
visited[y]=true;
cout<<" "<< y << " " << endl;
delete [] visited;
} //end breadthFirstTraversal
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.