CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Error: infinite loop in my code(Depth First Search)

1. Member
Join Date
Aug 2012
Posts
28

## Error: infinite loop in my code(Depth First Search)

I code the algorithm Depth First Search, but it infinite loop, what did i wrong?
Code:
```#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
const int maxx = 20;
{
int u, v;
for(int x = 0; x < vertices; ++x)
{
cout << "Enter u : \t";
cin >> u;
u--;
cout << "Enter v : \t";
cin >> v;
v--;
grid[u][v] = true;
grid[v][u] = true;
cout << "---------------------\n";
}
}

void Depth_first_search(bool grid[][maxx], std::vector<int> &trace,
int nodes, int u)
{

for(int v = 0; v < nodes; ++v)
{
if((grid[u][v] == true) && trace[v] == 0)
{

trace[v] = u;

//recursive step
Depth_first_search(grid, trace, nodes, v);

}
}
}

void Trace_result(std::vector<int> &trace, int start, int end, int nodes)
{
cout << "From _nodes" << start + 1 << " you can visit :\n";
for(int v = 0; v < nodes; ++v)
{
if(trace[v] != 0)
{
cout << " _nodes : " << v + 1 << " , ";
}
}

cout << "\n--------------------------------------------\n";
cout << "The path from " << start + 1 << " to " << end + 1 << '\n';

if(trace[end] == 0){
cout << "Unavailable.! to go to from " << end + 1
<< " to -> " << start + 1 << '\n';
}
else{
while(end != start)
{
cout << end + 1 << "<-";
end = trace[end];
}
cout << start + 1 << endl;
}
}

int main()
{
bool grid[maxx][maxx] = { false };
std::vector<int> trace(maxx, 0);
int nodes, vertices;
cout << "Please input the number of Node : \n";
cin >> nodes;
cout << "Please input the number of Vertices : \n";
cin >> vertices;

//Set value for all vertices.

int starting_position, finishing_position;
cout << "Please Input the Starting Node : \n";
cin >> starting_position;
cout << "Please Input the Finishing Node : \n";
cin >> finishing_position;
//Decrease to fit with index of C++ start from 0->size-1
starting_position--;
finishing_position--;

Depth_first_search(grid, trace, nodes, starting_position);
Trace_result(trace, starting_position, finishing_position, nodes);
system("pause");
return 0;
}```
I'm wrong in: Depth_first_search, is't it?

2. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Error: infinite loop in my code(Depth First Search)

Have you tried stepping through your code in the debugger?

3. Member
Join Date
Aug 2012
Posts
28

## Re: Error: infinite loop in my code(Depth First Search)

>"< i don't know how to use degger in C#, i'm begginer

4. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Error: infinite loop in my code(Depth First Search)

This is a C++ forum, not C#. You need to learn to use the debugger. It's not optional, and it's the first thing any of us here would do to figure out what was wrong with your code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•