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
X + y = 8 <br> how do i solve for x
IgorC [24]

Answer:

x = 8 - y

Step by step explaination:

==>x + y = 8

==> x = 8 - y

==> substract the value

==> Done

5 0
3 years ago
3^x = 27^a+b and a^2-b^2/(a-b)=5 What is x?
Colt1911 [192]
Your answer should be 3x-x+2=4 hope this helps..
4 0
3 years ago
Help please help!! my grade is down​
lisov135 [29]

plug in -2 for x

7*-2 -10 = -24

7 0
3 years ago
A}20<br>b}28<br>C}23<br>D}26<br>i need some help asap
DaniilM [7]
The answer would be 26 due to the fact that it’s just nearly above the 25 mark
3 0
3 years ago
CAn someone help me with thiss???
sashaice [31]
I used to have these in my middle school, i think searching up the lesson and lesson name will give you the entire answer key for that specific sheet
6 0
3 years ago
Other questions:
  • Please help!
    14·2 answers
  • ts) The unit price of a certain commodity evolves randomly from day to day with a general downward drift but with an occasional
    13·1 answer
  • Find the value of h for the parallelogram
    10·1 answer
  • Determine the volume of a sphere with a great circle of radius 12 cm.
    5·1 answer
  • Factor &amp; Finding Zeros<br> x^{2} +12x+35=0
    11·1 answer
  • What is the area of a semicircle with a diameter of 14
    9·2 answers
  • A central angle in a circle has a measure of 180°. The length of the arc it intercepts is 8 in.
    10·1 answer
  • 5(x + 7) = 15<br> what is the value of x
    12·2 answers
  • What are the missing operations?
    8·1 answer
  • Laurie was scuba diving. Each time she dove 5 feet deeper, she would stop and clear her ears. After Laurie had checked her instr
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!