13.boost有向无向图邻接表表示
阅读原文时间:2023年07月10日阅读:1

#include
#include
//图(矩阵实现)
#include
#include
#include
//图(链表实现)
#include
using namespace std;
using namespace boost;

//顶点名称
enum { A, B, C, D, E, F };
//顶点个数
#define N 6
const char *name = "ABCDEF";

//无向图
void main()
{
//vecS是vector数组的意思(每一个结点都是一个vector数组)
adjacency_list myg(N);
//每一个结点都是list
//adjacency_list myg;
//每一个结点都是set(红黑树)
//adjacency_list myg;
//每一个结点都是multiset(红黑树,每一个结点是一个链表)
//adjacency_list myg(5);

 add\_edge(A, B, myg);  
 add\_edge(A, C, myg);  
 add\_edge(A, D, myg);  
 add\_edge(A, E, myg);  
 add\_edge(B, C, myg);  
 add\_edge(F, C, myg);

 //输出所有结点  
 auto it = vertices(myg);  
 for (auto ib = it.first; ib != it.second; ib++)  
 {  
     cout << \*ib << endl;  
 }

 //输出所有边  
 auto ite = edges(myg);  
 for (auto ib = ite.first; ib != ite.second; ib++)  
 {  
     //cout << \*ib << endl;  
     //输出从哪到哪  
     cout << source(\*ib, myg) << "->" << target(\*ib, myg) << endl;  
 }

 cout << "顶点" << endl;  
 print\_vertices(myg, name);  
 cout << endl;

 cout << "边" << endl;  
 print\_edges(myg, name);  
 cout << endl;

 cout << "关联" << endl;  
 print\_graph(myg, name);  
 cout << endl;  
 cin.get();  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章