本文整理汇总了C++中ListGraph类的典型用法代码示例。如果您正苦于以下问题:C++ ListGraph类的具体用法?C++ ListGraph怎么用?C++ ListGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ListGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GenerateRandomEuclideanListGraph
//Generate a random complete euclidean ListGraph
bool GenerateRandomEuclideanListGraph(ListGraph &g,
NodeStringMap &vname, // node names
NodePosMap& px, // x-position of the nodes
NodePosMap& py, // y-position of the nodes
EdgeValueMap& weight, // weight of edges
int n, // number of nodes
double SizeX, // coordinate x is a random number in [0,SizeX)
double SizeY) // coordinate y is a random number in [0,SizeY)
{
int i,j; // n=number of nodes
Node *V;
V = new Node[n];
if (V==NULL){
cout << "Memory allocation error, number of nodes " << n << " too large\n";
exit(0);}
for (i=0;i<n;i++) { // insert nodes (random points in [0,100] x [0,100] )
V[i] = g.addNode(); // generate a new node
px[V[i]] = SizeX*drand48();
py[V[i]] = SizeY*drand48();
vname[V[i]] = IntToString(i+1); // name of the node is i+1
}
for (i=0;i<n;i++)
for (j=i+1;j<n;j++) {
Edge e = g.addEdge(V[i],V[j]); // generate an edge
weight[e] = sqrt(pow(px[V[i]]-px[V[j]],2) + pow(py[V[i]]-py[V[j]],2));
}
delete[] V;
return(true);
}
示例2: ViewTspCircuit
void ViewTspCircuit(TSP_Data &tsp)
{
ListGraph h;
ListGraph::NodeMap<string> h_vname(h); // node names
ListGraph::NodeMap<Node> h_g2h(tsp.g); // maps a node of g to a node of h
ListGraph::NodeMap<double> h_posx(h);
ListGraph::NodeMap<double> h_posy(h);
ListGraph::NodeMap<int> vcolor(h); // color of the vertices
ListGraph::EdgeMap<int> acolor(h); // color of edges
ListGraph::EdgeMap<string> aname(h); // name of edges
for (ListGraph::NodeIt v(tsp.g); v!=INVALID; ++v) {
Node hv;
hv = h.addNode();
h_g2h[v] = hv;
h_posx[hv] = tsp.posx[v];
h_posy[hv] = tsp.posy[v];
h_vname[hv] = tsp.vname[v];
vcolor[hv] = BLUE;
}
for (int i=0;i<tsp.NNodes;i++) {
ListGraph::Node u,v;
ListGraph::Edge a;
u = tsp.BestCircuit[i];
v = tsp.BestCircuit[(i+1) % tsp.NNodes];
a = h.addEdge(h_g2h[u] , h_g2h[v]);
aname[a] = "";
acolor[a] = BLUE;
}
ViewListGraph(h,h_vname,aname,h_posx,h_posy,vcolor,acolor,"TSP Circuit with cost "+DoubleToString(tsp.BestCircuitValue));
}
示例3: ViewGomoryHuTree
int ViewGomoryHuTree(ListGraph &g,
NodeStringMap &vname,
NodePosMap &px, // xy-coodinates for each node
NodePosMap &py, //
GomoryHu<ListGraph, EdgeValueMap > &ght,
string text)
{
ListGraph T;
NodeNodeMap map(g);
Edge te;
EdgeStringMap tename(T); // name of T edges
EdgeValueMap teweight(T); // name of T edges
NodeStringMap tvname(T); // name of T nodes
NodePosMap tpx(T); // xy-coodinates for each node
NodePosMap tpy(T); //
NodeColorMap tvcolor(T); // color of the vertices
EdgeColorMap tecolor(T); // color of the edges
for (NodeIt v(g); v != INVALID; ++v) {
map[v] = T.addNode();
tvname[map[v]] = vname[v];
tvcolor[map[v]] = WHITE;
tpx[map[v]] = px[v];
tpy[map[v]] = py[v];
}
for (NodeIt u(g); u != INVALID; ++u) {
if ((g).id(ght.predNode(u))==-1) continue;
te = T.addEdge(map[u], map[ght.predNode(u)]);
tename[te] = DoubleToString(ght.predValue(u));
teweight[te] = ght.predValue(u);
tecolor[te] = BLUE;
}
return(ViewListGraph(T,tvname,tename,tpx,tpy,tvcolor,tecolor,text));
}
示例4:
ExtendedEdge::ExtendedEdge(ListGraph &g, ListGraph::Node u,
ListGraph::Node v, int pos_u, int pos_v){
_adjacentNodes.push_back(g.id(u));
_adjacentNodes.push_back(g.id(v));
_adjacentNodesPos.push_back(pos_u);
_adjacentNodesPos.push_back(pos_v);
edge = g.addEdge(u,v);
_id = g.id(edge);
_length = 0;
}
示例5: GenerateTriangulatedListGraph
// Generate a triangulated ListGraph, building the Delaunay
// triangulation of random points
// Uses the geompack program, available in
// http://people.sc.fsu.edu/~jburkardt/cpp_src/geompack/geompack.html
bool GenerateTriangulatedListGraph(ListGraph &g, // return with generated graph
NodeStringMap &vname, // return with name of the nodes
NodePosMap& px, // return with x-position of the nodes
NodePosMap& py, // return with y-position of the nodes
EdgeValueMap& weight, // return with weight of edges
int n, // number of nodes
double SizeX, // coordinate x is a random number in [0,SizeX)
double SizeY) // coordinate y is a random number in [0,SizeY)
{
int i; // n=number of nodes
int ntri; // number of Delaunay triangles
Node *V = new Node[n];
double *p = new double[2*n+2];// node coodinates are (x;y) = ( p[2*i] ; p[2*i+1] )
int *tri = new int[6*n]; // Each 3 elements are the indexes of a triangle
int *tri_nabe = new int[6*n];
if ((V==NULL)||(p==NULL)||(tri==NULL)||(tri_nabe==NULL)){
cout << "Memory allocation error, number of nodes " << n << " too large\n";
exit(0);}
for (i=0;i<n;i++) {
V[i] = g.addNode(); // gera um vértice nó do grafo
px[V[i]] = SizeX*drand48(); // nodes are random points
py[V[i]] = SizeY*drand48();
p[2*i] = px[V[i]];
p[2*i+1] = py[V[i]];
vname[V[i]] = IntToString(i+1); // name of the node is i+1
}
if (r8tris2 ( n, p, &ntri, tri, tri_nabe )) { printf("ERROR\n");Pause(); }
for (i=0;i<ntri;i++) {
int a,b,c;
a = tri[3*i]-1; b = tri[3*i+1]-1; c = tri[3*i+2]-1;
// each triangle if formed with nodes V[a] , V[b] , V[c]
// insert edges without duplications
if ((findEdge(g,V[a],V[b])==INVALID) && (findEdge(g,V[b],V[a])==INVALID)){
Edge e = g.addEdge(V[a],V[b]);
weight[e] = sqrt(pow(px[V[a]]-px[V[b]],2) + pow(py[V[a]]-py[V[b]],2));
}
if ((findEdge(g,V[a],V[c])==INVALID) && (findEdge(g,V[c],V[a])==INVALID)){
Edge e = g.addEdge(V[a],V[c]);
weight[e] = sqrt(pow(px[V[a]]-px[V[c]],2) + pow(py[V[a]]-py[V[c]],2));
}
if ((findEdge(g,V[b],V[c])==INVALID) && (findEdge(g,V[c],V[b])==INVALID)) {
Edge e = g.addEdge(V[b],V[c]);
weight[e] = sqrt(pow(px[V[b]]-px[V[c]],2) + pow(py[V[b]]-py[V[c]],2));
}
}
delete[] V;
delete[] p;
delete[] tri;
delete[] tri_nabe;
return(true);
}
示例6: main
int main( void ){
Timer T(true);
int init_nodes_num, final_nodes_num, edge_addition_num;
init_nodes_num = 10;
final_nodes_num = 50;
edge_addition_num = 7;
typedef ListEdgeSet< ListGraph > EdgeSet;
ListGraph mListGraph;
EdgeSet mNewEdges( mListGraph );
FullGraph fg(init_nodes_num);
GraphCopy<FullGraph, ListGraph> cg( fg, mListGraph); // Create the seed nodes
cg.run();
int mNumEdges = countEdges( mListGraph );
EdgeSet::Edge e;
lemon::Random mRandom;
mRandom.seedFromTime();
ListGraph::Node newNode, randNode;
// new edges will be saved seperatly in an EdgeSet, not to change the original node degrees
for ( int i = init_nodes_num; i < final_nodes_num; i++){
mNumEdges = countEdges( mListGraph );
mNewEdges.clear();
newNode = mListGraph.addNode();
while ( countEdges( mNewEdges ) != edge_addition_num ) {
randNode = mListGraph.nodeFromId( mRandom[ mListGraph.maxNodeId() ] ) ;
// --- CALCULATE THE PROBABILITY
if ( mRandom.real() < (( (double)(countIncEdges(mListGraph, randNode)) / (double)( 2*mNumEdges ) )) ){
if ( findEdge( mNewEdges, newNode, randNode ) == INVALID){ // does the edge already exist?
mNewEdges.addEdge( newNode, randNode );
}
}
}
// Create the new edges in the original graph
for (EdgeSet::EdgeIt e( mNewEdges ); e!=INVALID; ++e){
mListGraph.addEdge( mNewEdges.u( e ), mNewEdges.v(e) );
}
}
cout << T.realTime() << endl;
cout << countEdges( mListGraph) << endl;
cout << countEdges( fg ) << endl;
}
示例7:
MatrixGraph::MatrixGraph(const ListGraph& listGraph) {
int edgesCount = listGraph.edgesCount();
int vertexCount = listGraph.vertexCount();
iMatrix = vector<vector<int> >(vertexCount, vector<int>(edgesCount, 0));
int edge = 0;
for (int i = 0; i < vertexCount; ++i) {
for (auto v : listGraph.getLists()[i]) {
iMatrix[i][edge] = -1;
iMatrix[v][edge] = 1;
++edge;
}
}
}
示例8: PrintListGraph
void PrintListGraph(ListGraph &g, NodeStringMap &vname, EdgeValueMap &graphweight)
{
int Nnodes = countNodes(g); // number of nodes in the input graph
int Nedges = countEdges(g); // number of edges in the input graph
printf("-------------------------------------------------------\n");
printf("Number of nodes: %d\n",Nnodes);
printf("Number of edges: %d\n",Nedges);
for (NodeIt v(g); v!=INVALID; ++v) printf("%s\n",vname[v].c_str()); printf("\n");
printf("-------------------------------------------------------\n");
for (EdgeIt a(g); a!=INVALID; ++a)
printf("%s -- %s %lf\n",vname[g.u(a)].c_str(),vname[g.v(a)].c_str(), graphweight[a]);
printf("\n");
}
示例9: ReadListGraphEdges
void ReadListGraphEdges(ListGraph &g,int nedges,ifstream & ifile,
#if __cplusplus >= 201103L
std::unordered_map<string,Node> & string2node,
#else
std::tr1::unordered_map<string,Node> & string2node,
#endif
EdgeValueMap &weight)
{
Node u,v;
Edge a;
string nomeu,nomev;
double peso;
for (int i=0;i<nedges;i++) {
// format: <node_u> <node_v> <edge_weight>
ifile >> nomeu; ifile >> nomev; ifile >> peso;
if (ifile.eof())
{cout << "Reached unexpected end of file.\n"; exit(0);}
auto test = string2node.find(nomeu);
if (test == string2node.end()) {cout<<"ERROR: Unknown node: "<<nomeu<<endl;exit(0);}
else u = string2node[nomeu];
test = string2node.find(nomev);
if (test == string2node.end()) {cout<<"ERROR: Unknown node: "<<nomev<<endl;exit(0);}
else v = string2node[nomev];
a = g.addEdge(u,v);
weight[a] = peso;
}
}
示例10: ReadListGraphNodes
// To read list of nodes in the format: <node_name> <double1> <double2>
void ReadListGraphNodes(ListGraph &g,int nnodes,ifstream & ifile,
#if __cplusplus >= 201103L
std::unordered_map<string,Node> & string2node,
#else
std::tr1::unordered_map<string,Node> & string2node,
#endif
NodeStringMap &vname,
NodePosMap &posx,
NodePosMap &posy)
{ string STR; Node u,v; string token;
for (int i=0;i<nnodes;i++) {
getline(ifile,STR);
if (ifile.eof()) {cout<<"Reached unexpected end of file.\n";exit(0);}
while (STR=="") getline(ifile,STR);
{ istringstream ins; // Declare an input string stream.
ins.str(STR); // Specify string to read.
for (int p=0; getline(ins, token, ' ') ; p++) {
if (p==0) { // For example, to read: node_name posx posy
auto test = string2node.find(token);
if (test!=string2node.end()){cout<<"ERROR: Repeated node: "<<token<<endl;exit(0);}
v = g.addNode(); string2node[token] = v; vname[v] = token;
posx[v]=DBL_MAX; posy[v]=DBL_MAX; }
else if (p==1) { posx[v] = atof(token.c_str());}
else if (p==2) { posy[v] = atof(token.c_str());}
}}
}
}
示例11: monitoramento_em_grafo_bipartido
int monitoramento_em_grafo_bipartido( ListGraph &g, NodeName &vname, ListGraph::NodeMap<double> &custo, ListGraph::NodeMap<int> &solucao)
{
int seed=0;
GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env);
model.getEnv().set(GRB_IntParam_Seed, seed);
model.set(GRB_StringAttr_ModelName, "Monitoramento em Grafo Bipartido"); // prob. name
model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem
// ------------------------------------------------------
// Construa o modelo daqui para baixo
// ------------------------------------------------------
// Exemplos de como voce pode declarar variaveis indexadas nos vertices ou nas arestas.
// Nao necessariamente voce precisa dos dois tipos
// ListGraph::NodeMap<GRBVar> x(g); // variables for each node
// ListGraph::EdgeMap<GRBVar> y(g); // variables for each edge
ListGraph::NodeMap<GRBVar> x(g); // variables for each node
ListGraph::EdgeMap<GRBVar> y(g); // variables for each edge
int name = 0;
char namme[100];
for(ListGraph::NodeIt v(g); v != INVALID; ++v) {
sprintf(namme,"PC_%s",vname[v].c_str());
x[v] = model.addVar(0.0, 1.0, custo[v],GRB_CONTINUOUS,namme); }
model.update();
try {
for(ListGraph::EdgeIt e(g); e != INVALID; ++e) {
//Para cada aresta, um dos lados e 1
GRBLinExpr expr;
expr += x[g.u(e)];
expr += x[g.v(e)];
model.addConstr(expr >= 1);
}
model.update();
// ------------------------------------------------------
// Construa o modelo daqui para cima
// ------------------------------------------------------
//model.write("model.lp"); system("cat model.lp");
model.optimize();
for (ListGraph::NodeIt v(g); v!=INVALID; ++v) {
if (x[v].get(GRB_DoubleAttr_X)>1-EPS) solucao[v] = 1;
else solucao[v] = 0;
//solucao[v] = 1;
}
return(1);
} catch (...) {cout << "Error during callback..." << endl; return(0);}
}
示例12: ViewListGraph
// This routine visualize a graph using a pdf viewer. It uses neato (from
// graphviz.org) to generate a pdf file and a program to view the pdf file. The
// pdf viewer name is given in the viewername parameter.
int ViewListGraph(ListGraph &g,
NodeStringMap &vname, // name of the nodes
EdgeStringMap &ename, // name of edges
NodePosMap& px, // x-position of the nodes
NodePosMap& py, // y-position of the nodes
NodeColorMap& vcolor, // color of node (see myutils.h)
EdgeColorMap& ecolor, // color of edge
string text) // text displayed below the figure
{
char tempname[1000],cmd[1000],outputname[1000];
FILE *fp;
double minpx=DBL_MAX,minpy=DBL_MAX,maxpx=-DBL_MAX,maxpy=-DBL_MAX,delta,factor;
// obtain a temporary file name
strcpy(tempname,".viewgraphtempname");
sprintf(outputname,"%s.pdf",tempname);
fp = fopen(tempname,"w+");
if (fp==NULL) {cout << "Error to open temporary file to visualize graph.\n"; return(0);}
for (NodeIt v(g); v!=INVALID; ++v) {
if (px[v] < minpx) minpx = px[v];
if (px[v] > maxpx) maxpx = px[v];
if (py[v] < minpy) minpy = py[v];
if (py[v] > maxpy) maxpy = py[v];
}
factor = 40; // quanto maior, menor o desenho do vértice
delta = fmax(maxpx - minpx,maxpy - minpy);
// Generate a text file with the graph format of neato program
fprintf(fp,"graph g {\n");
//fprintf(fp,"\tsize = \"10, 10\";\n");
//fprintf(fp,"\tnode [shape = \"circle\"];\n");
fprintf(fp,"\tnode [\n");
fprintf(fp,"shape = \"ellipse\",\n");
fprintf(fp,"style = \"bold\",\n");
fprintf(fp,"color = \"black\",\n");
fprintf(fp,"];\n");
for (NodeIt v(g); v!=INVALID; ++v) {
if (vcolor[v]==NOCOLOR) continue;
fprintf(fp,"\t%s [style = \"bold\", color=\"%s\", pos = \"%lf,%lf!\" ];\n",
vname[v].c_str(),ColorName(vcolor[v]).c_str(),factor*(px[v]-minpx)/delta,factor*(py[v]-minpy)/delta);
}
int nar=0;
for (EdgeIt e(g); e!=INVALID; ++e) {
nar++;
if (ecolor[e]==NOCOLOR) continue;
fprintf(fp,"\t%s -- %s [label = \"%s\", color=\"%s\" ];\n",vname[g.u(e)].c_str(),vname[g.v(e)].c_str(),ename[e].c_str(),ColorName(ecolor[e]).c_str());
//cout << "e_" << nar << " = ( " << vname[g.u(e)] << " , " << vname[g.v(e)] << ")\n";
}
//cout << "\n\n\nColocou "<< nar << " arestas\n\n\n";
fprintf(fp,"label=\"%s\";\nfontsize=50;\n",text.c_str());
fprintf(fp,"}\n");
fclose(fp);
sprintf(cmd,"neato -Tpdf %s -o %s",tempname,outputname); system(cmd);
//cout << "Grafo em "<< tempname << "\n";
view_pdffile(outputname);
//pause();
return(1);
}
示例13: some
/**
second relaxation of tsp
idea:
remove some (random) node
take mst
add shortest two edges of the removed node
*/
int TSPRelaxation::mstOnSubgraph()
{
// create a copy of the graph
ListGraph g;
ListGraph::EdgeMap<int> weight(g);
ListGraph::NodeMap<ListGraph::Node> nodemap(g);
GraphCopy<ListGraph, ListGraph> copy(this->g, g);
copy.edgeMap(this->weight, weight).nodeCrossRef(nodemap).run();
// remove a random node
// removed will contain the node of this->g corresponding to the removed one afterwards
int del = rand() % countNodes(g);
ListGraph::Node removed;
for (ListGraph::NodeIt n(g); n != INVALID; ++n)
{
if (del == 0)
{
removed = nodemap[n];
g.erase(n);
break;
}
del--;
}
// calculate mst
MST mst(g, weight);
int w = mst.prim();
// search for two shortest edges incident to the removed node
int mins[] = {numeric_limits<int>::max(), numeric_limits<int>::max()};
for (ListGraph::IncEdgeIt e(this->g, removed); e != INVALID; ++e)
{ // iterate over all incident nodes
if (this->weight[e] < mins[0])
{ // shortest found yet
mins[1] = mins[0];
mins[0] = this->weight[e];
}
// 2nd-shortest
else if (this->weight[e] < mins[1]) mins[1] = this->weight[e];
}
return w + mins[0] + mins[1];
}
示例14: ReadEuclideanListGraph
bool ReadEuclideanListGraph(string filename,
ListGraph &g,
NodeStringMap & vname,
EdgeValueMap & custo,
NodePosMap & posx,
NodePosMap & posy,
NodeBoolMap& is_terminal)
{
int i,n,m, terminal;
Node nu,nv;
Edge a;
char nomev[100];
Node v;
double px,py;
ifstream ifile; ifile.open(filename.c_str()); if (!ifile) return(false);
PulaBrancoComentario(ifile);
// format: <number_of_nodes> -1
// The value -1 is to indicate that there is no edge/arc, as edge weights
// are given by the euclidean distance
ifile >> n; ifile >> m;
if (m!=-1) {
printf("Wrong format in the euclidean graph of file %s.\n",filename.c_str());
return(false);
}
for (i=0;i<n;i++) {
ifile >> nomev; ifile >> px; ifile >> py; ifile >> terminal;
v = g.addNode(); vname[v] = nomev; posx[v] = px; posy[v] = py;
if(terminal == 0) is_terminal[v] = false;
else is_terminal[v] = true;
}
for (NodeIt v(g); v!=INVALID; ++v) {
NodeIt u(g);
u=v;
for (++u; u!=INVALID; ++u) {
a = g.addEdge(u,v);
custo[a] = sqrt((posx[u]-posx[v])*(posx[u]-posx[v]) +
(posy[u]-posy[v])*(posy[u]-posy[v]));
}
}
ifile.close();
return(true);
}
示例15: main
int main(int argc, char *argv[])
{
int n;
double box_width,box_height;
ListGraph g; // graph declaration
NodeName vname(g); // name of graph nodes
ListGraph::NodeMap<double> px(g),py(g); // xy-coodinates for each node
ListGraph::NodeMap<int> vcolor(g);// color of nodes
ListGraph::EdgeMap<int> ecolor(g); // color of edges
EdgeWeight lpvar(g); // used to obtain the contents of the LP variables
EdgeWeight weight(g); // edge weights
vector <Node> V;
srand48(1);
// double cutoff; // used to prune non promissing branches (of the B&B tree)
if (argc!=5) {cout<<"Usage: "<< argv[0]<<" <number_of_nodes_in_graph> <number_of_pairs> <box_width> <box_height>"<< endl;exit(0);}
n = atoi(argv[1]);
int kPairs = atoi(argv[2]);
box_width = atof(argv[3]);
box_height = atof(argv[4]);
GenerateTriangulatedListGraph(g,vname,px,py,weight,n,box_width,box_height);
int nV = countNodes(g);
int nA = countEdges(g);
cout << nA << " " << nV << " " << kPairs << endl;
for (NodeIt v(g);v!=INVALID;++v)
cout << vname[v] << " " << px[v] << " " << py[v] << endl;
for (EdgeIt e(g);e!=INVALID;++e)
cout << vname[g.u(e)] << " " << vname[g.v(e)] << " " << 20*drand48() << " " << weight[e] << " " << 5*drand48() <<endl;
for (int i = 0; i < kPairs; i++) {
int v1 = ((int)((nV-1)*drand48() + 1));
int v2 = ((int)((nV-1)*drand48() + 1));
if (v1 != v2) {
cout << v1 <<" " << v2 <<" "<< (25*drand48() + 25*drand48()) <<" " << (20*drand48() + nA)<< endl;
} else {
i--;
}
}
return 0;
}