Skip to content

Commit

Permalink
Update docs from rizinorg/cutter
Browse files Browse the repository at this point in the history
Original Commit:
bce9fe7732890934f750d9d91f4093983dbb5830 Update GraphGridLayout Documentation (#3325)
  • Loading branch information
actions-user committed Apr 8, 2024
1 parent 66cb717 commit c15f3d7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
77 changes: 42 additions & 35 deletions docs/api/widgets/classGraphGridLayout.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,43 @@ <h1>GraphGridLayout<a class="headerlink" href="#graphgridlayout" title="Link to
<p>Basic familiarity with graph algorithms is recommended.</p>
<p><em>Terms used:</em></p>
<p><ul class="simple">
<li><p><strong>Vertex</strong>, <strong>node</strong>, <strong>block</strong> - read description of graph for definition. Within this text vertex and node are used interchangeably with block due to code being written for visualizing basic block control flow graph.</p></li>
<li><p><strong>edge</strong> - read description of graph for definition for precise definition.</p></li>
<li><p><strong>DAG</strong> - directed acyclic graph, graph using directed edges which doesn’t have cycles. DAG may contain loops if following them would require going in both directions of edges. Example 1-&gt;2 1-&gt;3 3-&gt;2 is a DAG, 2-&gt;1 1-&gt;3 3-&gt;2 isn’t a DAG.</p></li>
<li><p><strong>Vertex</strong>, <strong>node</strong>, <strong>block</strong> - see the definition of graph. Within this text vertex/node/block are used interchangeably due to the code being purposed for visualizing basic block control flow graph.</p></li>
<li><p><strong>edge</strong> - see the definition of graph.</p></li>
<li><p><strong>DAG</strong> - directed acyclic graph, a graph using directed edges which doesn’t have cycles. A DAG may contain loops if following them would require going in both directions of edges. Example 1-&gt;2 1-&gt;3 3-&gt;2 is a DAG, 2-&gt;1 1-&gt;3 3-&gt;2 isn’t a DAG.</p></li>
<li><p><strong>DFS</strong> - depth first search, a graph traversal algorithm</p></li>
<li><p><strong>toposort</strong> - topological sorting, process of ordering a DAG vertices that all edges go from vertices earlier in the toposort order to vertices later in toposort order. There are multiple algorithms for implementing toposort operation. Single DAG can have multiple valid topological orderings, a toposort algorithm can be designed to prioritize a specific one from all valid toposort orders. Example: for graph 1-&gt;4, 2-&gt;1, 2-&gt;3, 3-&gt;4 valid topological orders are [2,1,3,4] and [2,3,1,4].</p></li>
<li><p><strong>toposort</strong> - topological sorting, the process of ordering a DAG vertices that results in all edges going from vertices earlier in the toposort order to vertices later in toposort order. There are multiple algorithms implementing toposort. A single DAG can have multiple valid topological orderings, a toposort algorithm can be designed to prioritize a specific one from all valid toposort orders. Example: for graph 1-&gt;4, 2-&gt;1, 2-&gt;3, 3-&gt;4 valid topological orders are [2,1,3,4] and [2,3,1,4].</p></li>
</ul>
</p>
<p><em>High level structure of the algorithm</em></p>
<p><em>High level algorithm structure</em></p>
<p><ol class="loweralpha simple">
<li><p>select subset of edges that form a DAG (remove cycles)</p></li>
<li><p>toposort the DAG</p></li>
<li><p>choose a subset of edges that form a tree and assign layers</p></li>
<li><p>assign node positions within grid using tree structure, child subtrees are placed side by side with parent on top</p></li>
<li><p>perform edge routing</p></li>
<li><p>calculate column and row pixel positions based on node sizes and amount edges between the rows</p></li>
<li><p>[optional] layout compacting</p></li>
<li><p>Select a subset of edges that form a DAG (remove cycles)</p></li>
<li><p>Toposort the DAG</p></li>
<li><p>Choose a subset of edges that form a tree and assign layers</p></li>
<li><p>Assign node positions within grid using tree structure, child subtrees are placed side by side with parent on top</p></li>
<li><p>Perform edge routing</p></li>
<li><p>Calculate column and row pixel positions based on node sizes and amount edges between the rows</p></li>
<li><p>[optional] Layout compacting</p></li>
</ol>
</p>
<p>Contrary to many other layered graph drawing algorithm this implementation doesn’t perform node reordering to minimize edge crossing. This simplifies implementation, and preserves original control flow structure for conditional jumps ( true jump on one side, false jump on other). Due to most of control flow being result of structured programming constructs like if/then/else and loops, resulting layout is usually readable without node reordering within layers.</p>
<p><em>Description of grid.</em></p>
<p>To simplify the layout algorithm initial steps assume that all nodes have the same size and edges are zero width. After placing the nodes and routing the edges it is known which nodes are in in which row and column, how many edges are between each pair of rows. Using this information positions are converted from the grid cells to pixel coordinates. Routing 0 width edges between rows can also be interpreted as every second row and column being reserved for edges. The row numbers in code are using first interpretation. To allow better centering of nodes one above other each node is 2 columns wide and 1 row high.</p>
<p>Contrary to many other layered graph-drawing algorithms this implementation doesn’t perform node reordering to minimize edge crossing. This simplifies the implementation, and preserves the original control-flow structure for conditional jumps ( true jump on one side, false jump on other). Due to most of the control flow resulting from structured programming constructs like if/then/else and loops, the resulting layout is usually readable without node reordering within layers.</p>
<p><em>Grid</em></p>
<p>To simplify the layout algorithm, its initial steps assume that all nodes have the same size and that edges are zero-width. After nodes placement and edges rounting, the row/column of nodes is known as well as the amount of edges between each pair of rows. Using this information, positions are converted from grid cells to pixel coordinates. Routing zero-width edges between rows can also be interpreted as every second row and column being reserved for edges. The row numbers in code are using the first interpretation. To allow better centering of nodes one above other, each node is 2 columns wide and 1 row high.</p>
<p><img alt="../../_images/graph_grid.svg" src="../../_images/graph_grid.svg" /></p>
<p><em>1-2 Cycle removal and toposort</em></p>
<p>Cycle removal and toposort are done at the same time during single DFS traversal. In case entrypoint is part of a loop DFS started from entrypoint. This ensures that entrypoint is at the top of resulting layout if possible. Resulting toposort order is used in many of the following layout steps that require calculating some property of a vertex based on child property or the other way around. Using toposort order such operations can be implemented iteration through array in either forward or reverse direction. To prevent running out of stack memory when processing large graphs DFS is implemented non-recursively.</p>
<p>Cycle removal and toposort are done in a single DFS traversal. In case the entrypoint is part of a loop, the DFS starts from the entrypoint. This ensures that the entrypoint is at the top of resulting layout, if possible. The resulting toposort order is used in many of the following layout steps that require calculating some property of a vertex based on a child property or the other way around. Using toposort order, such operations can be implemented by array iteration in either forward/backward direction. To prevent running out of stack memory when processing large graphs, DFS is implemented non-recursively.</p>
<p><em>Row assignment</em></p>
<p>Rows are assigned in toposort order from top to bottom, with nodes row being max(predecessor.row)+1. This ensures that loop edges are only ones going from deeper levels to previous layers.</p>
<p>To further simply node placement a subset of edges is selected which forms a tree. This turns DAG drawing problem into a tree drawing problem. For each node in level n following nodes which have level exactly n+1 are greedily assigned as child nodes in tree. If a node already has parent assigned then corresponding edge is not part of tree.</p>
<p><em>Node position assignment</em></p>
<p>Since the graph has been reduced to a tree, node placement is more or less putting subtrees side by side with parent on top. There is some room for interpretation what exactly side by side means and where exactly on top is. Drawing the graph either too dense or too big may make it less readable so there are configuration options which allow choosing these things resulting in more or less dense layout.</p>
<p>Once the subtrees are placed side by side. Parent node can be placed either in the middle of horizontal bounds or in the middle of direct children. First option results in narrower layout and more vertical columns. Second option results in nodes being more spread out which may help seeing where each edge goes.</p>
<p>In more compact mode two subtrees are placed side by side taking into account their shape. In wider mode bounding box of shorter subtree is used instead of exact shape. This gives slightly sparse layout without it being too wide.</p>
<p>Rows are assigned in toposort order from top to bottom, with nodes row being max(predecessor.row)+1. This ensures that loop back-edges are the only edges going from lower to higher layers.</p>
<p>To further simply node placement, a subset of edges is selected which forms a tree. This turns a DAG drawing problem into a tree drawing problem. For each node in level n the following nodes with level exactly n+1 are greedily assigned as child nodes in the tree. If a node already has a parent assigned then the corresponding edge is not part of the tree.</p>
<p><em>Node placement</em></p>
<p>Since the graph has been reduced to a tree, node placement is more or less putting subtrees side by side with parent on top. There is some room for interpretation as to what exactly side by side means and where exactly on top is: drawing the graph either too dense or too sparse may make it less readable, so there are configuration options which allow choosing these things resulting in more or less dense layout.</p>
<p>Once the subtrees are placed side by side, the parent node can be placed either in the middle of the horizontal bounds or in the middle of its direct children. The first option results in narrower layout and more vertical columns, while the second option results in more spread out layout which may help seeing where each edge goes.</p>
<p>In compact mode two subtrees are placed side by side accounting for their shape. In wider mode the bounding box of the shorter subtree is used instead of its exact shape. This gives slightly sparser layout without being too wide.</p>
<p><img alt="../../_images/graph_parent_placement.svg" src="../../_images/graph_parent_placement.svg" /></p>
<p><em>Edge routing</em></p>
<p>Edge routing can be split into: main column selection, rough routing, segment offset calculation.</p>
<p>Transition from source to target row is done using single vertical segment. This is called main column.</p>
<p>A sweep line is used for computing main columns: Blocks and edges are processed as events top to bottom based off their row (max(start row, end row) for edges). Blocked columns are tracked in a tree structure which allows searching nearest column with at least last N rows empty. The column of the starting block is favored for the main column, otherwise the target block’s column is chosen if it is not blocked. If both the source and target columns are blocked, nearest unblocked column is chosen. An empty column can always be found, in the worst case there are empty columns at the sides of drawing. If two columns are equally close, the tie is broken based on whether the edge is a true or false branch. In case of upward edges it is allowed to choose a column on the outside which is slightly further than nearest empty to reduce the chance of producing tilted figure 8 shaped crossing between two blocks.</p>
<p>Rough routing creates the path of edge using up to 5 segments using grid coordinates. Due to nodes being placed in a grid. Horizontal segments of edges can’t intersect with any nodes. The path for edges is chosen so that it consists of at most 5 segments, typically resulting in sideways U shape or square Z shape.<ul class="simple">
<p>Edge routing can be split into: main column selection, rough routing, and segment offset calculation.</p>
<p>Transition from source to target row is done using a single vertical segment. This segment is called the ‘main column.</p>
<p>Main columns are computed using a sweep line: blocks and edges are processed as events top to bottom based off their row (max(start row, end row) for edges). Blocked columns are tracked in a tree structure which allows searching nearest column with at least last N rows empty. The column of the starting block is favored for the main column, otherwise the target block’s column is chosen if it is not blocked. If both the source and target columns are blocked, nearest unblocked column is chosen. An empty column can always be found, in the worst case there are empty columns at the sides of drawing. If two columns are equally close, the tie is broken based on whether the edge is a true or false branch. In case of upward edges it is allowed to choose a column on the outside which is slightly further than nearest empty to reduce the chance of producing tilted figure 8 shaped crossing between two blocks.</p>
<p>Due to nodes being placed in a grid, horizontal segments of edges can’t intersect with any nodes. The path for edges is chosen so that it consists of at most 5 segments, typically resulting in sideways U shape or square Z shape:<ul class="simple">
<li><p>short vertical segment from node to horizontal line</p></li>
<li><p>move to empty column</p></li>
<li><p>vertical segment between starting row and end row</p></li>
Expand All @@ -110,21 +110,28 @@ <h1>GraphGridLayout<a class="headerlink" href="#graphgridlayout" title="Link to
<li><p>column above target node is empty - segments 3-5 are merged</p></li>
</ul>
</p>
<p>After rough routing segment offsets are calculated relative to their corresponding edge column. This ensures that two segments don’t overlap. Segment offsets within each column are assigned greedily with some heuristics for assignment order to reduce amount of edge crossings and result in more visually pleasing output for a typical CFG graph. Each segment gets assigned an offset that is maximum of previously assigned offsets overlapping with current segment + segment spacing. Assignment order is chosen based on: direction of previous and last segment - helps reducing crossings and place the segments between nodes segment length - reduces crossing when segment endpoints have the same structure as valid parentheses expression edge length - establishes some kind of order when single node is connected to many edges, typically a block with switch statement or block after switch statement.</p>
<p>After rough routing segment offsets are calculated relative to their corresponding edge column. This ensures that two segments don’t overlap. Segment offsets within each column are assigned greedily with some heuristics for assignment order to reduce amount of edge crossings and result in more visually pleasing output for a typical CFG graph. Each segment gets assigned an offset that is maximum of previously assigned offsets overlapping with current segment + segment spacing.</p>
<p>Assignment order is based on:<ul class="simple">
<li><p>direction of previous and last segment - helps reducing crossings and place the segments between nodes</p></li>
<li><p>segment length - reduces crossing when segment endpoints have the same structure as valid parentheses expression</p></li>
<li><p>edge length - establishes some kind of order when single node is connected to many edges, typically a block with switch statement or block after switch statement.</p></li>
</ul>
</p>
<p><em>Layout compacting</em></p>
<p>Doing the layout within a grid causes minimal spacing to be limited by widest and tallest block within each column and row. One common case is block with function entrypoint being wider due to function name causing wide horizontal space between branching blocks. Another case is rows in two parallel columns being aligned.</p>
<p>Doing the layout on a grid limits the minimal spacing to the widest block within a column and tallest block within a row. One common case is a function-entry block being wider due to the function name, causing wide horizontal space between branching blocks. Another case is rows in two parallel columns being aligned.</p>
<p><img alt="../../_images/layout_compacting.svg" src="../../_images/layout_compacting.svg" /></p>
<p>Both problems are mitigated by squishing graph. Compressing in each of the two direction is done separately. The process is defined as liner program. Each variable represents a position of edge segment or node in the direction being optimized.</p>
<p>Following constraints are used<ul class="simple">
<p>Both problems are mitigated by squishing the graph. Compressing in each of the two direction is done separately. The process is defined as liner program. Each variable represents a position of edge segment or node in the direction being optimized.</p>
<p>The following constraints are used:<ul class="simple">
<li><p>Keep the order with nearest segments.</p></li>
<li><p>If the node has two outgoing edges, one to the node on left side and other to the right, keep them on the corresponding side of node’s center.</p></li>
<li><p>For all edges keep the node which is above above. This helps when vertical block spacing is set bigger than double edge spacing and edge shadows relationship between two blocks.</p></li>
<li><p>If a node has two outgoing edges, one to the left and one to the right, keep them on the corresponding side of the node’s center.</p></li>
<li><p>Equality constraint to keep relative position between nodes and and segments directly connected to them.</p></li>
<li><p>Equality constraint to keep the node centered when control flow merges In the vertical direction objective function minimizes y positions of nodes and lengths of vertical segments. In the horizontal direction objective function minimizes lengths of horizontal segments.</p></li>
<li><p>For all blocks connected by forward edge, keep the vertical distance at least as big as configured block vertical spacing. This helps when vertical block-spacing is set bigger than double edge spacing and an edge shadows relationship between two blocks.</p></li>
<li><p>Equality constraint to keep a node centered when control flow merges.</p></li>
</ul>
</p>
<p>In the resulting linear program all constraints beside x_i &gt;= 0 consist of exactly two variables: either x_i - x_j &lt;= c_k or x_i = x_j + c_k.</p>
<p>Since it isn’t necessary get perfect solution and to avoid worst case performance current implementation isn’t using a general purpose linear programming solver. Each variable is changed until constraint is reached and afterwards variables are grouped and changed together. </p>
<p>In the vertical direction the objective function minimizes y positions of nodes and lengths of vertical segments. In the horizontal direction the objective function minimizes the lengths of horizontal segments.</p>
<p>In the resulting linear program all constraints besides x_i &gt;= 0 consist of exactly two variables: either x_i - x_j &lt;= c_k or x_i = x_j + c_k.</p>
<p>Since a perfect solution isn’t necessary and to avoid worst case performance, the current implementation isn’t using a general purpose linear solver. Instead, each variable is modified until a constraint is satisfied and afterwards variables are grouped and modified together. </p>
<div class="breathe-sectiondef docutils container">
<p class="breathe-sectiondef-title rubric" id="breathe-section-title-public-types">Public Types</p>
<dl class="cpp enum-class">
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

0 comments on commit c15f3d7

Please sign in to comment.