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

树的深度优先搜索

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', []))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秀秀_heo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值