B) Consider The Following Weighted Connected Graph...

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

6/20/22, 10:34 PM B) Consider The Following Weighted Connected Graph... | Chegg.

com

  Home Study tools


 My courses
 My books My folder Career Life 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / b) consider the following weighted connected graph. give…

Question: B) Consider the following weighted connected graph. Given tha… Post a question
Answers from our experts for your tough
homework questions

Urgents!! Enter question


Note: Write a C++ code with comments.

Continue to post
18 questions remaining

My Textbook Solutions

Show transcribed image text


Astronomy Fundament... Fundament...
0th Edition

Expert Answer 7th Edition 6th Edition

View all solutions

Anonymous answered this


Was this answer helpful? 0 1
151 answers

#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> iPair;

//structure for graph

struct Graph{

int V, E;

vector< pair<int, iPair> > edges;

Graph(int V, int E){

this->V = V;

this->E = E;

void addEdge(int u, int v, int w){

edges.push_back({w, {u, v}});

int kruskalMST();

};

struct DisjointSets{

int *parent, *rnk;

int n;

DisjointSets(int n){

this->n = n;

parent = new int[n+1];

rnk = new int[n+1];

for (int i = 0; i <= n; i++){

rnk[i] = 0;

parent[i] = i;

int find(int u){

if (u != parent[u])

parent[u] = find(parent[u]);

return parent[u];

void merge(int x, int y){

x = find(x), y = find(y);

if (rnk[x] > rnk[y])

parent[y] = x;

else

parent[x] = y;

if (rnk[x] == rnk[y])

rnk[y]++;

};

int Graph::kruskalMST(){

int mst_wt = 0;

sort(edges.begin(), edges.end());

DisjointSets ds(V);

vector< pair<int, iPair> >::iterator it;

for (it=edges.begin(); it!=edges.end(); it++){

int u = it->second.first;

int v = it->second.second;

int set_u = ds.find(u);

int set_v = ds.find(v);

if (set_u != set_v){

cout << u << " - " << v << endl;

mst_wt += it->first;

ds.merge(set_u, set_v);

return mst_wt;

int main(){

int V = 6, E = 8;

Graph g(V, E);

g.addEdge(0, 1, 10);

g.addEdge(0, 3, 12);

g.addEdge(0, 4, 9);

g.addEdge(1, 2, 11);

g.addEdge(1, 5, 6);

g.addEdge(2, 5, 7);

g.addEdge(3, 4, 8);

g.addEdge(4, 5, 2);

cout << "Edges of MST are \n";

int mst_wt = g.kruskalMST();

cout << "\nWeight of MST is " << mst_wt;

return 0;

Output:

Edges of MST are

4-5

1-5

2-5

3-4

0-4

Weight of MST is 32

Here s as 0, A AS 1,B AS 2,C AS 3,D AS 4,E AS 5 TOTAL VERTICES 6


AND Edges are 8

THANK YOU :)

IF YOU HAVE ANY DOUBTS PLEASE ASK.

View comments (1)




Questions viewed by other students

Q: Urgent!!
Q2: (05 Marks)
Write C++ code for the given scenario with
comments,
a) Suppose you are working in an
organization. You are given a
bundle of files
containing 2500 files with serial numbers starting from 1 to 2500.
You have
to search
for a file with the serial number 886. You have to search them by
dividing the piles
into two parts. Good news
is that the files are already...

A: See step-by-step answer

Q: There is a garage where the access road can accommodate any


number of trucks at one time. The garage is build
such a
way that only the last truck entered can be moved out. Each
of the trucks is identified by a positive integer
(a truck_id).
Write a program to handle truck moves, allowing for
the following commands:
a) On_road (truck_id); b) Enter_garage
(truck_ id);
c) Exit_garage...

A: See answer 100% (2 ratings)

Show more 

https://www.chegg.com/homework-help/questions-and-answers/b-consider-following-weighted-connected-graph-given-graph-starts-node-write-c-code-find-sh-q92205352 1/2
6/20/22, 10:34 PM B) Consider The Following Weighted Connected Graph... | Chegg.com

  Home Study tools


 My courses
 My books My folder Career Life 
COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE

About Chegg Advertising Choices Cheap Textbooks Chegg Math Solver EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Mobile Apps Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Sell Textbooks Thinkful Manage Subscription
Corporate Development Intellectual Property Rights Chegg Study Help Solutions Manual
Investor Relations Terms of Use College Textbooks Study 101
Jobs Global Privacy Policy eTextbooks Textbook Rental
Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Used Textbooks
Media Center Honor Code Learn Digital Access Codes
Site Map Honor Shield Uversity Chegg Life
Chegg Writing

© 2003-2022 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/b-consider-following-weighted-connected-graph-given-graph-starts-node-write-c-code-find-sh-q92205352 2/2

You might also like