算法 4.1节 无向图中DFS,BFS的实现和应用

//图对象
import edu.princeton.cs.algs4.Bag;

public class Graph {
    private final int V;
    private int E;
    private Bag<Integer>[] adj;

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

    public int V() {
        return V;
    }

    public int E() {
        return E;
    }

    public void addE(int v, int w) {
        adj[v].add(w);
        adj[w].add(v);
        E++;
    }

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


}
//DFS
import java.util.Stack;

public class DFS {
    private boolean[] marked;
    private int count;
    private int [] edgeTo;
    private final int s;

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

    private void dfs(Graph G, int s) {
        marked[s] = true;
        count++;
        for (int w : G.adj(s)) {
            if (!isMarked(w)) {
                edgeTo[w] = s;
                dfs(G,w);
            }
        }
    }

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

    public Iterable<Integer> pathTo(int v) {
        if (!isMarked(v)) {
            return null;
        }
        Stack<Integer> path = new Stack<>();
        for (int x = v; v != s; x = edgeTo[x]) {
            path.push(v);
        }
        path.push(s);
        return path;
    }

}
//BFS
import edu.princeton.cs.algs4.*;

public class BFS {
    private boolean[] marked;
    private int count;
    private int [] edgeTo;
    private final int s;

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

    private void bfs(Graph G, int s) {
        Queue<Integer> queue = new Queue<>() ;
        queue.enqueue(s);
        marked[s] = true;
        while (!queue.isEmpty()) {
            int temp = queue.dequeue();
            for (int w : G.adj(temp)) {
                if (!isMarked(w)) {
                    queue.enqueue(w);
                    marked[w] = true;
                    edgeTo[w] = temp;
                }
            }
        }
    }

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

    public Iterable<Integer> pathTo(int v) {
        if (!isMarked(v)) {
            return null;
        }
        java.util.Stack<Integer> path = new java.util.Stack<>();
        for (int x = v; v != s; x = edgeTo[x]) {
            path.push(v);
        }
        path.push(s);
        return path;
    }

}

//连通性问题
import edu.princeton.cs.algs4.Bag;

public class CC {
    private int[] id;
    private int count;
    private boolean[] marked;

    public CC(Graph G) {
        marked = new boolean[G.V()];
        id = new int[G.V()];
        for (int s = 0; s < G.V(); s++) {
            if (!isMarked(s)) {
                dfs(G,s);
                count++;
            }
        }
    }

    private void dfs(Graph G, int  s) {
        marked[s] =true;
        id[s] = count;
        for (int w : G.adj(s)) {
            if (!isMarked(w)) {
                dfs(w);
            }
        }
    }

    private boolean isMarked(int v) {
        return marked[v];
    }

    public int id(int v) {
        return id[v];
    }

    public boolean isConnected(int w, int v) {
        return id[v] == id[w];
    }
}
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.ST;

public class SymbolGraph {
    private ST<String, Integer> st;
    private String[] keys;
    private Graph G;

    public SymbolGraph(String stream, String split) {
        st = new ST<String, Integer>();
        In in = new In(stream);
        while (in.hasNextLine()) {
            String[] a = in.readLine().split(split);
            for (int i = 0;i< a.length; i++) {
                if (!st.contains(a[i])) {
                    st.put(a[i], st.size());
                }
            }
        }
        keys = new String[st.size()];
        for (String t : st.keys()) {
            keys[st.get(t)] = t;
        }

        G = new Graph(st.size());
        in = new In(stream);
        while (in.hasNextLine()) {
            String[] a = in.readLine().split(split);
            int v = st.get(a[0]);
            for (int i =1;i<a.length;i++) {
                G.addE(v, st.get(a[i]));
            }
        }
    }



}

//判断是否是二分图
import java.util.Stack;

public class DFS {
    private boolean[] marked;
    private int [] edgeTo;
    private boolean[] color;
    private boolean isTwoColors = true;

    public DFS(Graph G) {
        marked = new boolean[G.V()];
        edgeTo = new int[G.V()];
        color = new boolean[G.V()];
        color[0] = true;
        for (int s = 0;s < G.V(); s++) {
            dfs(G, s);
        }
    }

    private void dfs(Graph G, int s) {
        marked[s] = true;
       for (int w : G.adj(s)) {
            if (!isMarked(w)) {
                edgeTo[w] = s;
                color[w] = !color[s];
                dfs(G,w);
            } else if (color[s] == color[w]) {
                isTwoColors = false;
            }
        }
    }

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

    public boolean isTwoColors() {
        return  isTwoColors;
    }

}

猜你喜欢

转载自blog.csdn.net/winter_wu_1998/article/details/79856582