void main() { edge e[100]; int parent[100]; int n,i,j,m,k = 1,cost = 0; clrscr(); printf("KRUSKAL's ALGORITHM\n"); printf("Enter number of nodes\n"); scanf("%d",&n); for(i=0;i< n;++i) parent[i]=-1; i = 0; printf("Enter number of edges\n"); scanf("%d",&m); for(i=0;i< m;++i) { printf("enter an edge and wt\n"); scanf("%d %d %d", &e[i].node1,&e[i].node2,&e[i].wt); } sortedges(e,m); printf("\n\nEdges of the tree\n"); i = 0; while(k< n) { if(checkcycle(parent,e[i].node1,e[i].node2)) { k++; cost=cost+e[i].wt; i++; } } printf("cost = %d",cost); getch(); }
/* C++ | R INTERFACE; main function */ RcppExport SEXP cophen (SEXP tree) { /* * tree: a list of elements * ROOT: most internal node * MAXNODE: least internal internal node (and largest valued in edge matrix) * ENDOFCLADE: rows in edge matrix * ANC: first column of 'phylo' edge matrix (e.g., phy$edge[,1]) * DES: second column of 'phylo' edge matrix (e.g., phy$edge[,2]) * EDGES: edge lengths, sorted by node label and including the root (at position Ntip(phy)+1) * COPHEN: the current state of the variance-covariance matrix, initialized with 0s in R */ try { std::vector<int>::size_type i; /* call in parameters associated with 'phylo' object */ Rcpp::List phylo(tree); int root = (int) Rcpp::as<int>(phylo["ROOT"]); int maxnode = Rcpp::as<int>(phylo["MAXNODE"]); int endofclade = Rcpp::as<int>(phylo["ENDOFCLADE"]); std::vector<int> anc=phylo["ANC"]; std::vector<int> des=phylo["DES"]; std::vector<double> unsortededges=phylo["EDGES"]; std::vector<double> edges=phylo["EDGES"]; std::vector<double> V=phylo["COPHEN"]; std::vector<double> cophen=V; /* initialize edges */ for(i=0; i<edges.size(); i++) { edges.at(i)=0; } /* sort edges by node label */ sortedges(unsortededges, edges, des); /* call to primary function that updates VCV matrix */ vcv_internal(maxnode, root, endofclade, anc, des, edges, V); /* call to converter function for VCV to COPHENETIC */ vcv_to_cophenetic(root, V, cophen); /* PREPARE OUTPUT FOR R */ return Rcpp::List::create(Rcpp::Named("COPHEN",cophen)); } catch( std::exception &ex ) { forward_exception_to_r( ex ); } catch(...) { ::Rf_error( "C++ exception: unknown reason" ); } return R_NilValue; }