[英]C++ Creating a Weighted Graph?
如何創建C ++加權Graph,其中圖中的每個頂點都有一個權重(某個整數值)?
您可以在此處下載我的圖表項目(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”輸入的Graph數據(頂點數= 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圖庫 (BGL)提供MutablePropertyGraph
類型,其中每個邊和頂點都可以將權重存儲為屬性。 請參閱此處的示例 ,該示例構建具有加權邊的有向圖。
在鄰接列表中,不要讓它只存儲相鄰節點的索引,而是讓它存儲包含相鄰節點索引的結構,以及連接這些節點的邊的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.