图的领接矩阵和深度遍历

本文介绍了如何使用C++实现图的邻接矩阵存储,并提供了深度优先遍历的算法。通过读取文件"data.txt"来构建无向图,然后展示领接矩阵并进行深度优先遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >





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







                
### 邻接矩阵存储并进行深度优先遍历 #### 算法描述 深度优先遍历(Depth First Search, DFS)是一种用于遍历或搜索树或的算法。它从给定的起始节点开始,沿着一条路径尽可能深入地探索,直到无法继续为止,然后回溯到最近的一个未完全访问的节点,重复这一过程。 对于基于邻接矩阵表示的,可以通过递归的方式实现DFS。以下是具体实现方法: 1. 使用布尔数组 `visited` 来记录每个顶点是否已被访问过。 2. 对于每一个尚未被访问过的顶点,调用递归函数对其进行访问,并标记为已访问。 3. 在递归过程中,依次检查当前顶点与其他顶点之间的连接关系,若存在边且目标顶点未被访问,则对该顶点执行相同的递归操作。 --- #### C++ 实现代码 以下是一个完整的C++程序,展示了如何使用邻接矩阵来存储以及实现深度优先遍历的功能。 ```cpp #include <iostream> #include <string> using namespace std; #define MAX_VERTICES 100 // 最大顶点数 // 定义无向的邻接矩阵结构体 struct Graph { string vertices[MAX_VERTICES]; // 存储顶点名称 int edges[MAX_VERTICES][MAX_VERTICES]; // 邻接矩阵 int vertexCount; // 当前顶点数量 }; // 初始化 void initializeGraph(Graph &graph) { graph.vertexCount = 0; for (int i = 0; i < MAX_VERTICES; ++i) { for (int j = 0; j < MAX_VERTICES; ++j) { graph.edges[i][j] = 0; // 初始时所有边权重设为0 } } } // 添加顶点 bool addVertex(Graph &graph, const string &vertexName) { if (graph.vertexCount >= MAX_VERTICES) return false; // 超过最大容量则返回失败 graph.vertices[graph.vertexCount++] = vertexName; return true; } // 添加边 bool addEdge(Graph &graph, int start, int end) { if (start < 0 || start >= graph.vertexCount || end < 0 || end >= graph.vertexCount) return false; graph.edges[start][end] = 1; // 设置有边相连 graph.edges[end][start] = 1; // 如果是有向可以去掉这一步 return true; } // 深度优先遍历辅助函数(递归) void dfsHelper(const Graph &graph, bool visited[], int currentVertex) { cout << graph.vertices[currentVertex] << " "; // 访问当前顶点 visited[currentVertex] = true; // 标记为已访问 // 遍历其他顶点 for (int nextVertex = 0; nextVertex < graph.vertexCount; ++nextVertex) { if (!visited[nextVertex] && graph.edges[currentVertex][nextVertex]) { dfsHelper(graph, visited, nextVertex); // 继续递归访问相邻顶点 } } } // 主函数启动深度优先遍历 void depthFirstSearch(const Graph &graph, int startIndex) { bool visited[MAX_VERTICES] = {false}; // 创建一个布尔数组初始化为false dfsHelper(graph, visited, startIndex); } // 测试主函数 int main() { Graph g; initializeGraph(g); // 添加顶点 addVertex(g, "A"); addVertex(g, "B"); addVertex(g, "C"); addVertex(g, "D"); // 添加边 addEdge(g, 0, 1); // A-B addEdge(g, 0, 2); // A-C addEdge(g, 1, 3); // B-D cout << "深度优先遍历结果:" << endl; depthFirstSearch(g, 0); // 从顶点"A"开始遍历 cout << endl; return 0; } ``` --- #### 关键说明 1. **邻接矩阵构建** 邻接矩阵通过二维数组 `edges` 表示中的边关系。如果两个顶点之间存在边,则对应的矩阵位置值为1;否则为0[^2]。 2. **递归逻辑** 在递归过程中,每次都会尝试访问与当前顶点直接相连的所有未访问顶点。这种策略确保了能够按照深度方向逐步扩展遍历范围[^1]。 3. **时间复杂度分析** 假设中有 \( V \) 个顶点 \( E \) 条边,在最坏情况下,需要检查所有的顶点及其可能的连接情况,因此总的时间复杂度为 \( O(V^2) \)[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值