openGPMP
Open Source Mathematics Package
graphs.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 #ifndef __GRAPHS_HPP__
35 #define __GRAPHS_HPP__
36 
37 #include <cstdint>
38 // #include <limits>
39 #include <set>
40 #include <stack>
41 #include <vector>
42 
43 namespace gpmp {
44 
45 /*
46  * @class Graph
47  * @brief Graph class containing several utility methods and algorithms
48  */
49 class Graph {
50  public:
51  int vertices;
52  std::vector<std::vector<std::pair<int, int>>> adj_list;
53 
58  Graph(int v) : vertices(v), adj_list(v) {
59  }
60 
67  void add_edge(int v, int w, int weight);
68 
74  void dfs(int start);
75 
81  void dfs_rec(int v, std::vector<bool> &visited);
82 
88  void bfs(int start);
89 
95  void dijkstra(int start);
96 
102  void bellman_ford(int start);
103 
109  std::vector<std::pair<int, std::pair<int, int>>> kruskal();
110 
115  void floyd_warshall();
116 
120  void topo_sort();
121 
127  std::vector<std::vector<int>> connected_components();
128 
136  bool is_bipartite();
137 
143  std::vector<double> betweenness_centrality();
144 
150 
155  bool has_eulerian_path();
156 
162  int eccentricity(int vertex);
163 
168  int radius();
169 
174  std::vector<int> greedy_coloring();
175 
180  int chromatic_number();
181 
188  std::vector<std::pair<int, int>> match_cardinality();
189 
196  std::vector<std::pair<int, int>> match_wt();
197 
202  bool is_planar();
203 
210  void planar_gen(int num_vertices, double radius);
211 
218  bool has_k5();
219 
226  bool has_k33();
227 
235  bool is_k33(const std::set<int> &k_vertices);
236 
243  double euclid_dist(const std::pair<double, double> &point1,
244  const std::pair<double, double> &point2);
245 
250  std::vector<uint64_t> compress();
251 
256  void decompress(const std::vector<uint64_t> &compressed);
257 
258  private:
259  std::vector<bool> is_matched;
266  void topo_sort_util(int v,
267  std::vector<bool> &visited,
268  std::stack<int> &topo_stack);
269 
277  int find(std::vector<int> &parent, int i);
278 
285  void union_sets(std::vector<int> &parent, int x, int y);
286 
298  void dfs_connected_components(int v,
299  std::vector<bool> &visited,
300  std::vector<int> &component);
301 
307  int choose_random_neighbor(int vertex);
308 
317  bool hamiltonian_circuit_util(int v,
318  std::vector<bool> &visited,
319  std::vector<int> &path,
320  int count);
321 };
322 
323 } // namespace gpmp
324 
325 #endif
bool has_hamiltonian_circuit()
Checks if the graph contains a Hamiltonian circuit.
Definition: graphs.cpp:367
int radius()
Calculates the radius of the graph.
Definition: graphs.cpp:456
bool has_k5()
Checks if the graph contains a subgraph that is a subdivision of K5.
Definition: graphs.cpp:624
std::vector< std::pair< int, std::pair< int, int > > > kruskal()
Applies Kruskal's algorithm to find the minimum spanning tree of the graph.
Definition: graphs.cpp:139
void dfs(int start)
Performs Depth-First Search (DFS) traversal starting from a given vertex.
Definition: graphs.cpp:49
void bellman_ford(int start)
Applies Bellman-Ford algorithm to find the single-source shortest paths in the graph,...
Definition: graphs.cpp:116
bool is_bipartite()
Determines if a given graph is bipartite using BFS (Breadth First Search)
Definition: graphs.cpp:269
std::vector< bool > is_matched
Definition: graphs.hpp:259
std::vector< std::vector< std::pair< int, int > > > adj_list
Definition: graphs.hpp:52
void add_edge(int v, int w, int weight)
Adds an edge between two vertices with a specified weight.
Definition: graphs.cpp:44
std::vector< std::pair< int, int > > match_wt()
Finds a maximum weight matching in the graph using a greedy algorithm.
Definition: graphs.cpp:557
int chromatic_number()
Calculates the chromatic number of the graph.
Definition: graphs.cpp:467
int eccentricity(int vertex)
Calculates the eccentricity of a specified vertex in the graph.
Definition: graphs.cpp:431
void bfs(int start)
Performs Breadth-First Search (BFS) traversal starting from a given vertex.
Definition: graphs.cpp:65
int find(std::vector< int > &parent, int i)
Finds the representative element of the set that contains the given element.
Definition: graphs.cpp:242
std::vector< double > betweenness_centrality()
Calculates betweenness centrality for each vertex in the graph.
Definition: graphs.cpp:298
std::vector< int > greedy_coloring()
Performs graph coloring using the greedy algorithm.
Definition: graphs.cpp:511
bool hamiltonian_circuit_util(int v, std::vector< bool > &visited, std::vector< int > &path, int count)
Utility function for finding a Hamiltonian circuit.
Definition: graphs.cpp:395
double euclid_dist(const std::pair< double, double > &point1, const std::pair< double, double > &point2)
Calculates the Euclidean distance between two 2D points.
Definition: graphs.cpp:686
std::vector< uint64_t > compress()
Compresses the graph using Elias Gamma encoding.
Definition: graphs.cpp:693
std::vector< std::vector< int > > connected_components()
Finds connected components in an undirected graph using Depth-First Search (DFS)
Definition: graphs.cpp:254
int vertices
Definition: graphs.hpp:51
void decompress(const std::vector< uint64_t > &compressed)
Decompresses a compressed graph using Elias Gamma encoding.
Definition: graphs.cpp:719
void dfs_rec(int v, std::vector< bool > &visited)
Helper function for recursive Depth-First Search (DFS) traversal.
Definition: graphs.cpp:54
void topo_sort_util(int v, std::vector< bool > &visited, std::stack< int > &topo_stack)
Helper function for topological sorting.
Definition: graphs.cpp:227
void planar_gen(int num_vertices, double radius)
Generates a random planar graph using the random geometric graph model.
Definition: graphs.cpp:597
Graph(int v)
Constructor for the Graph class.
Definition: graphs.hpp:58
std::vector< std::pair< int, int > > match_cardinality()
Finds a maximum cardinality matching in the graph using a greedy algorithm.
Definition: graphs.cpp:535
bool has_eulerian_path()
Checks if the graph contains an Eulerian path.
Definition: graphs.cpp:381
int choose_random_neighbor(int vertex)
Chooses a random neighbor of a given vertex.
void floyd_warshall()
Applies Floyd-Warshall algorithm to find all-pairs shortest paths in the graph.
Definition: graphs.cpp:170
void dfs_connected_components(int v, std::vector< bool > &visited, std::vector< int > &component)
Performs Depth-First Search (DFS) traversal to find connected components in an undirected graph.
Definition: graphs.cpp:353
void dijkstra(int start)
Applies Dijkstra's algorithm to find the single-source shortest paths in the graph.
Definition: graphs.cpp:86
bool is_k33(const std::set< int > &k_vertices)
Checks if a set of vertices forms a subgraph that is a subdivision of K3,3.
Definition: graphs.cpp:672
void union_sets(std::vector< int > &parent, int x, int y)
Unions two sets by rank in the disjoint set data structure.
Definition: graphs.cpp:248
bool is_planar()
Checks if the graph is planar using Kuratowski's theorem.
Definition: graphs.cpp:589
bool has_k33()
Checks if the graph contains a subgraph that is a subdivision of K3,3.
Definition: graphs.cpp:654
void topo_sort()
Performs topological sorting on a Directed Acyclic Graph (DAG)
Definition: graphs.cpp:209
The source C++ openGPMP namespace.