Repository navigation
Expand file tree
/
Copy pathGraph.h
More file actions
175 lines (168 loc) · 4.82 KB
/
Copy pathGraph.h
File metadata and controls
175 lines (168 loc) · 4.82 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Created by yyi on 22/8/2019.
//
#ifndef GRAPHBASIC_GRAPH_H
#define GRAPHBASIC_GRAPH_H
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <fstream>
#include <cassert>
#include <algorithm>
using namespace std;
typedef vector<int> RVNodes;
typedef set<int> VNodes;
class Graph {
protected:
int V, E;
vector<VNodes> adj;
void validateVertex(int v){
if(v<0 || v>V)
throw string("vertex " + to_string(v) + " is invalid!");
}
public:
Graph():V(0),E(0) {};
virtual void createGraph(string& filename) {
freopen(filename.data(),"r",stdin);
cin>>V>>E;
if(V<0) throw "V must be non-negative!";
if(E<0) throw "E must be non-negative!";
adj = vector<set<int>> (V, set<int>());
for(int i=0;i<E;i++){
int a, b;
cin>>a>>b;
validateVertex(a); validateVertex(b);
if(a==b) throw "Self Loop is Detected!";
if(adj[a].find(b) != adj[a].end()) throw "Parallel Edge is Detected!";
adj[a].insert(b);
adj[b].insert(a);
}
fclose(stdin);
}
virtual void printGraph() {
cout<<"V = "<<V<<", E= "<<E<<endl;
for(int v=0;v<V;v++){
cout<<v<<": ";
for(auto w : adj[v])
cout<<w<<" ";
cout<<endl;
}
}
int getV() { return V; }
int getE() { return E; }
virtual bool hasEdge(int v, int w) = 0;
// O(degree(v))
virtual int degree(int v) = 0;
};
class GraphSet : public Graph {
public:
GraphSet() : Graph() {}
bool hasEdge(int v, int w) override {
validateVertex(v);
validateVertex(w);
return adj[v].find(w) != adj[v].end();
}
int degree(int v) override {
validateVertex(v);
return int(adj[v].size());
}
const VNodes& adjVertex(int v) {
validateVertex(v);
return adj[v];
}
};
class GraphMatrix : public Graph {
protected:
vector<RVNodes> adj;
public:
GraphMatrix() : Graph() {}
void createGraph(string& filename) override {
freopen(filename.data(), "r", stdin);
cin>>V>>E;
if(V<0) throw "V must be non-negative!";
if(E<0) throw "E must be non-negative!";
adj = vector<vector<int>>(V, vector<int>(V,0));
for(int i=0;i<E;i++){
int a, b;
cin>>a>>b;
validateVertex(a); validateVertex(b);
if(a==b) throw "Self Loop is Detected!";
if(adj[a][b] == 1) throw "Parallel Edge is Detected!";
adj[a][b] = 1;
adj[b][a] = 1;
}
fclose(stdin);
}
void printGraph() override {
cout<<"V = "<<V<<", E= "<<E<<endl;
for(int i=0;i<V;i++){
for(int j=0;j<V;j++)
cout<<adj[i][j]<<" ";
cout<<endl;
}
}
bool hasEdge(int u, int w) override {
validateVertex(u); validateVertex(w);
return adj[u][w]==1;
}
// find all edges connect v, O(V)
RVNodes adjVertex(int v) {
validateVertex(v);
vector<int> res;
for(int i=0;i<V;i++)
if(adj[v][i]==1)
res.push_back(i);
return res;
}
int degree(int v) override {
return int(adjVertex(v).size());
}
};
class GraphList : public Graph {
protected:
vector<RVNodes> adj;
public:
GraphList() : Graph() {}
void createGraph(string& filename) override {
freopen(filename.data(),"r",stdin);
cin>>V>>E;
if(V<0) throw "V must be non-negative!";
if(E<0) throw "E must be non-negative!";
adj = vector<vector<int>>(V,vector<int>());
for(int i=0;i<E;i++){
int a, b;
cin>>a>>b;
validateVertex(a); validateVertex(b);
if(a==b) throw "Self Loop is Detected!";
if(find(adj[a].begin(),adj[a].end(),b) != adj[a].end()) throw "Parallel Edge is Detected!";
adj[a].push_back(b);
adj[b].push_back(a);
}
fclose(stdin);
cout<<"Created GraphList"<<endl;
}
void printGraph() override {
cout<<"V = "<<V<<", E= "<<E<<endl;
for(int v=0;v<V;v++){
cout<<v<<": ";
for(auto w : adj[v])
cout<<w<<" ";
cout<<endl;
}
}
bool hasEdge(int v, int w) override {
validateVertex(v);
validateVertex(w);
return find(adj[v].begin(), adj[v].end(), w) != adj[v].end();
}
// O(degree(v))
RVNodes& adjVertex(int v) {
validateVertex(v);
return adj[v];
}
int degree(int v) override {
return int(adjVertex(v).size());
}
};
#endif //GRAPHBASIC_GRAPH_H