在 OGDF 中使用 GraphCopy::initByCC 维护 GraphAttributes

Maintaining GraphAttributes with GraphCopy::initByCC in OGDF

我正在尝试使用 OGDF 对从 GML 文件加载的图形执行一些处理。只有维护节点标签,这些图才有意义。不幸的是,OGDF 并没有让像标签这样的节点属性变得容易,因为它们是在一个名为 GraphAttributes 的单独数据结构中维护的。我的问题是 GraphAttributes 将节点标签与节点 索引 相关联,这些标签不是由我需要使用的一些图形转换维护的。

我需要对图执行的转换之一是在 GML 文件中拆分每个连接的子图。加载图形及其节点标签很简单:

ogdf::Graph graph;
ogdf::GraphAttributes attributes(graph, ogdf::GraphAttributes::nodeLabel);
ogdf::GraphIO::readGML(attributes, graph, FILENAME);

// this gives the correct label of the first node in the graph
attributes.label(graph.firstNode());

类似地,OGDF 提供CCsInfo class 来查找图的连通子图。因为,我想独立使用这些子图,所以我使用 GraphCopy::initByCC 方法创建单独的 Graph 个实例。

ogdf::CCsInfo info(graph);
ogdf::GraphCopy copy(graph);
ogdf::EdgeArray< ogdf::edge > edgeArray(graph);
// where i (int) is the number of the connected subgraph to copy
copy.initByCC(info, i, edgeArray);

// this now gives the wrong label for the first node in copy
attributes.label(copy.firstNode());

这行得通,copy只包含连通子图的节点和边。但是,副本中节点的索引与原始图中节点的索引不同。这意味着标签到 attributes 对象中节点的映射不适用于 copy.

中的节点

有没有办法对 attributes 对象执行相同的转换,以便我可以为复制的连接子图中的节点获得正确的标签?

原来这并没有我想的那么难。我遗漏的关键部分是您可以使用 GraphCopy::original 方法从原始图中获取具有索引的节点,然后使用该节点获取标签。

// get the correct label for the first node of the GraphCopy object copy
attributes.label(copy.original(copy.firstNode()));