Graph(图)

1.邻接表表示法

const Colors = {
    WHITE: 0,
    GREY: 1,
    BLACK: 2
}
// 辅助对象用于管理顶点的访问状态,开始默认都是白色
const initializeColor = vertices => {
    const color = {};
    for (let i = 0; i < vertices.length; i++) {
        color[vertices[i]] = Colors.WHITE;
    }
    return color;
}
class Graph {
    constructor(isDirected = false) {
        this.isDirected = isDirected
        this.vertices = []
        this.adjList = new Dictionary()
    }
    addVertex(v) {
        if (!this.vertices.includes[v]) {
            this.vertices.push(v)
            this.adjList.set(v, [])
        }
    }
    addEdge(v, w) {
        if (!this.adjList.get(v)) {
            this.addVertex(v)
        }
        if (!this.adjList.get(w)) {
            this.addVertex(w)
        }
        this.adjList.get(v).push(w)
        if (!this.isDirected) {
            this.adjList.get(w).push(v)
        }
    }
    getVertices() {
        return this.vertices
    }
    getAdjList() {
        return this.adjList
    }
    toString() {
        let s = '';
        // 遍历顶点
        for (let i = 0; i < this.vertices.length; i++) {
            // 获取边key
            s += `${this.vertices[i]} -> `;
            // 获取领接表
            const neighbors = this.adjList.get(this.vertices[i]);
            // 遍历相邻节点
            for (let j = 0; j < neighbors.length; j++) {
                s += `${neighbors[j]}`;
            }
            s += `\n`;
        }
        return s;
    }
}

2.图的遍历

广度优先搜索

const breadthFirstSearch = (graph, startVertex, callback) => {
    // 获取所有的顶点
    const vertices = graph.getVertices();
    // 获取邻接表
    const adjList = graph.getAdjList();
    // 初始化颜色
    const color = initializaColor(vertices);

    // 新建一个队列
    const queue = new Queue();
    // 将起始节点放入队列
    queue.enqueue(startVertex);

    // 如果队列不为空
    while(!queue.isEmpty()) {
        //  取出元素
        const u = queue.dequeue();
        // 获取邻接点
        const neighbors = adjList.get(u);
        // 标记颜色被访问过
        color[u] = Colors.GREY;
        // 遍历子节点
        for(let i = 0; i< neighbors.length; i++) {
            // 获取当前一个字节点
            const w = neighbors[i];
            // 如果未曾被访问过,就标记一下然后放入队列中
            if(color[w] === Colors.WHITE) {
                color[w] = Colors.GREY;
                queue.enqueue(w);
            }
        }
        // 标记颜色为黑色,已经访问过并且已经被探索过
        color[u] = Colors.BLACK;
        // 如果callback函数存在
        if(callback) {
            // 调用callback进行打印之类对的处理
            callback(u);
        }
    }
}

深度优先搜索

// 深度优先遍历
const depthFirstSearch = (graph, callback) => {
    const vertices = graph.getVertices();
    const adjList = graph.getAdjList();
    const color = initializeColor(vertices);

    for(let i = 0; i < vertices.length; i++) {
        if(color[vertices[i]] === Colors.WHITE) {
            // 深度优先遍历
            depthFirstSearchVisit(vertices[i], color, adjList, callback);
        }
    }
}

// 深度优先遍历,参数:(索引,颜色标记,邻接表,回调函数)
const depthFirstSearchVisit = (u, color, adjList, callback) => {
    // 标记u已经被访问
    color[u] = Colors.GREY;
    // 如果回调函数可用
    if(callback) {
        callback(u);
    }
    // 获取邻节点
    const neighbors = adjList.get(u);
    // 遍历邻接节点
    for(let i = 0; i < neighbors.length; i++) {
        // 获取某邻接节点
        const w = neighbors[i];
        // 如果当前节点未曾被访问则递归进行深度优先遍历
        if(color[w] === Colors.WHITE) {
            depthFirstSearchVisit(w, color, adjList, callback);
        }
    }
    // 最优把该节点置为已经被访问过
    color[u] = Colors.BLACK;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值