有向网的遍历

#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
*/

 

转载于:https://my.oschina.net/lafter/blog/906958

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值