GeezerVison is showing github.com — Traverse Function with branched graph · maxGraph/maxGraph · Discussion #1098
Traverse Function with branched graph
#1098
Jul 3, 2026
·
2 comments
·
1 reply
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.
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?
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
Replies:
2 comments
·
1 reply
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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
1 reply
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.
|
Trying to figure out how it would work. Like if you started at root, would you end up at # 1, # 3, or # 4?
|
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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
0 replies
|