m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/graph.h
blob: 85c66e7c2d66bb5e57516911e848ba6acaf6bf19 (plain)
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
#ifndef GRAPH_H
#define GRAPH_H

#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <vector>

class Graph {
public:
    Graph() : vertices_(), graph_(), number_vertices_(0) {}

    void add_vertex(int vertex) {
        if (vertices_.find(vertex) == vertices_.end()) {
            vertices_.insert(vertex);
            orderable_vertices_.push_back(vertex);
            graph_.push_back(std::vector<int>());
            has_out_edges_.push_back(false);
            number_vertices_++;
        }
    }

    void add_edge(int from, int to) {
        graph_[mapped_to_[from]].push_back(mapped_to_[to]);
        has_out_edges_[mapped_to_[from]] = true;
    }

    void done_with_vertices() {
        sort_vertices_();
        map_vertices_();
    }

    int get_number_vertices() const {
        return number_vertices_;
    }

    const std::vector<int>& get_neighbors(int vertex) const {
        return graph_[vertex];
    }

    bool has_out_edges(int vertex) const {
        return has_out_edges_[vertex];
    }

    int get_real_vertex(int vertex) const {
        return orderable_vertices_[vertex];
    }
private:
    std::unordered_set<int> vertices_;
    std::vector<int> orderable_vertices_;
    std::vector<std::vector<int>> graph_;
    std::vector<bool> has_out_edges_;
    std::unordered_map<int, int> mapped_to_;
    int number_vertices_;

    void sort_vertices_() {
        std::sort(orderable_vertices_.begin(), orderable_vertices_.end());
    }

    void map_vertices_() {
        for (int i = 0; i < number_vertices_; i++) {
            mapped_to_[orderable_vertices_[i]] = i;
        }
    }
};

#endif