Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:57:55+00:00 2026-05-27T15:57:55+00:00

This code implements the breadth first search. #define N 9 //nodes #define MAXNUM 65555

  • 0

This code implements the breadth first search.

#define N 9     //nodes
#define MAXNUM 65555
#define FALSE 0
#define TRUE 1

int main() {
int i, j;
int network[N][N]; //Adjacency matrix
int dist[N]; //distances from node u
int u = 0; //choose first node
void bfs(int, int [N][N], int [N]);

for (i = 0; i < N; i++) {
    dist[i] = MAXNUM;
    for (j = 0; j < N; j++) {
        if (i == j) {
            network[i][j] = 0;
        } else {
            network[i][j] = MAXNUM;
            network[j][i] = MAXNUM;
        }
    }
}

network[0][1]=1; network[0][3]=1; network[0][5]=1;
network[1][0]=1; network[1][2]=1; network[1][6]=1;
network[2][1]=1; network[2][3]=1; network[2][7]=1;
network[3][0]=1; network[3][2]=1; network[3][4]=1;
network[4][3]=1; network[4][5]=1; network[4][7]=1;
network[5][0]=1; network[5][4]=1; network[5][6]=1;
network[6][1]=1; network[6][5]=1; network[6][7]=1;
network[7][2]=1; network[7][4]=1; network[7][6]=1;

bfs(u, network, dist);

return 0;
}

void bfs(int u, int network[N][N], int dist[N]) {
int w, v, onScanQ[N], ScanQ[N], Qsize = 0; 
int k;

for (v = 0; v < N; v++) {
    dist[v] = MAXNUM;
    onScanQ[v] = FALSE;
}

dist[u] = 0;
ScanQ[1] = u;
onScanQ[u] = TRUE;
Qsize = 1;
k = 1;

printf("\nBFS has started examining:\n");

do {
    v = ScanQ[k];
    printf("%d ", v);
    for (w = 0; w < N; w++) {
        if ((network[v][w] < MAXNUM) && (!onScanQ[w])) {
            Qsize++;
            ScanQ[Qsize] = w;
            onScanQ[w] = TRUE;
            dist[w] = dist[v] + 1;
            printf("(%d) ", w);
        }
        k++;
    }
} while (k <= Qsize);
    printf("\n");}

But instead of these results:

BFS has started examining:
0 (1) (3) (5) 1 (2) (6) 3 (4) 5 2 (7) 6 4 7

i take from output, only this:

BFS has started examining:
0 (1) (3) (5)

what’s missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:57:56+00:00Added an answer on May 27, 2026 at 3:57 pm

    A quick printf inside the while loop shows that your condition in the while is not equated to true and so printed only one set of output!

        printf("Qsize=%d k=%d\n", Qsize, k); 
    } while (k <= Qsize);
    

    Output:

    Qsize=4 k=10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code: public class Messager implements Runnable { public static void main(String[] args)
Consider this code: import java.util.*; class jm45 implements Comparator<jm45> { private int x; jm45(int
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
I have written this piece of code public class Test{ public static void main(String[]
With this code: private class WRYLocationListener implements LocationListener //private class WRYLocationListener extends LocationListener <-
I have this code: public class VoiceActivity extends Activity implements OnClickListener { private TextView
Why does this code public class SponsoredComparator implements Comparator<SRE> { public boolean equals(SRE arg0,
If a class implements another class... what does that mean? I found this code
I was wondering if this code could be improved. The IProvider implements IProvider and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.