假设图G有n个顶点e条边,则存储该图需要O(n^2)
不适用稀疏图的存储
对于图的每个顶点vi,将所有邻接于vi的顶点链成一个单链表,称为顶点vi的边表(对于有向图则称为出边表),所有边表的头指针和存储顶点信息的一维数组构成了顶点表。
- struct ArcNode{
- int adjvex;
- struct ArcNode *next;
- };
-
- template <class T>
- struct VertexNode{
- T vertex;
- struct ArcNode *firstEdge;
- };

邻接表的空间复杂度为O(n+e)
更适用于有向图的存储
顶点i的边表中结点的个数
测试顶点vi的边表中是否有终点为j的结点
顶点i的边表中结点的个数
各顶点的出边表中以顶点i为终点的结点个数

- struct ArcNode{
- int adjvex;
- struct ArcNode *next;
- };
-
- template <class T>
- struct VertexNode{
- T vertex;
- struct ArcNode *firstEdge;
- };
- template <class T>
- class ALGraph{
- private:
- VertexNode
adjList[MAX_VERTEX]; - int vertexNum,arcNum;
- public:
- ALGraph(T v[],int n,int e);
- ~ALGraph();
- void DFSTraverse();
- void BFSTraverse();
- };
- template <class T>
- ALGraph
::ALGraph(T v[],int n,int e){ - int vi,vj;
- ArcNode *s;
- vertexNum=n;
- arcNum=e;
- for(int i=0;i
- adjList[i].vertex=v[i];
- adjList[i].firstEdge=NULL;
- }
- for(int i=0;i
- cin>>vi>>vj;
- s=new ArcNode;
- s->adjvex=vj;
- s->next=adjList[vi].firstEdge;
- adjList[vi].firstEdge=s;
- }
- }
- template <class T>
- ALGraph
::~ALGraph(){ - int i,j;
- ArcNode *p;
-
-
相关阅读:
ClickHouse 运维DBA实用场景
[资源推荐] 飞书画板模板
【Docker】实现JMeter分布式压测
Pdfjs使用
力扣解法汇总934. 最短的桥
vue父子组件通信方式
ES6 入门教程 14 Set 和 Map 数据结构 14.1 Set
apache集合工具类ListUtils
Flink Yarn Per Job - 启动AM
前端周刊第十三期
-
原文地址:https://blog.csdn.net/Hsianus/article/details/134475213