skip to main content
Diagrammer > Programmer's documentation > Using graph layout algorithms > Layout algorithms > Typical ways to choose a layout
 
Typical ways to choose a layout
Explains possible ways to choose a graph layout algorithm.
*Choosing a layout algorithm
*Explains the difference between automatic and semiautomatic layout selection.
*Choosing the layout algorithm dynamically
*Explains how to choose a layout algorithm automatically at run time.
*Hard-coding a layout at programming time
*Explains how to choose a layout at programming time.
*Hard-coding a layout at run time
*Explains how to choose a layout at run time.
Choosing a layout algorithm
The choice of the appropriate algorithm for a graph can be done either by the user at run time or by the programmer when developing the application. This process can be semiautomatic, when the user is involved, or automatic, when the application does everything with no user intervention.
As a programmer of applications, you can choose Semiautomatic layout to involve the user in the choice of the layout, or Automatic layout, in which case the application does everything with no user action.
Semiautomatic layout
For applications using a semiautomatic layout, the choice of the layout algorithm is done by the user. The application can provide a menu or some other way to select the layout algorithm.
In some cases, it can be an iterative process. The user may try different layout algorithms with different values for the parameters or may apply manual refinements to find the best layout, or both. The application can provide some help by using textual explanations or by automatically checking the graph to find out to which class it belongs.
For example, to detect whether the graph that has been attached to a layout instance is a tree, the IlvGraphLayoutUtil class provides the method:
 
static boolean IsTree(IlvGraphLayout layout, Object startNode)
For details on this method, see IsTree. See also Attaching or detaching a grapher.
Automatic layout
If an automatic layout is needed, the choice of the layout algorithm can be:
*Chosen dynamically at run time with heuristics or rules to determine the appropriate layout algorithm depending on the structure or size of the graph, or both
*Hard-coded if the developer knows what types of graphs are used and can determine the appropriate layout algorithm.
Choosing the layout algorithm dynamically
If nothing is known about the graphs that the application needs to lay out, the developer can write a routine that automatically chooses the layout algorithm at run time. The following simple rules could be applied:
1. If the nodes of the graph cannot be moved (they are geopositioned), use a Link Layout.
2. If the graph is a tree, use the Tree Layout.
3. Otherwise, use one of the layout algorithms that are less restricted to a specific category of graph, especially the Uniform Length Edges Layout. (The preferred length of the links could also be computed with respect to the size of the nodes.)
4. If the graph is too large, apply a “divide-and-conquer” strategy. Cut the graph into several subgraphs and apply the layout separately to each subgraph. If the graph is disconnected, you can use the built-in support provided by the layout library to perform this task automatically. (See Layout of connected components.)
5. If the graph is nested, use the Recursive Layout algorithm that controls which subgraph is laid out by which (flat) sublayout. Use steps 1 to 4 to determine the sublayouts for the subgraphs. The Hierarchical Layout and the Tree Layout also have special modes for nested graphs, see Recursive mode and Recursive layout.
Hard-coding a layout at programming time
A special case occurs when the application will deal with only a small set of graphs that are known at the time the application is built. In this case, the layout can be performed at programming time. A possible step-by-step procedure can be:
1. Create each graph manually with a graph editor or in Java™.
2. Try different layout algorithms and choose the best for each graph.
3. Apply manual refinements to the layout if needed.
4. Store the result of the layout by saving the coordinates of the nodes in the XML file of the diagram component, or by saving the graphers in .ivl files.
5. Provide these files with the application.
When the application is used, the ivl files are loaded. No need to perform the layout again, because it is already done.
Hard-coding a layout at run time
If the choice of the layout algorithm is hard-coded, but the layout must be performed at run time because the graphs are not known at programming time, one possible step-by-step procedure for the choice of the appropriate layout algorithm can be:
1. Look at sample graphs for your domain.
2. Try to determine some generalities about the properties of the structure and the size of the graph (Is the graph cyclic? Is the graph a tree? Is the graph a combination of the two? What is the number of nodes and links in the graph?)
3. Pick an appropriate layout algorithm.
4. Try out the algorithm on one or more samples.

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.