• 图02--[图边删除&&图顶点删除&&图遍历BFS&&DFS]


    1.图顶点和边删除

    1.1删除边

    @Override
        public void removeEdge(V from, V to) {
            Vertex<V, E> fromVertex = vertices.get(from);
            if (fromVertex == null) return;
            
            Vertex<V, E> toVertex = vertices.get(to);
            if (toVertex == null) return;
            
            Edge<V, E> edge = new Edge<>(fromVertex, toVertex);
            if (fromVertex.outEdges.remove(edge)) {
                toVertex.inEdges.remove(edge);
                edges.remove(edge);
            }
        }
    View Code

    1.2删除点

    @Override
        public void removeVertex(V v) {
            Vertex<V, E> vertex = vertices.remove(v);
            if (vertex == null) return;
    
            for (Iterator<Edge<V, E>> iterator = vertex.outEdges.iterator(); iterator.hasNext();) {
                Edge<V, E> edge = iterator.next();
                edge.to.inEdges.remove(edge);
                // 将当前遍历到的元素edge从集合vertex.outEdges中删掉
                iterator.remove(); 
                edges.remove(edge);
            }
    
            for (Iterator<Edge<V, E>> iterator = vertex.inEdges.iterator(); iterator.hasNext();) {
                Edge<V, E> edge = iterator.next();
                edge.from.outEdges.remove(edge);
                // 将当前遍历到的元素edge从集合vertex.inEdges中删掉
                iterator.remove(); 
                edges.remove(edge);
            }
        }
    View Code

    2.图的遍历

    2.1常见方式

     2.2广度优先搜索(Breadth First Search)

     

     2.3广度优先搜索--思路

     2.4广度优先搜索-实现

    @Override
        public void bfs(V begin, VertexVisitor<V> visitor) {
            if (visitor == null) return;
            Vertex<V, E> beginVertex = vertices.get(begin);
            if (beginVertex == null) return;
            
            Set<Vertex<V, E>> visitedVertices = new HashSet<>();
            Queue<Vertex<V, E>> queue = new LinkedList<>();
            queue.offer(beginVertex);
            visitedVertices.add(beginVertex);
            
            while (!queue.isEmpty()) {
                Vertex<V, E> vertex = queue.poll();
                if (visitor.visit(vertex.value)) return;
                
                for (Edge<V, E> edge : vertex.outEdges) {
                    if (visitedVertices.contains(edge.to)) continue;
                    queue.offer(edge.to);
                    visitedVertices.add(edge.to);
                }
            }
        }
    View Code

    2.5深度优先搜索(Depth First Search)

     

     2.6深度优先递归实现

    @Override
        public void dfs(V begin, VertexVisitor<V> visitor) {
            if (visitor == null) return;
            Vertex<V, E> beginVertex = vertices.get(begin);
            if (beginVertex == null) return;
    
            Set<Vertex<V, E>> visitedVertices = new HashSet<>();
            Stack<Vertex<V, E>> stack = new Stack<>();
            
            // 先访问起点
            stack.push(beginVertex);
            visitedVertices.add(beginVertex);
            if (visitor.visit(begin)) return;
            
            while (!stack.isEmpty()) {
                Vertex<V, E> vertex = stack.pop();
                
                for (Edge<V, E> edge : vertex.outEdges) {
                    if (visitedVertices.contains(edge.to)) continue;
                    
                    stack.push(edge.from);
                    stack.push(edge.to);
                    visitedVertices.add(edge.to);
                    if (visitor.visit(edge.to.value)) return;
                    
                    break;
                }
            }
        }
    View Code

     2.7 深度优先搜索--非递归版本

    @Override
        public void dfs(V begin, VertexVisitor<V> visitor) {
            if (visitor == null) return;
            Vertex<V, E> beginVertex = vertices.get(begin);
            if (beginVertex == null) return;
    
            Set<Vertex<V, E>> visitedVertices = new HashSet<>();
            Stack<Vertex<V, E>> stack = new Stack<>();
            
            // 先访问起点
            stack.push(beginVertex);
            visitedVertices.add(beginVertex);
            if (visitor.visit(begin)) return;
            
            while (!stack.isEmpty()) {
                Vertex<V, E> vertex = stack.pop();
                
                for (Edge<V, E> edge : vertex.outEdges) {
                    if (visitedVertices.contains(edge.to)) continue;
                    
                    stack.push(edge.from);
                    stack.push(edge.to);
                    visitedVertices.add(edge.to);
                    if (visitor.visit(edge.to.value)) return;
                    
                    break;
                }
            }
        }
    View Code
  • 相关阅读:
    Android 获取Bitmap方式
    o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    idea 全部报错找不到包
    idea Plugin "Maven Integration Extension" was not loaded: required plugin "Maven Integration" is disabled
    okhttp 通过网关请求服务端返回数据
    mongo 过滤查询条件后分组、排序
    OKhttp3
    linux安装consul集群
    spring cloud 订单调用用户
    WebSphere概要文件管理工具
  • 原文地址:https://www.cnblogs.com/ggnbnb/p/12604943.html
Copyright © 2020-2023  润新知