《算法4》——图

本文介绍了使用Java实现的图数据结构,包括邻接表表示法,以及深度优先搜索(DFS)和广度优先搜索(BFS)算法。通过DFS进行路径查找,并提供路径追踪和路径到目标节点的迭代器。同时,讨论了Iterable接口的应用以及队列操作的细节,如offer()和poll()的区别。
摘要由CSDN通过智能技术生成

参考:

大佬博客:https://www.cnblogs.com/xiaohuiduan/p/11352209.html
https://blog.csdn.net/leonliu1995/article/details/78509599
书籍:算法4

/*
 * @Description: 
 * @Author: YPY
 * @Date: 2021-11-21 23:17:54
 * @LastEditTime: 2021-11-22 23:08:36
 * @LastEditors: YPY
 * @Reference: 
 */
package com.ypy.suanfa;

import java.util.ArrayList;
import java.util.List;

public class Graph {
    private static final String NEWLINE = System.getProperty("line.separator");
    private final int V;
    private int E;
    private List<Integer>[] adj;

    public Graph(int V) {
        this.V = V;
        this.E = 0;
        adj = (List<Integer>[]) new List[V];
        for (int v = 0; v < V; v++) {
            adj[v] = new ArrayList<Integer>(V);
        }
    }

    public int V() {
        return V;
    }

    public int E() {
        return E;
    }
/**
 * 添加边,根据邻接表的含义,一条边连接两个顶点,在顶点A的邻接表放入顶点B,在顶点B的邻接表放入顶点A,则构成边。
 * @param v
 * @param w
 */
    public void addEdge(int v, int w) {
        adj[v].add(w);
        adj[w].add(v);
        //边的个数加1
        E++;
    }

    public Iterable<Integer> adj(int v) {
        return adj[v];
    }

    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(V + " vertices, " + E + " edges " + NEWLINE);
        for (int v = 0; v < V; v++) {
            s.append(v + ": ");
            for (int w : adj[v]) {
                s.append(w + " ");
            }
            s.append(NEWLINE);
        }
        return s.toString();

    }
}

package com.ypy.suanfa;
/*
 * @Description: 基于深度优先的路径查找。
 * @Author: YPY
 * @Date: 2021-11-21 23:12:10
 * @LastEditTime: 2021-11-24 10:50:59
 * @LastEditors: YPY
 * @Reference: 
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

public class DFS {
    private boolean[] marked;
    // 从A到B经历的边的条数
    private int[] edgeTo;
    // 起点
    private final int s;
    private int count;

    public DFS(Graph G, int s) {
        marked = new boolean[G.V()];
        edgeTo = new int[G.V()];
        this.s = s;
        count = 0;
        dfs(G, s);

    }

    /**
     * 深度优先
     * 
     * @param G
     * @param v
     */
    private void dfs(Graph G, int v) {
        // 把v点记为被标记过,后面就不会遍历到v
        marked[v] = true;
        //
        count++;
        for (int w : G.adj(v)) {
            if (!marked[w]) {
                dfs(G, w);
            }

        }

    }

    public boolean marked(int w) {
        return marked[w];
    }

    public int getcount() {
        return count;
    }

    public boolean hasPathTo(int v) {
        return marked(v);
    }

    public Iterable<Integer> search(Graph G, int s) {
        DFS dfs = new DFS(G, s);
        List<Integer> list = new ArrayList<Integer>(dfs.getcount());
        for (int v = 0; v < G.V(); v++) {
            if (dfs.marked[v]) {
                list.add(v);
            }
        }
        return (Iterable<Integer>) list;
    }

    public Iterable<Integer> PathTo(int v) {
        if (!hasPathTo(v)) {
            return null;
        }
        Stack<Integer> path = new Stack<Integer>();
        for (int x = v; x != s; x = edgeTo[x]) {
            path.push(x);
        }
        path.push(s);
        return path;
    }
    public Iterable<Integer> PathTo(Graph G,int s,int v){
        DFS dfspath=new DFS(G, s);
        return dfspath.PathTo(v);
    }

}

关于Iterable作为返回类型的博文: https://blog.51cto.com/jition/2509344

queue不用add()用offer()的原因https://blog.csdn.net/meism5/article/details/89883976

同理:
Queue 中 remove() 和 poll()都是用来从队列头部删除一个元素。
在队列元素为空的情况下,remove() 方法会抛出NoSuchElementException异常,poll() 方法只会返回 null 。

/*
 * @Description: 
 * @Author: YPY
 * @Date: 2021-11-24 10:53:54
 * @LastEditTime: 2021-11-24 11:09:53
 * @LastEditors: YPY
 * @Reference: 
 */
package com.ypy.suanfa;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BreadFirstSearch {
    private boolean[] marked;
    private int s;
    private int[] edgeTo;

    public void BreadFirstSearch(Graph G, int s) {
        this.s = s;
        this.marked = new boolean[G.V()];
        this.edgeTo = new int[G.V()];
        bfs(G, s);
    }

    public void bfs(Graph G, int s) {
        Queue<Integer> queue = new LinkedList<>();
        // s访问过的标记
        marked[s] = true;
        queue.offer(s);
        if (!queue.isEmpty()) {
            int v = queue.poll();
            for (int w : G.adj(v)) {
                if (!marked[w]) {
                    edgeTo[w] = v;
                    marked[w] = true;
                    queue.offer(w);
                }
            }
        }
    }

    public boolean hasPathTo(int v) {
        return marked[v];
    }

    public Iterable<Integer> pathTo(int v) {
        if (!hasPathTo(v)) {
            return null;
        }

        Stack<Integer> path = new Stack<>();
        for (int i = v; i != s; i = edgeTo[i]) {
            path.push(i);
        }
        path.push(s);
        return path;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值