50 std::vector<bool> visited(vertices,
false);
51 dfs_rec(start, visited);
56 std::cout << v <<
" ";
58 for (
const auto &neighbor : adj_list[v]) {
59 if (!visited[neighbor.first]) {
60 dfs_rec(neighbor.first, visited);
66 std::vector<bool> visited(vertices,
false);
67 std::queue<int> bfs_queue;
69 visited[start] =
true;
70 bfs_queue.push(start);
72 while (!bfs_queue.empty()) {
73 int v = bfs_queue.front();
75 std::cout << v <<
" ";
77 for (
const auto &neighbor : adj_list[v]) {
78 if (!visited[neighbor.first]) {
79 visited[neighbor.first] =
true;
80 bfs_queue.push(neighbor.first);
87 std::priority_queue<std::pair<int, int>,
88 std::vector<std::pair<int, int>>,
89 std::greater<std::pair<int, int>>>
91 std::vector<int> dist(vertices, std::numeric_limits<int>::max());
97 int u = pq.top().second;
100 for (
const auto &neighbor : adj_list[
u]) {
101 int v = neighbor.first;
102 int weight = neighbor.second;
104 if (dist[
u] + weight < dist[v]) {
105 dist[v] = dist[
u] + weight;
106 pq.push({dist[v], v});
111 std::cout <<
"Dijkstra's Shortest Paths from vertex " << start <<
":\n";
112 for (
int i = 0; i < vertices; ++i)
113 std::cout <<
"Vertex " << i <<
": " << dist[i] <<
"\n";
117 std::vector<int> dist(vertices, std::numeric_limits<int>::max());
120 for (
int i = 0; i < vertices - 1; ++i) {
121 for (
int u = 0;
u < vertices; ++
u) {
122 for (
const auto &neighbor : adj_list[
u]) {
123 int v = neighbor.first;
124 int weight = neighbor.second;
126 if (dist[
u] != std::numeric_limits<int>::max() &&
127 dist[
u] + weight < dist[v]) {
128 dist[v] = dist[
u] + weight;
134 std::cout <<
"Bellman-Ford Shortest Paths from vertex " << start <<
":\n";
135 for (
int i = 0; i < vertices; ++i)
136 std::cout <<
"Vertex " << i <<
": " << dist[i] <<
"\n";
140 std::vector<std::pair<int, std::pair<int, int>>> edges;
141 for (
int u = 0;
u < vertices; ++
u) {
142 for (
const auto &neighbor : adj_list[
u]) {
143 int v = neighbor.first;
144 int weight = neighbor.second;
145 edges.emplace_back(weight, std::make_pair(
u, v));
149 std::sort(edges.begin(), edges.end());
151 std::vector<std::pair<int, std::pair<int, int>>> min_span_tree;
152 std::vector<int> parent(vertices, -1);
154 for (
const auto &edge : edges) {
155 int u = edge.second.first;
156 int v = edge.second.second;
158 int setU = find(parent,
u);
159 int setV = find(parent, v);
162 min_span_tree.push_back(edge);
163 union_sets(parent, setU, setV);
167 return min_span_tree;
171 std::vector<std::vector<int>> dist(
173 std::vector<int>(vertices, std::numeric_limits<int>::max()));
175 for (
int i = 0; i < vertices; ++i) {
177 for (
const auto &neighbor : adj_list[i]) {
178 int v = neighbor.first;
179 int weight = neighbor.second;
184 for (
int k = 0; k < vertices; ++k) {
185 for (
int i = 0; i < vertices; ++i) {
186 for (
int j = 0; j < vertices; ++j) {
187 if (dist[i][k] != std::numeric_limits<int>::max() &&
188 dist[k][j] != std::numeric_limits<int>::max() &&
189 dist[i][k] + dist[k][j] < dist[i][j]) {
190 dist[i][j] = dist[i][k] + dist[k][j];
196 std::cout <<
"Floyd-Warshall Shortest Paths:\n";
197 for (
int i = 0; i < vertices; ++i) {
198 for (
int j = 0; j < vertices; ++j) {
199 std::cout <<
"From " << i <<
" to " << j <<
": ";
200 if (dist[i][j] == std::numeric_limits<int>::max())
203 std::cout << dist[i][j];
210 std::stack<int> topo_stack;
211 std::vector<bool> visited(vertices,
false);
213 for (
int i = 0; i < vertices; ++i) {
215 topo_sort_util(i, visited, topo_stack);
219 std::cout <<
"Topological Sorting: ";
220 while (!topo_stack.empty()) {
221 std::cout << topo_stack.top() <<
" ";
224 std::cout << std::endl;
228 std::vector<bool> &visited,
229 std::stack<int> &topo_stack) {
232 for (
const auto &neighbor : adj_list[v]) {
233 int u = neighbor.first;
235 topo_sort_util(
u, visited, topo_stack);
245 return find(parent, parent[i]);
249 int setX = find(parent, x);
250 int setY = find(parent, y);
255 std::vector<std::vector<int>> components;
256 std::vector<bool> visited(vertices,
false);
258 for (
int v = 0; v < vertices; ++v) {
260 std::vector<int> component;
261 dfs_connected_components(v, visited, component);
262 components.push_back(component);
270 std::vector<int> color(vertices, -1);
271 std::queue<int> bfs_queue;
273 for (
int start = 0; start < vertices; ++start) {
274 if (color[start] == -1) {
276 bfs_queue.push(start);
278 while (!bfs_queue.empty()) {
279 int v = bfs_queue.front();
282 for (
const auto &neighbor : adj_list[v]) {
283 int u = neighbor.first;
284 if (color[
u] == -1) {
285 color[
u] = 1 - color[v];
287 }
else if (color[
u] == color[v]) {
299 std::vector<double> centrality(vertices, 0.0);
301 for (
int s = 0; s < vertices; ++s) {
302 std::vector<int> predecessors(vertices, -1);
303 std::vector<int> distance(vertices, -1);
304 std::vector<int> num_shortest_paths(vertices, 0);
305 std::stack<int> stack;
306 std::queue<int> bfs_queue;
309 num_shortest_paths[s] = 1;
312 while (!bfs_queue.empty()) {
313 int v = bfs_queue.front();
317 for (
const auto &neighbor : adj_list[v]) {
318 int w = neighbor.first;
319 if (distance[w] == -1) {
320 distance[w] = distance[v] + 1;
324 if (distance[w] == distance[v] + 1) {
325 num_shortest_paths[w] += num_shortest_paths[v];
331 std::vector<double> dependency(vertices, 0.0);
333 while (!stack.empty()) {
337 for (
const auto &predecessor : adj_list[w]) {
338 int v = predecessor.first;
339 dependency[v] += (
static_cast<double>(num_shortest_paths[v]) /
340 num_shortest_paths[w]) *
345 centrality[w] += dependency[w];
354 std::vector<bool> &visited,
355 std::vector<int> &component) {
357 component.push_back(v);
359 for (
const auto &neighbor : adj_list[v]) {
360 int u = neighbor.first;
362 dfs_connected_components(
u, visited, component);
368 std::vector<int> path;
369 std::vector<bool> visited(vertices,
false);
371 for (
int v = 0; v < vertices; ++v) {
373 if (hamiltonian_circuit_util(v, visited, path, 1)) {
386 for (
int v = 0; v < vertices; ++v) {
387 if (adj_list[v].size() % 2 != 0) {
392 return odd_degrees == 0 || odd_degrees == 2;
396 std::vector<bool> &visited,
397 std::vector<int> &path,
402 if (count == vertices) {
405 for (
const auto &neighbor : adj_list[v]) {
406 if (neighbor.first == path[0]) {
413 for (
const auto &neighbor : adj_list[v]) {
414 if (!visited[neighbor.first]) {
415 if (hamiltonian_circuit_util(neighbor.first,
432 std::vector<int> distance(vertices, std::numeric_limits<int>::max());
433 distance[vertex] = 0;
435 std::queue<int> bfs_queue;
436 bfs_queue.push(vertex);
438 while (!bfs_queue.empty()) {
439 int v = bfs_queue.front();
442 for (
const auto &neighbor : adj_list[v]) {
443 int u = neighbor.first;
444 if (distance[
u] == std::numeric_limits<int>::max()) {
445 distance[
u] = distance[v] + 1;
452 return *std::max_element(distance.begin(), distance.end());
457 int min_ecntrc = std::numeric_limits<int>::max();
459 for (
int v = 0; v < vertices; ++v) {
460 int vEccentricity = eccentricity(v);
461 min_ecntrc = std::min(min_ecntrc, vEccentricity);
468 std::vector<int> result(vertices, -1);
475 std::vector<bool> available(vertices,
false);
476 for (
const auto &neighbor : adj_list[0]) {
477 int v = neighbor.first;
478 if (result[v] != -1) {
479 available[result[v]] =
true;
484 for (
int v = 1; v < vertices; ++v) {
485 for (
int c = 0; c < vertices; ++c) {
493 std::fill(available.begin(), available.end(),
false);
496 for (
const auto &neighbor : adj_list[v]) {
497 int u = neighbor.first;
498 if (result[
u] != -1) {
499 available[result[
u]] =
true;
505 int chrom_num = *std::max_element(result.begin(), result.end()) + 1;
512 std::vector<int> result(vertices, -1);
515 for (
int v = 0; v < vertices; ++v) {
516 std::vector<bool> available(vertices,
false);
517 for (
const auto &neighbor : adj_list[v]) {
518 int u = neighbor.first;
519 if (result[
u] != -1) {
520 available[result[
u]] =
true;
524 for (
int c = 0; c < vertices; ++c) {
536 std::vector<std::pair<int, int>> matching;
539 for (
int v = 0; v < vertices; ++v) {
540 if (!is_matched[v]) {
541 for (
const auto &neighbor : adj_list[v]) {
542 int u = neighbor.first;
543 if (!is_matched[
u]) {
544 matching.push_back({v,
u});
545 is_matched[v] =
true;
546 is_matched[
u] =
true;
558 std::vector<std::pair<int, int>> matching;
561 std::vector<std::pair<std::pair<int, int>,
int>> wt_edges;
562 for (
int v = 0; v < vertices; ++v) {
563 for (
const auto &neighbor : adj_list[v]) {
564 int u = neighbor.first;
565 int weight = neighbor.second;
566 wt_edges.push_back({{v,
u}, weight});
570 std::sort(wt_edges.rbegin(),
572 [](
const auto &a,
const auto &b) { return a.second < b.second; });
576 for (
const auto &edge : wt_edges) {
577 int v = edge.first.first;
578 int u = edge.first.second;
579 if (!is_matched[v] && !is_matched[
u]) {
580 matching.push_back({v,
u});
581 is_matched[v] =
true;
582 is_matched[
u] =
true;
592 return !has_k5() && !has_k33();
598 vertices = num_vertices;
599 adj_list.resize(vertices);
602 std::vector<std::pair<double, double>> points;
603 for (
int v = 0; v < vertices; ++v) {
604 double x =
static_cast<double>(rand()) / RAND_MAX;
605 double y =
static_cast<double>(rand()) / RAND_MAX;
606 points.push_back({x, y});
611 for (
int v = 0; v < vertices; ++v) {
612 for (
int u = v + 1;
u < vertices; ++
u) {
613 double distance = euclid_dist(points[v], points[
u]);
614 if (distance < radius) {
625 for (
int v = 0; v < vertices; ++v) {
626 if (adj_list[v].size() >= 5) {
627 std::set<int> neighbors;
628 for (
const auto &neighbor : adj_list[v]) {
629 neighbors.insert(neighbor.first);
632 for (
const auto &
u : neighbors) {
633 std::set<int> common_neighbors;
634 for (
const auto &neighbor : adj_list[
u]) {
635 if (neighbors.find(neighbor.first) != neighbors.end()) {
636 common_neighbors.insert(neighbor.first);
640 for (
const auto &w : common_neighbors) {
641 if (neighbors.find(w) != neighbors.end()) {
655 for (
int v = 0; v < vertices; ++v) {
656 if (adj_list[v].size() >= 3) {
657 std::set<int> neighbors;
658 for (
const auto &neighbor : adj_list[v]) {
659 neighbors.insert(neighbor.first);
662 if (is_k33(neighbors)) {
673 if (k_vertices.size() == 6) {
675 for (
int v : k_vertices) {
676 degree_sum += adj_list[v].size();
680 return degree_sum == 2 *
static_cast<int>(k_vertices.size());
687 const std::pair<double, double> &point2) {
688 double dx = point1.first - point2.first;
689 double dy = point1.second - point2.second;
690 return std::sqrt(dx * dx + dy * dy);
694 std::vector<uint64_t> compressed;
695 compressed.push_back(vertices);
697 for (
int v = 0; v < vertices; ++v) {
699 std::bitset<64> binary_representation(adj_list[v].size() + 1);
700 int length = log2(adj_list[v].size() + 1) + 1;
703 compressed.push_back(length);
706 compressed.push_back(
707 std::stoull(binary_representation.to_string(), 0, 2));
710 for (
const auto &neighbor : adj_list[v]) {
711 compressed.push_back(neighbor.first);
721 vertices = compressed[index++];
723 adj_list.resize(vertices);
725 for (
int v = 0; v < vertices; ++v) {
726 int length = compressed[index++];
727 uint64_t encoded_neighbors = compressed[index++];
728 std::bitset<64> binary_representation(encoded_neighbors);
732 std::stoi(binary_representation.to_string().substr(64 - length),
738 for (
int i = 0; i < num_neighbors; ++i) {
739 adj_list[v].emplace_back(compressed[index++],
bool has_hamiltonian_circuit()
Checks if the graph contains a Hamiltonian circuit.
int radius()
Calculates the radius of the graph.
bool has_k5()
Checks if the graph contains a subgraph that is a subdivision of K5.
std::vector< std::pair< int, std::pair< int, int > > > kruskal()
Applies Kruskal's algorithm to find the minimum spanning tree of the graph.
void dfs(int start)
Performs Depth-First Search (DFS) traversal starting from a given vertex.
void bellman_ford(int start)
Applies Bellman-Ford algorithm to find the single-source shortest paths in the graph,...
bool is_bipartite()
Determines if a given graph is bipartite using BFS (Breadth First Search)
std::vector< std::vector< std::pair< int, int > > > adj_list
void add_edge(int v, int w, int weight)
Adds an edge between two vertices with a specified weight.
std::vector< std::pair< int, int > > match_wt()
Finds a maximum weight matching in the graph using a greedy algorithm.
int chromatic_number()
Calculates the chromatic number of the graph.
int eccentricity(int vertex)
Calculates the eccentricity of a specified vertex in the graph.
void bfs(int start)
Performs Breadth-First Search (BFS) traversal starting from a given vertex.
int find(std::vector< int > &parent, int i)
Finds the representative element of the set that contains the given element.
std::vector< double > betweenness_centrality()
Calculates betweenness centrality for each vertex in the graph.
std::vector< int > greedy_coloring()
Performs graph coloring using the greedy algorithm.
bool hamiltonian_circuit_util(int v, std::vector< bool > &visited, std::vector< int > &path, int count)
Utility function for finding a Hamiltonian circuit.
double euclid_dist(const std::pair< double, double > &point1, const std::pair< double, double > &point2)
Calculates the Euclidean distance between two 2D points.
std::vector< uint64_t > compress()
Compresses the graph using Elias Gamma encoding.
std::vector< std::vector< int > > connected_components()
Finds connected components in an undirected graph using Depth-First Search (DFS)
void decompress(const std::vector< uint64_t > &compressed)
Decompresses a compressed graph using Elias Gamma encoding.
void dfs_rec(int v, std::vector< bool > &visited)
Helper function for recursive Depth-First Search (DFS) traversal.
void topo_sort_util(int v, std::vector< bool > &visited, std::stack< int > &topo_stack)
Helper function for topological sorting.
void planar_gen(int num_vertices, double radius)
Generates a random planar graph using the random geometric graph model.
std::vector< std::pair< int, int > > match_cardinality()
Finds a maximum cardinality matching in the graph using a greedy algorithm.
bool has_eulerian_path()
Checks if the graph contains an Eulerian path.
void floyd_warshall()
Applies Floyd-Warshall algorithm to find all-pairs shortest paths in the graph.
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.
void dijkstra(int start)
Applies Dijkstra's algorithm to find the single-source shortest paths in the graph.
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.
void union_sets(std::vector< int > &parent, int x, int y)
Unions two sets by rank in the disjoint set data structure.
bool is_planar()
Checks if the graph is planar using Kuratowski's theorem.
bool has_k33()
Checks if the graph contains a subgraph that is a subdivision of K3,3.
void topo_sort()
Performs topological sorting on a Directed Acyclic Graph (DAG)