图的领接矩阵和深度遍历
来源:互联网 发布:iphone软件无法联网 编辑:程序博客网 时间:2024/05/01 21:28
#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;}
0 0
- 图的领接矩阵和深度遍历
- 连通图的(领接矩阵储存)创建和遍历(包含BFS,DFS两种算法)
- 矩阵版本图的深度优先和广度优先遍历
- 图(领接矩阵和vector)的dfs和bfs
- 图的深度遍历和广度遍历
- 图的广度遍历和深度遍历
- 图的深度遍历和广度遍历
- 图的深度遍历和广度遍历
- 图的深度遍历和广度遍历
- 图的遍历(深度遍历和广度遍历)
- 图的遍历:深度优先遍历和广度优先遍历
- 各种图的创建以及广度,深度优先遍历(临接矩阵存储)
- C语言实现图的领接矩阵表示6.1.1
- 图的遍历(广度和深度)
- 图的广度和深度优先遍历
- 图的深度和广度遍历(邻接矩阵)
- 图的深度和广度优先遍历
- 图的深度和广度遍历
- fdisk运用
- 微服务实战(一):微服务架构的优势与不足
- 单例模式的七种写法
- 更改browser背景色-保护眼睛
- 常规功能和模块自定义系统 (cfcmms)—042模块Grid的排序和筛选
- 图的领接矩阵和深度遍历
- http://blog.csdn.net/ta893115871/article/details/46955791/
- RecyclerView 的 RecyclerView.Adapter 通用版的实现
- HTTP状态码大全
- ubuntu12.04借助wine安装国际版qq(亲测成功版)
- SSL证书(HTTPS)背后的加密算法
- iOS 工程瘦身
- 递归和迭代
- 英语词根词缀记忆法