1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Aleks04 [339]
3 years ago
8

Using the Breadth-First Search Algorithm, determine the minimum number of edges that it would require to reach

Mathematics
1 answer:
jekas [21]3 years ago
8 0

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;

}

You might be interested in
Mr. and mrs. Hawell want to file their income tax return together. Which filing status should they use?
Morgarella [4.7K]

Answer:

The one that ends with jointly!

Step-by-step explanation:

7 0
3 years ago
3/7 of students at a school are boys . If there are 2282 students at the school. How many are girls .
Natasha2012 [34]
If 3/7 are boys, then 4/7 have to be girls.

2282 / 7 = 326
326 x 4 = 1304

There are 1304 girls. 
7 0
3 years ago
Which expression is equivalent to 5a+20?
abruzzese [7]

Answer: 5(a+4)

Step-by-step explanation:

8 0
3 years ago
Read 2 more answers
On Monday Godswill ate some grapes. On Tuesday he
garri49 [273]

Answer: He ate 8 grapes on Monday.

Step-by-step explanation:

Let be "x" the number of grapes Godswill ate on Monday.

You know that he ate 6 more grapes than he had on Monday. This can be expressed with:

x+6

Since each day that week, he ate 6 more grapes than the day before and he realized on Friday he had eaten a total of 100 grapes this week, you can write the following expression and solve for "x":

x+(x+6)+(x+12)+(x+18)+(x+24)=100\\\\5x+60=100\\\\x=\frac{40}{5}\\\\x=8

5 0
3 years ago
Read 2 more answers
What is answer?<br><br> Not really good. At this
Rama09 [41]
J
Because 15 - 13 is 2 x (8 + 4 = 24 and divided by 6 is 4
8 0
3 years ago
Other questions:
  • The energy information administration reported that the mean retail price per gallon of regular grade gasoline was $3.43. Suppos
    13·1 answer
  • What expression is equivalent to m-0.25m
    5·1 answer
  • a car can travel 19.6 miles for every 1 gallon of gasoline. calculate the number of miles the car can travel on 13.8 gallons of
    11·1 answer
  • What type of sequence is this: 3, 5, 7, 9…?
    5·1 answer
  • Help me figure this out noww
    15·1 answer
  • Question 7 Unsaved Brycen can cover half a basketball court (about 14 meters) in 4.0 seconds flat! How fast can he run? Question
    6·2 answers
  • W Write an equation of A-line that passes through the point (2,0) and is perpendicular to the line y= -1/2x + 3
    11·1 answer
  • Name the triangle congruent to triangle ABC.
    5·1 answer
  • Can someone plz help me
    8·2 answers
  • Cora works as a plumber and earns $50 per house call, plus $18 for every hour of work. She billed a customer $104, but forgot ho
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!