I tried to make the first width algorithm. And that is what I still have.
void BreadthFirst(int A[][100], int a, int nNodes)
{
int visited[100];
for (int i = 0; i < nNodes; i++)
visited[i] = 0;
int Q[100];
int readPtr = 0, writePtr = 0;
visited[a] = 1;
cout << char(a + 'a');
Q[writePtr++] = a;
while (readPtr != writePtr)
{
for (int n = 0; n < nNodes; n++)
{
if (A[n][readPtr] == 1)
{
if (visited[n] == 0)
{
visited[n] = 1;
cout << char(n + 'a');
Q[writePtr++] = n;
}
}
}
readPtr++;
}
cout << endl;
}
I used the following graph to check it out:

which has the following adjacency table:

Using this table, I wrote my main function:
int main()
{
int nNodes = 11;
int a = 0;
int A[][100] =
{
{ 0,1,0,0,1,0,0,1,1,0,0 },
{ 1,0,1,0,0,0,0,0,0,0,0 },
{ 0,1,0,1,1,1,0,0,0,0,0 },
{ 0,0,1,0,0,1,1,0,0,0,0 },
{ 1,0,1,0,0,0,0,1,0,1,0 },
{ 0,0,1,1,0,0,1,0,0,0,0 },
{ 0,0,0,1,0,1,0,0,0,0,0 },
{ 1,0,0,0,1,0,0,0,1,1,1 },
{ 1,0,0,0,0,0,0,1,0,0,1 },
{ 0,0,0,0,1,0,0,1,0,0,0 },
{ 0,0,0,0,0,0,0,1,1,0,0 },
};
BreadthFirst(A, a, nNodes);
return 0;
}
And it does not work. The exit should be
abehicjkdfg
Instead i get
abehicdfgjk
Can you help me fix this please?