0

0

双向路径搜索算法的Java实现及路径构建详解

聖光之護

聖光之護

发布时间:2025-10-12 13:50:17

|

925人浏览过

|

来源于php中文网

原创

双向路径搜索算法的java实现及路径构建详解

本文旨在帮助开发者理解和实现双向路径搜索算法。通过分析常见的实现错误,并提供改进方案,本文将详细介绍如何使用Java构建高效的双向搜索树,并从搜索树中正确提取完整的路径信息,最终实现从起点到终点的完整路径搜索。

理解双向路径搜索

双向路径搜索是一种在图中寻找从起点到终点路径的优化算法。它同时从起点和终点开始搜索,当两个搜索方向相遇时,就找到了连接起点和终点的路径。相比于单向搜索,双向搜索通常能够更快地找到目标路径,尤其是在搜索空间较大的情况下。

常见的实现错误

原始代码中存在一些关键问题,导致无法正确构建和使用双向搜索树:

  1. 共享搜索树: 使用单一的 searchTreeParentByChild 存储两个方向的搜索结果是不正确的。因为两个方向的路径是从不同的起点构建,并且方向相反。使用同一个树结构无法区分路径的方向。
  2. 路径构建方向错误: searchTreeParentByChild 只能从子节点追溯到父节点,而无法反向查找。这导致无法从相遇点正确构建从起点到终点的完整路径。
  3. containsValue 的错误使用: 在 curEnd 的循环中,使用 searchTreeParentByChild.containsValue(e.to()) 来判断顶点是否被访问过是错误的。containsValue 用于检查 Map 中是否存在特定的值,而不是键。正确的做法是使用 containsKey(e.to())。

改进的实现方案

为了解决上述问题,我们需要进行以下改进:

立即学习Java免费学习笔记(深入)”;

天工大模型
天工大模型

中国首个对标ChatGPT的双千亿级大语言模型

下载
  1. 使用两个独立的搜索树: 为从起点和终点开始的搜索分别创建 searchTreeParentByChildFromStart 和 searchTreeParentByChildFromEnd。这两个 Map 分别存储从起点和从终点开始的搜索树。
  2. 正确的顶点访问检查: 使用 containsKey() 方法检查顶点是否已经被访问过。
  3. 完整路径构建: 当两个搜索方向相遇时,需要分别从相遇点向起点和终点追溯路径,然后将两条路径合并。

以下是改进后的 Java 代码示例:

import java.util.*;

public class BidirectionalSearch {

    private final Graph graph;
    private final Map<Vertex, Vertex> searchTreeParentByChildFromStart = new HashMap<>();
    private final Map<Vertex, Vertex> searchTreeParentByChildFromEnd = new HashMap<>();

    public BidirectionalSearch(Graph graph) {
        this.graph = graph;
    }

    public BidirectionalSearch buildSearchTree(Vertex start, Vertex end) {
        if (!graph.vertices().containsAll(List.of(start, end)))
            throw new IllegalArgumentException("start or stop vertices not from this graph");

        if (start.equals(end))
            return this;

        searchTreeParentByChildFromStart.clear();
        searchTreeParentByChildFromEnd.clear();

        Queue<Vertex> unvisitedVertexQueueFromStart = new ArrayDeque<>();
        Queue<Vertex> unvisitedVertexQueueFromEnd = new ArrayDeque<>();

        unvisitedVertexQueueFromStart.add(start);
        unvisitedVertexQueueFromEnd.add(end);

        searchTreeParentByChildFromStart.put(start, null);
        searchTreeParentByChildFromEnd.put(end, null);

        Vertex intersectionVertex = null;

        while (!unvisitedVertexQueueFromStart.isEmpty() && !unvisitedVertexQueueFromEnd.isEmpty()) {
            Vertex curStart = unvisitedVertexQueueFromStart.poll();

            for (Edge e : curStart.edges()) {
                Vertex neighbor = e.to();
                if (!searchTreeParentByChildFromStart.containsKey(neighbor)) {
                    searchTreeParentByChildFromStart.put(neighbor, curStart);
                    unvisitedVertexQueueFromStart.add(neighbor);

                    if (searchTreeParentByChildFromEnd.containsKey(neighbor)) {
                        intersectionVertex = neighbor;
                        break; // Found intersection
                    }
                }
            }

            if (intersectionVertex != null) break;


            Vertex curEnd = unvisitedVertexQueueFromEnd.poll();

            for (Edge e : curEnd.edges()) {
                Vertex neighbor = e.to();
                if (!searchTreeParentByChildFromEnd.containsKey(neighbor)) {
                    searchTreeParentByChildFromEnd.put(neighbor, curEnd);
                    unvisitedVertexQueueFromEnd.add(neighbor);

                    if (searchTreeParentByChildFromStart.containsKey(neighbor)) {
                        intersectionVertex = neighbor;
                        break; // Found intersection
                    }
                }
            }

            if (intersectionVertex != null) break;

        }

        return this;
    }

    public List<Vertex> getPath(Vertex start, Vertex end) {
        buildSearchTree(start, end);
        Vertex intersection = findIntersection(start, end);

        if (intersection == null) {
            return Collections.emptyList(); // No path found
        }

        List<Vertex> pathToIntersectionFromStart = buildPath(searchTreeParentByChildFromStart, intersection);
        List<Vertex> pathToIntersectionFromEnd = buildPath(searchTreeParentByChildFromEnd, intersection);

        Collections.reverse(pathToIntersectionFromEnd); // Reverse the end path

        List<Vertex> fullPath = new ArrayList<>();
        fullPath.addAll(pathToIntersectionFromStart);
        fullPath.addAll(pathToIntersectionFromEnd.subList(1, pathToIntersectionFromEnd.size())); // Avoid duplicate intersection vertex

        return fullPath;
    }

    private Vertex findIntersection(Vertex start, Vertex end) {
        for (Vertex vertex : searchTreeParentByChildFromStart.keySet()) {
            if (searchTreeParentByChildFromEnd.containsKey(vertex)) {
                return vertex;
            }
        }
        return null;
    }

    private List<Vertex> buildPath(Map<Vertex, Vertex> searchTree, Vertex intersection) {
        List<Vertex> path = new LinkedList<>();
        Vertex current = intersection;

        while (current != null) {
            path.add(0, current); // Add to the beginning to reverse the path
            current = searchTree.get(current);
        }

        return path;
    }

    // Example Graph, Vertex and Edge classes (replace with your actual implementations)
    static class Graph {
        private final Set<Vertex> vertices = new HashSet<>();

        public void addVertex(Vertex vertex) {
            vertices.add(vertex);
        }

        public Set<Vertex> vertices() {
            return vertices;
        }
    }

    static class Vertex {
        private final String name;
        private final List<Edge> edges = new ArrayList<>();

        public Vertex(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void addEdge(Edge edge) {
            edges.add(edge);
        }

        public List<Edge> edges() {
            return edges;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Vertex vertex = (Vertex) o;
            return Objects.equals(name, vertex.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }

    static class Edge {
        private final Vertex from;
        private final Vertex to;
        private final int weight;

        public Edge(Vertex from, Vertex to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }

        public Vertex to() {
            return to;
        }
    }

    public static void main(String[] args) {
        // Example Usage
        Graph graph = new Graph();
        Vertex start = new Vertex("A");
        Vertex b = new Vertex("B");
        Vertex c = new Vertex("C");
        Vertex end = new Vertex("D");

        graph.addVertex(start);
        graph.addVertex(b);
        graph.addVertex(c);
        graph.addVertex(end);

        start.addEdge(new Edge(start, b, 1));
        b.addEdge(new Edge(b, c, 1));
        c.addEdge(new Edge(c, end, 1));
        end.addEdge(new Edge(end, c, 1));

        BidirectionalSearch search = new BidirectionalSearch(graph);
        List<Vertex> path = search.getPath(start, end);

        if (!path.isEmpty()) {
            System.out.println("Path found: ");
            for (Vertex vertex : path) {
                System.out.print(vertex.getName() + " ");
            }
            System.out.println();
        } else {
            System.out.println("No path found.");
        }
    }
}

代码解释:

  • searchTreeParentByChildFromStart 和 searchTreeParentByChildFromEnd:分别存储从起点和终点开始的搜索树,记录每个节点的父节点。
  • getPath(Vertex start, Vertex end): 构建搜索树,找到两个方向相遇的顶点,然后分别构建从起点到相遇点和从终点到相遇点的路径,最后合并这两条路径。
  • buildPath(Map<Vertex, Vertex> searchTree, Vertex intersection): 从相遇点开始,通过搜索树向上追溯到起点或终点,构建单向路径。
  • findIntersection(Vertex start, Vertex end): 找到两个搜索树的相交顶点。

注意事项

  • 图的表示: 上述代码示例中使用了简单的 Graph, Vertex 和 Edge 类。在实际应用中,你需要根据你的图的结构来调整这些类的实现。
  • 性能优化: 双向搜索的性能高度依赖于图的结构。在某些情况下,单向搜索可能更有效。可以考虑使用启发式函数来指导搜索方向,进一步优化性能。
  • 路径权重: 上述代码只关注是否存在路径,没有考虑路径的权重。如果需要找到最短路径,需要修改代码,在搜索过程中维护每个节点的距离信息,并使用合适的优先队列(例如,使用 PriorityQueue)来选择下一个要访问的节点。
  • 环路处理: 在构建搜索树时,需要小心处理环路,避免无限循环。可以使用一个额外的 visited 集合来记录已经访问过的节点。

总结

双向路径搜索是一种强大的图搜索算法,可以有效地找到起点和终点之间的路径。 通过使用两个独立的搜索树,并正确地构建和合并路径,可以避免常见的实现错误,获得正确的搜索结果。 在实际应用中,需要根据图的结构和性能需求,对算法进行适当的优化。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
edge是什么浏览器
edge是什么浏览器

Edge是一款由Microsoft开发的网页浏览器,是Windows 10操作系统中默认的浏览器,其目标是提供更快、更安全、更现代化的浏览器体验。本专题为大家提供edge浏览器相关的文章、下载、课程内容,供大家免费下载体验。

1729

2023.08.21

IE浏览器自动跳转EDGE如何恢复
IE浏览器自动跳转EDGE如何恢复

ie浏览器自动跳转edge的解决办法:1、更改默认浏览器设置;2、阻止edge浏览器的自动跳转;3、更改超链接的默认打开方式;4、禁用“快速网页查看器”;5、卸载edge浏览器;6、检查第三方插件或应用程序等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

397

2024.03.05

如何解决Edge打开但没有标题的问题
如何解决Edge打开但没有标题的问题

若 Microsoft Edge 浏览器打开后无标题(窗口空白或标题栏缺失),可尝试以下方法解决: 重启 Edge:关闭所有窗口,重新启动浏览器。 重置窗口布局:右击任务栏 Edge 图标 → 选择「最大化」或「还原」。 禁用扩展:进入 edge://extensions 临时关闭插件测试。 重置浏览器设置:前往 edge://settings/reset 恢复默认配置。 更新或重装 Edge:检查最新版本,或通过控制面板修复

1038

2025.04.24

golang map内存释放
golang map内存释放

本专题整合了golang map内存相关教程,阅读专题下面的文章了解更多相关内容。

77

2025.09.05

golang map相关教程
golang map相关教程

本专题整合了golang map相关教程,阅读专题下面的文章了解更多详细内容。

40

2025.11.16

golang map原理
golang map原理

本专题整合了golang map相关内容,阅读专题下面的文章了解更多详细内容。

67

2025.11.17

java判断map相关教程
java判断map相关教程

本专题整合了java判断map相关教程,阅读专题下面的文章了解更多详细内容。

47

2025.11.27

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

497

2023.08.14

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 4.3万人学习

C# 教程
C# 教程

共94课时 | 11.2万人学习

Java 教程
Java 教程

共578课时 | 81万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号