java實現判斷圖的連通性


圖的連通性的概念:

連通無向圖:如果無向圖任意兩個頂點都連通,則稱為連通無向圖
連通有向圖:如果有向圖任意兩個頂點vi,vj,從vi到vj和從vj到vi都有路徑,則稱有向圖是強連通有向圖

public class Connect {

    public static void main(String[] args) {
        Set<String> nodes = ImmutableSet.of("A", "B", "C", "D");
        List<String> hop = Lists.newArrayList("C->A", "C->D");
        List<Pair<String, String>> objects = Lists.newArrayList();
        for (String s : hop) {
            String[] split = s.split("->");
            String from = split[0];
            String to = split[1];
            Pair<String, String> pair = new Pair<>(from, to);
            objects.add(pair);
        }
        Boolean isConnect = unDirectGraphConnect(nodes,objects);
        System.out.println("無向圖連通性");
        System.out.println(isConnect);
        System.out.println("有向圖連通性");
        Boolean isConnect2 = directGraphConnect(nodes, objects);
        System.out.println(isConnect2);
    }

    /**
     * 連通無向圖判斷,最后應該只有一個連通分量,而且可以連通所有節點
     *
     * @param pairList
     * @return
     */
    public static Boolean unDirectGraphConnect(Set<String> nodes, List<Pair<String, String>> pairList) {
        Multimap<String, String> map = HashMultimap.create();

        for (Pair<String, String> pair : pairList) {
            String from = pair.getFrom();
            String to = pair.getTo();
            //默認from、to都不存在
            boolean fromPresent = false;
            boolean toPresent = false;
            if (isPresent(map, from)) {
                fromPresent = true;
            }
            if (isPresent(map, to)) {
                toPresent = true;
            }
            //from/to都不存在,最簡單,from做key,將from和to放value里
            if (!fromPresent && !toPresent) {
                map.put(from, from);
                map.put(from, to);
                //from存在,to不存在,要區分from是key還是value
            } else if (!toPresent) {
                boolean inKey = map.containsKey(from);
                if (inKey) {
                    map.put(from, to);
                } else {
                    String valKey = getKeyByValue(map, from);
                    map.put(valKey, to);
                }
                //to存在,from不存在,也要區分to是key還是value
            } else if (!fromPresent) {
                boolean toInKey = map.containsKey(to);
                if (toInKey) {
                    map.put(to, from);
                } else {
                    String valKey = getKeyByValue(map, to);
                    map.put(valKey, from);
                }
            }
            //剩下最后一種可能,from/to都存在,那就不需要處理了
        }
        System.out.println(map);
        //只有一個連通分量,且能連通所有節點
        return map.keySet().size() == 1 && map.values().containsAll(nodes);

    }

    /**
     * 有向圖連通性判斷
     * 理論上有多少頂點就有多少key,且value都等於頂點數
     *
     * @param pairList
     * @return
     */
    public static Boolean directGraphConnect(Set<String> nodes, List<Pair<String, String>> pairList) {
        Multimap<String, String> map = HashMultimap.create();
        //對map初始化
        for (String node : nodes) {
            map.put(node, node);
        }

        for (Pair<String, String> pair : pairList) {
            String from = pair.getFrom();
            String to = pair.getTo();

            Collection<String> values = map.get(from);
            //把to加入from連通map
            if (!values.contains(to)) {
                map.put(from, to);
            }
            //所有之前能到from的,現在也能到to了
            for (String key : map.keySet()) {
                Collection<String> values2 = map.get(key);
                if (values2.contains(from) && !values2.contains(to)) {
                    values2.add(to);
                }
            }
            //所有之前to能到的,現在from也能到了
            Collection<String> value5 = map.get(to);
            for (String s : value5) {
                if (!map.get(from).contains(s)) {
                    map.put(from, s);
                }
            }


        }
        boolean connect = true;
        int nodeSize = nodes.size();
        for (String key : map.keySet()) {
            Collection<String> values3 = map.get(key);
            if (values3.size() != nodeSize) {
                connect = false;
            }
        }
        System.out.println(map);
        return connect;

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM