美文网首页
2021-02-03 图的创建与遍历

2021-02-03 图的创建与遍历

作者: 止戈学习笔记 | 来源:发表于2021-02-03 23:49 被阅读0次

下面介绍图的创建与遍历,就算是给的不是int[][] metrics数组,我们转换成这样的数组,然后再应用图的基础算法就行。

/**
 * @Author: vividzcs
 * @Date: 2021/1/24 11:43 上午
 */
public class Graph {
    private Map<Integer, Node> nodes = new HashMap<>();
    private Set<Edge> edges = new HashSet<>();

    public static Graph createGraph(int[][] metrics) {
        Graph graph = new Graph();
        for (int i=0; i<metrics.length; i++) {
            int weight = metrics[i][0];
            int from = metrics[i][1];
            int to = metrics[i][2];

            if (!graph.nodes.containsKey(from)) {
                graph.nodes.put(from, new Node(from));
            }

            if (!graph.nodes.containsKey(to)) {
                graph.nodes.put(to, new Node(to));
            }

            Node currentFrom = graph.nodes.get(from);
            Node currentTo = graph.nodes.get(to);
            currentFrom.getNexts().add(currentTo);
            currentFrom.setOut(currentFrom.getOut() + 1);
            currentTo.setIn(currentTo.getIn() + 1);

            Edge edge = new Edge(weight, currentFrom, currentTo);
            currentFrom.getEdges().add(edge);
            graph.edges.add(edge);

        }
        return graph;
    }

    public void bfs(Graph graph, Node start) {
        if (start == null) {
            return;
        }
        List<Node> queue = new ArrayList<>();
        Set<Node> sets = new HashSet<>();
        queue.add(start);
        sets.add(start);
        while (queue.size()!=0) {
            Node currentNode = queue.remove(0);
            System.out.println(currentNode.getValue());
            for (Node node : currentNode.getNexts()) {
                if (!sets.contains(node)) {
                    queue.add(node);
                    sets.add(node);
                }
            }
        }
    }

    public void dfs(Graph graph, Node start) {
        List<Node> stack = new ArrayList<>();
        Set<Node> set = new HashSet<>();

        stack.add(start);
        set.add(start);

        System.out.println(start.getValue());
        while (stack.size()!=0) {
            Node currentNode = stack.remove(stack.size() - 1);
            for (Node node : currentNode.getNexts()) {

                if (!set.contains(node)) {
                    stack.add(currentNode);
                    stack.add(node);
                    set.add(node);
                    System.out.println(node.getValue());
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        int [][] metrics = {
                {2,1,3},
                {4,1,5},
                {4,5,1},
                {6,1,7},
                {2,3,5},
                {2,5,3},
                {2,5,7},
                {3,3,6},
                {1,5,6}
        };
        Graph graph = Graph.createGraph(metrics);
        graph.bfs(graph, graph.nodes.get(1));
        System.out.println();
        graph.dfs(graph, graph.nodes.get(1));
    }
}

/**
 * @Author: vividzcs
 * @Date: 2021/1/24 11:38 上午
 */
public class Node {
    private int value;
    private int in;
    private int out;
    private List<Node> nexts = new ArrayList<>();
    private List<Edge> edges = new ArrayList<>();

    public Node(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getIn() {
        return in;
    }

    public void setIn(int in) {
        this.in = in;
    }

    public int getOut() {
        return out;
    }

    public void setOut(int out) {
        this.out = out;
    }

    public List<Node> getNexts() {
        return nexts;
    }

    public void setNexts(List<Node> nexts) {
        this.nexts = nexts;
    }

    public List<Edge> getEdges() {
        return edges;
    }

    public void setEdges(List<Edge> edges) {
        this.edges = edges;
    }
}

/**
 * @Author: vividzcs
 * @Date: 2021/1/24 11:40 上午
 */
public class Edge {
    private int weight;
    private Node from;
    private Node to;

    public Edge(int weight, Node from, Node to) {
        this.weight = weight;
        this.from = from;
        this.to = to;
    }
}

相关文章

  • 2021-02-03 图的创建与遍历

    下面介绍图的创建与遍历,就算是给的不是int[][] metrics数组,我们转换成这样的数组,然后再应用图的基础...

  • 图的邻接表创建与遍历

    对比矩阵创建图## 如果图的边数,比较少,使用矩阵,会有大量空间浪费;这个时候,考虑另外一种存储结构方式,将数组和...

  • 图的创建和遍历

    图的存储结构常见的有两种:邻接矩阵和邻接表。 邻接表,存储方法跟树的孩子链表示法相类似,是一种顺序分配和链式分配相...

  • 图的创建和遍历

    目录 图的定义 数据结构中,图由顶点和边构成如下: 上图中数字代表顶点(Vertex),连接顶点的是边(Edge)...

  • 进程的遍历、获取与销毁

    进程的遍历、获取与销毁 创建快照遍历 函数实现 进程的获取与销毁 exp:

  • 数据结构

    二叉树的创建与遍历

  • 数据结构

    二叉树的创建与遍历 哈夫曼树的创建

  • 11.图的广度优先遍历与无权图的最短路径

    图的广度优先遍历与无权图的最短路径 点击这里,前提知晓... 一、图的广度优先遍历 和树的广度优先遍历的思想一样,...

  • 图与遍历

    图 图:图是一组由边组成的节点 相邻定点:AB是相邻的,AD是相邻的,AC是相邻的,但是AE不是相邻的路径:是顶点...

  • 图的存储与遍历

    图的存储与遍历 一.实验目的 掌握图的存储结构以及图的深度优先搜索遍历、最小生成树算法。 二.实验要求与内容 自构...

网友评论

      本文标题:2021-02-03 图的创建与遍历

      本文链接:https://www.haomeiwen.com/subject/yhsvtltx.html