-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGraph.cpp
More file actions
79 lines (79 loc) · 1.34 KB
/
Graph.cpp
File metadata and controls
79 lines (79 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<vector>
#include<cassert>
using namespace std;
//稠密图 邻接矩阵
class DenseGraph{
private:
int n,m;//n是点数m是边数
bool directed;
vector<vector<bool>> g;
public:
DenseGraph(int n,bool directed){
this->n=n;
this->m=0;
this->directed=directed;
for(int i=0;i<n;i++)
g.push_back(vector<bool>(n,false));
}
~DenseGraph(){
}
void addEdge(int v,int w){
assert(v>=0&&v<n);
assert(w>=0&&w<n);
if(hasEdge(v,w))
return;
g[v][w]=true;
if(!directed)
g[w][v]=true;
m++;
}
bool hasEdge(int v,int w){
assert(v>=0&&v<n);
assert(w>=0&&w<n);
return g[v][w];
}
};
//稀疏图 邻接表
class SparseGraph{
private:
int n,m;
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(){
return n;
}
int E(){
return m;
}
void addEdge(int v,int w){
assert(v>=0&&v<n);
assert(w>=0&&w<n);
g[v].push_back(w);
//处理自环边
if(v!=w&&!directed)
g[w].push_back(v);
m++;
}
bool hasEdge(int v,int w){
assert(v>=0&&v<n);
assert(w>=0&&w<n);
for(int i=0;i<g[v].size();i++)
if(g[v][i]==w)
return true;
return false;
}
};
int main(){
return 0;
}