Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar11 / bfs-dfs.cpp
blob: 19ef1b94b40c669cf33bbfae71c0d444a1d8ded2 (plain) (blame)
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
#include<iostream>
#include"Graph-Impl/AdjacencyListGraph/AdjListGraph.h"
#include"Graph-Impl/MatrixGraph/MatrixGraph.h"
#include<queue>

void BFS(const MatrixGraph& g, MatrixGraph::vertex_t start) {
    std::vector<bool> visited(g.vertexCount(), false);

    std::queue<MatrixGraph::vertex_t> q;

    q.push(start);

    while(!q.empty()) {
        MatrixGraph::vertex_t current = q.front();
        q.pop();

        if(visited[current])
            continue;
        
        visited[current] = true;

        std::cout << current << " ";

        auto successors = g.getSuccessors(current);

        for(const auto& currentVertex : successors) {
            if(!visited[currentVertex])
                q.push(currentVertex);
        }
    }
}

void DFSRec(std::vector<bool>& visited, const AdjListGraph& g, AdjListGraph::vertex_t start) {
    visited[start] = true;

    auto child = g.adjacent(start);

    std::cout << "Visited " << start << std::endl;

    for(const auto& currChild : child) {
        if(!visited[currChild.to]) {
            DFSRec(visited, g, currChild.to);
        }
    }
}

void DFS(const AdjListGraph& g, AdjListGraph::vertex_t start) {
    std::vector<bool> visited(g.vertexCount(), false);
    DFSRec(visited, g, start);
}

int main() {
    MatrixGraph g(5);
    // AdjListGraph g(5);
    g.addEdge(0, 2, 1);
    g.addEdge(0, 3, 1);
    g.addEdge(0, 1, 1);
    g.addEdge(2, 3, 1);
    g.addEdge(1, 4, 1);
    g.addEdge(3, 4, 1);
    g.addEdge(1, 3, 1);

    BFS(g, 0);
}