Using the Breadth-First Search Algorithm, determine the minimum number of edges that it would require to reachvertex 'H' starting from vertex 'A'>

Answers

Answer 1
Answer:

Answer:

The algorithm is given below.

#include <iostream>

#include <vector>

#include <utility>

#include <algorithm>

using namespace std;

const int MAX = 1e4 + 5;

int id[MAX], nodes, edges;

pair <long long, pair<int, int> > p[MAX];

void initialize()

{

   for(int i = 0;i < MAX;++i)

       id[i] = i;

}

int root(int x)

{

   while(id[x] != x)

   {

       id[x] = id[id[x]];

       x = id[x];

   }

   return x;

}

void union1(int x, int y)

{

   int p = root(x);

   int q = root(y);

   id[p] = id[q];

}

long long kruskal(pair<long long, pair<int, int> > p[])

{

   int x, y;

   long long cost, minimumCost = 0;

   for(int i = 0;i < edges;++i)

   {

       // Selecting edges one by one in increasing order from the beginning

       x = p[i].second.first;

       y = p[i].second.second;

       cost = p[i].first;

       // Check if the selected edge is creating a cycle or not

       if(root(x) != root(y))

       {

           minimumCost += cost;

           union1(x, y);

       }    

   }

   return minimumCost;

}

int main()

{

   int x, y;

   long long weight, cost, minimumCost;

   initialize();

   cin >> nodes >> edges;

   for(int i = 0;i < edges;++i)

   {

       cin >> x >> y >> weight;

       p[i] = make_pair(weight, make_pair(x, y));

   }

   // Sort the edges in the ascending order

   sort(p, p + edges);

   minimumCost = kruskal(p);

   cout << minimumCost << endl;

   return 0;

}


Related Questions

The amount of time a passenger waits at an airport check-in counter is random variable with mean 10 minutes and standard deviation of 2 minutes. Suppose a random sample of 50 customers is observed. Calculate the probability that the average waiting time waiting in line for this sample is (a) less than 10 minutes (b) between 5 and 10 minutes
Help pleasseeeeeeeeee 6. If Ai charges $3.25 per serving of ice cream, how much will she make if she sells it to 12½ people (children under 5 get half scoops for half the amount, which is why there is ahalf person).
8 Line in the xy-plane contains points from each of Quadrants II, III, and IV, but no points from Quadrant I. Which of the following must be true? A) The slope of line is undefined. B) The slope of line is zero. C) The slope of line is positive. D) The slope of line is negative. CONTINUE
What is 12 x q greater than 12 but less than 24 Enter a positive value for q that makes this statement true. 12 x q is greater than 12 but less than 24.Immersive Reader(3 Points)2 3/42 1/21 6/81 3/41 1/25/65Question(1 Point) wich one is it and i need 2 answer's
What is 1 whole and 5/8 as a decimal

8 more than a number is greater than or equal to 0. Find the number. - - - - - - - - -

(Although completely unnecessary, if you could explain how to write this out into an equation that would be amazing. As much as I’d appreciate an answer, it’s also be cool to know how to do it. C:)

Answers

we are given that

8 more than a number is greater than or equal to 0

Let's assume that number as 'x'

8 more than a number is

x+8

now, it is greater than or equal to 0

so, we can write as

x+8\geq 0

now, we can solve for x

Subtract both sides 8

x+8-8\geq 0-8

x\geq -8...............Answer


Stephanie was doing a science experiment to see what brand of cereal with raisinsactually had more raisins. She scooped out one cup of cereal and counted 27 raisins. If
there were 12 cups of cereal in the box, predict about how many raisins should there
be in the box?

Answers

Answer:

counted 27 raisins. If

there were 12

Which EXPRESION has the greatest value when x=3

Answers

What is the options to answer

Answer: 2x^3+5

Step-by-step explanation:

ANSWER ASAP:Shayla wants to buy some CDs that each cost $14 and a DVD that costs
$23. She has $65 that she is able to spend. Which equation can be used to
determine how many CDs Shayla could buy?

Answers

14x+23=65
65 is the total money
23 is with the dvd
14 is how many she can buy

Translate the sentence into an inequality.A number w increased by 6 is greater than or equal to 27.

Answers

Answer:

w + 6 ≥ 27

Step-by-step explanation:

A number w is the variable w.

Increased by 6 means being added by 6.

PLEASE HELP ME OUT M

Answers

Answer:

what?

Step-by-step explanation: