Java实现无向图深度优先遍历广度优先遍历

import java.util.*;

/**
 * 图中节点值唯一标识这个节点
 *
 */

public class graphTest {

    static Map<Integer,LinkedList<Integer>> undirectedGraph = new LinkedHashMap<>();
    static Map<Integer,Boolean> visited = new HashMap();
    static Iterator<Map.Entry<Integer,LinkedList<Integer>>> it;
    /**
     * 递归深度优先遍历
     * @param start
     */
    static void DFS(Integer start){
        System.out.print(start+" ");
        visited.put(start,true);
        LinkedList<Integer>  neighbor = undirectedGraph.get(start);
        for(Integer i : neighbor){
            if(!visited.get(i)){
                DFS(i);
            }
        }
    }

    /**
     * 非递归深度优先遍历
     * @param start
     */
    static void DFSwithStack(Integer start){
        Stack<Integer> st = new Stack();
        System.out.print(start+" ");
        st.push(start);
        visited.put(start,true);
        while(!st.isEmpty()){
            int node = st.peek();
            LinkedList<Integer>  neighbor = undirectedGraph.get(node);
            boolean flag = false;
            if(neighbor.size() != 0) {
                for (Integer next : neighbor) {
                    if (!visited.get(next)) {
                        st.push(next);
                        System.out.print(next + " ");
                        visited.put(next, true);
                        flag = true;
                        break;
                    }
                }
            }
            if(!flag) {
                st.pop();
            }
        }
    }

    /**
     * 广度优先遍历
     */
    static void BFS(Integer start){
        Queue<Integer> q = new LinkedList();
        q.offer(start);
        System.out.print(start+" ");
        visited.put(start,true);
        while(!q.isEmpty()){
            int node = q.poll();
            LinkedList<Integer> neighbor = undirectedGraph.get(node);
            for(Integer i : neighbor){
                if(!visited.get(i)){
                    q.offer(i);
                    System.out.print(i+" ");
                    visited.put(i,true);
                }
            }
        }
    }

    static void initBeforeTravel(){
         Set<Integer> keys = visited.keySet();
         for(Integer key:keys){
             visited.put(key,false);
         }
        it = undirectedGraph.entrySet().iterator();
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图中节点个数:");
        int sumOfNode = sc.nextInt();
        System.out.println("请输入图中" + sumOfNode + "个节点:");
        for(int i=0;i<sumOfNode;i++){
            int value = sc.nextInt();
            undirectedGraph.put(value,new LinkedList());
            visited.put(value,false);
        }
        System.out.println("请输入图中所有边:");

        String line = sc.nextLine();

        while(sc.hasNextLine()){
            line = sc.nextLine();
            if("".equals(line)){
                break;
            }
            String[] edge = line.split(" ");
            int node1 = Integer.parseInt(edge[0]);
            int node2 = Integer.parseInt(edge[1]);
            undirectedGraph.get(node1).add(node2);
            undirectedGraph.get(node2).add(node1);
        }

        initBeforeTravel();
        System.out.println("深度优先遍历序列:");
        while(it.hasNext()) {
            int start = it.next().getKey();
            if(!visited.get(start)) {
                DFS(start);
            }
        }

        initBeforeTravel();
        System.out.println("\n深度优先遍历序列:");
        while(it.hasNext()) {
            int start = it.next().getKey();
            if(!visited.get(start)) {
                DFSwithStack(start);
            }
        }

        initBeforeTravel();
        System.out.println("\n广度优先遍历序列:");
        while(it.hasNext()) {
            int start = it.next().getKey();
            if(!visited.get(start)) {
                BFS(start);
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsws100

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值