转置邻接表表示的无向、有向图.没有给出头文件,因为考虑到最近的头文件经常要append一些新东西,所以现在不给出来了.
实现的时候用了位操作的方法,目的是标记.避免了V^2的空间浪费.当然了,只能应对整型的数据了.其实对浮点型应该也是可以的,但是位操作就不是这样的了.也是可以实现的.
代码很简单,贴出来.
void GraphRepresentAsAdjacentMatrix ::transpose (void)
{
if (m_currentSize < m_size)
{
std ::cerr << m_size - m_currentSize << "Vertex(es) has(have) not been input yet, can't transpose the graph." << std ::endl ;
return ;
}
// Using a skill to transpose the matrix.
// The skill is set bit mode to target new edge and old edge.
for (int i = 0; i < m_size; ++i)
{
for (int j = 0; j < m_size; ++j)
{
if (m_matrix[i][j] != NotAdjoinTo)
{
// Judging if m_matrix[i][j] is an old edge.
// New edge will be processed next because of that can process all new edge.
if ((m_matrix[i][j] & 0x70000000) != 0x70000000)
{
// m_matrix[i][j] is an old edge.
m_matrix[j][i] = m_matrix[i][j] | 0x70000000 ;
m_matrix[i][j] = NotAdjoinTo ;
}
}
}
}
for (int i = 0; i < m_size; ++i)
{
for (int j = 0; j < m_size; ++j)
{
if (m_matrix[i][j] != NotAdjoinTo && (m_matrix[i][j] & 0x70000000) == 0x70000000)
{
m_matrix[i][j] = m_matrix[i][j] & 0x8FFFFFFF ;
}
}
}
}