Graph::plotGridGraph

Plots a Graph in a grid layout

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

Graph::plotGridGraph(G, <PointSize = n>, <VerticesPerLine = n>, <VertexOrder = [n1, …,nm]>, <SpecialVertices = [v1, …,vn]>, <SpecialEdges = [e1, …,en]>, <EdgeColor = RGB>, <SpecialEdgeColor = RGB>, <SpecialVertexColor = RGB>, <Vertex1Color = RGB>, <Vertex2Color = RGB>)

Description

Graph::plotGridGraph(G) returns a plot::Scene object in which the vertices are square ordered (topmost left to downmost right). The number of vertices per line is the floor of the squareroot of the number of the vertices. The first vertex is drawn in RGB::Blue and the second in RGB::Green. All other vertices are drawn in RGB::Red. The edges are drawn in RBG::Red. The width of the points is predefined with 40. If the last line contains only one vertex, it will be drawn centered in the middle of the line.

Graph::plotGridGraph(G, VerticesPerLine=n) returns a plot::Scene object like described above with one exception. In every line there are exactly n vertices. They appear in sorted order depending on their name. If the last row consists of only one vertex, this one will be centered.

Graph::plotGridGraph(G, VerticesPerLine=[v1..vn]) returns a plot::Scene object like described above with one exception. In line 1 there are exactly v1 vertices placed. In line 2 there are v2 vertices and so on. The last line contains vn vertices. They appear in sorted order depending on their name. The sum of the numbers specified in VerticesPerLine must equal the number of vertices in the graph.

Graph::plotGridGraph(G, VerticesPerLine=n, VertexOrder=[v1..vn]) returns a plot::Scene object like described above with one exception. In every line there are exactly n vertices. They appear in sorted order depending on the order that was specified in VertexOrder. vi can consist of any vertex defined as well as the substitute None. Nevertheless the number of vertices in G must not exceed the number of VerticesPerLine. If the last line holds only one vertex, it will be centered.

Examples

Example 1

First, a small graph is created and plotted with the default values:

G := Graph([a, b, c, d], [[a, b], [b, c], [c, d], [d, a]]):
plot(Graph::plotGridGraph(G))

Now, we exchange the two vertices c and d. The order given above was [a, b, c, d]:

plot(Graph::plotGridGraph(G, VertexOrder = [a, b, d, c]))

If only one vertex is placed in the last line, it will be centered:

plot(Graph::plotGridGraph(G, VertexOrder = [a, b, d, c],
                          VerticesPerLine = 3))

The same result can be gained by defining the Vertices per line specifically (in this case the number of vertices must be no less than the number of vertices in the graph):

plot(Graph::plotGridGraph(G, VertexOrder = [a, b, d, c],
                          VerticesPerLine = [3, 1]))

Now we get to the point, why the plot routine got the name Grid. The substitute None can be used whenever a place should be skipped. Think of some drawing paper with caskets. The layout is exactly the same. In this case it would consist of 3 caskets in each row. None leaves it blank, while a vertex from the Graph is drawn. Because the first casket is empty, the first color is omitted, too. The vertex a which is placed in the second casket is drawn as predefined in Vertex2Color (RGB::Green):

plot(Graph::plotGridGraph(G, 
       VertexOrder = [None, a,    None, 
                      b,    None, c,
                      None, d,    None], 
       VerticesPerLine = 3))

Example 2

With the knowledge obtained so far, it is possible to get deeper into the art of creating objects. One of the most usefull outputs is that of a tree. Thus a Graph is created to be used for the tree output:

TreeGraph := Graph([a, b, c, d, e, f, g, h, i, j, k, l],
             [[a, b], [a, c], [b, d], [b, e], [c, f], [c, g],
              [d, h], [e, i], [e, j], [f, k], [g, l]], Directed):

Next we define a special vertex order, because the vertices are not drawn the way they were defined:

vOrder := 
[None, None, None, None, None, None,
 a,    None, None, None, None, None,
 None, None, b,    None, None, None,
 None, None, None, c,    None, None,
 None, d,    None, None, e,    None,
 None, f,    None, None, g,    None,
 h,    None, None, i,    None, j,   
 None, None, k,    None, None, l    ]:

Now it is time to have a look at how the tree looks:

plot(Graph::plotGridGraph(TreeGraph,
              VerticesPerLine = 12, VertexOrder = vOrder))

Now we want to see the path from vertex a to Vertexb. For this example it will be given explicitly. For bigger graphs one of the shortestPath procedures is recommended:

specialPath := [[a, c], [c, f], [f, k]]:

Finally we draw the path inside the Graph and have a good overview about the path it takes:

plot(Graph::plotGridGraph(TreeGraph,
         VerticesPerLine = 12, VertexOrder = vOrder, 
         SpecialEdges = specialPath, SpecialEdgeColor = RGB::Blue))

Example 3

To show what can be done with more detailed and complex examples a small outer face is being drawn (have a close look ath the Vertices which are not drawn, because the color is set to RBG::White and thus equals the background-color). Additionaly, the "eyes" have been colored differently, so the usage of SpecialVertexColor could be presented:

Smile := Graph([1,2,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
               21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36],
         [[1,4],[2,5],[4,5],[7,4],[5,8],[7,10],[9,10],[10,14],[9,13],
          [13,19],[19,20],[20,14],[14,15],[15,16],[16,17],[17,11],
          [ 8,11],[11,12],[12,18],[18,22],[22,21],[21,17],[21,24],
          [24,28],[28,32],[32,36],[36,35],[35,34],[34,33],[33,29],
          [29,25],[25,23],[23,20],[26,30],[30,31],[31,27]]):
plot(
  Graph::plotGridGraph(Smile, VerticesPerLine = 10, 
     VertexOrder = [
       None,None,None,   1,None,None,   2,None,None,None,
       None,None,None,None,   4,   5,None,None,None,None,
       None,None,None,   7,None,None,   8,None,None,None,
       None,   9,  10,None,None,None,None,  11,  12,None,
         13,None,None,  14,  15,  16,  17,None,None,  18,
       None,  19,  20,None,None,None,None,  21,  22,None,
       None,None,  23,None,None,None,None,  24,None,None,
       None,None,  25,  26,None,None,  27,  28,None,None,
       None,None,  29,None,  30,  31,None,  32,None,None,
       None,None,None,  33,  34,  35,  36,None,None,None],
     SpecialEdges = [[13,9],[9,10],[10,14],[14,20],[20,19],[19,13],
       [14,15],[15,16],[16,17],[17,21],[21,22],[22,18],[18,12],[12,11],
       [11,17]],
     SpecialVertices = [1,2,4,5,7,8,23,24,25,26,27,28,29,30,31,
       32,33,34,35,36], 
     VertexColor = RGB::White, 
     SpecialVertexColor = RGB::Green, EdgeColor = RGB::Green, 
     SpecialEdgeColor = RGB::Black)
)

Example 4

The next example is a complete graph drawn with the vertices ordered in a square so that all vertices can be connected inside the square:

CG := Graph::createCompleteGraph(30):
plot(Graph::plotGridGraph(CG,
       VerticesPerLine = [8, 2, 2, 2, 2, 2, 2, 2, 8], 
       Vertex1Color = RGB::Red, Vertex2Color = RGB::Red))

Using the default values in every line vertices are drawn and the graph looks not as "dense" as the above one:

plot(Graph::plotGridGraph(CG, Vertex1Color = RGB::Red, 
                          Vertex2Color = RGB::Red))

Example 5

The last example shows a small kite inside a frame:

Shield := Graph([1, 2, 3, 4, 5, 6, 7, 8, 9],
          [[1, 2], [1, 8], [2, 9], [8, 9], [3, 5], [4, 5],
           [5, 6], [5, 7], [3, 6], [6, 7], [3, 4], [4, 7]]):
plot(Graph::plotGridGraph(Shield, Vertex1Color = RGB::White, 
       Vertex2Color = RGB::White, VertexColor = RGB::White,
       VerticesPerLine = 7, 
       VertexOrder = [
           1,None,None,None,None,None,   2,
        None,None,None,   3,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,   4,None,   5,None,   6,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,None,None,None,None,
        None,None,None,   7,None,None,None,
           8,None,None,None,None,None,   9],
       SpecialVertices = [3, 4, 5, 6, 7], 
       SpecialVertexColor = RGB::Blue,
       SpecialEdges = [[3, 5], [4, 5], [5, 6], [5, 7],
                       [3, 6], [6, 7], [3, 4], [4, 7]],
       SpecialEdgeColor = RGB::Blue))

Parameters

G

Graph

n

a positive integer

[n1, …,nm]

a list of positive integers

[v1, …,vn]

a list of vertices

[e1, …,en]

a list of edges

Options

PointSize

Defines the thickness in which the points are drawn. Default is 40.

VerticesPerLine

If specified as single number, this many vertices are placed in every row. If specified as list, the number of vertices per line are read out of the list. If the vertices are specified as list, the number of vertices must match either the number of vertices defined in the graph, or, if specified, the number defined in VertexOrder.

VertexOrder

Defines an order in which the vertices are to be placed. It starts in the upper left and ends in the lower right. The number of specified vertices must match the number of vertices defined in the graph or the sum of the vertices specified in VerticesPerLine.

SpecialVertices

Defines a set of vertices. This option makes only sense if used with the option SpecialVertexColor.

SpecialEdges

Defines a set of edges. This option makes only sense if used with the option SpecialEdgeColor.

EdgeColor

Defines a color with which to draw the edges. Default is RGB::Red

SpecialEdgeColor

Defines a color to be used to draw the set of edges specified. This option makes only sense if used with the option SpecialEdges

VertexColor

Defines a color with which to draw the vertices. If this option is specified, the first two vertices are set to this color, too. They must be specified via Vertex1Color and Vertex2Color to distinct them again. Default is RGB::Red

SpecialVertexColor

Defines a color to be used to draw the set of vertices specified. This option makes only sense if used with the option SpecialVertices

Vertex1Color

Defines a color with which to draw the uppermost left (first) vertex with (the starting vertex). If VertexOrder holds a None for this vertex, it will be skipped. Default is RGB::Blue

Vertex2Color

Defines a color with which to draw the second vertex with. If VertexOrder holds a None for this vertex, it will be skipped. Default is RGB::Green

Return Values

Plot::Scene.

Was this topic helpful?