尋找強連通分支C語言

來源:互聯網
上載者:User

  這東西,寫了8小時.比較慢了.實現是一步一步地完成的.方法概述如下:

  1.對原圖進行一次深度優先搜尋,以後序對各頂點標號.

  2.將原圖反轉.

  3.在新圖中,按照頂點標號從大到小的順序對所有頂點逐個檢查,並進行深度優先搜尋.每一次調用深度優先搜尋之後,下次調用時到調用結束階段,聲場的深度優先搜尋樹中的所有節點是強聯通的.

  這個的原理書上說得我看不懂.我理解就是,新圖中能夠訪問到的,在原圖中以反向也能夠訪問到.於是所構成的集合是強連通的.至於使用後序標號,是保證能夠按照將圖反轉前的訪問方向的反向訪問反轉後的圖.

   貼代碼吧,能夠寫出來,我很高興了.

/*9-27-12-23-22.26.c -- 第九章第二十七題*/<br />#include <stdio.h><br />#include "new_adjacenty_list.h"</p><p>#define SIZE (7)</p><p>int visited[SIZE], postorder[SIZE] ;<br />int count = 0, kind = 1 ;</p><p>int mian (void) ;<br />void initialize_graph (Adjacenty_List * const padj, Hash_Table * const pht) ;<br />void initialize_array (int * const array, const int size, const int value) ;<br />void search_all (const Adjacenty_List * const padj, const Hash_Table * const pht) ;<br />void depth_first_search (const Adjacenty_List * const padj, const Hash_Table * const pht, const int index) ;<br />void depth_first_search_without_number (const Adjacenty_List * const padj, const Hash_Table * const pht, const int index) ;<br />void invert_graph (Adjacenty_List * const padj, const Hash_Table * const pht) ;<br />void find_connected (const Adjacenty_List * const padj, const Hash_Table * const pht) ;<br />void print_result (const Adjacenty_List * const padj, const Hash_Table * const pht) ;</p><p>int main (void)<br />{<br />Adjacenty_List adj ;<br />Hash_Table ht ;<br />int capacity = SIZE ;</p><p>Initialize_A (&adj, capacity) ;<br />Initialize_H (&ht, capacity * 2) ;</p><p>initialize_array (visited, capacity, FALSE) ;<br />/*Initialize the graph*/<br />initialize_graph (&adj, &ht) ;<br />/*Reverse the graph*/<br />Reverse_A (&adj, &ht) ;<br />/*Number it*/<br />search_all (&adj, &ht) ;<br />initialize_array (visited, capacity, FALSE) ;<br />find_connected (&adj, &ht) ;<br />print_result (&adj, &ht) ;</p><p>Release_A (&adj) ;<br />Release_H (&ht) ;</p><p>return 0 ;<br />}</p><p>void initialize_graph (Adjacenty_List * const padj, Hash_Table * const pht)<br />{<br />InitializeALine_A (padj, pht, 0, 'a', 0, 4, 'b', 0, 'c', 0) ;<br />InitializeALine_A (padj, pht, 1, 'b', 0, 6, 'c', 0, 'e', 0, 'g', 0) ;<br />InitializeALine_A (padj, pht, 2, 'c', 0, 4, 'd', 0, 'e', 0) ;<br />InitializeALine_A (padj, pht, 3, 'd', 0, 4, 'a', 0, 'f', 0) ;<br />InitializeALine_A (padj, pht, 4, 'e', 0, 4, 'd', 0, 'f', 0) ;<br />InitializeALine_A (padj, pht, 5, 'f', 0, 0) ;<br />InitializeALine_A (padj, pht, 6, 'g', 0, 2, 'e', 0) ;<br />}</p><p>void initialize_array (int * const array, const int size, const int value)<br />{<br />int i ;</p><p>for (i = 0; i < size; i++)<br />array[i] = value ;<br />}</p><p>void search_all (const Adjacenty_List * const padj, const Hash_Table * const pht)<br />{<br />int i, size ;</p><p>for (i = 0, size = (*padj) -> capacity; i < size; i++)<br />{<br />if (FALSE == visited[i])<br />depth_first_search (padj, pht, i) ;<br />}<br />}</p><p>void depth_first_search (const Adjacenty_List * const padj, const Hash_Table * const pht, const int index)<br />{<br />Adjoin_To_Vertex * scan ;<br />int w ;</p><p>visited[index] = TRUE ;<br />scan = (*padj) -> list[index].adjoin_to ;<br />while (scan)<br />{<br />w = (*pht) -> lists[scan -> hash_value].index_in_adjacenty_list ;<br />if (FALSE == visited[w])<br />depth_first_search (padj, pht, w) ;<br />scan = scan -> next ;<br />}<br />postorder[count++] = (*padj) -> list[index].hash_value ;<br />}</p><p>void depth_first_search_without_number (const Adjacenty_List * const padj, const Hash_Table * const pht, const int index)<br />{<br />Adjoin_To_Vertex * scan ;<br />int w ;</p><p>visited[index] = kind ;<br />scan = (*padj) -> list[index].adjoin_to ;<br />while (scan)<br />{<br />w = (*pht) -> lists[scan -> hash_value].index_in_adjacenty_list ;<br />if (FALSE == visited[w])<br />depth_first_search_without_number (padj, pht, w) ;<br />scan = scan -> next ;<br />}<br />}</p><p>void find_connected (const Adjacenty_List * const padj, const Hash_Table * const pht)<br />{<br />int i, size ;</p><p>/*The first element in the array is visited in the end*/<br />for (i = 0, size = (*padj) -> capacity; i < size; i++)<br />{<br />if (FALSE == visited[(*pht) -> lists[postorder[i]].index_in_adjacenty_list])<br />{<br />depth_first_search_without_number (padj, pht, (*pht) -> lists[postorder[i]].index_in_adjacenty_list) ;<br />kind++ ;<br />}<br />}<br />}</p><p>void print_result (const Adjacenty_List * const padj, const Hash_Table * const pht)<br />{<br />int i, size ;</p><p>for (i = 0, size = (*padj) -> capacity; i < size; i++)<br />printf ("%c is in gather %d./n", (*pht) -> lists[(*padj) -> list[i].hash_value].name, visited[i]) ;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.