博客
关于我
【Lintcode】291. Second Diameter
阅读量:188 次
发布时间:2019-02-28

本文共 2355 字,大约阅读时间需要 7 分钟。

一棵树型无向带权图的次大直径是指所有两点间距离中的第二长,包括相同长度的路径。为了求解次大直径,我们可以参考以下步骤:

1. 求直径的两个端点

首先,使用广度优先搜索(BFS)从任意节点出发,找到最远的节点u。接着,再次从u出发,进行BFS找到最远的节点v。这样,u和v就是直径的两个端点。

2. 计算次大直径

从u出发,排除v,进行BFS,找到最远的节点a。同样地,从v出发,排除u,进行BFS,找到最远的节点b。次大直径即为a到u的距离与b到v的距离中的较大者。

3. 算法正确性

根据树的性质,任何最长路径都必然连接直径的两个端点。因此,从任意节点出发的最远点必定是直径的一个端点。

4. 代码实现

import java.util.*;public class Solution {    private long res;    public long getSecondDiameter(int[][] edge) {        int n = edge.length + 1;        Map
> graph = buildGraph(edge); int far1 = bfs(0, graph, -1, new boolean[n]); int far2 = bfs(far1, graph, -1, new boolean[n]); bfs(far1, graph, far2, new boolean[n]); bfs(far2, graph, far1, new boolean[n]); return res; } private int bfs(int v, Map
> graph, int u, boolean[] visited) { Queue
queue = new LinkedList<>(); queue.offer(new long[]{v, 0}); visited[v] = true; int farV = v; long farDis = 0; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { long[] cur = queue.poll(); int nextV = (int) cur[0]; if (graph.containsKey(nextV)) { for (int[] next : graph.get(nextV)) { if (next[0] != u && !visited[next[0]]) { if (next[1] + cur[1] > farDis) { farDis = next[1] + cur[1]; farV = next[0]; } visited[next[0]] = true; queue.offer(new long[]{next[0], next[1] + cur[1]}); } } } } } if (u != -1) { res = Math.max(res, farDis); } return farV; } private Map
> buildGraph(int[][] edges) { Map
> graph = new HashMap<>(); for (int[] edge : edges) { graph.putIfAbsent(edge[0], new ArrayList<>()); graph.get(edge[0]).add(new int[]{edge[1], edge[2]}); graph.putIfAbsent(edge[1], new ArrayList<>()); graph.get(edge[1]).add(new int[]{edge[0], edge[2]}); } return graph; }}

5. 时空复杂度

  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

该算法通过两次BFS找到直径,再从直径端点出发计算次大直径,确保了在树结构下的高效计算。

转载地址:http://adjs.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>