I am trying to create the "Kruskal Function" for a longer program I have, but I can't seem to get passed a few things. Right now, I get errors from "union" having no tag and "find" being a local illegal function definition. Can somebody tell me what's wrong and how to fix it?

template <class vType, int size>void msTreeType<vType, size>::kruskal(vType sVertex){ int i, x, y, max, num; setofsets set(size); int k, l, xx, yy; int parent[size]; for(i=0; i<max; i++) { int find (int x) { y = x; while (parent[y] != y) y = parent[y]; parent[x] = y; return y; } } { union (int x, int y) xx = parent[x]; yy = parent[y]; parent[yy] = xx; } if (parent[k] == parent[l]) find (k,l); else union(k,l); } //end kruskal#endif

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

union is a keyword like class and struct. you may not use it for function name.

Sara

0

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Thank you! Down to three errors now... (I should have realized that in VS when "union" was blue'd!) The function doesn't like the two embedded functions "find" and now "checkUnion" ... the error is "local function definitions are illegal" ... (one of the original problems) ...

That last "union" is supposed to merge the sets in which k and l are present. How would I do that otherwise? (I thought "union" was then an internal command, not a function...)

are very dangerous cause the x and y must be a valid index in the range from 0 to size-1.

i can't see anything in your code which would make that sure. if you access the array parent out of boundaries the program would crash.

and if you have a function find that takes 1 int, you can't call it with

find(k, l, i);

Sara

0

Member_2_4213139Author Commented:

Dear Sara,

This is a Kruskal Algorithm. I'm trying to first sort the edges in ascending order based on their weights, considering each edge in that order. Then, I want to include it in an MST solution if it does not result in cycle, and reject it otherwise.

I wanted the "union" to merge the sets in which k and l are present.

i am now offline a few hours. i'll will find out about kruskal in the meantime. you could make a sample of a graph and tell what should happen.

Sara

0

Member_2_4213139Author Commented:

Dear Sara,

I understand! I had to leave for a few hours, myself! I was trying to tell you that, but the EE site started taking forever to load, so I had to abandon the comment! Sorry!!!

The data is fed to the program via a text file. If the algorithm is working properly, it should give data similar to the following:

i checked the code you posted and though there are a few unknowns like the msTreeType and the template type vType i found nothing which actually can be taken as a base to proceed on. for example you have an array parent[size] which is not initialised. but you do a loop on it like it was properly filled with vertex data.

also the results you posted for kruskal do not build a minimum spanning tree.

can you post the source of the prim2 algorithm which 'is working perfectly'?

can you also post the msTreeType header and the intented type for vType?

Sara

0

Member_2_4213139Author Commented:

Dear Sara,

I was afraid of that! As I looked to other Kruskal examples, I noticed some other parts of my code were lacking, but each had the same inner core that mine had, so I thought I was good.

Here is the Prim2 Algorithm:

template <class vType, int size>void msTreeType<vType, size>::prim2(vType sVertex){ int i, j, k; double minWeight; source = sVertex; bool visited[size]; for (j = 0; j < gSize; j++) { visited[j] = false; edges[j] = source; edgeWeights[j] = weights[source][j]; } visited[source] = true; edgeWeights[source] = 0; for (i = 0; i < gSize; i++) { minWeight = infinity; k = -1; for (j = 0; j < gSize; j++) if (!visited[j] && edgeWeights[j] < minWeight) { k = j; minWeight = edgeWeights[j]; } if (k != source && k != -1) { visited[k] = true; for (j = 0; j < gSize; j++) if (!visited[j]) if (weights[k][j] < edgeWeights[j]) { edges[j] = k; edgeWeights[j] = weights[k][j]; } } } //end for} //end prim2

template<class vType, int size>class msTreeType: public graphType<vType, size> { public: void createSpanningGraph(); //Function to create the graph and the weight matrix. void minimalSpanning(vType sVertex); //Function to create the edges of the minimal //spanning tree. The weight of the edges is also //saved in the array edgeWeights. void printTreeAndWeight(); //Function to output the edges and the weight of the //minimal spanning tree. void printKruskal(); //Function to output the edges and the weight of the //minimal spanning tree for Kruskal algorithm. void prim2(vType sVertex); //This function creates the edges of the minimal //spanning tree. The weight of the edges is also //saved in the array edgeWeights void kruskal(vType sVertex); //This function finds the minimal spanning tree by //finding smallest weights. The weight of the edges is //also saved in the array edgeWeights protected: vType source; double weights[size][size]; int edges[size]; double edgeWeights[size]; edgeContainer eList[size*size]; int arrayX[size]; int arrayY[size]; };

"vType" is from the Type Class. and its lengthy. Would you still need it?

0

Member_2_4213139Author Commented:

Dear Sara,

What I actually figured was that I would take what I have in the Prim2 and put the code that I have for Kruskal on it, as then Prim2 would then act as Kruskal. Thoughts?

the prim2 code is indeed much much better than that you posted for kruskal. for example, see the setting of the visited, edges, and edgesWeights array at begin of the function (though the use of global variables is not good design).

you could use the prim2 code as a base and feed it with the graph you have for kruskal but if that is your first c program of your own, there is little chance to get it working. i cannot write it for you cause the idea of ee is to help but not to do the work.

if you want to make an attempt, take the prim2 code and change the name to kruskal and put the new data to the source and weights variables. try to get it run. if you got errors you best open a new q. and ask for specific help on that error.

Sara

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

THANK YOU! I know that ee is to help and not do the work! I am both grateful and appreciative for (well, most of) the help I get here!!! (YOU are included in the gratefulness and appreciation!!!)

when you want to procede with new questions you always can ask what to do next here in that thread.

much luck.

Sara

0

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for freeEdge Out The Competitionfor your dream job with proven skills and certifications.Get started todayStand Outas the employee with proven skills.Start learning today for freeMove Your Career Forwardwith certification training in the latest technologies.Start your trial today

22: {

23: union (int x, int y)

put this instead:

22: union (int x, int y)

23: {