圖的領接矩陣和深度遍歷
阿新 • • 發佈:2019-01-24
#include"fstream" #include"iostream" using namespace std; const int MaxInt = 32767;//表示極大值 const int MVNum = 100;//最大頂點數 typedef int Status; bool visited[MVNum]; int i, j; typedef struct { <span style="white-space:pre"> </span>char vexs[MVNum]; //建立頂點表 <span style="white-space:pre"> </span>int arcs[MVNum][MVNum];//領接矩陣 <span style="white-space:pre"> </span>int vexnum, arcnum; //圖的當前點數和邊數 }AMGraph; int LocateVex(AMGraph G, char v)//圖的基本操作,尋找V的位置 { <span style="white-space:pre"> </span>int i = 0; <span style="white-space:pre"> </span>while (i < G.vexnum && v != G.vexs[i]) <span style="white-space:pre"> </span>i++; <span style="white-space:pre"> </span>return i; } int CreateUDN(AMGraph &G) { <span style="white-space:pre"> </span>char v1, v2; int w; <span style="white-space:pre"> </span>fstream in; <span style="white-space:pre"> </span>in.open("data.txt", ios::in); <span style="white-space:pre"> </span>in >> G.vexnum; <span style="white-space:pre"> </span>in >> G.arcnum; <span style="white-space:pre"> </span>for (i = 0; i < G.vexnum; i++)//輸入頂點表 <span style="white-space:pre"> </span>in >> G.vexs[i];//頂點表 <span style="white-space:pre"> </span>for (i = 0; i < G.vexnum; i++) <span style="white-space:pre"> </span>for (j = 0; j < G.vexnum; j++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>G.arcs[i][j] = MaxInt; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>for (int k = 0; k < G.arcnum; k++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//cout << "請輸入該弧所依附的兩個頂點和權值" << endl; <span style="white-space:pre"> </span>in >> v1 >> v2 >> w; <span style="white-space:pre"> </span>i = LocateVex(G, v1); <span style="white-space:pre"> </span>j = LocateVex(G, v2); <span style="white-space:pre"> </span>G.arcs[i][j] = w; <span style="white-space:pre"> </span>G.arcs[j][i] = G.arcs[i][j]; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>in.close(); <span style="white-space:pre"> </span>//return 0; <span style="white-space:pre"> </span>for (int n = 0; n < G.vexnum; n++) <span style="white-space:pre"> </span>cout << G.vexs[n]; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout << endl; <span style="white-space:pre"> </span>cout << endl; <span style="white-space:pre"> </span>cout << "該圖的領接矩陣為" << endl; <span style="white-space:pre"> </span>for (int x = 0; x < G.vexnum; x++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>for (int y = 0; y < G.vexnum; y++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>if (G.arcs[x][y] != MaxInt) <span style="white-space:pre"> </span>cout << G.arcs[x][y] << " "; <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>cout << " "; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>cout << endl; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return 0; }<span style="white-space:pre"> </span> void DFSM(AMGraph &G, int i) { <span style="white-space:pre"> </span>cout << "深度優先遍歷結點:" << G.vexs[i] << endl;//訪問頂vi <span style="white-space:pre"> </span>visited[i] = true; <span style="white-space:pre"> </span>for (int j = 0; j < G.vexnum; j++) //依次搜尋vi鄰接點 <span style="white-space:pre"> </span>if (!visited[j] && G.arcs[i][j] < MaxInt) <span style="white-space:pre"> </span>DFSM(G, j); } void DFSTraverseM(AMGraph &G) { <span style="white-space:pre"> </span>int i; <span style="white-space:pre"> </span>for (i = 0; i < G.vexnum; i++) <span style="white-space:pre"> </span>visited[i] = false; <span style="white-space:pre"> </span>for (i = 0; i < G.vexnum; i++) <span style="white-space:pre"> </span>if (!visited[i]) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>DFSM(G, i); <span style="white-space:pre"> </span>} } int main() { <span style="white-space:pre"> </span>AMGraph G; <span style="white-space:pre"> </span>CreateUDN(G); <span style="white-space:pre"> </span>DFSTraverseM(G);<span style="white-space:pre"> </span>//不連通 <span style="white-space:pre"> </span>return 0; }