当前位置: 首页>>代码示例>>C++>>正文


C++ graph::num_of_vertices方法代码示例

本文整理汇总了C++中graph::num_of_vertices方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::num_of_vertices方法的具体用法?C++ graph::num_of_vertices怎么用?C++ graph::num_of_vertices使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在graph的用法示例。


在下文中一共展示了graph::num_of_vertices方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: prim

void mst::prim(graph g) {
    int num = g.num_of_vertices();
    minheap* candidates = new minheap(num);
    reset();

    // find the starting point and initialize the heap
    int start_node = 0;
    while(candidates->size() == 0) {
        for(int i=0; i<num; ++i) {
            float c = g.cost(start_node, i);
            if (c != 0) candidates->update(c, i);
        }
        ++start_node;
    }
    closed_set.insert(start_node-1);

    while (candidates->size()!=0) {
        heapitem t = candidates->pop();
        int node = t.get_node();
        // not record the duplicated probed nodes
        if (closed_set.find(node)!=closed_set.end())
            continue;
        closed_set.insert(node);
        path_cost += t.get_key();
        vector<int> n = g.neighbors(node);
        // update the heap with newly found nodes and edges
        for(vector<int>::iterator i=n.begin(); i!=n.end(); ++i) {
            candidates->update(g.cost(node,*i), *i);
        }
    }
    // ckeck if there are isolated nodes
    if (closed_set.size() < num-1) path_cost=-1;
}
开发者ID:chubbypanda,项目名称:learning,代码行数:33,代码来源:mst.cpp

示例2: kruskal

void mst::kruskal(graph g) {
    int num = g.num_of_vertices();
    vector< vector<float> > edges;
    union_find explored;
    reset();

    // put all connected vertices in "edges"
    for(int i=0; i<num; ++i) {
        for(int j=0; j<num; ++j) {
            float c = g.cost(i, j);
            if (c!=0) {
                vector<float> temp;
                temp.push_back(i);
                temp.push_back(j);
                temp.push_back(c);
                edges.push_back(temp);
            }
        }
    }
    sort(edges.begin(), edges.end(), kruskal_compare);

    for(vector<vector<float> >::iterator  p=edges.begin(); p!=edges.end(); ++p) {
        // both nodes in the closed set ==> detecting a cycle
        vector<float> temp = *p;
        int f1 = explored.find(temp[0]);
        int f2 = explored.find(temp[1]);
        if(f1!=-1 && f2!=-1 && f1==f2) continue;
        path_cost += temp[2];
        explored.insert(temp[0], temp[1]);
        //cout <<"From "<<temp[0]<<" To "<<temp[1]<<" -- Cost "<<temp[2]<<endl;
    }
    // ckeck if there are isolated nodes
    if (explored.num_of_unions() != 1) path_cost=-1;
}
开发者ID:chubbypanda,项目名称:learning,代码行数:34,代码来源:mst.cpp

示例3: path

// calculate the path using dijkstra's algo.
void dijkstra::path(graph g, int u, int v) {
    int num = g.num_of_vertices();
    minheap* candidates = new minheap(num);
    // reset the path_cost and clear the closed_set
    reset();

    // initialize the heap
    // the nodes in the heap are those of "open set"
    for (int i=0; i<num; ++i) {
        float c = g.cost(u, i);
        if (c!=0)
            candidates->update(c, i);
    }

    while (candidates->size()!=0) {
        heapitem t = candidates->pop();
        int node = t.get_node();
        // not record the duplicated probed nodes
        if (closed_set.find(node)!=closed_set.end())
            continue;
        closed_set.insert(node);
        path_cost = t.get_key();
        // terminated if arrives at the destination
        if (node == v)
            return;
        vector<int> n = g.neighbors(node);
        // update the heap with newly found nodes
        for(vector<int>::iterator i=n.begin(); i!=n.end(); ++i) {
            candidates->update(path_cost+g.cost(node,*i), *i);
        }
    }
    // after iteration, the v is not found
    path_cost = -1;
}
开发者ID:chubbypanda,项目名称:learning,代码行数:35,代码来源:dijkstra.cpp


注:本文中的graph::num_of_vertices方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。