{ "title": "igraph 图对象添加功能:使用第一列作为源节点,第二列作为目标节点", "description": "本文介绍如何使用 igraph 库在 C++ 中将数据的第一列作为源节点,第二列作为目标节点来创建一个图。提供完整代码并解释修改步骤,帮助您理解如何实现该功能。", "keywords": "igraph, 图对象, 源节点, 目标节点, C++, 代码, 编程", "content": "## igraph 图对象添加功能:使用第一列作为源节点,第二列作为目标节点

本文介绍如何使用 igraph 库在 C++ 中将数据的第一列作为源节点,第二列作为目标节点来创建一个图。提供完整代码并解释修改步骤,帮助您理解如何实现该功能。

代码示例

#include <igraph/igraph.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <vector>

int main() {
    std::ifstream file("matrix.txt");
    std::string line;

    std::vector<int> sources;
    std::vector<int> targets;
    std::vector<double> weights;
    std::vector<std::string> node_names;

    std::unordered_map<std::string, int> node_map;
    int node_count = 0;

    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string u, v;
        double w;

        if (!(iss >> u >> v >> w)) {
            break;  // error
        }

        if (node_map.find(u) == node_map.end()) {
            node_map[u] = node_count;
            node_names.push_back(u);
            node_count++;
        }

        if (node_map.find(v) == node_map.end()) {
            node_map[v] = node_count;
            node_names.push_back(v);
            node_count++;
        }

        sources.push_back(node_map[u]);
        targets.push_back(node_map[v]);
        weights.push_back(w);
    }

    igraph_t graph;
    igraph_set_attribute_table(&igraph_cattribute_table);
    igraph_empty(&graph, node_count, IGRAPH_UNDIRECTED); // Specify the number of vertices

    igraph_vector_int_t source_vector;
    igraph_vector_int_init(&source_vector, 0);

    for (auto &source : sources) {
        igraph_vector_int_push_back(&source_vector, source);
    }

    igraph_vector_int_t target_vector;
    igraph_vector_int_init(&target_vector, 0);

    for (auto &target : targets) {
        igraph_vector_int_push_back(&target_vector, target);
    }

    igraph_add_edges(&graph, &source_vector, &target_vector);

    igraph_vector_t weight_vector;
    igraph_vector_init(&weight_vector, 0);

    for (auto &weight : weights) {
        igraph_vector_push_back(&weight_vector, weight);
    }

    SETEANV(&graph, "weight", &weight_vector);

    for (size_t i = 0; i < node_names.size(); ++i) {
        SETVAS(&graph, "name", i, node_names[i].c_str());
    }

    FILE *outfile = fopen("gg.graphml", "w");
    igraph_write_graph_graphml(&graph, outfile, false);
    fclose(outfile);
    igraph_destroy(&graph);
    igraph_vector_int_destroy(&source_vector);
    igraph_vector_int_destroy(&target_vector);
    igraph_vector_destroy(&weight_vector);

    return 0;
}

解释

  1. 创建源节点和目标节点向量:

    • 在读取数据之前,创建两个向量 sourcestargets 来存储源节点和目标节点的索引。
  2. 读取数据并填充向量:

    • 在读取数据的过程中,将第一列的值添加到 sources 向量,将第二列的值添加到 targets 向量。
  3. 创建图对象:

    • 使用 igraph_empty 函数创建一个空图对象,指定节点数量和图类型(无向图)。
  4. 将边添加到图中:

    • 使用 igraph_add_edges 函数,将 sourcestargets 向量作为参数,将边添加到图中。
  5. 设置边的权重:

    • 使用 SETEANV 函数设置边的权重,将 weights 向量作为参数。
  6. 设置节点名称:

    • 使用 SETVAS 函数设置节点名称,将 node_names 向量作为参数。
  7. 写入 GraphML 文件:

    • 使用 igraph_write_graph_graphml 函数将图对象写入 GraphML 文件。

总结

通过上述步骤,您可以在 igraph 中使用数据的第一列作为源节点,第二列作为目标节点来创建一个图。请根据您的实际需要进行修改和调整。

igraph 图对象添加功能:使用第一列作为源节点,第二列作为目标节点

原文地址: https://www.cveoy.top/t/topic/fxOg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录