学习:图的存储(领接表和领接矩阵)
#include <iostream>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
//稀疏图 - 领接表
class SparseGraph
{
private:
int n,m;//顶点个数、边的2
bool directed;//是否为有向图
vector<vector<int> > g;
public:
SparseGraph(int n, bool directed)
{
this->n = n;
this->m = 0;
this->directed = directed;
for(int i=0; i<n; i++)
g.push_back(vector<int>());
}
~SparseGraph(){
}
int V() const
{
return n;
}
int E() const
{
return m;
}
void addEdge(int v, int w)//在顶点v和顶点w之间加入一条边
{
assert(v>=0 && v<n