Algorithm of minimum spanning tree--prim algorithm

Source: Internet
Author: User

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#define Max_vex 6

typedef struct{
string S;
int lowcast;
}mm;

String ss[max_vex]={"v1", "V2", "V3", "V4", "V5", "V6"};

MM Closedge[max_vex];
MM Arcs[max_vex][max_vex];
Vector<string> G (Ss,ss+max_vex); Class
Vector<string> U; Used to record nodes added sequentially


int Locatevex (vector<string> g,string u)
{

int iter1=0;
while (Iter1<g.size ())
{
if (U==g[iter1]) return iter1;
++iter1;
}

}



int minimum (mm *closedge)
{
int min=32;
int p;
for (int i=0;i<max_vex;i++)
if (min>closedge[i].lowcast &&closedge[i].s!= "NULL") {p=i; min=closedge[i].lowcast;}
return p;
}

int main (int argc, char *argv[])
{
int sum=0;
String U;

int i,j;

cout<< "Please input cars for initial array:" <<endl;
for (i=0;i<max_vex;i++)
for (j=0;j<max_vex;j++)
cin>>arcs[i][j].s>>arcs[i][j].lowcast;

u= "V1";
cout<<u; Determine the first node.
cout<<endl;

int K=locatevex (G,U);

For (J=0;j<g.size (); j + +)
if (j!=k) {closedge[j].s=u; Closedge[j].lowcast=arcs[k][j].lowcast} Initialization of secondary arrays

closedge[k].lowcast=0;
Closedge[k].s= "NULL";
U.push_back (U); Add the first node to the vector u

For (I=1;i<g.size (); i++)
{
K=minimum (Closedge); Find a subscript for the shortest side

cout<<closedge[k].lowcast;
cout<<g[k];
cout<<endl;

Sum+=closedge[k].lowcast; Add the paths between the two nodes and
U.push_back (G[k]);

closedge[k].lowcast=0;
Closedge[k].s= "NULL";
For (J=0;j<g.size (); j + +)
if (arcs[k][j].lowcast<closedge[j].lowcast)
{
Closedge[j].lowcast=arcs[k][j].lowcast;
CLOSEDGE[J].S=G[K];
}
}
cout<<endl;

For (I=0;i<u.size (); i++)
cout<<u[i]<< "";

cout<< "Output minimum path is:" <<endl;
cout<<sum<<endl;
System ("PAUSE");
return 0;
}

Recently, perhaps because of the algorithm analysis on the day, their interest also with the class only increase, in fact, the smallest spanning trees in the big 21 period to learn. Just because I saw the algorithm on the book dizzy, I can not understand what it means, perhaps the most important thing is that they did not work hard, also not to very calmly to think about the problem, until now, the last class teacher just talked about the train of thought, but always feel that they will think should be able to make it, the afternoon tutor came back to want to rest early, But have no intention to see the data structure of the book, Think of the last class of their own also think of a sudden minimum generation algorithm, slowly made up, feel good, and finally understand a lot of things as long as the beginning, it is really not difficult, although today did not do anything, this topic is not difficult to feel, but also let me feel more about the use of containers, It seems that they are really a good thing. Although nothing of great value, this little problem has raised a little bit of satisfaction. Should be more patient in the future. There are a lot of tasks waiting. Come on slowly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.