布图
布图表示有三种:
- 如何将一个布图转化为一个约束图对
- 如何将一个布图转化为一个序列对
- 如何将一个序列对转化为一个布图
PS:现代的序列对算法不需要直接计算约束图,但约束图提供了一个有用的布图和序列对之间的中间表示,这样从概念上会更易于理解。
从布图到一个约束图对
- 对于每个约束图,为n个块中的一个mi创建一个块节点vi, 1<= i <= n,一个源点s和一个汇点t;
- 对于垂直(水平)约束图,如果mi在mj下面(左边),则增加一条有向边(vi,vj);
- 对于每个约束图,删除所有不能通过传递到达的边。
从布图到一个序列对
- 给定两个块a和b,在序列对中,如果在S+中,a在b前面,即<…a…b…>,且在S-中,a也在b之前,即<…a…b…>,则a在b左边;
- 如果在S+中,a在b前面,即<…a…b…>,且在S-中,a在b之后,即<…b…a…>,则a在b上边;
- 理论上,先生成约束图,然后才用块的排序规则生成序列对,但是,约束图并不需要明确地建立起来。
从序列对到一个布图
必要信息
- 布图的起始点;
- 每个块的宽和高;
- 布图的布局方向。
流程图
C++编码源码
#include <iostream>
using namespace std;
const int BLOCKS = 5;
const char s1[] = { 'a', 'c', 'd', 'b', 'e' };
const char s2[] = { 'c', 'd', 'a', 'e', 'b' };
const char s3[] = { 'a', 'b', 'c', 'd', 'e' };
void xPosition(int Lengths[], int Width[], int positionX[])
{
int index_1, index_2, index;
int blocksOrder[BLOCKS] = { 0 };
for (index_1 = 0; index_1 < BLOCKS; ++index_1)
{
for (index_2 = 0; index_2 < BLOCKS; ++index_2)
{
if (s3[index_1] == s2[index_2])
{
blocksOrder[index_1] = index_2;
break;
}
}
}
for (index_1 = 0; index_1 < BLOCKS; ++index_1)
{
for (index = 0; index < BLOCKS; ++index)
{
if (s1[index_1] == s3[index])
break;
}
positionX[index] = Lengths[blocksOrder[index]];
for (index_2 = blocksOrder[index]; index_2 < BLOCKS; ++index_2)
{
if (Lengths[index_2] < (positionX[index] + Width[index]))
Lengths[index_2] = positionX[index] + Width[index];
}
}
}
void yPosition(int Lengths[], int height[], int positionY[])
{
int index_1, index_2, index, s1_R[BLOCKS];
int blocksOrder[BLOCKS] = { 0 };
for (index = 0; index < BLOCKS; ++index)
{
s1_R[BLOCKS - index - 1] = s1[index];
}
for (index_1 = 0; index_1 < BLOCKS; ++index_1)
{
for (index_2 = 0; index_2 < BLOCKS; ++index_2)
{
if (s3[index_1] == s2[index_2])
{
blocksOrder[index_1] = index_2;
break;
}
}
}
for (index_1 = 0; index_1 < BLOCKS; ++index_1)
{
for (index = 0; index < BLOCKS; ++index)
{
if (s1_R[index_1] == s3[index])
break;
}
positionY[index] = Lengths[blocksOrder[index]];
for (index_2 = blocksOrder[index]; index_2 < BLOCKS; ++index_2)
{
Lengths[index_2] = positionY[index] + height[index];
}
}
}
int main()
{
int widthAndHeight[2 * BLOCKS] = {8,4,4,3,4,5,4,5,4,6};
int width[BLOCKS], height[BLOCKS];
int index;
for (index = 0; index < BLOCKS; ++index)
{
width[index] = widthAndHeight[2 * index];
height[index] = widthAndHeight[2 * index + 1];
}
int xLengths[BLOCKS] = { 0, 0, 0, 0, 0 };
int yLengths[BLOCKS] = { 0, 0, 0, 0, 0 };
int positonX[BLOCKS];
int positonY[BLOCKS];
xPosition(xLengths, width, positonX);
yPosition(yLengths, height, positonY);
for (index = 0; index < BLOCKS; ++index)
{
cout << s3[index] << "( " << positonX[index] << " , " << positonY[index] << " )" << endl;
}
return 0;
}