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
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
ADVERTISEMENT