栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【数据结构的魅力】008.图

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【数据结构的魅力】008.图

自定义简单的图结构 点集
import java.util.ArrayList;

//点结构的描述
public class Node {
    public int value;
    //入度
    public int in;
    //出度
    public int out;
    public ArrayList nexts;
    public ArrayList edges;

    public Node(int value){
        this.value = value;
        in = 0;
        out = 0;
        nexts = new ArrayList<>();
        edges = new ArrayList<>();
    }
}
边集
public class Edge{
    public int weight;
    public Node from;
    public Node to;

    public Edge(int weight,Node from,Node to){
        this.weight = weight;
        this.from = from;
        this.to = to;
    }
}
图集
import java.util.HashMap;
import java.util.HashSet;

public class Graph {
    public HashMap nodes;
    public HashSet edges;

    public Graph(){
        nodes = new HashMap<>();
        edges = new HashSet<>();
    }
}
其他问题转化为此自定义结构的接口
public class GraphGenerator {
    public static Graph createGraph(Integer[][] matrix){
        Graph graph = new Graph();
        for(int i=0;i 
广度优先 
import java.util.HashSet;
import java.util.linkedList;
import java.util.Queue;

public class BFS {
    public static void bfs(Node node){
        if(node == null){
            return;
        }
        Queue queue = new linkedList<>();
        HashSet set = new HashSet<>();
        queue.add(node);
        set.add(node);
        while (!queue.isEmpty()){
            Node cur = queue.poll();
            System.out.println(cur.value);
            for(Node next : cur.nexts){
                if(!set.contains(next)){
                    set.add(next);
                    queue.add(next);
                }
            }
        }
    }
}
深度优先
import java.util.HashSet;
import java.util.Stack;

public class DFS {
    public static void dfs(Node node){
        if(node == null){
            return;
        }
        Stack stack = new Stack<>();
        HashSet set = new HashSet<>();
        stack.add(node);
        set.add(node);
        System.out.println(node.value);
        while (!stack.isEmpty()){
            Node cur = stack.pop();
            for(Node next : cur.nexts){
                if(!set.contains(next)){
                    stack.push(cur);
                    stack.push(next);
                    set.add(next);
                    System.out.println(next.value);
                    break;
                }
            }
        }
    }
}
拓扑排序
import java.util.*;

public class TopologySort {
    public static List sortedTopology(Graph graph){
        HashMap inMap = new HashMap<>();
        //只有入度为0的点才能进入队列
        Queue zeroInQueue = new linkedList<>();
        for(Node node : graph.nodes.values()){
            inMap.put(node, node.in);
            if(node.in == 0){
                zeroInQueue.add(node);
            }
        }
        //拓扑排序的结果,依次加入result
        List result = new ArrayList<>();
        while (!zeroInQueue.isEmpty()){
            Node cur = zeroInQueue.poll();
            result.add(cur);
            for(Node next : cur.nexts){
                inMap.put(next,inMap.get(next)-1);
                if(inMap.get(next) == 0){
                    zeroInQueue.add(next);
                }
            }
        }
        return result;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号