首页 文章

Java中的定向图实现

提问于
浏览
0

我正在学习java中的数据结构课程,我们将弄清楚如何实现digraph类 .
就在顶部,我认为有一个带有值字段的链接列表类类和一个链接(自引用)字段数组,如下面的代码:

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

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

在我写完这篇文章之后,我意识到这对于接近这个提示并不是一点点麻烦 . 我怎样才能实现一个不像我的代码那么混乱的有向图?或者这是一个好的方法吗?

1 回答

  • 1

    我认为使用链接列表方式来实现图表并不是一个好的ieda . Unlike linked list and tree, Graph is not a recursion structure in nature.

    我将基于 a graph is consisted of a list of Vertexs, and each Vertex is connected to its neighbours by 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;
    }
    

    但表示图形的最简单方法是使用Adjacency_matrix

相关问题