networks-guide.mws

Using the Maple `networks' Package

Philip Zeyliger

July 10, 2003

Abstract: This worksheet explains, by example, how to use Maple's networks package.

Preliminaries:

To use the networks package, you first need to read it in, like so:

>    with(networks):

For more detailed help, you'll want to peruse the only help accessible with "?networks".

Creating Graphs:

There are several different ways to create graphs.  We will illustrate by building the complete graph on four vertices.  The first is to create an empty graph using new and add on edges and vertices:

>    G:=new():

>    addvertex({1,2,3,4},G):

>    addedge([{1,2},{2,3},{3,4},{1,3},{1,4},{2,4}],G):

If we had wanted to specify specific weights, we could have done:

>    addedge([{1,2},{2,3},{3,4},{1,3},{1,4},{2,4}],weights=[1,2,3,4,5,6],G):

Directed graphs are created by specifying lists ([x,y]) instead of sets ({x,y}) as the edges.

>    addedge([[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]],weights=[1,2,3,4,5,6],G):

Alternately, if you want a graph with default edge weights (which is 1), you can use the graph constructor:

>    G:=graph({1,2,3,4},{{1,2},{2,3},{3,4},{1,3},{1,4},{2,4}}):

The other way to make a graph is use a procedure that creates it for you.  Maple has a few built-in, and "laplacian.mpl" has a few more.  Here are Maple's built-in ones:

>    G:=cycle(4):              # Circle with 4 vertices

>    G:=complete(3,3):         # Complete graph K_{3,3}

>    G:=complete(4):           # Complete graph K_4

>    G:=dodecahedron():
G:=icosahedron():
G:=tetrahedron():
G:=cube():
G:=octahedron():          # Platonic solids

Drawing Graphs

After you've created a graph it's helpful to visualize it by asking Maple to draw it.  The drawing isn't perfect--Maple will sometimes decide to plot an edge right on top of another edge so you can't tell them apart.  Unfortunately, though Maple labels the vertices, it does not label the edges or draws their weights.  It is possible, though not implemented, to change the procedures to add that labelling.  Also note that the lenghts of the drawn edges does not correspond to the weights assigned.

>    draw(dodecahedron());        # standard 2D graph

[Maple Plot]

>    draw(Linear([1,2,3]),complete(3,3)); # bi-partite drawing

[Maple Plot]

>    draw(Concentric([2,3,4]),octahedron()); # 2,3,4 are concentric

[Maple Plot]

>    draw3d(icosahedron());

[Maple Plot]

Manipulating and Exploring Graphs

Now comes the more interesting part; exploring the structure of the graph object (which is really a procedure of type GRAPH).  The vertices and edges are just stored in a set; all the names have to be unique (you can't have an edge and a vertex with the same name), but otherwise names are free form.  The default thing is to name the vertices 1 through n and label the edges e1 through em.  For example,

>    G:=cube():vertices(G);edges(G);

{0, 1, 2, 3, 4, 5, 6, 7}

{e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12}

To find the vertices attached to a (non-directed) edge, use the ends command:

>    ends(e1,G);

{6, 7}

To find the edge from a pair of vertices, you have to use the EdgeIndex, which is somewhat of a pain:

>    G(_EdgeIndex)[6,7];

{e1}

The edge weights are stored in a table which can be found by the eweight command.  You can modify that table to change the weights, e.g.:

>    eweight(G);

TABLE([e8 = 1, e5 = 1, e1 = 1, e3 = 1, e10 = 1, e4 = 1, e7 = 1, e12 = 1, e9 = 1, e2 = 1, e6 = 1, e11 = 1])
TABLE([e8 = 1, e5 = 1, e1 = 1, e3 = 1, e10 = 1, e4 = 1, e7 = 1, e12 = 1, e9 = 1, e2 = 1, e6 = 1, e11 = 1])
TABLE([e8 = 1, e5 = 1, e1 = 1, e3 = 1, e10 = 1, e4 = 1, e7 = 1, e12 = 1, e9 = 1, e2 = 1, e6 = 1, e11 = 1])

>    eweight(G)[e2]:=3;

eweight(G)[e2] := 3

>    eweight(G);

TABLE([e8 = 1, e5 = 1, e1 = 1, e3 = 1, e10 = 1, e4 = 1, e7 = 1, e12 = 1, e9 = 1, e2 = 3, e6 = 1, e11 = 1])
TABLE([e8 = 1, e5 = 1, e1 = 1, e3 = 1, e10 = 1, e4 = 1, e7 = 1, e12 = 1, e9 = 1, e2 = 3, e6 = 1, e11 = 1])
TABLE([e8 = 1, e5 = 1, e1 = 1, e3 = 1, e10 = 1, e4 = 1, e7 = 1, e12 = 1, e9 = 1, e2 = 3, e6 = 1, e11 = 1])

To assign random weights (integers 1 through 10, say) we can write a little for loop:

>    for e in edges(G) do
   eweight(G)[e]:= (rand() mod 10) + 1;
end do:
eweight(G);

TABLE([e8 = 2, e5 = 8, e1 = 9, e3 = 6, e10 = 6, e4 = 3, e7 = 1, e12 = 9, e9 = 2, e2 = 5, e6 = 1, e11 = 4])
TABLE([e8 = 2, e5 = 8, e1 = 9, e3 = 6, e10 = 6, e4 = 3, e7 = 1, e12 = 9, e9 = 2, e2 = 5, e6 = 1, e11 = 4])
TABLE([e8 = 2, e5 = 8, e1 = 9, e3 = 6, e10 = 6, e4 = 3, e7 = 1, e12 = 9, e9 = 2, e2 = 5, e6 = 1, e11 = 4])

To make a copy of a graph, use the duplicate command, e.g.:

>    G:=cube():H:=duplicate(G):G:=cycle(3):

>    vertices(G);           # a cycle

{1, 2, 3}

>    vertices(H);           # a cube

{0, 1, 2, 3, 4, 5, 6, 7}

What else is there?

The help for "networks" lists many more functions which you can use to manipulate graphs.  A different good place to start is the laplacian.mpl code that has functions relating to metrized graphs but uses the graphs package extensively.