#include<iostream>
#include<vector>
#include<map>
#include<queue>
using namespace std;
class Graph{
protected:
int nv; // number of vertices
bool directed; // 1 represents directed graph
vector<string> vertices;
map<string, int> iov; // index of vertices
vector<bool> visited;
virtual void dfs(int v) = 0;
public:
Graph(bool dir = false){
directed = dir;
nv = 0;
}
Graph(vector<string> v, bool dir = false){
directed = dir;
nv = (int)v.size();
vertices = v; // use default method of copying
for(int i = 0; i < nv; i++) iov[v[i]] = i;
}
Graph(int n, bool dir = false){
directed = dir;
nv = n;
vertices.resize(n); // 只需要一次扩容操作即可
for(int i = 0; i < n; i++){
vertices[i] = to_string(i); // to_string() can change other type into string
iov[to_string(i)] = i;
}
}
virtual void print() = 0;
virtual void insertV(){
insertV(to_string(nv));
}
virtual bool insertV(string v){
if(iov.find(v) != iov.end()) return false;
vertices.push_back(v);
iov[v] = nv;
nv++;
return true;
}
virtual bool insertE(string src, string dst, int weight = 1){
insertV(src);
insertV(dst);
return insertE(iov[src], iov[dst], weight);
}
virtual bool insertE(int src, int dst, int weight = 1) = 0;
virtual bool removeE(string src, string dst){
if(iov.find(src) == iov.end() || iov.find(dst) == iov.end()) return false;
return removeE(iov[src], iov[dst]);
}
virtual bool removeE(int src, int dst) = 0;
virtual void dfs(string v){
if(iov.find(v) == iov.end()) return;
visited.resize(nv);
for(int i = 0; i < nv; i++) visited[i] = false;
dfs(iov[v]);
}
};
class MGraph: public Graph{
protected:
vector<vector<int>> adjM; //adjacent matrix
void setAdjM(){
adjM.resize(nv);
for(int i = 0; i < nv; i++){
adjM[i].resize(nv);
for(int j = 0; j < nv; j++)
adjM[i][j] = INT_MAX;
}
}
void dfs(int v){
cout << vertices[v] << " ";
visited[v] = true;
for(int i = 0; i < nv; i++){
if(adjM[v][i] != INT_MAX && !visited[i])
dfs(i);
}
}
public:
MGraph(bool dir = false) : Graph(dir){}
MGraph(vector<string> v, bool dir = false) : Graph(v, dir){ setAdjM(); }
MGraph(int n, bool dir = false) : Graph(n, dir){ setAdjM(); }
void print(){
cout << '\t';
for(int i = 0; i < nv; i++) cout << vertices[i] << '\t';
cout << endl;
for(int i = 0; i < nv; i++){
cout << vertices[i] << ":\t";
for(int j = 0; j < nv; j++)
if(adjM[i][j] == INT_MAX) cout << "-\t";
else cout << adjM[i][j] << '\t';
cout << endl;
}
}
void insertV(){
insertV(to_string(nv));
}
bool insertV(string v){
bool r = Graph::insertV(v);
if(!r) return false;
for(int i = 0; i < nv - 1; i++) adjM[i].push_back(INT_MAX);
adjM.push_back(vector<int>(nv, INT_MAX));
return true;
}
virtual bool insertE(string src, string dst, int weight = 1){
return Graph::insertE(src, dst, weight);
}
virtual bool insertE(int src, int dst, int weight = 1){
if(src < 0 || dst < 0 || src >= nv || dst >= nv) return false;
if(adjM[src][dst] != INT_MAX) return false;
adjM[src][dst] = weight;
if(!directed) adjM[dst][src] = weight;
return true;
}
virtual bool removeE(string src, string dst){
return Graph::removeE(src, dst);
}
virtual bool removeE(int src, int dst){
if(src < 0 || dst < 0 || src >= nv || dst >= nv) return false;
if(adjM[src][dst] == INT_MAX) return false;
adjM[src][dst] = INT_MAX;
if(!directed) adjM[dst][src] = INT_MAX;
return true;
}
void dfs(string v){
Graph::dfs(v);
}
virtual void bfs(string v){
if(iov.find(v) == iov.end()) return;
int iv = iov[v];
visited.resize(nv);
for(int i = 0; i < nv; i++) visited[i] = false;
queue<int> q;
cout << v << ' ';
visited[iv] = true;
q.push(iv);
int w;
while(!q.empty()){
w = q.front();
q.pop();
for(int i = 0; i < nv; i++){
if(!visited[i] && adjM[w][i] != INT_MAX){
cout << vertices[i] << ' ';
visited[i] = true;
q.push(i);
}
}
}
}
};
class LGraph: public Graph{
protected:
vector<map<int, int>> adjL;
void setAdjL(){
adjL.resize(nv);
for(auto x : adjL) x.clear();
}
void dfs(int v){
cout << vertices[v] << " ";
visited[v] = true;
for(auto x : adjL[v])
if(!visited[x.first])
dfs(x.first);
}
public:
LGraph(bool dir = false) : Graph(dir){};
LGraph(vector<string> v, bool dir = false) : Graph(v, dir){ setAdjL(); }
LGraph(int n, bool dir = false) : Graph(n, dir){ setAdjL(); }
void print(){
for(int i = 0; i < nv; i++){
cout << vertices[i] << '[' << i << "]-->";
for(auto x : adjL[i])
cout << vertices[x.first] << '(' << x.second << ") ";
cout << endl;
}
}
void insertV(){
insertV(to_string(nv));
}
bool insertV(string v){
bool r = Graph::insertV(v);
if(!r) return false;
adjL.push_back(map<int, int>());
return true;
}
virtual bool insertE(string src, string dst, int weight = 1){
return Graph::insertE(src, dst, weight);
}
virtual bool insertE(int src, int dst, int weight = 1){
if(src < 0 || dst < 0 || src >= nv || dst >= nv) return false;
if(adjL[src].find(dst) != adjL[src].end()) return false;
adjL[src].insert(pair<int, int>(dst, weight));
if(!directed) adjL[dst].insert(pair<int, int>(src, weight));
return true;
}
virtual bool removeE(string src, string dst){
return Graph::removeE(src, dst);
}
virtual bool removeE(int src, int dst){
if(src < 0 || dst < 0 || src >= nv || dst >= nv) return false;
if(adjL[src].find(dst) == adjL[src].end()) return false;
adjL[src].erase(dst);
if(!directed) adjL[dst].erase(src);
return true;
}
void dfs(string v){
Graph::dfs(v);
}
virtual void bfs(string v){
if(iov.find(v) == iov.end()) return;
int iv = iov[v];
visited.resize(nv);
for(int i = 0; i < nv; i++) visited[i] = false;
queue<int> q;
cout << v << ' ';
visited[iv] = true;
q.push(iv);
int w;
while(!q.empty()){
w = q.front();
q.pop();
for(auto x : adjL[w]){
if(!visited[x.first]){
cout << vertices[x.first] << ' ';
visited[x.first] = true;
q.push(x.first);
}
}
}
}
};
//void (*visit)(string): Function pointer.
//首先是一个指针,指针的参数为string,返回值为void
int main(){
vector<string> v = {"AA", "BB", "CC", "DD", "EE"};
MGraph g(v);
g.insertE("AA", "CC");
g.insertE(1, 3);
g.insertE("DD", "EE", 6);
g.insertE("X", "EE");
g.insertE("CC", "X");
g.insertE("EE", "BB");
g.print();
g.dfs("AA");
cout << endl;
g.bfs("AA");
return 0;
}
图的邻接矩阵,邻接表结构及打印、插入、删除、深搜、广搜
猜你喜欢
转载自blog.csdn.net/LightInDarkness/article/details/104564594
今日推荐
周排行