3.无向图(无向图的深度优先搜索)

此篇文章为实现《算法》一书中提到的大部分无向图算法,分为多篇,

即多个Java文件,可以直接复制,便于学习;

第三篇,java无向图的类实现,此篇主要列出实现的方法,需要参考其他类中方法(后续实现);

参考链接:https://www.cnblogs.com/xiaohuiduan/p/11352209.html#e5b9bfe5baa6e4bc98e58588e9818de58e86_6

/**
 * FileName: DepthFirstSearch
 * Author:   Jerry
 * Date:     2020/2/10 20:09
 * Description: 无向图的深度优先搜索
 */
package graph;

public class DepthFirstSearch {
    //标记数组
    private boolean[] marked;
    //遍历结点个数
    private int count;

    public DepthFirstSearch(UndirGraph graph, int s) {
        //默认初始化为false
        marked = new boolean[graph.V()];
        dfs(graph, s);
    }

    /**
     * 针对一个结点的深度搜索
     */
    private void dfs(UndirGraph graph, int s) {
        marked[s] = true;
        count++;
        for (int v : graph.adj(s)) {
            if (!marked[v]) {
                dfs(graph, v);
            }
        }
    }

    /**
     * 返回是否标记过
     *
     * @param w
     * @return
     */
    public boolean getMarked(int w) {
        return marked[w];
    }

    /**
     * 返回标记次数
     *
     * @return
     */
    public int getCount() {
        return count;
    }


}

  

   

   

原文地址:https://www.cnblogs.com/AccompanyingLight/p/12294074.html