图的BFS

//邻接矩阵表示法
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<queue>
#include<limits.h>
using namespace std;
const int M=100;
enum COLOR{WHITE,GRAY,BLACK};
struct VERTEX{
    int index;//点的编号
    int distance;
    int previous;//前驱结点编号,0代表无
    COLOR color;
};
class GRAPH{
public:
    VERTEX vertex[M];//点集
    bool adjacency[M][M];//邻接矩阵。adjacency[i][j]=true代表点i,j之间有边
    int n;//点数
    void read(){
        scanf("%d\n",&n);
        memset(adjacency,0,sizeof(adjacency));
        for(int i=1;i<=n;i++){
            stringstream stream;string line;
            getline(cin,line);
            stream<<line;
            int pointindex;
            //读入邻接点
            while(stream>>pointindex){
                //printf("%d\n",pointindex);
                if(pointindex==0) break;//此行为0代表没有邻接点
                adjacency[i][pointindex]=true;
            }
        }
    }
    void BFS(int srcindex){
        for(int i=1;i<=n;i++)
            if(i!=srcindex){
                vertex[i].distance=INT_MAX;
                vertex[i].previous=0;
                vertex[i].color=WHITE;
            }
        vertex[srcindex].distance=0;
        vertex[srcindex].previous=0;
        vertex[srcindex].color=GRAY;
        queue<int> QUEUE;
        QUEUE.push(srcindex);
        while(!QUEUE.empty()){
            int v=QUEUE.front();QUEUE.pop();
            printf("visit:%d\n",v);
            for(int adj=1;adj<=n;adj++)
                if(adjacency[v][adj])//v的所有邻接点入队
                if(vertex[adj].color==WHITE){
                    QUEUE.push(adj);
                    vertex[adj].distance=vertex[v].distance+1;
                    vertex[adj].previous=v;
                    vertex[adj].color=GRAY;//刚被发现
                }
            vertex[v].color=BLACK;//所有邻接点已发现
        }
    }
    void print(){
        for(int i=1;i<=n;i++){
            printf("%d:",i);
            for(int j=1;j<=n;j++)
                if(adjacency[i][j]) printf("%d ",j);
            printf("\n");
        }
    }
    void PRINT_PATH(int srcindex,int endindex){
        if(srcindex==endindex) printf("%d",srcindex);
        else if(vertex[endindex].previous==0)
            printf("no path from %d to %d.",srcindex,endindex);
        else{
            PRINT_PATH(srcindex,vertex[endindex].previous);
            printf("%d",endindex);
        }
    }
};
//图的输入。第1行是点的数量。
//接下来的2~n+1行,第i+1行是点i的邻接点,以空格隔开。
//对于有向图,adjacency[i][j]=1代表有i指向j的边。
//若无邻接点,此行填0。
/*
8
2 5
6 1
4 6 7
3 7 8
1
2 3 7
3 4 6 8
4 7
*/

int main()
{
    //freopen("1.txt","r",stdin);
    GRAPH graph;
    graph.read();
    graph.print();
    graph.BFS(3);
    graph.PRINT_PATH(3,1);
    return 0;
}



邻接链表表示法


  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<sstream>  
  4. #include<queue>  
  5. #include<limits.h>  
  6. using namespace std;  
  7. const int M=100;  
  8. enum COLOR{WHITE,GRAY,BLACK};  
  9. struct LINK_LIST{  
  10.     int index;LINK_LIST* next;  
  11.     LINK_LIST(int index){this->index=index;}  
  12. };  
  13. struct VERTEX{  
  14.     int index;//点的编号  
  15.     int distance;  
  16.     int previous;//前驱结点编号,0代表无  
  17.     COLOR color;  
  18. };  
  19. class GRAPH{  
  20. public:  
  21.     VERTEX vertex[M];//点集  
  22.     LINK_LIST* adjacency[M];//点集的邻接点链表  
  23.     int n;//点数  
  24.     void read(){  
  25.         scanf("%d\n",&n);  
  26.         for(int i=1;i<=n;i++) adjacency[i]=NULL;  
  27.         for(int i=1;i<=n;i++){  
  28.             stringstream stream;string line;  
  29.             getline(cin,line);  
  30.             stream<<line;  
  31.             int pointindex;LINK_LIST*current,*last=NULL;  
  32.             //读入邻接点  
  33.             while(stream>>pointindex){  
  34.                 //printf("%d\n",pointindex);  
  35.                 if(pointindex==0) break;//此行为0代表没有邻接点  
  36.                 current=new LINK_LIST(pointindex);  
  37.                 if(adjacency[i]==NULL)//还没读过邻接点  
  38.                     adjacency[i]=current;  
  39.                 else last->next=current;  
  40.                 last=current;  
  41.             }  
  42.             if(last) last->next=NULL;  
  43.         }  
  44.     }  
  45.     void BFS(int srcindex){  
  46.         for(int i=1;i<=n;i++)  
  47.             if(i!=srcindex){  
  48.                 vertex[i].distance=INT_MAX;  
  49.                 vertex[i].previous=0;  
  50.                 vertex[i].color=WHITE;  
  51.             }  
  52.         vertex[srcindex].distance=0;  
  53.         vertex[srcindex].previous=0;  
  54.         vertex[srcindex].color=GRAY;  
  55.         queue<int> QUEUE;  
  56.         QUEUE.push(srcindex);  
  57.         while(!QUEUE.empty()){  
  58.             int v=QUEUE.front();QUEUE.pop();  
  59.             printf("visit:%d\n",v);  
  60.             LINK_LIST* node=adjacency[v];  
  61.             while(node){  
  62.                 if(vertex[node->index].color==WHITE){//v的所有邻接点入队  
  63.                     int adj=node->index;  
  64.                     QUEUE.push(adj);  
  65.                     vertex[adj].distance=vertex[v].distance+1;  
  66.                     vertex[adj].previous=v;  
  67.                     vertex[adj].color=GRAY;//刚被发现  
  68.                 }  
  69.                 node=node->next;  
  70.             }  
  71.             vertex[v].color=BLACK;//所有邻接点已发现  
  72.         }  
  73.     }  
  74.     void print(){  
  75.         for(int i=1;i<=n;i++){  
  76.             printf("%d:",i);  
  77.             LINK_LIST* node=adjacency[i];  
  78.             while(node){  
  79.                 printf("%d ",node->index);  
  80.                 node=node->next;  
  81.             }  
  82.             printf("\n");  
  83.         }  
  84.     }  
  85.     void PRINT_PATH(int srcindex,int endindex){  
  86.         if(srcindex==endindex) printf("%d",srcindex);  
  87.         else if(vertex[endindex].previous==0)  
  88.             printf("no path from %d to %d.",srcindex,endindex);  
  89.         else{  
  90.             PRINT_PATH(srcindex,vertex[endindex].previous);  
  91.             printf("%d",endindex);  
  92.         }  
  93.     }  
  94. };  
  95. //图的输入。第1行是点的数量。  
  96. //接下来的2~n+1行,第i+1行是点i的邻接点,以空格隔开。  
  97. //对于有向图,adjacency[i]链表存的是点i射向的点。  
  98. //若无邻接点,此行填0。  
  99. /* 
  100. 8 
  101. 2 5 
  102. 6 1 
  103. 4 6 7 
  104. 3 7 8 
  105. 1 
  106. 2 3 7 
  107. 3 4 6 8 
  108. 4 7 
  109. */  
  110. int main()  
  111. {  
  112.     freopen("1.txt","r",stdin);  
  113.     GRAPH graph;  
  114.     graph.read();  
  115.     graph.print();  
  116.     graph.BFS(8);  
  117.     graph.PRINT_PATH(8,5);  
  118.     return 0;  

无向图bfs

  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<sstream>  
  4. #include<queue>  
  5. #include<limits.h>  
  6. using namespace std;  
  7. const int M=100;  
  8. enum COLOR{WHITE,GRAY,BLACK};  
  9. struct LINK_LIST{  
  10.     int index;LINK_LIST* next;  
  11.     LINK_LIST(int index){this->index=index;}  
  12. };  
  13. struct VERTEX{  
  14.     int index;//点的编号  
  15.     int distance;  
  16.     int previous;//前驱结点编号,0代表无  
  17.     COLOR color;  
  18. };  
  19. class GRAPH{  
  20. public:  
  21.     VERTEX vertex[M];//点集  
  22.     LINK_LIST* adjacency[M];//点集的邻接点链表  
  23.     int n;//点数  
  24.     void read(){  
  25.         scanf("%d\n",&n);  
  26.         for(int i=1;i<=n;i++) adjacency[i]=NULL;  
  27.         for(int i=1;i<=n;i++){  
  28.             stringstream stream;string line;  
  29.             getline(cin,line);  
  30.             stream<<line;  
  31.             int pointindex;LINK_LIST*current,*last=NULL;  
  32.             //读入邻接点  
  33.             while(stream>>pointindex){  
  34.                 //printf("%d\n",pointindex);  
  35.                 if(pointindex==0) break;//此行为0代表没有邻接点  
  36.                 current=new LINK_LIST(pointindex);  
  37.                 if(adjacency[i]==NULL)//还没读过邻接点  
  38.                     adjacency[i]=current;  
  39.                 else last->next=current;  
  40.                 last=current;  
  41.             }  
  42.             if(last) last->next=NULL;  
  43.         }  
  44.     }  
  45.     void BFS(int srcindex){  
  46.         for(int i=1;i<=n;i++)  
  47.             if(i!=srcindex){  
  48.                 vertex[i].distance=INT_MAX;  
  49.                 vertex[i].previous=0;  
  50.                 vertex[i].color=WHITE;  
  51.             }  
  52.         vertex[srcindex].distance=0;  
  53.         vertex[srcindex].previous=0;  
  54.         vertex[srcindex].color=GRAY;  
  55.         queue<int> QUEUE;  
  56.         QUEUE.push(srcindex);  
  57.         while(!QUEUE.empty()){  
  58.             int v=QUEUE.front();QUEUE.pop();  
  59.             printf("visit:%d\n",v);  
  60.             LINK_LIST* node=adjacency[v];  
  61.             while(node){  
  62.                 if(vertex[node->index].color==WHITE){//v的所有邻接点入队  
  63.                     int adj=node->index;  
  64.                     QUEUE.push(adj);  
  65.                     vertex[adj].distance=vertex[v].distance+1;  
  66.                     vertex[adj].previous=v;  
  67.                     vertex[adj].color=GRAY;//刚被发现  
  68.                 }  
  69.                 node=node->next;  
  70.             }  
  71.             vertex[v].color=BLACK;//所有邻接点已发现  
  72.         }  
  73.     }  
  74.     void print(){  
  75.         for(int i=1;i<=n;i++){  
  76.             printf("%d:",i);  
  77.             LINK_LIST* node=adjacency[i];  
  78.             while(node){  
  79.                 printf("%d ",node->index);  
  80.                 node=node->next;  
  81.             }  
  82.             printf("\n");  
  83.         }  
  84.     }  
  85.     void PRINT_PATH(int srcindex,int endindex){  
  86.         if(srcindex==endindex) printf("%d",srcindex);  
  87.         else if(vertex[endindex].previous==0)  
  88.             printf("no path from %d to %d.",srcindex,endindex);  
  89.         else{  
  90.             PRINT_PATH(srcindex,vertex[endindex].previous);  
  91.             printf("%d",endindex);  
  92.         }  
  93.     }  
  94. };  
  95.   
  96. int main()  
  97. {  
  98.     freopen("1.txt","r",stdin);  
  99.     GRAPH graph;  
  100.     graph.read();  
  101.     graph.print();  
  102.     graph.BFS(8);  
  103.     graph.PRINT_PATH(8,5);  
  104.     return 0;  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值