简体   繁体   English

Java中的有向图实现

[英]Directed graph implementation in Java

I'm taking a class on data structures in java right now and we are to figure out how to implement a digraph class. 我现在正在使用Java中的数据结构类,我们将弄清楚如何实现有向图类。
Just off the top, I thought that having a Linked List kind class with a value field and an array of link (self referential) fields like the code below: 就在最上面,我认为拥有一个具有值字段和链接(自引用)字段数组的链接列表种类类,如下面的代码:

    public class Digraph<T>
    {
        T vertex;
        Digraph<T>[] edge;

        public Digraph(T val, int maxDegree)
        {
            vertex = val;
            edge = new Digraph<T>[maxDegree];
        }
    }

After I wrote this out, I realized that this isn't a little to no mess method of approaching this prompt. 在我写完这些之后,我意识到这不是接近此提示的一种简单方法。 How can I implement a digraph that isn't as messy as my code bit up there? 如何实现不像我的代码那么混乱的有向图? Or is this an okay approach? 还是这样可以吗?

I think using your linked-list way to implement a graph is not a good ieda. 我认为使用链接列表方法来实现图形并不是一个好的想法。 Unlike linked list and tree, Graph is not a recursion structure in nature. 与链表和树不同,Graph本质上不是递归结构。

I will implement it based on the fact that a graph is consisted of a list of Vertexs, and each Vertex is connected to its neighbours by Edges : 我将基于以下事实来实现它: 一个图形由一个顶点列表组成,并且每个顶点都通过Edges连接到其邻居

class Graph<E> {
    public List<Vertex<E>> vertexs; 
}

class Vertex<E> {
    public E val;
    public List<Edge<E>> connections; 
}

class Edge<E> {
    public Vertex<E> source;
    public Vertex<E> destination;
}   

But the simplest way to represent a graph is to use an Adjacency_matrix 但是,表示图的最简单方法是使用Adjacency_matrix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM