Advanced maths

Stack and queue graph layout calculator with a vertex ordering witness

Optimize vertex order and edge pages for stack or queue layouts of a small simple graph.

Your numbers

Valid JSON; preserve the shown array or object structure.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A stack or queue layout first chooses a linear order of the graph's vertices. Each edge is then assigned to one page. The page count measures how many compatible edge groups are needed under the selected rule, and changing the vertex order can change that count.

THE FORMULAFor every vertex permutation modulo reversal, order each edge endpoints (a,b) with a<b. Build edge-conflict graph: stack conflict iff a<c<b<d or c<a<d<b; queue conflict iff a<c<d<b or c<a<b<d. Edges sharing an endpoint never conflict. Compute exact chromatic number of each conflict graph via bounded coloring search; minimize over orders. Return order, page assignment and all conflicts; an optimum requires every competing order and smaller page count resolved. Distinguish fixed-order mode from global optimization.

A worked example

With the prefilled inputs, minimumPages = 2.

Good to know

Order is optimized; shared-endpoint edges never conflict. Queue pages forbid nesting, stack pages forbid interleaving.

Place vertices along a line before assigning pages

A stack or queue layout first chooses a linear order of the graph's vertices. Each edge is then assigned to one page. The page count measures how many compatible edge groups are needed under the selected rule, and changing the vertex order can change that count.

Stack pages forbid pairs with alternating endpoints; queue pages forbid strictly nested edge pairs. The two restrictions solve different problems on the same input. Combining both modes on one page avoids treating a changed layout rule as an unrelated calculator or a duplicate graph entry.

A two-stack certificate for the complete four-vertex graphK4 needs two stack pages0123Stack 1: all except 1–3Stack 2: edge 1–3Edges 0–2 and 1–3 alternate in every order.
Scroll across to see the full diagram.
Put edge 1–3 on a second page. The interleaving edges 0–2 and 1–3 prove that one stack page cannot suffice.

Compare with the Minimum rectilinear graph crossings calculator on supplied planar sites. Optimize edge pages under forbidden alternating or nested endpoints in a linear vertex order; planar straight-line crossing minimization is a different layout problem.

Crossing and nesting are different inequalities

Use the prefilled input structure and preserve the declared column or index order. For positions a less than b and c less than d, the edges cross when their four distinct endpoints alternate, such as a<c<b<d. They nest when one interval lies strictly inside the other, such as a<c<d<b. Edges sharing an endpoint are compatible under these conventions.

The input graph must be simple, but isolated vertices should remain in the declared order. An edge drawn as an arc may look complicated because of the chosen geometry; compatibility is decided by endpoint positions, not by accidental intersections introduced by a plotting routine.

Color an edge-conflict graph for each order

Create a new conflict graph whose vertices correspond to the original edges. Join two conflict vertices precisely when those edges violate the selected stack or queue rule. A proper coloring of this graph assigns compatible edges to common pages, making the minimum page count its chromatic number.

For global optimization, repeat that exact coloring task over vertex orders, using reversal symmetry to avoid duplicate work. A solution for one order is only an upper bound for the graph's global stack or queue number until the other relevant orders have been considered.

Why the complete graph on four vertices needs two stacks

In any ordering of four vertices, the edge joining positions one and three alternates with the edge joining positions two and four. Both edges exist in the complete graph, so one stack page cannot hold every edge without a forbidden pair.

Two pages suffice: place one of that alternating pair on a second page and keep the remaining edges together. There is only one alternating pair among the six edges for a fixed four-vertex order. The lower-bound argument and the explicit assignment therefore meet at two.

Use an order-and-pages figure as a certificate

The SVG should place vertices on a labeled baseline and draw each page in a separate horizontal band. Page labels, distinct stroke patterns and a text edge list make the assignment legible without color. The same order must be used in the returned table and drawing.

A conflict table is especially useful when comparing stack and queue modes. An edge pair forbidden in one mode can be permitted in the other. Highlighting that pair explains the structural distinction more clearly than showing two unrelated decorative graph layouts.

Fixed-order answers are not global optima

A user-supplied ordering can answer how many pages that specific ordering requires. Optimizing the order asks an additional factorial search question. The result title and certificate must state which task was completed so readers do not mistake a fixed-order minimum for a graph invariant.

Empty-edge graphs deserve an explicit zero-page convention. Graphs with at least one edge need at least one page. Reporting a default page for an edgeless graph may be acceptable in another convention, but mixing those conventions between the table, example and tests would be misleading.

Report unresolved optimization honestly

The supported graph has at most seven vertices and twelve edges. Vertex permutations and conflict-graph colorings share finite budgets. If a budget is exceeded, the implementation raises an unresolved error rather than displaying the best assignment found so far as an optimum or exposing a partial upper-bound result.

Test the conflict predicates with shared endpoints, alternating endpoints and nested endpoints separately. Then test permutation and reversal invariance of the completed optimum. Those checks isolate indexing mistakes that could otherwise produce convincing but incorrect page drawings. The public prefill is the complete four-vertex graph in stack mode. Page numbers are zero-based labels for compatible edge groups, not geometric heights. The result gives one optimized vertex order and one page assignment; equivalent orders or page renamings can describe the same minimum. Queue mode uses the same form with its different nesting predicate. Order is optimized; shared-endpoint edges never conflict. Queue pages forbid nesting, stack pages forbid interleaving.

A few questions, answered

Can edges that meet at a vertex share a page?

Yes. Sharing an endpoint is allowed; the forbidden crossing or nesting patterns require four distinct endpoint positions.

Are stack and queue numbers always equal?

No. They use different forbidden patterns, so their optimal page counts can differ on the same graph.

Does a two-page drawing prove that two is optimal?

It proves an upper bound. Optimality also requires excluding one page across the relevant vertex orders.

Why does the calculator search vertex orders?

Compatibility depends on endpoint order. A poor fixed order can require more pages than a better ordering of the same graph.

Find your calculator

Start typing to explore the library.