从深度搜索的路径中删除带有死胡同的节点

发布于 2025-02-13 03:29:12 字数 561 浏览 0 评论 0原文

如果我委托console.log访问了节点,我会得到很多我不想要的备用节点。

我正是开始结束节点所需的节点(不需要最短),但是我不希望算法在找到正确的路径之前所采取的所有其他路径/节点

function dfs(start, visited = new Set()) {

    console.log(start)
    
    visited.add(start);

    const destinations = adjacencyList.get(start);

    for (const destination of destinations) {

        if (destination === 'BKK') { 
            console.log(`DFS found Bangkok`)
            return;
        }
        
        if (!visited.has(destination)) {
            dfs(destination, visited);
        }

    }

}

dfs('PHX')

If I console.log visited nodes, I get a ton of spare nodes that I don't want.

I just what the nodes necessary to get for start to end node (does not need to be the shortest), But I don't want all the other paths/nodes the algo took before it found the right path

function dfs(start, visited = new Set()) {

    console.log(start)
    
    visited.add(start);

    const destinations = adjacencyList.get(start);

    for (const destination of destinations) {

        if (destination === 'BKK') { 
            console.log(`DFS found Bangkok`)
            return;
        }
        
        if (!visited.has(destination)) {
            dfs(destination, visited);
        }

    }

}

dfs('PHX')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2025-02-20 03:29:12

这里的想法是跟踪您访问的节点目标>目标导致纠正路径。因此,您应该在耗尽特定节点后返回,是否可以通过此节点访问end节点。

function dfs(start, visited = new Set()) {
    visited.add(start);

    const destinations = adjacencyList.get(start);
    var result = false;
    
    for (const destination of destinations) {
        
        if (destination === 'BKK') { 
            console.log(`DFS found Bangkok`);
            console.log(destination);
            return true;
        }
        
        if (!visited.has(destination)) {
             result |= dfs(destination, visited); // you can visit end from this node
        }

    }

    if(result) {
      console.log(start);
    }

    return result;
}

dfs('PHX')

请注意,这将从开始 - >从结束到开始时以相反的顺序结束。
要从开始..-> .. finish的路径,您可以存储在某些数组(而不是Console.log)中,并在DFS完成后逆转。

The idea here is to track if the node destination you visited leads to correct path or not. Hence, you should return after exhausting a particular node whether end node can be visited through this node or not.

function dfs(start, visited = new Set()) {
    visited.add(start);

    const destinations = adjacencyList.get(start);
    var result = false;
    
    for (const destination of destinations) {
        
        if (destination === 'BKK') { 
            console.log(`DFS found Bangkok`);
            console.log(destination);
            return true;
        }
        
        if (!visited.has(destination)) {
             result |= dfs(destination, visited); // you can visit end from this node
        }

    }

    if(result) {
      console.log(start);
    }

    return result;
}

dfs('PHX')

Notice that this will print nodes from start -> finish in reverse order i.e. from finish to start.
To have path from start ..->..finish, you can store in some array(instead of console.log) and reverse that after dfs is complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文