【图论】图的C++实现代码

ops/2024/11/13 3:50:23/

在这个例程中我们用类实现了节点、(无向图)连边、无向图,实现了节点度的计算、无向图聚类系数计算、度分布统计、无向图的Dijkstra算法(已知起止点计算最短路的算法)

#include <iostream>
#include<vector>
#include<set>
#include<unordered_map>
using namespace std;class Edge
{
public:int v1;int v2;int weight;Edge(int v1, int v2, int w){this->v1 = v1;this->v2 = v2;this->weight = w;}
};class DJPathInfo // 存放已知最短路
{
public:vector<vector<int>> shortest_path;vector<int> shortest_path_len;DJPathInfo(int n){vector<int> empty;this->shortest_path=vector<vector<int>>(n, empty);this->shortest_path_len= vector<int>(n, 0);}
};class Node
{
public:std::vector<Edge> edges;int id;Node(int i){this->id = i;}int degree(){return this->edges.size();}vector<int> neighbors(){vector<int> res;if (this->edges.size() > 0){for (auto edge : this->edges){if (edge.v1 == this->id) res.push_back(edge.v2);else res.push_back(edge.v1);}return res;}else return res;}float cluster_coeff(vector<vector<bool>> adj_mat){int num_neighbor = this->edges.size() - 1;if (num_neighbor < 2) return 0;vector<int> neighbors = this->neighbors();float E = 0; //邻居之间的边数for (auto i : neighbors){for (auto j : neighbors){if (i >= j) continue;if (adj_mat[i][j] == true) E++;}}cout << 2 * E / (neighbors.size() * (neighbors.size() - 1)) << endl;return 2 * E / (neighbors.size() * (neighbors.size() - 1));}
};class Graph
{
public:vector<vector<bool>> adj_mat;vector<vector<float>> w_mat;int node_num;vector<Edge> edges;vector<Node> nodes;Graph(vector<vector<bool>> A, vector<vector<float>> W){this->adj_mat = A;this->w_mat = W;this->node_num = A.size();for (int i = 0; i < node_num; i++) this->nodes.push_back(Node(i));for (int i = 0; i < node_num; i++)for (int j = 0; j < i; j++){if (A[i][j] == true) this->add_edge(i, j, W[i][j]);}}Graph(int n){vector<float> zero(n, 0);vector<bool> all_false(n, false);vector<vector<bool>> A(n, all_false);vector<vector<float>> W(n, zero);this->adj_mat = A;this->w_mat = W;this->node_num = A.size();for (int i = 0; i < node_num; i++) this->nodes.push_back(Node(i));for (int i = 0; i < node_num; i++)for (int j = 0; j < i; j++){if (A[i][j] == true) this->add_edge(i, j, W[i][j]);}}void add_edge(int id1, int id2, int weight){Edge e = Edge(id1, id2, weight);this->adj_mat[id1][id2] = true;this->adj_mat[id2][id1] = true;this->w_mat[id1][id2] = weight;this->w_mat[id2][id1] = weight;this->edges.push_back(e);this->nodes[id1].edges.push_back(e);this->nodes[id2].edges.push_back(e);}void tell_info(){for (auto v : this->nodes){std::cout << "Node ID:" << v.id << std::endl;std::cout << "Neighbor/Weight:";for (auto i : v.neighbors()) cout <<'(' << i<<','<< this->w_mat[v.id][i] << ')' << ',';std::cout<<std::endl;}}vector<int> DJ(int s, int dest) // Dijkstra算法{DJPathInfo info(this->nodes.size());vector<int> res = {s};set<int> S;S.insert(s);set<int> U;for (auto v : this->nodes[s].neighbors()) U.insert(v);if (U.size() == 0) return res;for (auto v : U){info.shortest_path[v].push_back(v);info.shortest_path_len[v] = this->w_mat[s][v];}info.shortest_path_len[s] = 0;while (U.size() != 0){int n = *U.begin();int n_len = info.shortest_path_len[n];for (auto it = U.begin(); it != U.end(); it++){if (info.shortest_path_len[n] > info.shortest_path_len[*it]){n = *it;n_len = info.shortest_path_len[*it];}}S.insert(n);U.erase(n);for (auto v : this->nodes[n].neighbors()){if (S.find(v) != S.end()) continue;U.insert(v);if (info.shortest_path[v].size() == 0){info.shortest_path[v] = info.shortest_path[n];info.shortest_path[v].push_back(v);info.shortest_path_len[v] = info.shortest_path_len[n] + this->w_mat[n][v];continue;}if (info.shortest_path_len[n] + this->w_mat[n][v] < info.shortest_path_len[v]){info.shortest_path[v] = info.shortest_path[n];info.shortest_path[v].push_back(v);info.shortest_path_len[v] = info.shortest_path_len[n] + this->w_mat[n][v];}}}if (S.find(dest) != S.end()){for (auto v : info.shortest_path[dest]) res.push_back(v);info.shortest_path_len[dest];}return res;}float cluster_coeff(){float res=0;for (auto node : this->nodes) res += node.cluster_coeff(this->adj_mat);return res / float(this->nodes.size());}void degree_distribution() // 度分布{vector<float> res;unordered_map<int, int> degree_count;for (auto v : this->nodes){degree_count.emplace(v.degree(), 0);}for (auto v : this->nodes){degree_count[v.degree()]++;}for (auto it : degree_count){std::cout << "degree =" << it.first << ', '<<" prob=" << float(it.second) / this->nodes.size() << endl;}}
};int main()
{Graph G = Graph(11);G.add_edge(0, 1, 2);G.add_edge(0, 2, 9);G.add_edge(0, 3, 1);G.add_edge(1, 4, 1);G.add_edge(1, 2, 6);G.add_edge(2, 4, 5);G.add_edge(2, 5, 1);G.add_edge(2, 6, 2);G.add_edge(2, 3, 7);G.add_edge(3, 6, 9);G.add_edge(4, 7, 2);G.add_edge(4, 8, 9);G.add_edge(4, 5, 3);G.add_edge(5, 8, 6);G.add_edge(5, 6, 4);G.add_edge(6, 8, 3);G.add_edge(6, 9, 1);G.add_edge(7, 8, 7);G.add_edge(7, 10, 9);G.add_edge(8, 10, 2);G.add_edge(8, 9, 1);G.add_edge(9, 10, 4);G.tell_info();cout << "Djkstra Test: shortest 0 -> 10: ";for (const auto& p : G.DJ(0, 10)) cout << p << ',';cout<<endl;cout << "clustring coefficient=" << G.cluster_coeff() << endl;cout << "degree distribution:"  << endl;G.degree_distribution();return 0;
}

http://www.ppmy.cn/ops/132164.html

相关文章

MySQL第六章,数据库企业级开发技术

这一章节也是非常重要的一章&#xff0c;非常常用&#xff01; 一、事务&#xff08;Transactions&#xff09; 1. 事务的概念 事务是数据库操作的基本单位&#xff0c;它保证了一组数据库操作要么全部执行成功&#xff0c;要么全部回滚&#xff0c;从而保持数据的一致性和完…

Python并发编程库:Asyncio的异步编程实战

Python并发编程库&#xff1a;Asyncio的异步编程实战 在现代应用中&#xff0c;并发和高效的I/O处理是影响系统性能的关键因素之一。Python的asyncio库是专为异步编程设计的模块&#xff0c;提供了一种更加高效、易读的并发编程方式&#xff0c;适用于处理大量的I/O密集型任务…

How to use ffmpeg to convert video format from .webm to .mp4

The .mp4 container format doesn’t support the VP8 codec, which is commonly used in .webm files. MP4 containers typically use the H.264 codec for video and AAC for audio. You’ll need to re-encode the video using the H.264 codec and re-encode the audio us…

uniapp组件样式运行至小程序失效

文章目录 一、uniapp样式穿透打包运行至微信小程序失效 一、uniapp样式穿透打包运行至微信小程序失效 组件样式隔离文章参考 解决方案 options: {styleIsolation: "shared",},这个配置项改变了小程序组件的样式隔离模式&#xff0c;使得组件的样式能够共享和继承。…

AscendC从入门到精通系列(一)初步感知AscendC

1 什么是AscendC Ascend C是CANN针对算子开发场景推出的编程语言&#xff0c;原生支持C和C标准规范&#xff0c;兼具开发效率和运行性能。基于Ascend C编写的算子程序&#xff0c;通过编译器编译和运行时调度&#xff0c;运行在昇腾AI处理器上。使用Ascend C&#xff0c;开发者…

Java打造智能语音陪聊软件?提升用户体验的新路径

在现在的日常生活中&#xff0c;大家做什么都会寻找一个“搭子”&#xff0c;例如“游戏搭子”&#xff0c;很多时候一时半会找不到就会很苦恼&#xff0c;就因此诞生了语音陪聊软件。然而Java作为一种广泛使用的编程语言&#xff0c;在开发高效、稳定的应用程序方面具有显著优…

mysql if函数如何处理无匹配记录的情况?使用聚合函数

问题描述&#xff1a;编者在使用mysql中的if(car_number,"监管车辆","非监管车辆")函数时&#xff0c;场景为在一个car表中如果能查到具体某辆车这辆车就是我司监管车辆&#xff0c;差不到就不是我司监管车辆显示非监管车辆&#xff0c;遇到匹配不到的数据…

基于ASP.NET+SQL Server实现简单小说网站(包括PC版本和移动版本)

一、网站简介 1.1 设计思路 根据一般人阅读小说的顺序&#xff0c;利用了HTML5、CSS3制作一个普通pc端和跨平台移动端。 PC端&#xff1a;小说的首页、小说某类具体信息、某小说详细信息页移动端&#xff1a;小说的首页、小说分类、小说某类具体信息、小说详情 1.2 网站的主…