先入先出的数据结构

广度优先搜索(BFS) 的伪代码——应用队列

参考 https://leetcode-cn.com/explore/learn/card/queue-stack/217/queue-and-bfs/870/

/**
 * Return the length of the shortest path between root and target node.
 */
int BFS(Node root, Node target) {
    Queue<Node> queue;  // store all nodes which are waiting to be processed
    Set<Node> used;     // store all the used nodes
    int step = 0;       // number of steps neeeded from root to current node
    // initialize
    add root to queue;
    add root to used;
    // BFS
    while (queue is not empty) {
        step = step + 1;
        // iterate the nodes which are already in the queue
        int size = queue.size();
        for (int i = 0; i < size; ++i) {
            Node cur = the first node in queue;
            return step if cur is target;
            for (Node next : the neighbors of cur) {
                if (next is not in used) {
                    add next to queue;
                    add next to used;
                }
            }
            remove the first node from queue;
        }
    }
    return -1;          // there is no path from root to target
}
全部评论

相关推荐

2025-11-22 16:20
已编辑
用友_Java开发实习生(实习员工)
等闲_:感觉有好多地方会被问穿,mysql存储向量这个方案问题应该很大的,如果深问的的话,为什么不用es,不用pg,不用mivus,分块策略是怎么做的,向量化是怎么向量化的,稠密向量还是稀疏向量,再深问余弦相似度,HSWM算法,Bm25算法,为什么不用混合检索或者Rank重排序优化?其他的项目不停机分库分表咋实现的,切库过程中数据有diff的话有没有补偿策略?既然有了分库分表了有没有碰到业务上不好优化的慢sql,让这个sql读从库?而且点评的话,最好自己压测过,要不这个数据也不好解释。现在就27的情况来看,很多同学已经有了中大厂实习,这个节点也会偏向这些有大厂实习的92同学,而且hc也不多,所以坚持海投吧
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务