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


C++ AdjacencyList::add_edge方法代码示例

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


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

示例1: main

int main()
{
    std::map<unsigned int, unsigned int> hexToVertex;
    std::map<unsigned int, unsigned int> vertexToHex;
    int hex1 = 0, hex2 =0;
    string weight;
    string hex1_str;
    string hex2_str;
    ifstream file("mapedges.data");
    AdjacencyList mygameboard;
    //while(!file.eof())
    while(file >> hex1 >> hex2 >> weight)
    {
        if(weight == "w")
            continue;
        if(hexToVertex.count(hex1)==0)
        {
            unsigned int v1=mygameboard.add_vertex(); //x will be ??
            //cout << "vertex " << v1 << " hex " << hex1 << endl;
            hexToVertex[hex1]=v1;
            vertexToHex[v1]=hex1;
        }
        if(hexToVertex.count(hex2)==0)
        {
            unsigned int v2=mygameboard.add_vertex(); //x will be ??
            //cout << "vertex " << v2 << " hex " << hex2 << endl;
            hexToVertex[hex2]=v2;
            vertexToHex[v2]=hex2;
        }
        mygameboard.add_edge(hexToVertex[hex1], hexToVertex[hex2]);
    }
    //end of while loop //file read

    //Add edges to map
    Graph *g = new AdjacencyList(hexToVertex.size());
    for(int i =0; i < mygameboard.order(); i++)
    {
        set<unsigned int> neighbor = mygameboard.neighbors(i);
        for(auto it = neighbor.begin(); it != neighbor.end(); it++)
        {
            g->add_edge(i, *it);
        }
    }
    //TODO- test if the graph g is correct
    //cout << "************* Graph " << endl;
    //g->display();
    //cout << "************* Graph " << endl;
    /*
    int size = g->order();
    cout << " size is " << size << endl;
    for(int i =0; i < size; i++)
    {
        cout << i << "(" << vertexToHex[i] << ")";
        set<unsigned int> n = g->neighbors(i);
        for(auto it = n.begin(); it != n.end(); it++)
        {
            cout << ", " << *it << "(" << vertexToHex[*it] << ")";
        }
        cout << endl;
    }
    */

    unsigned int startVertex = hexToVertex[1605];
    unsigned int endVertex = hexToVertex[309];
    //cout << "start " << startVertex << " end " << endVertex << endl;

    //Implement BFS on the graph g
    map<unsigned int, unsigned int> sc;
    HexMap hexMap(309);
    //hexMap.add(endVertex);
    sc = g->breadth_first_search(startVertex);
    /*
    cout << "size of the sc " << sc.size() << endl;
    for(auto it = sc.begin(); it != sc.end(); it++)
    {
        cout << vertexToHex[it->first] << "-->" << vertexToHex[it->second] << endl;
    }
    */
    while(sc.count(endVertex)==1)
    {
        int hex = vertexToHex[endVertex];
        //cout << hex << endl;
        hexMap.add(hex); 
        endVertex=sc[endVertex]; //assigns the previous 
    }
    return 0;
}
开发者ID:sagarnanda,项目名称:CSCI-311-Algorithms-and-Data-Structures,代码行数:87,代码来源:project2.cpp


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