数据结构与算法 DFS-BFS遍历&MST

DFS(深度优先) 时间复杂性:采用邻接矩阵存储结构时,查找所有顶点的邻接点所需时间为O(|V|2);
而采用邻接表时,找邻接点所需时间为O(|E|)。因此,DFS的时间复杂度为O(|V|+|E|) 。
BFS和DFS一样;
以邻接矩阵为存储结构的Prim算法中,时间复杂度为O(|V|2);
邻接表作为存储结构,Prim时间复杂度改进为O(|E|log|V|)。
Kruskal算法总体时间复杂度为O( |E|·log|V| )
1-1
Prim’s algorithm is to maintain a forest and to merge two trees into one at each stage. F
1-2
Kruskal’s algorithm is to maintain a forest and to merge two trees into one at each stage. T
1-3
Kruskal’s algorithm is to grow the minimum spanning tree by adding one edge, and thus an associated vertex, to the tree in each stage. F
Kruskal(应为Prim) 的算法是通过在每个阶段向树添加一条边并因此添加一个关联的顶点来生长最小生成树。
1-4
The minimum spanning tree of a connected weighted graph with vertex set V={ A, B, C, D, E } and weight set W={ 1, 3, 2, 5, 2, 7, 9, 8, 4 } must be unique. F
1-5
If graph G is a connected graph, the spanning tree of G is a maximal connected subgraph containing all n vertices of G. F
1-6
If graph G is a connected graph, then G must have a unique minimum spanning tree. F
2-1
Which of the following is true? A
A.Prim’s algorithm initialises(初始化) with a vertex(顶点)
B.Prim’s algorithm initialises with a edge(边)
C.Prim’s algorithm initialises with a vertex which has smallest edge
D.Prim’s algorithm initialises with a forest
2-2
Consider the following statements.
S1. Kruskal’s algorithm might produce a non-minimal spanning tree.
S2. Kruskal’s algorithm can efficiently implemented using the disjoint-set data structure. D
(Kruskal 算法可以使用不相交集数据结构有效地实现)
A.S1 is true but S2 is false
B.Both S1 and S2 are false
C.Both S1 and S2 are true
D.S2 is true but S1 is false
2-5
The minimum spanning tree of any connected weighted graph ____ B
A.must be unique
B.must have a unique minimum weight
C.must not be unique
D.may not exist

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以邻接矩阵为存储结构的图,可以通过遍历矩阵中的元素来实现 DFSBFS 遍历。其中,矩阵中的每个元素表示对应两个顶点之间的边,值为 1 表示存在边,值为 0 表示不存在边。 DFS 遍历: 1. 从起点开始,将起点标记为已访问。 2. 遍历当前节点的所有邻居节点,如果邻居节点未访问,则递归访问邻居节点。 3. 遍历完所有邻居节点后,回溯到上一层节点,继续遍历其未访问过的邻居节点。 4. 重复步骤 2 和 3,直到遍历完所有节点。 具体实现可以参考以下代码: ```python def dfs(graph, start, visited): visited[start] = True print(start, end=' ') for i in range(len(graph)): if graph[start][i] == 1 and not visited[i]: dfs(graph, i, visited) # 示例代码: graph = [[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]] visited = [False] * len(graph) dfs(graph, 0, visited) ``` BFS 遍历: 1. 将起点加入队列中,并标记为已访问。 2. 队列不为空时,弹出队首元素,遍历其所有邻居节点,如果邻居节点未访问,则将其加入队列中,并标记为已访问。 3. 重复步骤 2,直到队列为空。 具体实现可以参考以下代码: ```python from collections import deque def bfs(graph, start, visited): queue = deque([start]) visited[start] = True while queue: node = queue.popleft() print(node, end=' ') for i in range(len(graph)): if graph[node][i] == 1 and not visited[i]: queue.append(i) visited[i] = True # 示例代码: graph = [[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]] visited = [False] * len(graph) bfs(graph, 0, visited) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值