【发布时间】:2020-12-02 19:16:48
【问题描述】:
假设我的邻接列表如下:
0 : 1
1 : 0
它的反向应该是空的,因为初始列表中有两条弧:
0:
1:
或
0 : 1
1 :
变成
0 :
1 : 0
另一个例子:
0 :
1 :
变成
0 : 1
1 : 0
最后一个例子
0 : 1, 4
1 : 0, 4
2 : 0, 1, 3, 4
3 :
4 : 3, 1
变成
0 : 2, 3
1 : 2, 3
2 :
3 : 0, 1, 2, 4
4 : 0, 2
有没有算法可以做到这一点?
Graphe *Graphe::grapheInverse( void ){
Graphe *r = new Graphe (_adjacences.size() );
for (unsigned i = 0; i < _adjacences.size(); i++)
for ( unsigned j = 0; j < _adjacences[i]->size(); j++ )
if ( (*_adjacences[i])[j] == 1 ) // or 0 or 2 or 3 or 4 or ... like last example
//r->addArcs( i, (*_adjacences[i])[j] ); //adds an arc from A to B
return r;
}
【问题讨论】:
-
这与your last question有何不同?
-
这个例子更多,代码更少
-
您没有通过反转图表清楚地说明您的意思。当然它不符合例如algorithms.tutorialhorizon.com/reverse-the-directed-graph 。从示例中,您似乎希望原始图中不存在所有可能的弧,并且不包括自指向的“循环”弧。
-
@RalfKleberhoff 是的,初始图中可用的内容将被删除,而反向图中不可用的内容将可用
标签: c++ algorithm graph adjacency-list