C++ 创建加权图?

标签 c++ graph

如何创建一个 C++ 加权图,其中图中的每个顶点都有一个权重(某个整数值)?

您可以下载我的图表项目here (RapidShare):

下面是根据存储在文本文件中的图形数据创建图形的函数:

void GraphType::createGraph()
{
    ifstream infile;
    char fileName[50];

    int index;
    int vertex;
    int adjacentVertex;

    if(gSize != 0)
        clearGraph();

    cout << "Enter input file name: ";
    cin >> fileName;
    cout << endl;

    infile.open(fileName);

    if(!infile)
    {
            cout << "Cannot open input file." << endl;
            return;
    }

    infile >> gSize;

    graph = new UnorderedLinkList[gSize];

    for(index = 0; index < gSize; index++)
    {
            infile >> vertex;
            infile >> adjacentVertex;

            while(adjacentVertex != -999)
            {
                graph[ vertex ].insertLast(adjacentVertex);
                infile >> adjacentVertex;
            }
    }
    infile.close();
}

这是从文本文件“Network2.txt”输入的图形数据(顶点数 = 10,顶点 0 到 9 和相邻顶点):

10

0 1 2 9 -999

1 0 2 -999

2 0 1 9 8 3 -999

3 2 8 5 -999

4 3 8 6 5 -999

5 4 6 7 -999

6 4 7 8 -999

7 8 6 5 -999

8 9 2 3 4 6 7 -999

9 0 2 8 -999

我的问题是,如何为顶点 0 到 9 分配唯一值或权重?任何帮助将不胜感激。提前致谢!

最佳答案

Boost Graph Library (BGL) 提供类型 MutablePropertyGraph ,其中每个边和顶点都可以将权重存储为属性。查看example here ,它构建了一个带加权边的有向图。

关于C++ 创建加权图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1856867/

相关文章:

javascript - C3 图表 Error/bug - 步进折线图

c++ - 我如何从第三方框架和我的源代码创建框架?

Java JNI JAWT 错误未解析的外部符号 __imp__JAWT_GetAWT@8

c++ - 着色器存储缓冲区中的 OpenGL 顶点

javascript - HighCharts 中的分布图

azure - 问 : Azure Cosmos DB Graph: How to run queries in Graph API when Indexing Policy is defined as Manual?

c++ - C语言中*(int *)是什么意思

c++ - 邻接表实现

javascript - 平面 JSON 展开为具有多个父级的层次结构作为字符串

c++ - 给定一个 DI-Graph。检查所有节点对是否存在来自 (u,v) 或 (v,u) 的路径