Algorithm and data structure (17) graph correlation algorithm 1

Related algorithms of graphs (1)

Supplementary - Code of the diagram connecting the table

If you don't know the picture, please refer to the previous article:

knapsack

Knapsack is a data structure that does not support deleting elements from it. Its purpose is to collect elements and iterate through all elements collected. The order of storing data is not saved in knapsack;

Later, the data structure of the backpack will be used when the structure of the graph of the connection table is implemented

public class Bag<T> implements Iterable{
    private T[] array;
    private int bagSize;
    private static final int DEFAULT_CAPACITY = 10;

    public Bag() {
        ensureCapacity(DEFAULT_CAPACITY);
    }

    public Bag(int size) {
        if (size>DEFAULT_CAPACITY)
            ensureCapacity(size);
        else
            ensureCapacity(DEFAULT_CAPACITY);
    }

    public void add(T t){
        if (array.length == bagSize)
            ensureCapacity(array.length<<1);
        array[bagSize++]=t;
    }

    private void ensureCapacity(int capactity){
        T[] newArray = (T[])new Object[capactity];
        for (int i = 0; i < array.length; i++) {
            newArray[i]=array[i];
        }
        array = newArray;
    }
    @Override
    public Iterator<T> iterator() {
        return new ArrayIterator();
    }
    
    private class ArrayIterator implements Iterator<T>{
        int currentPosition;
        
        @Override
        public boolean hasNext() {
            return currentPosition<bagSize;
        }
        
        @Override
        public T next() {
            if (!hasNext()) throw new NoSuchElementException();
            return array[currentPosition++];
        }
        
    }
}

Adjacency table - diagram

public class Graph {
    private final int V;//Vertex digit
    private int E;//Number of edges
    private Bag<Integer>[] adj;

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

    public int V() {
        return V;
    }

    public int E() {
        return E;
    }

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

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

Use depth first search to find paths in a graph

public class DepthFirstPath {
    private boolean[] marked;//
    private int[] edgeTo;//The last vertex of a known path from a start point to a vertex
    private final int s;//Starting point

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

    private void dfs(Graph G, int v) {
        marked[v]=true;
        Bag<Integer> adj = G.adj(v);
        for (int w : G.adj(v)) {
            edgeTo[w] = v;
            dfs(G,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 x= v ; x!=s;x = edgeTo[x]){
            path.push(x);
        }
        path.push(s);
        return path;
    }
}

Using breadth first search to find the path in breadth map

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

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

    private void bfs(Graph G, int v) {
        Queue<Integer> queue = new Queue<>();
        marked[v] = true;
        queue.enqueue(v);
        while (!queue.isEmpty()){
            int s = queue.dequeue();
            for (int w : G.adj(s)) {
                if (!marked[w]){
                    queue.enqueue(w);
                    edgeTo[s] = w;
                    marked[w] = true; 
                }
            }
        }
    }
    // The hasPathTo and pathTo methods are the same as those in dfs;
}
Published 17 original articles, won praise 0, visited 332
Private letter follow

Posted by neylitalo on Sat, 15 Feb 2020 06:14:45 -0800