如何创建一个 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/