在這篇文章中,你可以找到一個鄰接矩陣:
Breadth and depth first search - part 3
如何實現的呢?
// Adjacency matrix
int map[21][21] = {
/* A B C D E F G H I L M N O P R S T U V Z */
{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1}, // Arad
{2,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,1,0,0}, // Bucharest
{3,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0}, // Craiova
{4,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, // Dobreta
{5,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0}, // Eforie
{6,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, // Fagaras
{7,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // Girgiu
{8,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0}, // Hirsova
{9,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0}, // Iasi
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0}, // Lugoj
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0}, // Mehadia
{2,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, // Neamt
{3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1}, // Oradea
{4,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}, // Pitesti
{5,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0}, // Rimnicu Vilcea
{6,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0}, // Sibiu
{7,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0}, // Timisoara
{8,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0}, // Urziceni
{9,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0}, // Vaslui
{0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0} // Zerind
};
請注意,第一條註釋行代表每個城市名稱的首字母。用鄰接矩陣完成的映射是指這些字母,以便更容易理解。例如,獲得引用阿拉德的鄰接矩陣的第一個條目:我們有阿拉德的路徑將我們引向錫比烏,蒂米什瓦拉和澤林德,因此我們在代表這些城市的列上設置了1的值,在這種情況下,字母S,T和Z下面的列。這就是映射完成的方式。我們在其他列上設置了0的值,表示沒有路徑將我們引導到這些城市。
給出你的圖,重複它的邊並創建你的鄰接矩陣。
謝謝你的幫助,我只是設法讓它工作.. – Ali 2010-03-09 16:14:32
不客氣! – 2010-03-09 20:07:24