GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Traverse Function with branched graph · maxGraph/maxGraph · Discussion #1098

Traverse Function with branched graph #1098

Unanswered
JaRoberts512 asked this question in Q&A
Traverse Function with branched graph #1098
@JaRoberts512 JaRoberts512
Jul 3, 2026 · 2 comments · 1 reply
Return to top
Discussion options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

JaRoberts512
Jul 3, 2026

This is an mxGraph question, if you want me to take this question elsewhere let me know

Quick question about the traverse function called on mxGraph types, how does it work with a graph that branches into multiple cells (example below)? If you're going in reverse starting from a node, then it's fine, as there is only one path back to the root node and you can specify the traverse function to stop at the root cell, but if you want the traversal to start at a root cell, and move to the end of a particular branch (node 3 for example), how does this work?

image

Also, I'm use the inverse param to specify reverse traversals, how does this work with the directed param? Is the 'only source to target' condition reversed to 'only target to source'?

Edit: Just remembered another thing, in the traverse function, edge is described as optional, but it is used in the visitor function so surely it must be neccessary?

You must be logged in to vote
All reactions

Replies: 2 comments · 1 reply

Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

u-m-i
Jul 18, 2026

Hi there! Are you trying to figure how the algorithm inside the library would work or trying to code a traverse algorithm, or both ( or neither xD)?

You must be logged in to vote
All reactions
1 reply
@JaRoberts512
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

JaRoberts512 Aug 18, 2026
Author

Trying to figure out how it would work. Like if you started at root, would you end up at # 1, # 3, or # 4?

All reactions
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

younghwan91
Nov 25, 2026

traverse doesn't choose a branch. It's a plain depth-first walk that visits every vertex reachable from the start once, so starting at the root you don't "end up" at 1, 3 or 4: your visitor is called for all of them. The order is depth-first in the order of each vertex's edges (model.getEdgeAt(vertex, i), i.e. roughly the order the edges were connected): first branch fully, then the next one (mxGraph.js).

Your other questions, from the same code:

  • return false from the visitor only stops descending below that vertex; sibling branches are still visited. There's no built-in "stop everything".
  • The edge test is !directed || (!inverse == isSource): with directed = true, inverse = true it follows only incoming edges (target to source); with directed = false, inverse is ignored and edges are followed both ways.
  • edge is optional for you as the caller (you normally don't pass it). The visitor always gets the edge used to reach the vertex, which is null/undefined for the start vertex.

So to get the root -> node 3 branch, let the walk record how each vertex was reached and walk back from the target:

function pathFromRoot(graph, root, target) {
  const model = graph.getModel();
  const incoming = new Map();          // vertex -> edge used to reach it
  graph.traverse(root, true, (vertex, edge) => {
    if (edge != null) incoming.set(vertex, edge);
    return vertex !== target;          // don't go below the target
  });
  if (target !== root && !incoming.has(target)) return null; // not reachable

  const path = [target];
  for (let v = target; v !== root; ) {
    v = model.getTerminal(incoming.get(v), true);  // source of the incoming edge
    path.unshift(v);
  }
  return path;                          // [root, ..., target]
}

In maxGraph the method has the same logic but takes an object: layout.traverse({ vertex, directed, func }) on a GraphLayout (GraphLayout.ts), and it has no inverse option.

If this solves it, please mark it as the answer so others with the same problem can find it.

You must be logged in to vote
All reactions
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
@JaRoberts512 @younghwan91 @u-m-i
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.