#pragmaonce#defineMaxSize10//!!!!#include<iostream>#include"Queue.h"usingnamespace std;//邻接矩阵_无向classMGraph_ND{public:int Vertex[MaxSize];int Edge[MaxSize][MaxSize];int Vertex_num =0;int Edge_num =0;MGraph_ND(int mode){if(mode ==1){
std::cout <<"请输入顶点,输入-1结束"<< endl;int a =0;
cin >> a;if(a ==-1)return;while(a !=-1){
Vertex[this->Vertex_num++]= a;
cin >> a;}
cout << endl;
std::cout <<"请输入边之间的关系,0表示边不存在,1表示边存在,大于1表示权值"<< endl;for(int i =0; i < Vertex_num; i++){
cout <<"结点:"<< i <<" ";for(int j =0; j < Vertex_num; j++){
cin >> a;if(a >0&& Edge[j][i]<1){this->Edge_num++;}//无向图处理this->Edge[i][j]= a;}}}else{int a[]={0,1,2,3,4};int b[][5]={{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,0,1,0,0},{0,1,1,0,0}};this->Vertex_num =5;this->Edge_num =6;for(int i =0; i <5; i++){this->Vertex[i]= a[i];}for(int i =0; i <5; i++){for(int j =0; j <5; j++){this->Edge[i][j]= b[i][j];}}}}//点x,y是否有边<x,y>boolAdjacent(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)returnfalse;if(x == y)returnfalse;//无环if(Edge[Vertex[x]][Vertex[y]]>=1)returntrue;returnfalse;};//返回点x的所有相邻结点
Queue<int>*Neighbors(int x){
Queue<int>* que =new Queue<int>();if(x <0|| x >= Vertex_num)return que;for(int i =0; i <this->Vertex_num; i++){if(Edge[x][i]>0){
que->EnQueue(i);}}return que;};//往顺序结点后新增一个结点intInsertVertex(){this->Vertex[this->Vertex_num]=this->Vertex_num;this->Vertex_num++;returnthis->Vertex_num -1;};//删除对应结点,并抛弃对应空间boolDeleteVertex(int x){if(x<0|| x>=Vertex_num)returnfalse;else{for(int i =0; i <this->Vertex_num; i++){if(this->Edge[x][i]!=0)Edge_num--;this->Edge[x][i]=0;}for(int i =0; i <this->Vertex_num; i++){this->Edge[i][x]=0;}
Vertex[x]=-1;//删了以后这个空间就再也不能用了
Vertex_num--;returntrue;}};boolAddEdge(int x,int y,int weight){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)returnfalse;if(x == y)returnfalse;//无环if(weight <1)returnfalse;if(Edge[x][y]>0)returnfalse;//已有边this->Edge[x][y]= weight;this->Edge[y][x]= weight;
Edge_num++;returntrue;};boolRemoveEdge(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)returnfalse;if(x == y)returnfalse;//无环if(Edge[x][y]==0)returnfalse;//不存在边this->Edge[x][y]=0;this->Edge[y][x]=0;
Edge_num--;returntrue;};intFirstNeighbor(int x){if(x <0|| x >=this->Vertex_num)return-1;
Queue<int>* que =Neighbors(x);if(que->Empty())return-1;return que->GetHead()->data;};//除y以外x的邻接点intNextNeighbor(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)return-1;
Queue<int>* que =Neighbors(x);if(que->Empty())return-1;int a;
que->DeQueue(a);while(a != y &&!que->Empty()){
que->DeQueue(a);}if(que->Empty())return-1;//没找到邻接点y或y是最后一个邻接点
que->DeQueue(a);return a;};intGet_edge_value(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)return-1;return Edge[x][y];// 0 或 1 或 大于1};};
邻接表(无向图)
#pragmaonce#defineMaxSize100#include<iostream>#include"Queue.h"usingnamespace std;//边表结点classArcNode{public:int Vertex_Pos;
ArcNode* next;ArcNode(){
Vertex_Pos =0;
next =nullptr;}ArcNode(int vertex_pos){this->Vertex_Pos = vertex_pos;}};//顶点表结点classVNode{public:int V_num;//当前点的邻接结点总数
ArcNode* Head;//Head的data域可有可无VNode(){
V_num =0;
Head =newArcNode();}};//邻接表_无向 无环classALGraph_ND{public:
VNode* Vnode[MaxSize];int Vertex_num =0;int Edge_num =0;ALGraph_ND(int mode){if(mode ==1){
std::cout <<"请输入顶点,输入-1结束"<< endl;int a =0;
cin >> a;if(a ==-1)return;while(a !=-1){
Vnode[Vertex_num]=newVNode();
Vnode[Vertex_num++]->Head->Vertex_Pos = a;
cin >> a;}
cout << endl;
std::cout <<"请输入关联结点 输入-1结束当前结点创建 注意无向图的对称"<< endl;for(int i =0; i < Vertex_num; i++){
cout <<"结点:"<< Vnode[i]->Head->Vertex_Pos<<" ";
cin >> a;
ArcNode* p = Vnode[i]->Head;while(a !=-1){
Vnode[i]->V_num++;
Edge_num++;
p->next =newArcNode();
p->next->Vertex_Pos = a;
p = p->next;
cin >> a;}}
Edge_num = Edge_num /2;//无向图边重复}else{int a[]={0,1,2,3,4};int b[][5]={{0,1,0,0,1},{1,0,1,1,1},{0,1,0,1,0},{0,1,1,0,1},{1,1,0,1,0}};
Vertex_num =5;
Edge_num =7;for(int i =0; i <5; i++){
Vnode[i]=newVNode();
ArcNode* p = Vnode[i]->Head;
p->Vertex_Pos = i;for(int j =0; j <5; j++){if(b[i][j]>0){
Vnode[i]->V_num++;
p->next =newArcNode();
p->next->Vertex_Pos =j;
p = p->next;}}}}}//点x,y是否有边<x,y>boolAdjacent(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)returnfalse;if(x == y)returnfalse;//无环if(Vnode[x]->Head->next ==nullptr)returnfalse;//无边相连
ArcNode* p = Vnode[x]->Head;while(p->next!=nullptr){
p = p->next;if(p->Vertex_Pos == y)returntrue;}returnfalse;};//返回点x的所有相邻结点
Queue<int>*Neighbors(int x){
Queue<int>* que =new Queue<int>();if(x <0|| x >=Vertex_num)return que;
ArcNode* p = Vnode[x]->Head->next;while(p->next !=nullptr){
p = p->next;
que->EnQueue(p->Vertex_Pos);}return que;};//往顺序结点后新增一个结点intInsertVertex(){
Vnode[Vertex_num]=newVNode();
Vnode[Vertex_num]->Head->Vertex_Pos = Vertex_num;
Vertex_num++;return Vertex_num;};//删除对应结点,并抛弃对应空间 较难,时间复杂度搞boolDeleteVertex(int x){if(x <0|| x >= Vertex_num)returnfalse;else{for(int i =0; i <Vertex_num;i++){if(i == x)continue;//被删结点不需处理if(Vnode[i]->V_num ==0)continue;//无相连结点if(Vnode[i]->Head->next->Vertex_Pos == x){
ArcNode* p = Vnode[i]->Head->next;
Vnode[i]->Head->next = p->next;delete p;
Edge_num--;
Vnode[i]->V_num--;continue;}
ArcNode* before;
ArcNode* current;
ArcNode* after;
before = Vnode[i]->Head;
current = Vnode[i]->Head->next;//删除的是current
after = Vnode[i]->Head->next->next;while(current!=nullptr){if(current->Vertex_Pos == x){delete current;
before->next = after;
Edge_num--;
Vnode[i]->V_num--;break;}
before = before->next;
current = current->next;if(after ==nullptr)break;//current在链尾,after为nullptr
after = after->next;}}
Vertex_num--;delete Vnode[x];returntrue;}};boolAddEdge(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >this->Vertex_num)returnfalse;if(x == y)returnfalse;//无环
ArcNode* p = Vnode[x]->Head;
ArcNode* q = Vnode[y]->Head;while(p->next !=nullptr){
p = p->next;if(p->Vertex_Pos == y){returnfalse;//重复 }}}
p->next =newArcNode();
p->next->Vertex_Pos = y;
Vnode[x]->V_num++;
Edge_num++;while(q->next !=nullptr){
q = q->next;}
q->next =newArcNode();
q->next->Vertex_Pos = x;
Vnode[y]->V_num++;returntrue;};//类似删除结点boolRemoveEdge(int x,int y){if(x <0|| y <0|| x >=this->Vertex_num || y >=this->Vertex_num)returnfalse;if(x == y)returnfalse;if(Vnode[x]->V_num ==0)returnfalse;//无相连结点
ArcNode* p = Vnode[x]->Head;
ArcNode* q = Vnode[y]->Head;//三指针法删除结点if(Vnode[x]->Head->next->Vertex_Pos == y){
ArcNode* p = Vnode[x]->Head->next;
Vnode[x]->Head->next = p->next;delete p;
Edge_num--;
Vnode[x]->V_num--;}else{
ArcNode* before;
ArcNode* current;
ArcNode* after;
before = Vnode[x]->Head;
current = Vnode[x]->Head->next;//删除的是current
after = Vnode[x]->Head->next->next;while(current !=nullptr){if(current->Vertex_Pos == y){delete current;
before->next = after;
Edge_num--;//上下相同 只减一份
Vnode[x]->V_num--;break;}
before = before->next;
current = current->next;if(after ==nullptr)break;//current在链尾,after为nullptr
after = after->next;}}if(Vnode[y]->Head->next->Vertex_Pos == x){
ArcNode* p = Vnode[y]->Head->next;
Vnode[y]->Head->next = p->next;delete p;
Vnode[y]->V_num--;}else{
ArcNode* before;
ArcNode* current;
ArcNode* after;
before = Vnode[y]->Head;
current = Vnode[y]->Head->next;//删除的是current
after = Vnode[y]->Head->next->next;while(current !=nullptr){if(current->Vertex_Pos == x){delete current;
before->next = after;
Vnode[y]->V_num--;break;}
before = before->next;
current = current->next;if(after ==nullptr)break;//current在链尾,after为nullptr
after = after->next;}}returntrue;};intFirstNeighbor(int x){if(Vnode[x]->V_num ==0)return-1;return Vnode[x]->Head->next->Vertex_Pos;};intNextNeighbor(int x,int y){int flag =0;if(Vnode[x]->V_num <2)return-1;
ArcNode* p = Vnode[x]->Head;while(p->next!=nullptr){
p = p->next;if(p->Vertex_Pos == y){
flag =1;break;}}if(flag ==1&& p->next !=nullptr){return p->next->Vertex_Pos;}return-1;};};
二 遍历
**广度优先遍历**
//对图进行广度搜索voidBFSTraverse(){bool visited[MaxSize];
Queue<int>* que =new Queue<int>();//在此处初始化队列,同时处理多个联通分支int count_banch =0;for(int i =0; i <this->Vertex_num; i++){
visited[i]=false;}for(int i =0; i <this->Vertex_num; i++){//如果是连通图,这里只执行一次循环//如果是非联通图,则会调用多次BFSif(visited[i]==false){
cout <<"连通分支:"<<(++count_banch)<<" ";BFS(i, que, visited);
cout << endl;}}}//广搜voidBFS(int i, Queue<int>*& que,bool* visited){
que->EnQueue(i);Visit(i);
visited[i]=true;int a =0;while(!que->Empty()){
que->DeQueue(a);for(int x =FirstNeighbor(a); x !=-1; x =NextNeighbor(a, x)){if(visited[x]==false){Visit(x);
visited[x]=true;//如果放在外层访问会出现重复
que->EnQueue(x);}}}}
**深度优先遍历** (递归)
//对图进行深度搜索,使用工作栈voidDFSTraverse(){bool visited[MaxSize];int count_banch =0;for(int i =0; i <this->Vertex_num; i++){
visited[i]=false;}for(int i =0; i <this->Vertex_num; i++){//如果是连通图,这里只执行一次循环//如果是非联通图,则会调用多次BFSif(visited[i]==false){
cout <<"连通分支:"<<(++count_banch)<<" ";DFS(i, visited);
cout << endl;}}}//深搜voidDFS(int i,bool* visited){Visit(i);
visited[i]=true;for(int a =FirstNeighbor(i); a !=-1; a =NextNeighbor(i, a)){if(visited[a]==false){DFS(a, visited);}}}