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

Maintaining GraphAttributes with GraphCopy::initByCC in OGDF

本文关键字:initByCC 维护 GraphAttributes GraphCopy OGDF      更新时间:2023-10-16

我正在尝试使用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类来查找图的连通子图。由于我想独立处理这些子图,所以我使用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()));