美文网首页
用队列实现广度优先的原理

用队列实现广度优先的原理

作者: HelenYin | 来源:发表于2021-06-23 16:02 被阅读0次

    伪代码

    // 构件图
    const graph = {};
    graph['you'] = ['alice', 'bob', 'claire'];
    graph['alice'] = ['peggy'];
    graph['claire'] = ['thom', 'jonny'];
    graph['peggy'] = [];
    graph['thom'] = [];
    graph['jonny'] = [];
    
    function BFS () {
        let person = null;
        const queue = [];
        queue.push(...graph['you']);
        while(graph.length > 0) {
            person = graph.shift();
            if (isSeller(person)) {
                console.log(`${person} is seller`);
                return true;
            } else {
                queue.push(...graph[person])
            }
        }
        return false;
    }
    

    相关文章

      网友评论

          本文标题:用队列实现广度优先的原理

          本文链接:https://www.haomeiwen.com/subject/ypmbyltx.html