zoukankan      html  css  js  c++  java
  • 无权图的遍历

     
    public class UnweightedGraph {
    
    
        private final List<String> vertexes = new ArrayList<String>();
        private final Map<String, List<String>> linkTable = new TreeMap<String, List<String>>();
    
    
        public List<String> getVertexes() {
            return vertexes;
        }
    
    
        public void addVertexes(String vertex) {
            if (!this.vertexes.contains(vertex.trim())) {
                this.vertexes.add(vertex.trim());
            }
        }
    
    
        public void removeVertexes(String vertex) {
            this.vertexes.remove(vertex.trim());
            linkTable.remove(vertex.trim());
            for (Map.Entry<String, List<String>> en : this.linkTable.entrySet()) {
                Iterator<String> iterator = en.getValue().iterator();
                while (iterator.hasNext()) {
                    String next = iterator.next();
                    if (next.trim().equals(vertex)) {
                        iterator.remove();
                    }
                }
            }
        }
    
    
        public void addEdge(String from, String to) {
            if (!this.vertexes.contains(from) || !this.vertexes.contains(to)) {
                return;
            }
            List<String> toSet = this.linkTable.get(from);
            if (toSet == null) {
                toSet = new ArrayList<>();
                this.linkTable.put(from, toSet);
            }
            if (!toSet.contains(to)) {
                toSet.add(to);
            }
        }
    
    
        public void addDoubleEdge(String from, String to) {
            this.addEdge(from, to);
            this.addEdge(to, from);
        }
    
    
        public void removeEdge(String from, String to) {
            if (!this.vertexes.contains(from) || !this.vertexes.contains(to)) {
                return;
            }
            List<String> toSet = this.linkTable.get(from);
            if (toSet != null) {
                toSet.remove(to);
            }
        }
    
    
      
    
    
        @Override
        public String toString() {
            String str = "";
            for (Map.Entry<String, List<String>> en : this.linkTable.entrySet()) {
                str += en.getKey() + " -> [ ";
                Iterator<String> iterator = en.getValue().iterator();
                while (iterator.hasNext()) {
                    String next = iterator.next();
                    str += next + "  ";
                }
                str += " ]
    ";
            }
            return "UnweightedGraph{
    " + str + '}';
        }
    
    
        //深度优先
        public List<String> dfs() {
            if (this.vertexes.isEmpty()) {
                return Collections.EMPTY_LIST;
            }
            List<String> results = new ArrayList<>();
            Stack<String> stack = new Stack<>();
            String first = this.getVertexes().get(0);
    
    
            results.add(first);
            stack.push(first);
    
    
            while (!stack.isEmpty()) {
                String top = stack.peek();
                List<String> linkedVertexes = this.linkTable.get(top);
                String theOne = null;
                if (linkedVertexes != null) {
                    for (String s : linkedVertexes) {
                        if (!results.contains(s)) {
                            theOne = s;
                            break;
                        }
                    }
                }
                if (theOne == null) {
                    stack.pop();
                } else {
                    results.add(theOne);
                    stack.push(theOne);
                }
    
    
            }
            return results;
        }
    
    
        //广度优先
        public List<String> bfs() {
            if (this.vertexes.isEmpty()) {
                return Collections.EMPTY_LIST;
            }
            List<String> results = new ArrayList<>();
            Queue<String> queue = new LinkedList<>();
            String first = this.getVertexes().get(0);
    
    
            results.add(first);
            queue.add(first);
    
    
            while (!queue.isEmpty()) {
                String top = queue.peek();
    
    
                List<String> linkedVertexes = this.linkTable.get(top);
                String theOne = null;
                if (linkedVertexes != null) {
                    for (String s : linkedVertexes) {
                        if (!results.contains(s)) {
                            theOne = s;
                            break;
                        }
                    }
                }
                 if (theOne == null) {
                    queue.poll();
                } else {
                    results.add(theOne);
                    queue.add(theOne);
                }
    
    
            }
            return results;
        }
    
    
        public static void main(String[] args) {
            UnweightedGraph g = new UnweightedGraph();
            g.addVertexes("a");
            g.addVertexes("b");
            g.addVertexes("c");
            g.addVertexes("d");
            g.addVertexes("e");
    
    
            g.addDoubleEdge("a", "b");
            g.addDoubleEdge("a", "c");
            g.addDoubleEdge("b", "e");
            g.addDoubleEdge("b", "d");
            g.addDoubleEdge("c", "d");
            g.addDoubleEdge("d", "e");
    
    
            System.out.println(g.toString());
    
    
            List<String> dfs = g.dfs();
            System.out.println(dfs.toString());
    
    
            List<String> bfs = g.bfs();
            System.out.println(bfs.toString());
    
    
        }
    
    
    }
    
  • 相关阅读:
    【IdentityServer4文档】- 整体情况
    【IdentityServer4文档】- 欢迎来到 IdentityServer4 (ASP.NET Core 3.x)
    证券相关基础概念
    [基于NetCore的简单博客系统]-登录
    JAVA mysql数据库 配置
    简单验证码识别及登陆并下载页面
    Java 无法初始化Connection的问题
    MUI scroll 定位问题
    CentOS7 安装 MySql
    ASP.NET CORE 2.0 文档中文正式版已经出来了
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276144.html
Copyright © 2011-2022 走看看