演算法導論22.1-3

來源:互聯網
上載者:User

  轉置鄰接表表示的無向、有向圖.沒有給出標頭檔,因為考慮到最近的標頭檔經常要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 ;}}}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.