【数据结构与算法】深度优先搜索和广度优先搜索

树的深度优先搜索

image.png

function Node(value) {
    this.value = value
    this.children = []
}

let a = new Node('a')
let b = new Node('b')
let c = new Node('c')
let d = new Node('d')
let e = new Node('e')
let f = new Node('f')
a.children.push(c)
a.children.push(b)
a.children.push(f)
b.children.push(d)
b.children.push(e)

function deepSearch(root, target) {
    if (root == null) return false
    if (root.value == target) return true
    let result = false
    for (let i = 0; i < root.children.length; i++) {
        result |= deepSearch(root.children[i], target)
    }
    return result ? true : false
}

console.log(deepSearch(a, 'c'))

树的广度优先搜索

function Node(value) {
    this.value = value
    this.children = []
}

let a = new Node('a')
let b = new Node('b')
let c = new Node('c')
let d = new Node('d')
let e = new Node('e')
let f = new Node('f')
a.children.push(c)
a.children.push(b)
a.children.push(f)
b.children.push(d)
b.children.push(e)

function bfs(roots, target) {
    if (roots == null || roots.length == 0) return false
    let children = []
    for (let i = 0; i < roots.length; i++) {
        if (roots[i].value == target) {
            return true
        } else {
            children = children.concat(roots[i].children)
        }
    }
    return bfs(children, target)
}

console.log(bfs([a], 'n'))

图的深度优先搜索

image.png

function Node(value) {
    this.value = value
    this.neighbor = []
}

let a = new Node('a')
let b = new Node('b')
let c = new Node('c')
let d = new Node('d')
let e = new Node('e')
a.neighbor.push(b)
a.neighbor.push(c)
b.neighbor.push(a)
b.neighbor.push(c)
b.neighbor.push(d)
c.neighbor.push(a)
c.neighbor.push(b)
c.neighbor.push(d)
d.neighbor.push(b)
d.neighbor.push(c)
d.neighbor.push(e)
e.neighbor.push(d)


function deepSearch(node, target, path) {
    if (node == null) return false
    if (path.indexOf(node) > -1) return false
    if (node.value == target) return true
    path.push(node)
    let result = false
    for (let i = 0; i < node.neighbor.length; i++) {
        result |= deepSearch(node.neighbor[i], target, path)
    }
    return result ? true : false
}

console.log(deepSearch(a, 'd', []))

图的广度优先搜索

function Node(value) {
    this.value = value
    this.neighbor = []
}

let a = new Node('a')
let b = new Node('b')
let c = new Node('c')
let d = new Node('d')
let e = new Node('e')
a.neighbor.push(b)
a.neighbor.push(c)
b.neighbor.push(a)
b.neighbor.push(c)
b.neighbor.push(d)
c.neighbor.push(a)
c.neighbor.push(b)
c.neighbor.push(d)
d.neighbor.push(b)
d.neighbor.push(c)
d.neighbor.push(e)
e.neighbor.push(d)


function bfs(nodes, target, path) {
    if (nodes == null || nodes.length == 0) return false
    let nextNodes = []
    for (let i = 0; i < nodes.length; i++) {
        if (path.indexOf(nodes[i]) > -1) continue
        path.push(nodes[i])
        if (nodes[i].value == target) return true
        else nextNodes = nextNodes.concat(nodes[i].neighbor)
    }
    return bfs(nextNodes, target, path)
}

console.log(bfs([a], 'c', []))

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 17:26:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 17:26:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 17:26:04       82 阅读
  4. Python语言-面向对象

    2024-04-05 17:26:04       91 阅读

热门阅读

  1. linux内核常用调优参数

    2024-04-05 17:26:04       31 阅读
  2. Android U user+root实现方案

    2024-04-05 17:26:04       31 阅读
  3. FTP协议

    FTP协议

    2024-04-05 17:26:04      29 阅读
  4. vue3监听div高宽变化自定义分析

    2024-04-05 17:26:04       23 阅读
  5. Postman 请求参数传递指南:Query、Path 和 Body 详解

    2024-04-05 17:26:04       29 阅读
  6. 输出杨辉三角形

    2024-04-05 17:26:04       32 阅读
  7. Oracle备份和还原的几种方式

    2024-04-05 17:26:04       37 阅读