深度优先遍历DFS 与 广度优先遍历BFS(java实现)

图的遍历方式有俩种:

  • 深度优先遍历(DFS)
  • 广度优先遍历(BFS)

(1)深度优先遍历(利用栈和递归来实现)
思路:先以一个点为起点,这里假如是点A,那么就将A相邻的点放入堆栈,然后在栈中再取出栈顶的顶点元素(假如是点B),再将B相邻的且没有访问过的点放入栈中,不断这样重复操作直至栈中元素清空。这个时候你每次从栈中取出的元素就是你依次访问的点,以此实现遍历。

class Node {
    int x;
    Node next;
    public Node(int x) {
        this.x = x;
        this.next = null;
    }
}
public class DFS {
    public Node first;
    public Node last;

    public static int run[] = new int[9];
    public static DFS head[] = new DFS[9];

    public static void dfs(int current) {
        run[current] = 1;
        System.out.print("[" + current + "]");

        while (head[current].first != null) {
            if(run[head[current].first.x] == 0) { //如果顶点尚未遍历,就进行dfs递归
                dfs(head[current].first.x);
            }
            head[current].first = head[current].first.next;
        }
    }

    public boolean isEmpty() {
        return first == null;
    }
    public void print() {
        Node current = first;
        while(current != null) {
            System.out.print("[" + current.x + "]");
            current = current.next;
        }
        System.out.println();
    }
    public void insert(int x) {
        Node newNode = new Node(x);
        if(this.isEmpty()) {
            first = newNode;
            last = newNode;
        }
        else {
            last.next = newNode;
            last = newNode;
        }
    }

    public static void main(String[] args) {
        int Data[][] = { {1,2}, {2,1}, {1,3}, {3,1}, {2,4}, {4,2},
                {2,5}, {5,2}, {3,6}, {6,3}, {3,7}, {7,3}, {4,5}, {5,4},
                {6,7}, {7,6}, {5,8}, {8,5}, {6,8}, {8,6} };
        int DataNum;
        int i,j;
        System.out.println("图形的邻接表内容为:");
        for(i=1;i<9;i++) {
            run[i] = 0;
            head[i] = new DFS();
            System.out.print("顶点" + i + "=>");
            for (j=0;j<20;j++) {
                if(Data[j][0] == i) {
                    DataNum = Data[j][1];
                    head[i].insert(DataNum);
                }
            }
            head[i].print();
        }
        System.out.println("深度优先遍历顶点:");
        dfs(1);
        System.out.println("");
    }
}

深度优先遍历DFS 与 广度优先遍历BFS(java实现)_第1张图片

(2)广度优先遍历:(基于队列和递归的方式)
思路:基本操作是和深度优先差不多的,只不过这里是通过队列来实现的,找到一个起点A,并将A相邻的点放入队列中,这时将队首元素B取出,并将B相邻且没有访问过的点放入队列中,不断重复这个操作,直至队列清空,这个时候依次访问的顶点就是遍历的顺序。

class Node {
    int x;
    Node next;
    public Node(int x) {
        this.x = x;
        this.next = null;
    }
}
public class BFS {
    public Node first;
    public Node last;

    public static int run[] = new int[9];
    public static BFS head[] = new BFS[9];
    public final static int MAXSIZE = 10;
    static int[] queue = new int[MAXSIZE];
    static int front = -1;
    static int rear = -1;

    public static void enqueue(int value) {
        if(rear>=MAXSIZE) return;
        rear++;
        queue[rear] = value;
    }

    public static int dequeue() {
        if(front == rear) return -1;
        front++;
        return queue[front];
    }

    public static void bfs(int current) {
        Node tempnode;
        enqueue(current);
        run[current] = 1;
        System.out.print("[" + current + "]");
        while (front != rear) {
            current = dequeue();
            tempnode = head[current].first;
            while (tempnode != null) {
                if(run[tempnode.x] == 0) {
                    enqueue(tempnode.x);
                    run[tempnode.x] = 1;
                    System.out.print("[" + tempnode.x + "]");
                }
                tempnode = tempnode.next;
            }
        }
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void print() {
        Node current = first;
        while(current != null) {
            System.out.print("[" + current.x + "]");
            current = current.next;
        }
        System.out.println();
    }
    public void insert(int x) {
        Node newNode = new Node(x);
        if(this.isEmpty()) {
            first = newNode;
            last = newNode;
        }
        else {
            last.next = newNode;
            last = newNode;
        }
    }

    public static void main(String[] args) {
        int Data[][] = { {1,2}, {2,1}, {1,3}, {3,1}, {2,4}, {4,2},
                {2,5}, {5,2}, {3,6}, {6,3}, {3,7}, {7,3}, {4,5}, {5,4},
                {6,7}, {7,6}, {5,8}, {8,5}, {6,8}, {8,6} };
        int DataNum;
        int i,j;
        System.out.println("图形的邻接表内容为:");
        for(i=1;i<9;i++) {
            run[i] = 0;
            head[i] = new BFS();
            System.out.print("顶点" + i + "=>");
            for (j=0;j<20;j++) {
                if(Data[j][0] == i) {
                    DataNum = Data[j][1];
                    head[i].insert(DataNum);
                }
            }
            head[i].print();
        }
        System.out.println("深度优先遍历顶点:");
        bfs(1);
        System.out.println("");
    }

}

深度优先遍历DFS 与 广度优先遍历BFS(java实现)_第2张图片

你可能感兴趣的:(java,数据结构)