Algorithm 高效的图整形
我有一个Algorithm 高效的图整形,algorithm,graph,Algorithm,Graph,我有一个有向无环图,它的顶点最多有2条传入边。顶点分为级别,通过这种方式计算:没有传入边的顶点的级别为0,不属于级别0的顶点的级别v可以计算为max{level(u)+1},对于属于图形的每个边(u,v) 我需要重新塑造图形,使每个级别的顶点(“宽度”)不超过一组k。在大多数情况下,k必须是1024 在这种情况下,必须对图形进行整形,使其具有最大水平宽度2。红色节点从级别0移动到级别1,导致级别1宽度变为3,因此需要再次向上移动 现在,我正在使用对Bellman Ford算法的轻微修改来计算图中
有向无环图
,它的顶点最多有2条
传入边。顶点分为级别,通过这种方式计算:没有传入边的顶点的级别为0
,不属于级别0
的顶点的级别v
可以计算为max{level(u)+1}
,对于属于图形的每个边(u,v)
我需要重新塑造图形,使每个级别的顶点(“宽度”)不超过一组k
。在大多数情况下,k
必须是1024
在这种情况下,必须对图形进行整形,使其具有最大水平宽度2
。红色节点从级别0
移动到级别1
,导致级别1
宽度变为3
,因此需要再次向上移动
现在,我正在使用对Bellman Ford算法的轻微修改来计算图中每个节点的级别:我添加一个连接到所有级别0
节点(已知)的“源”节点,并运行该算法,选择最大距离而不是最小距离
为了重塑图形,我将顶点按级别划分,并从级别0
开始,向上移动超过的顶点。在每次移动之后,我需要更新级别分区,因为所有连接到移动顶点的子图都会随之上移。对于我正在处理的图形,这种方法被证明是低效的,因为我需要重复地向上移动元素,导致贝尔曼福特的多次运行造成了瓶颈
以下是我的重塑功能的代码:
void Graph::Reshape(int width) // Width is the maximum width of any level
{
// Run Bellman-Ford to create the level partitioning in m_levels
Levelize();
int currentLevel = 0;
do
{
int levelWidth = m_levels[currentLevel].size();
int widthDifference = levelWidth - width;
if (widthDifference > 0)
{
for (int i = 0; i < widthDifference; i++)
{
// Get the first vertex in the current level to move it up
Vertex v = m_levels[currentLevel].front();
// Add a vertex f to the graph, between v and all the vertices
// directly connected to v (resulting in v moving 1 level higher)
Vertex f = boost::add_vertex(m_graph);
auto inEdges = boost::in_edges(v, m_graph);
for (auto it = inEdges.first; it != inEdges.second; it++)
{
Vertex s = boost::source(*it, m_graph);
boost::add_edge(s, f, m_graph);
}
boost::add_edge(f, v, m_graph);
}
// Run Bellman-Ford again to update the level partitioning
Levelize();
}
} while (++currentLevel < m_levels.size());
}
void图形::重塑(int-width)//宽度是任何级别的最大宽度
{
//运行Bellman Ford以创建m_级别中的级别分区
水平化();
int currentLevel=0;
做
{
int levelWidth=m_levels[currentLevel].size();
int widthDifference=levelWidth-宽度;
如果(宽度差>0)
{
对于(int i=0;i
有没有什么聪明的方法可以加快速度