adj_matrix.h
Go to the documentation of this file.
1 /*
2  * This file is part of the alglib project.
3  *
4  * (c) Divyanshu Kakwani <divkakwani@gmail.com>
5  *
6  * For the full copyright and license information, please view the LICENSE file
7  * that was distributed with this source code.
8  */
9 
10 #pragma once
11 
12 #include <vector>
13 #include <memory>
14 
15 #include <alglib/bimap/bimap.h>
17 
18 namespace alglib {
19 namespace graph {
20 namespace models {
21 
22 template<typename vertex_t, typename edge_t>
23 class adj_matrix : public graph_model<vertex_t, edge_t> {
24 
25  public:
26  /* typedefs */
27  typedef vertex_t vertex_type;
28  typedef edge_t edge_type;
30 
31  private:
32 
33  /* Establishes one-to-one mapping between vertices and ints */
35 
36  /* The adjacent matrix is represented as a vector of vectors.
37  * Each cell, matrix[i][j], stores a unique_ptr to the corresponding edge if
38  * there exists an edge between vertex i and vertex j, otherwise it refers to
39  * null. The edge is the cell's copy of the edge and hence it is its owner.
40  * Once it is set to null, the edge is automatically freed.
41  */
42  std::vector<std::vector<std::unique_ptr<edge_t>>> matrix;
43 
44  /* vsize tells the no of vertices; The is required because the `matrix` can be
45  * preallocated vertices. Hence, its size would be larger than actual vsize.
46  */
47  int vsize;
48 
49  /* The following members record the in-degrees and out-degrees - to fasten
50  * the indeg and outdeg methods. These require to be updated by every mutating method
51  * on the graph.
52  */
53  std::vector<int> indeg_rec;
54  std::vector<int> outdeg_rec;
55 
61  void grow_matrix(int dim);
62 
63  public:
64 
65 /*
66  * Which implementation?
67  * Matrix view
68  * Operations:
69  * 1. Constant time access of M[i][j]
70  * 2. Add vertices
71  * 3. Remove vertices
72  * 4.
73  */
74  adj_matrix() = default;
75 
76  void add_vertex(const vertex_t& u) {
77  vertex_to_int.insert(u, vsize++);
78  grow_matrix(vsize);
79  }
80  // FIXME How?
81  void remove_vertex() {}
82 
83  int indeg(const vertex_t& u) const {
84  return indeg_rec[vertex_to_int.get_image(u)];
85  }
86  int outdeg(const vertex_t& u) const {
87  return indeg_rec[vertex_to_int.get_image(u)];
88  }
89  int num_vertices() const {
90  return vertex_to_int.size();
91  }
92  int num_edges() const {
93 
94  }
95 
96  const_viterator vbegin() const {
97  vertex_to_int.domain_begin();
98  }
99  const_viterator vend() const {
100  return vertex_to_int.domain_end();
101  }
102 
103 
104 
105 };
106 
107 } // end of models namespace
108 } // end of graph namespace
109 } // end of alglib namespace
110 
int indeg(const vertex_t &u) const
Definition: adj_matrix.h:83
edge_t edge_type
Definition: adj_matrix.h:28
const_viterator vbegin() const
Definition: adj_matrix.h:96
Definition: bimap.h:17
vertex_t vertex_type
Definition: adj_matrix.h:27
void insert(const type1 &elt1, const type2 &elt2)
Definition: bimap.h:104
The interface of a graph model.
Definition: graph_model.h:25
void add_vertex(const vertex_t &u)
Definition: adj_matrix.h:76
Definition: edge.h:19
void remove_vertex()
Definition: adj_matrix.h:81
const_viterator vend() const
Definition: adj_matrix.h:99
const codomain_type & get_image(const domain_type &elt) const
Definition: bimap.h:120
domain_iterator domain_end() const
Definition: bimap.h:188
alglib::bimap::bimap< vertex_t, int >::type1_iterator const_viterator
Definition: adj_matrix.h:29
int outdeg(const vertex_t &u) const
Definition: adj_matrix.h:86
Definition: adj_matrix.h:23
int num_edges() const
Definition: adj_matrix.h:92
Definition: bimap.h:26
domain_iterator domain_begin() const
Definition: bimap.h:182
int num_vertices() const
Definition: adj_matrix.h:89