• 图论-深度优先和广度优先(均非递归)


    图论-深度优先和广度优先(均非递归)

      不使用递归的原因我这在这不重复。因此如何替代递归呢?请接着看:

        深度优先:使用Stack(栈)替代

        广度优先:使用Queue(队列)替代

      C++代码献上:

      1 #include <iostream>
      2 #include<random>
      3 #include <stack>
      4 #include <queue>
      5 using namespace std;
      6 
      7 class Image
      8 {
      9 private:
     10     int ImageArray[8][8]= {{0},{0},{0}};
     11     int length=8;
     12 public:
     13     Image(void) {}
     14     int getImage(int x,int y)
     15     {
     16         return ImageArray[x][y];
     17     }
     18     int getLength(void)
     19     {
     20         return length;
     21     }
     22     bool setImage(int x,int y,int value)
     23     {
     24         if(x<length&&x>-1&&-1<y&&y<length)
     25         {
     26             ImageArray[x][y]=value;
     27             return true;
     28         }
     29         else return false;
     30     }
     31     void create(void)
     32     {
     33         for(int i=0; i<length; ++i)
     34             for(int j=0; j<i; ++j)
     35             {
     36                 ImageArray[i][j]=rand()%2;
     37                 ImageArray[j][i]=ImageArray[i][j];
     38             }
     39     }
     40 
     41     /***
     42     @name:深度优先
     43     @rule:ImageVisited[8] 0:未访问 1:已经访问
     44     @hint:相当于二叉树原前序遍历(根、左、右)
     45     ***/
     46     void dfs(int node)
     47     {
     48         int ImageVisited[8]= {0};
     49         stack<int> ImageStack;
     50 
     51         ImageStack.push(node);
     52         cout<<ImageStack.top();
     53         ImageVisited[node]=1;
     54         while(!ImageStack.empty())
     55         {
     56             bool over=false;
     57             //cout<<"已经进行中"<<endl;
     58             for(int j=0,i=ImageStack.top(); j<length; ++j)
     59             {
     60                 if(!ImageVisited[j]&&ImageArray[i][j])
     61                 {
     62                     ImageStack.push(j);
     63                     cout<<ImageStack.top();
     64                     ImageVisited[j]=1;
     65                     break;
     66                 }
     67                 if(j+1==length)
     68                     over=true;
     69             }
     70             if(over)
     71             {
     72                 over=false;
     73                 ImageStack.pop();
     74                 //  cout<<"*Size of ImageStack*"<<ImageStack.size()<<endl;
     75             }
     76         }
     77         cout<<endl;
     78     }
     79 
     80     /***
     81     @name:广度优先
     82      @rule:与上同
     83     @hint:相当于树的层次遍历
     84     ***/
     85     void bfs(int node)
     86     {
     87         int ImageVisited[8] {0};
     88         queue<int> ImageQueue;
     89         ImageQueue.push(node);
     90         ImageVisited[node]=1;
     91         cout<<ImageQueue.front();
     92         while(!ImageQueue.empty())
     93         {
     94             for(int j=0,i=ImageQueue.front(); j<length; ++j)
     95             {
     96                 if(!ImageVisited[j]&&ImageArray[i][j])
     97                 {
     98                     ImageQueue.push(j);
     99                     ImageVisited[j]=1;
    100                     cout<<j;
    101                 }
    102             }
    103             ImageQueue.pop();
    104         }
    105         cout<<endl;
    106     }
    107 
    108     void printImage()
    109     {
    110         for(int i=0; i<length; ++i)
    111         {
    112             for(int j=0; j<length; ++j)
    113             {
    114                 cout<<ImageArray[i][j]<<",";
    115             }
    116             cout<<endl;
    117         }
    118     }
    119 };
    120 
    121 
    122 int main()
    123 {
    124     Image image {};
    125     image.create();
    126     image.printImage();
    127 
    128     image.dfs();
    129     image.bfs();
    130     return 0;
    131 }

     

  • 相关阅读:
    Postgresql修改序列方法 select setval('dataset_id_seq',1,false);
    再谈Elasticsearch全文搜索:你不知道的query_string、match、term、match_phrase的区别
    crosstab(unknown, unknown) does not exist
    Elasticsearch High Level Rest Client 发起请求的过程分析
    提取经过Jar2Exe编译加密的源代码 教程1
    Unity 协程与线程
    box unboxing(装箱 拆箱) C#编程指南
    C++基础笔记(四)C++内存管理
    C++ 箭头-> 双冒号:: 点号.操作符区别
    ref out 方法参数
  • 原文地址:https://www.cnblogs.com/orangebook/p/3763520.html
Copyright © 2020-2023  润新知