• c++ 关于bfs和dfs的相对统一写法


    文章目录

    简介

    • 有向图bfs
    • 有向图dfs
    • 有向图拓扑排序

    着重看看bfs 和 dfs 实现的差异性, 了解二者的相似和不同

    代码

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    struct Edge {
        int to;
        int weight;
    };
    
    struct Vertex {
        vector<Edge> adjacent;
    };
    
    // 无向图
    class Graph {
    public:
        int n{};
        vector<Vertex> nodes;
    
    protected:
        [[nodiscard]] virtual Graph *clone() const {
            return new Graph(*this);
        }
    
    public:
        Graph() = default;
    
        Graph(const Graph &other) {
            this->n = other.n;
            this->nodes = other.nodes;
        }
    
        virtual void addEdge(int v, int w, int weight = 0) {
            nodes[v].adjacent.push_back({.to = w, .weight = weight});
            nodes[w].adjacent.push_back({.to = v, .weight = weight});
        }
    
        void addAllEdge(int n, vector<vector<int>> &edges) {
            this->n = n;
            nodes.resize(n);
            for (auto &vec: edges) {
                int from = vec[0];
                int to = vec[1];
                int weight = 0;
                if (vec.size() >= 3) {
                    weight = vec[2];
                }
                addEdge(from, to, weight);
            }
        }
    
        [[nodiscard]] int Vex() const {
            return n;
        }
    
        [[nodiscard]] const vector<Edge> &getEdges(int v) const {
            return nodes[v].adjacent;
        }
    
        [[nodiscard]] virtual Graph *reverse() const {
            auto g = this->clone();
            return g;
        }
    
    };
    
    // 有向图
    class DiGraph : public Graph {
    public:
        DiGraph() = default;
    
        [[nodiscard]] Graph *reverse() const override {
            Graph* g = new DiGraph;
            g->n = this->n;
            g->nodes.resize(g->n);
    
            for (int from = 0; from < Vex(); from++) {
                for (auto &e: getEdges(from)) {
                    int to = e.to;
                    int weight = e.to;
                    g->addEdge(to, from, weight);
                }
            }
            return g;
        }
    
        void addEdge(int v, int w, int weight) override {
            nodes[v].adjacent.push_back({.to = w, .weight = weight});
        }
    
    private:
        [[nodiscard]] Graph *clone() const override {
            return new DiGraph(*this);
        }
    };
    
    class QAbs {
    public:
        virtual void enQ(int id) = 0;
    
        virtual int deQ() = 0;
    
        virtual bool empty() = 0;
    };
    
    class DfsQ : public QAbs {
    private:
        stack<int> q;
    
    public:
        void enQ(int id) override {
            q.push(id);
        }
    
        int deQ() override {
            int id = q.top();
            q.pop();
            return id;
        }
    
        bool empty() override {
            return q.empty();
        }
    };
    
    class BfsQ : public QAbs {
    private:
        queue<int> q;
    public:
        void enQ(int id) override {
            q.push(id);
        }
    
        int deQ() override {
            int id = q.front();
            q.pop();
            return id;
        }
    
        bool empty() override {
            return q.empty();
        }
    };
    
    enum class Order {
        BFS = 0,
        DFS = 1,
    };
    
    class TraversalAbs {
    public:
        Graph *g;
        vector<bool> known;
        QAbs *q;
    
    protected:
        virtual void pre(int v) {
            cout << v << "\t";
        }
    
        virtual void afterOneTraversal() {
            cout << endl;
        }
    
        virtual void afterTraversal() {
        }
    
    public:
        TraversalAbs(Graph *g, Order order) : g(g), known(g->Vex()) {
            if (order == Order::BFS) {
                q = new BfsQ;
            } else {
                q = new DfsQ;
            }
        }
    
        void traversal(int v) {
            known[v] = true;
            q->enQ(v);
            while (!q->empty()) {
                int id = q->deQ();
                // 先序
                pre(id);
                for (auto w: g->getEdges(id)) {
                    int to = w.to;
                    if (known[to]) {
                        continue;
                    }
                    known[to] = true;
                    q->enQ(to);
                }
            }
            afterOneTraversal();
        }
    
        void traversal() {
            for (int i = 0; i < g->Vex(); i++) {
                if (known[i]) {
                    continue;
                }
                traversal(i);
            }
            afterTraversal();
        }
    };
    
    class DfsTopology : public TraversalAbs {
    public:
        explicit DfsTopology(Graph *g) : TraversalAbs(g, Order::DFS) {}
    
    private:
        stack<int> st;
        stack<int> ans;
    
    protected:
        void pre(int v) override {
            st.push(v);
        }
    
        void afterTraversal() override {
            while (!ans.empty()) {
                cout << ans.top() << "\t";
                ans.pop();
            }
            cout << endl;
        }
    
        void afterOneTraversal() override {
            while (!st.empty()) {
                ans.push(st.top());
                st.pop();
            }
        }
    };
    
    
    int main() {
        vector<vector<int>> data = {
                {0, 1, 2},
                {1, 2, 2},
                {2, 3, 1},
                {3, 4, 2},
                {0, 4, 3},
        };
        auto g = new DiGraph;
        g->addAllEdge(5, data);
    
        auto rg = g->reverse();
    
        // bfs 遍历
        TraversalAbs btr(g, Order::BFS);
        btr.traversal(0);
    
        // dfs 遍历
        TraversalAbs dtr(g, Order::DFS);
        dtr.traversal(0);
    
        // 拓扑排序
        auto df = new DfsTopology(rg);
        df->traversal();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267

    输出

    在这里插入图片描述

    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法
    c++ 关于bfs和dfs的相对统一写法

  • 相关阅读:
    2023年上半年系统集成项目管理工程师什么时候报考?(附具体报名步骤)
    马可尼 光传输设备 全新原装板卡
    【力扣练习】找一个字符串中不含有重复字符的最长字串的长度
    小白还不懂电脑图片转PDF格式怎么弄吗?这些方法你都试过吗?
    0 基础 Java 自学之路(5)
    Springboot学习笔记——3
    C++ web httpserver
    Spring5学习笔记08--Scope
    含文档+PPT+源码等]精品基于SSM的校园二手交易平台[包运行成功]程序设计源码计算机毕设
    Redis&Mysql同步
  • 原文地址:https://blog.csdn.net/qq_34179431/article/details/126607625