Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this homework in C++, I was planning on using breadth first sea

ID: 3716897 • Letter: I

Question

I need help with this homework in C++, I was planning on using breadth first search but was having trouble implementing this idea. Could I get some help, will give thumbs up!

2. Input: 1) A number n denoting the number of computers in a network (labelled from 0 to n-1) 2) A list of computers that are connected to each other. 3) A number denoting a computer that just got infected with a virus. For example, below there are 10 computers (named 0-9). Computer 3 is connected to computer 2, etc. Finally, the last line states that computer 1 has just become infected. 10 3 2 7 8 12 3 9 9 1 01 6 7 8 6 Output: List all computers that will be infected with the virus, ie, all computers that can be reached from computer 1. In this case, the answer is 1, 0, 9, 2, 3 Hints: This is similar to a problem you've solved in the past.

Explanation / Answer

/* Program for creation of adjacency matrix */

#include<stdio.h>

#define max 20

int adj[max][max]; /*Adjacency matrix */

int n; /* Denotes number of nodes in the graph */

main()

{

int max_edges,i,j,origin,destin,v;

int i,stack[MAX],top=-1,pop_v,j,t;

char graph_type;

printf("Enter number of nodes : ");

scanf("%d",&n);

max_edges=n*(n-1);

for(i=1;i<=max_edges;i++)

{

printf("Enter edge %d( 0 0 to quit ) : ",i);

scanf("%d %d",&origin,&destin);

if( (origin==0) && (destin==0) )

break;

if( origin > n || destin > n || origin<=0 || destin<=0)

{

printf("Invalid edge! ");

i--;

}

else

{

adj[origin][destin]=1;

if( graph_type=='u')

adj[destin][origin]=1;

}

}/*End of for*/

printf("The adjacency matrix is : ");

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

printf("%4d",adj[i][j]);

printf(" ");

}

printf("Enter the virus affected computer ");

std::cin >> v;

printf("Connected computers are ");

for(i=1;i<=n;i++)

{

if(adj[v][i]==1)

std::cout << i << std::endl;

}

}/*End of main()*/