#include <iostream>
using namespace std;
//深度遍历
bool visited[10];
bool visited2[10];
#define MaxInt 32767;
#define MVNum 100;
#define MAX_SIZE 100
//邻接矩阵,深度便利,有向网
typedef char VerTexType;
typedef int ArcType;
typedef int QElemType;
typedef int Status;
using namespace std;
typedef struct {
VerTexType vexs[10];
ArcType arcs[10][10];
int vexnum; //point
int arcnum; //
}AMGraph;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;
SqQueue Q;
Status InitQueue(SqQueue &Q){
Q.base = new QElemType[MAX_SIZE];
if(!Q.base){
cout<<"分配数组失败";
return -1;
}
Q.front = Q.rear = 0;
return 1;
}
//int LocateVex(AMGraph G,char v){
// for(int i=0;i<G.vexnum;i++){
// if(G.arcs[i]==v){
// return i;
// }
// }
// return -1;
//}
Status EnQueue(SqQueue &Q,QElemType e){
if((Q.rear+1)%MAX_SIZE==Q.front){return -1;}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAX_SIZE;
return 1;
}
Status DeQueue(SqQueue &Q,QElemType &e){
if((Q.front==Q.rear)){cout<<"没有元素,出队失败! "<<endl;return -1;}
e = Q.base[Q.front];
Q.front = (Q.front+1)%MAX_SIZE;
return 1;
}
Status QueueEmpty(SqQueue &Q){
return (Q.rear-Q.front+MAX_SIZE)%MAX_SIZE;
}
void display(SqQueue &Q){
for(int i=Q.front;i<Q.rear-Q.front;i++){
cout<<Q.base[i]<<endl;
}
}
int init(AMGraph &G){
for(int i=0;i<G.vexnum;i++){
for(int j=0;j<G.vexnum;j++){
G.arcs[i][j] = MaxInt;
}
}
}
int CreatUDN(AMGraph &G){
//init(G);
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
G.arcs[i][j] = 0;
}
}
cout<<"please input vexnum and arcnum:"<<endl;
cin>>G.vexnum>>G.arcnum;
for(int i=0;i<G.vexnum;i++){
cout<<"please input the "<<i<<" point"<<endl;
cin>>G.vexs[i];
}
for(int i=0;i<G.arcnum;i++){
VerTexType v1,v2;
int w = 0;
int w1 = 0;
int w2 = 0;
cout<<"please input v1 ,v2 and w "<<""<<endl;
cin>>v1>>v2>>w;
for(int j=0;j<G.vexnum;j++){
if(v1==G.vexs[j]) w1 = j;
if(v2==G.vexs[j]) w2 = j;
}
G.arcs[w1][w2] = w;
}
return 1;
}
int InsertARC(AMGraph G,int num){
VerTexType v1,v2;
int w1,w2;
int w;
cout<<"please input v1 ,v2 and w "<<""<<endl;
for(int i=0;i<num;i++){
for(int j=0;j<G.arcnum;j++){
if(v1==G.vexs[j]) w1 = j;
if(v2==G.vexs[j]) w2 = j;
}
G.arcs[w1][w2] = w;
}
}
int DeleteARC(AMGraph G,int num){
VerTexType v1,v2;
int w1,w2;
int w;
cout<<"please input v1 ,v2 and w "<<""<<endl;
for(int i=0;i<num;i++){
for(int j=0;j<G.arcnum;j++){
if(v1==G.vexs[j]) w1 = j;
if(v2==G.vexs[j]) w2 = j;
}
G.arcs[w1][w2] = MaxInt;
}
}
void UDN_dis(AMGraph G){
cout<<" ";
for(int i=0;i<G.vexnum;i++){
cout<<G.vexs[i]<<" ";
}
cout<<endl;
for(int i=0;i<G.vexnum;i++){
cout<<G.vexs[i]<<" ";
for(int j=0;j<G.vexnum;j++){
cout<<G.arcs[i][j]<<" ";
}
cout<<endl;
}
}
int DFS_AM(AMGraph G,int v){ //邻接矩阵
cout<<"vexs["<<v<<"]="<<G.vexs[v]<<",";
if(v==G.vexnum-1){
cout<<endl;
}
//cout<<v<<" ";
visited[v] = true;
for(int w=0;w<G.vexnum;w++){
if( (G.arcs[v][w]!=0)&&(!visited[w]) ) DFS_AM(G,w);
}
return 1;
}
int FirstAdjVex(AMGraph G,int u){//u行第一个点
int i,j;
//cout<<"first adjvex "<<u<<endl;
for (i=0; i<G.vexnum;i++)
{
if (G.arcs[u][i]>=1 && i!=u)
{
return i;
}
}
return -1;
}
int NextAdjVex(AMGraph G,int u,int w){//u行相对u的第一个点
int i,j;
if(w<G.vexnum-1){
for (i=w+1;i<G.vexnum;i++)
{
if (G.arcs[u][i]>=1&&i!=u&&i!=w)
{
return i;
}
}
}
return -1;
}
int BFS_AM(AMGraph G,int v){
cout<<"vexs["<<v<<"]="<<G.vexs[v]<<","<<QueueEmpty(Q)<<",";
int u = 0;
EnQueue(Q,v);
while(QueueEmpty(Q)!=0){
DeQueue(Q,u);
for(int w = FirstAdjVex(G,u);w>=0;){
if(visited2[w]==false){
cout<<"vexs["<<w<<"]="<<G.vexs[w]<<",";
visited2[w] = true;
EnQueue(Q,w);
//cout<<"【"<<v<<"="<<w<<",】";
}
w=NextAdjVex(G,u,w);
//cout<<"zhognxing "<<u<<" "<<w<<","<<visited2[w]<<endl;
}
}
}
int main()
{
AMGraph amg;
for(int i=0;i<10;i++){
visited[i] = false;
visited2[i] = false;
}
CreatUDN(amg);
UDN_dis(amg);
InitQueue(Q);
DFS_AM(amg,0);
cout<<endl<<endl;
BFS_AM(amg,0);
return 0;
}
/**
6 5
a b c d e f
a b 1 a c 1 c d 1 c e 1 d f 1
*/