Question

Problem definition: Give the program that implement Prim’s algorithm. Input: First line is N, denotes the...

Problem definition:

Give the program that implement Prim’s algorithm.

Input:
First line is N, denotes the amount of test case, then there are Ns graph data with an option number (determine whether output the selected edges or not).

Each graph is undirected and connected, it is composed of V (the number of vertices, <= 1000), E (the number of edges, <=10000), then followed by Es edges which are denoted by pair of vertex and weight (e.g., 2 4 10 means an edge connecting vertices 2 and 4, and its weight is 10).

The first data of each measurement on behalf of the test vertex number, edge number and option number.

The first data’s weight of each graph is option number. It could be 1 or 2, output the selected edge and the sum of all minimum spanning tree’s weight if it is 1, or only the sum if it is 2.

We restrict that selected node of Prim always start from 0, and there is no “tree edge” with same weight.

Output:
If option is 1:
The selected edges which forms the spanning tree. Order is important! The sum of all edges weight in minimum spanning tree. Note that the edge should put smaller node first, e.g., if the edge (4, 2) is selected, it should be output by 2 4, not 4 2.

If option is 2:
The sum of all edges weight in minimum spanning tree.

Example:
Input:
2

5 7 1

0 2 1

2 1 6

4 2 7

1 4 2

1 3 5

3 0 3

3 2 4

6 12 2

1 0 5

0 4 1

4 5 10

4 3 4

3 0 9

0 5 2

2 0 8

2 1 3

5 2 11

2 3 6

3 5 7

1 5 12

Output: (Prim’s algorithm)

0 2

0 3

1 3

1 4

11

15

please using c++ programming language and follow my INPUT and OUTPUT formats ,thank you .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#define MAXX 1000

char faces[10];
int weights[10][10];
int span_weights[10][10];
int src;
struct Matrix
{
int vert1,vert2;
int wa8;
}queue[20];
int ii,op,ght,kju;
int sick(int sup,int doc)
{
int io,bcv;
if(src==doc)
return 1;
for(io=0;io<ii;io++)
if(span_weights[doc][io]!=MAXX && sup!=io)
{
if(sick(doc,io))
return 1;
}
return 0;
}
void bld_mat()
{
int ter,io,w,bcv,cnt=0;
for(cnt=0;cnt<ii;ght++)
{
ter=queue[ght].vert1;
io=queue[ght].vert2;
w=queue[ght].wa8;
span_weights[ter][io]=span_weights[io][ter]=w;
src=ter;
bcv=sick(ter,io);
if(bcv)
span_weights[ter][io]=span_weights[io][ter]=MAXX;
else
cnt++;
}
}
void interchange(int *ter,int *io)
{
int oiu;
oiu=*ter;
*ter=*io;
*io=oiu;
}
void main()
{
int ter,io,bcv=0,temporary;
int add=0;
clrscr();
printf("\t\t\tKRUSKAL'S ALGORITHM\t\n");
printf("\t\tPlease enter the number of nodes : ");
scanf("%d",&ii);
for(ter=0;ter<ii;ter++)
{
printf("\n\tEnter %d value : ",ter+1);
fflush(stdin);
scanf("%c",&faces[ter]);
for(io=0;io<ii;io++)
{
weights[ter][io]=MAXX;
span_weights[ter][io]=MAXX;
}
}
printf("\t\nGetting the weights\t");
for(ter=0;ter<ii;ter++)
for(io=ter+1;io<ii;io++)
{
printf("\nPlease enter 0 if the path does not exists in between of %c to %c : ",faces[ter],faces[io]);
scanf("%d",&op);
if(op>=1)
{
weights[ter][io]=weights[io][ter]=op;
queue[kju].vert1=ter;
queue[kju].vert2=io;
queue[kju].wa8=weights[ter][io];
if(kju)
{
for(bcv=0;bcv<kju;bcv++)
if(queue[bcv].wa8>queue[kju].wa8)
{
interchange(&queue[bcv].wa8,&queue[kju].wa8);
interchange(&queue[bcv].vert1,&queue[kju].vert1);
interchange(&queue[bcv].vert2,&queue[kju].vert2);
}
}
kju++;
}
}
clrscr();
printf("\t\tGRAPH MATRIX\t\t");
printf("\t\tWeighted Matrix\t\t\t");
for(ter=0;ter<ii;ter++,printf("\t\t"))
for(io=0;io<ii;io++,printf("\t"))
printf("%doc",weights[ter][io]);
bld_mat();
printf("\t\t\t\tMininum Spanning Tree\ii\ii");
printf("\t\t\tNumber Of Edges\t\t");
for(ter=0;ter<ii;ter++)
for(io=ter+1;io<ii;io++)
if(span_weights[ter][io]!=MAXX)
{
printf("\t\t\t%c ------ %c = %d ",faces[ter],faces[io],span_weights[ter][io]);
add+=span_weights[ter][io];
}
printf("\t\t\t\tTotal Weight Of Matrix : %d ",add);
getch();
}

Add a comment
Know the answer?
Add Answer to:
Problem definition: Give the program that implement Prim’s algorithm. Input: First line is N, denotes the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me with this C++ I would like to create that uses a minimum spanning tree algorithm in C++. I would like the program to graph the edges with weights that are entered and will display the r...

    Please help me with this C++ I would like to create that uses a minimum spanning tree algorithm in C++. I would like the program to graph the edges with weights that are entered and will display the results. The contribution of each line will speak to an undirected edge of an associated weighted chart. The edge will comprise of two unequal non-negative whole numbers in the range 0 to 99 speaking to diagram vertices that the edge interfaces. Each...

  • in c++ The Bellman-Ford Algorithm In this assignment, you are asked to implement the Bellman-Ford Algorithm...

    in c++ The Bellman-Ford Algorithm In this assignment, you are asked to implement the Bellman-Ford Algorithm which solves the single-source shortest-paths problem. Specifically, you are given as input a directed graph G = (V. E) with weight w(u, v) on each edge (u, v) E E along with a source vertex s EV. Edges may have negative weights. Input The input has the following format. There are two integers on the first line. The first integer represents the number of...

  • C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected...

    C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected graph G = (V, E), edge e corresponds to a weight w, a minimum weight spaning tree can be found on the graph. Into trees. Input file format At the beginning, there will be a positive integer T, which means that there will be T input data. The first line of each input has two positive integers n,m, representing n points and m edges...

  • Given the following weighted graph G. use Prim's algorithm to determine the Minimum-Cost Spanning Tree (MCST)...

    Given the following weighted graph G. use Prim's algorithm to determine the Minimum-Cost Spanning Tree (MCST) with node 1 as the "root". List the vertices in the order in which the algorithm adds them to the solution, along with the edge and its weight used to make the selection, one per line. Each line should look like this: add vertex y: edge = (x,y), weight = 5 When the algorithm ends there are, generally, edges left in the heap. List...

  • In this problem, you are expected to implement Prim's Algorithm on an undirected simple graph. Write...

    In this problem, you are expected to implement Prim's Algorithm on an undirected simple graph. Write a method that is part of a class that implements Graph as an adjacency matrix. This method should generate a minimum spanning tree using Prim's Algorithm, and print out the edge added by the algorithm on each iteration. 3 10 4 8 Output: 1 2 1 3 34 35 5 6 17 3 12 34 5 1 6 8 20 4 Output: 26 65...

  • Write a c or c++ program to write a prims algorithm and for problem 2(b) use kruskal algorithm.

    write a c or c++ program to write a prims algorithm and for problem 2(b) use kruskal algorithm. Problem 2 (A) (Prim's Algorithm): Apply Prim's algorithm to the following graph. Include in the priority queue only the fringe vertices (the vertices not in the current tree which are adjacent to at least one tree vertex) Problem 2 (B) (Kruskal Algorithm): Apply Kruskaľ's algorithm to find a minimum spanning tree of the following graphs. 4 3 2 2 4 3 6...

  • 1. What is the definition of a safe edge for the minimum spanning tree algorithm? 2....

    1. What is the definition of a safe edge for the minimum spanning tree algorithm? 2. Give an example graph with 4 nodes and 5 edges with exactly three strongly connected components. 3. What is the running time of the Kruskal spanning tree algorithm on a graph with n nodes and n log n edges?

  • a. You have 5 problems in this assignment. b. G++ compiler will be used to compile...

    a. You have 5 problems in this assignment. b. G++ compiler will be used to compile your source codes. c. Your program will be tested on Ubuntu 16.04. d. You are not allowed to use global variables in your implementation. e. Your program will get two arguments, input and output file names, from the command line: >> Your_Executable INPUT_FILE_NAME OUTPUT_FILE_NAME 1. Given a number ? , we initially have ?+1 different sets which are {0}, {1}, {2}, ... , {?}....

  • Minimum Spanning Trees Networks & Graphs 1. Create a spanning tree using the breadth-first search algorithm....

    Minimum Spanning Trees Networks & Graphs 1. Create a spanning tree using the breadth-first search algorithm. Start at A (i..0) and label cach vertex with the correct number after A and show your path. How many edges were used to create a spanning tree? 2. Create a spanning tree using the breadth-first search algorithm. Start at G (ie. O) and label each vertex with the correct number after A and show your path How many edges were used to create...

  • PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node...

    PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node 0 to node 9) must be implemented. You are supposed to denote the distance of the edges via an adjacency matrix (You can assume the edge weights are either 0 or a positive value). The adjacency matrix is supposed to be a 2-D array and it is to be inputted to the graph. Remember that the adjacency list denotes the edge values for the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT