图的领接矩阵和深度遍历





#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;
}




    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/Vw__c/article/details/51392675
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞