简体   繁体   中英

how to represent graph data strucutre using edge list in OCaml?

I am trying to represent a graph using disjoint union and record. The following code cause a syntax error. How to define two variables when they refer to each other?

type 'a vertex = 
  |Empty
  |Vertex of 'a * 'a list;; (*a tuple consisting of any type of element and a list of vertex*);

let v0 = Vertex(0,[v1]) in
let v1=Vertex(1,[v0]);;

If I revise the code in to:

let rec v0 = Vertex(0,[v1]) and  v1=Vertex(1,[v0]);;

I will get v0:

[Vertex (1,
  [Vertex (0,
    [Vertex (1,
      [Vertex (0,
        [Vertex (1,
          [Vertex (0,
            [Vertex (1,
              [Vertex (0,
                [Vertex (1,
                  [Vertex (0,
                    [Vertex (1,
                      [Vertex (0,
                        [Vertex (1,
                          [Vertex (0,
                            [Vertex (1,
                              [Vertex (0,
                                [Vertex (1,
                                  [Vertex (0,
                                    [Vertex (1,
                                      [Vertex (0,
                                        [Vertex (1,
                                          [Vertex (0,
                                            [Vertex (1,
                                              [Vertex (0,
                                                [Vertex (1,
                                                  [Vertex (0,
                                                    [Vertex (1,
                                                      [Vertex (0,
                                                        [Vertex (1,
                                                          [Vertex (0,
                                                            [Vertex (1,
                                                              [Vertex (0,
                                                                [Vertex (1,
                                                                  [Vertex (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [...])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])]

This appears not what I want...

What is the best way to define a graph record containing a list or array of vertex? Obviously I cannot do this:

type graph = {
    vertex_set:array};;

I get the following message:

Error: The type constructor array expects 1 argument(s),
       but is here applied to 0 argument(s)

There is an error in your type definition. I'm pretty sure what you want is:

type 'a vertex = 
  |Empty
  |Vertex of 'a * 'a vertex list

You can define a value of this type using let rec :

let rec v0 = Vertex(0, [v1]) and v1 = Vertex(1, [v0])

In my experience it's difficult to deal with values of this sort. OCaml is an eager language, so it's difficult to compute with cyclic values.

For the second question, the compiler is trying to tell you that array by itself isn't a type. You need to say what it's an array of .

type 'a vertices = {
    vertex_set: 'a vertex array;
}

If you want a list of vertices rather than an array (probably a good idea), it would look like this:

type 'a vertices = {
    vertex_set: 'a vertex list;
}

It seems to me you have two problems. First, you need to learn how to specify your types and values in OCaml. This isn't so hard, and you can get good advice here. Second, you need to decide on what data structure to use to represent your graphs. This is harder, and there's no overall best way to do it. You probably don't want to use a data structure that is itself a graph (as above). More realistic possibilities are: an adjacency matrix, or a collection of vertices with lists of their outgoing edges. To avoid creating cyclic structures, you can just refer to vertices rather than including them directly in your edge lists. If the vertices were in an array, for example, you could refer to them by array index.

There is a library named ocamlgraph that might be a better source of ideas than anything I can come up with!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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