0% found this document useful (0 votes)
11 views4 pages

Umesh Prog7

The document describes a program that connects multiple offices using a minimum cost spanning tree algorithm. It prompts the user to input the number of vertices and the weights of edges between them, then calculates and displays the minimum cost to connect all offices. The output shows the selected edges and their weights, along with the total minimum cost.

Uploaded by

softengineer201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Umesh Prog7

The document describes a program that connects multiple offices using a minimum cost spanning tree algorithm. It prompts the user to input the number of vertices and the weights of edges between them, then calculates and displays the minimum cost to connect all offices. The output shows the selected edges and their weights, along with the total minimum cost.

Uploaded by

softengineer201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name : Umesh .V.

Jadhav

Practical No : 7(C)

Batch : S1

Roll No : 20

Branch : AI&DS

/*You have a business with several offices; you want to lease phone lines to connect them up with
each other; and the phone company charges different amounts of money to connect different pairs
of cities. You want a set of lines that connects all your offices with a minimum total cost. Solve the
problem by suggesting appropriate data structures.*/

#include <iostream>

#include <limits.h>

using namespace std;

int main() {

int n, i, j, k, row, col, mincost = 0, min;

char op;

cout << "Enter number of vertices: ";

cin >> n;

int cost[n][n], visit[n] = {0};

// Initialize cost matrix with -1 (no connection)

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

for (j = 0; j < n; j++) {

cost[i][j] = -1;

// Input edges and weights


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

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

cout << "Do you want an edge between " << i + 1 << " and " << j + 1 << " (y/n): ";

cin >> op;

if (op == 'y' || op == 'Y') {

cout << "Enter weight: ";

cin >> cost[i][j];

cost[j][i] = cost[i][j]; // Since it's an undirected graph

visit[0] = 1; // Start from the first vertex

for (k = 0; k < n - 1; k++) {

min = INT_MAX;

row = col = -1;

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

if (visit[i] == 1) {

for (j = 0; j < n; j++) {

if (visit[j] == 0 && cost[i][j] != -1 && cost[i][j] < min) {

min = cost[i][j];

row = i;

col = j;

if (row != -1 && col != -1) {


visit[col] = 1;

mincost += min;

cout << "Edge selected: " << row + 1 << " -> " << col + 1 << " (Weight: " << min << ")\n";

cout << "\nMinimum Cost: " << mincost << endl;

return 0;

}
OUTPUT :

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ cd ..

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:/home$ cd computer/

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ g++ Program7.cpp

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ ./a.out

Enter number of vertices: 4

Do you want an edge between 1 and 2 (y/n): y

Enter weight: 5

Do you want an edge between 1 and 3 (y/n): y

Enter weight: 10

Do you want an edge between 1 and 4 (y/n): n

Do you want an edge between 2 and 3 (y/n): y

Enter weight: 4

Do you want an edge between 2 and 4 (y/n): y

Enter weight: 8

Do you want an edge between 3 and 4 (y/n): 6

Edge selected: 1 -> 2 (Weight: 5)

Edge selected: 2 -> 3 (Weight: 4)

Edge selected: 2 -> 4 (Weight: 8)

Minimum Cost: 17

You might also like