生成图数据 使用随机数生成器生成300个不重复的顶点坐标,然后对于每个顶点,随机选择1-5个其他顶点,计算与它们之间的路径权值,并将这些信息存储到sqlite数据库中。 存取图数据 使用QT提供的QSqlite类库连接到sqlite数据库,读取和写入图数据。 使用QT代码实现
时间: 2024-02-16 22:04:33 浏览: 72
生成图数据的代码如下:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <sqlite3.h>
struct Point {
int x;
int y;
};
struct Edge {
int u;
int v;
int weight;
};
int main() {
const int n = 300;
const int m = 5;
std::vector<Point> points(n);
std::vector<std::vector<int>> adj(n);
std::vector<Edge> edges;
// 生成顶点坐标
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 1000);
for (int i = 0; i < n; ++i) {
points[i] = {dist(gen), dist(gen)};
}
// 构造邻接表和边集
for (int i = 0; i < n; ++i) {
std::vector<int> neighbors;
std::sample(points.begin(), points.end(), std::back_inserter(neighbors), m, gen);
std::sort(neighbors.begin(), neighbors.end());
neighbors.erase(std::unique(neighbors.begin(), neighbors.end()), neighbors.end());
adj[i] = neighbors;
for (int j : neighbors) {
if (i < j) {
edges.push_back({i, j, abs(points[i].x - points[j].x) + abs(points[i].y - points[j].y)});
}
}
}
// 存入sqlite数据库
sqlite3* db;
int rc = sqlite3_open("graph.db", &db);
if (rc) {
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
}
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS points (id INTEGER PRIMARY KEY, x INTEGER, y INTEGER)", nullptr, nullptr, nullptr);
if (rc) {
std::cerr << "Can't create table: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS edges (u INTEGER, v INTEGER, weight INTEGER)", nullptr, nullptr, nullptr);
if (rc) {
std::cerr << "Can't create table: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2(db, "INSERT INTO points (id, x, y) VALUES (?, ?, ?)", -1, &stmt, nullptr);
if (rc) {
std::cerr << "Can't prepare statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
for (int i = 0; i < n; ++i) {
rc = sqlite3_bind_int(stmt, 1, i);
rc |= sqlite3_bind_int(stmt, 2, points[i].x);
rc |= sqlite3_bind_int(stmt, 3, points[i].y);
if (rc) {
std::cerr << "Can't bind values: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "Can't execute statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
rc = sqlite3_reset(stmt);
if (rc) {
std::cerr << "Can't reset statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
}
rc = sqlite3_prepare_v2(db, "INSERT INTO edges (u, v, weight) VALUES (?, ?, ?)", -1, &stmt, nullptr);
if (rc) {
std::cerr << "Can't prepare statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
for (const auto& e : edges) {
rc = sqlite3_bind_int(stmt, 1, e.u);
rc |= sqlite3_bind_int(stmt, 2, e.v);
rc |= sqlite3_bind_int(stmt, 3, e.weight);
if (rc) {
std::cerr << "Can't bind values: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "Can't execute statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
rc = sqlite3_reset(stmt);
if (rc) {
std::cerr << "Can't reset statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return 1;
}
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
```
存取图数据的代码如下:
```cpp
#include <iostream>
#include <vector>
#include <map>
#include <sqlite3.h>
#include <QtSql>
struct Point {
int x;
int y;
};
int main() {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("graph.db");
if (!db.open()) {
std::cerr << "Can't open database: " << db.lastError().text().toStdString() << std::endl;
return 1;
}
QSqlQuery query(db);
if (!query.exec("SELECT COUNT(*) FROM points")) {
std::cerr << "Can't execute query: " << query.lastError().text().toStdString() << std::endl;
db.close();
return 1;
}
query.next();
int n = query.value(0).toInt();
std::vector<Point> points(n);
std::map<std::pair<int, int>, int> weights;
if (!query.exec("SELECT id, x, y FROM points")) {
std::cerr << "Can't execute query: " << query.lastError().text().toStdString() << std::endl;
db.close();
return 1;
}
while (query.next()) {
int id = query.value(0).toInt();
int x = query.value(1).toInt();
int y = query.value(2).toInt();
points[id] = {x, y};
}
if (!query.exec("SELECT u, v, weight FROM edges")) {
std::cerr << "Can't execute query: " << query.lastError().text().toStdString() << std::endl;
db.close();
return 1;
}
while (query.next()) {
int u = query.value(0).toInt();
int v = query.value(1).toInt();
int weight = query.value(2).toInt();
weights[{u, v}] = weight;
weights[{v, u}] = weight;
}
// 进行图算法操作,这里只是打印邻接表和边权值
for (int i = 0; i < n; ++i) {
std::cout << i << ": ";
for (int j : adj[i]) {
std::cout << j << "(" << weights[{i, j}] << ") ";
}
std::cout << std::endl;
}
db.close();
return 0;
}
```
注意事项:
1. 生成图数据的代码需要链接sqlite3库。
2. 存取图数据的代码需要链接QtSql库,并在运行前将sqlite3.dll放到可执行文件所在目录(Windows平台)。
阅读全文