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

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

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

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

    void add_edge(int from, int to) {
        add_vertex(from);
        add_vertex(to);
        graph_[from].push_back(to);
        has_out_edges_.insert(from);
    }

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

    const std::vector<int> & get_vertices() const {
        return orderable_vertices_;
    }

    const std::vector<int> & get_neighbors(int vertex) const {
        return graph_.find(vertex)->second;
    }

    bool has_out_edges(int vertex) const {
        return has_out_edges_.count(vertex) > 0;
    }

private:
    std::unordered_set<int> vertices_;
    std::vector<int> orderable_vertices_;
    std::unordered_map<int, std::vector<int>> graph_;
    std::unordered_set<int> has_out_edges_;
};

#endif