I want to do some manual tracing of algorithms involving trees. I'd like to be able to create printable trees with empty nodes, like the one shown below, but to at least a couple more levels of depth. I'd also like to be able to have different numbers of branches - i.e. binary, ternary, quaternary trees etc.
Can anyone recommend a tool or tool/syntax combination for getting this done with the least cognitive overhead please?
To produce
First create the g.dot file
graph {
a [label="root"]
b [label=""]
c [label=""]
d [label=""]
ba [label=""]
ca [label=""]
da [label=""]
bc [label=""]
cc [label=""]
dc [label=""]
bd [label=""]
cd [label=""]
dd [label=""]
a -- { b c d }
b -- { ba ca da }
c -- { bc cc dc }
d -- { bd cd dd }
}
Then run the graphviz dot program
dot -Tpng -o sample.png g.dot
Related
Let's say I have the following data:
String[][] edges = { "A", "B"}, {"B", "C"}, {"C", "A"} };
Is there a library that would allow me to generate the equivalent in say mermaid:
stateDiagram
A --> B
B --> C
C --> A
or in Plantuml:
#startuml
A --> B
B --> C
C --> A
#enduml
or in dot:
digraph G {
A -> B
B -> C
C -> A
}
Obviously generating the above isn't too difficult as it has no customisation, but I'd like to know if there are libraries allowing for easier generation of above.
The motivation is to have a state machine describing a process and to generate the edges list mentioned above generated through code and the generated graph acting as live documentation.
Sample Data
A knows B
A knows C
A knows D
B knows E
C knows F
Desired Output
B
C
D
E
F
I tried the following query, but it's not working,
g.V('A').
out('knows').
as('x').
out('knows').
as('x').
project('Employee').
by(select('x'))
If you just want to get all the vertices in the path you can do:
g.V('A').repeat(out("knows")).emit().label()
example: https://gremlify.com/c533ij58a98z8
I use GrapViz graphs through R package DiagrammeR and RStudio. In my graphs international symbols like Ąą Čč Ęę Ėė Įį Šš Ųų Ūū Žž which are either in "Latin-4" ("ISO-8859-4") or in "Latin-6" ("ISO-8859-10") chart sets are not displayed correctly. I am aware of parameter charset [link], but it does not seem working with ("Latin-4" or "Latin-6").
Is there a way to use the listed symbols in GraphViz? I wonder if there is a possibility to indicate encoding in some other way?
digraph Latin6 {
graph [label = "Latin 6";
charset = "ISO-8859-10"]
A [label = "<A> Ąą"]
C [label = "<C> Čč"]
E [label = "<E> Ęę"]
E2 [label = "<E2> Ėė"]
I [label = "<I> Įį"]
S [label = "<S> Šš"]
U [label = "<U> Ųų"]
U2 [label = "<U2> Ūū"]
Zz [label = "<Zz> Žž"]
A -> C;
C -> E;
E -> E2;
E2 -> I;
S -> U;
U -> U2;
U2 -> Zz
}
I know that HTML symbols like #261; from UTF-8 Latin Extended A table may be used. If this conversion from international to HTML symbols is the only option, I would like to know if there is a function in R dedicated for direct (International symbol to HTML code) and backward (vice versa) conversion?
I have been given a problem to solve that I am fairly certain its insoluble.
For a system I am working in I need to take piece of branching logic (graph) and translate it to a linear path(flatten it), without node repeats. Given a tree I know that I can do this.
The rules are that the path must be traversed in order, but can 'skip' any panel if some condition is met.
Given the tree:
A > B > C
&&
A > D > E
Our tree can be flattened to:
A > B > C > D > E
So in this case B and C share the same conditional, and D, and E have the inverse of that condition. Thus if B is met so is C, but D and E will be skipped. Conversely, if B is not met, B and C are skipped, but D and E aren't.
So far, so simple. I am fairly convinced this is true for any tree. The problem I have is that the objects I have been given to flatten are graphs, and contain simple cycles, and closed walks.
After that huge preamble my questions are:
Am I right in stating that it is impossible to guarantee that such a graph can be flattened?
I know that closed walks cannot follow my rules (by virtue of returning to a node), but are there any other rules that describe a 'flatten-able' graph versus a 'non-flatten-able' one?
Cheers
If it is a directed graph, then the graph can be flattened if there are no directed cycles.
There is a handy library available JGraphT which will provide the necessary methods to determine if there is a cycle presnet as well a TopologicalOrderIterator which will perform the flattening as well.
Using the graph library and the example above this code will show an exmaple of how to do it.
DirectedGraph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
// A > B
graph.addEdge("A", "B");
// B > C
graph.addEdge("B", "C");
// A > D
graph.addEdge("A", "D");
// D > E
graph.addEdge("D", "E");
// Uncomment the following line to create a cyclic graph.
//graph.addEdge("E", "D");
CycleDetector<String, DefaultEdge> cycleDector = new CycleDetector<String, DefaultEdge>(graph);
if (cycleDector.detectCycles()) {
System.err.println("Cyclic graph");
} else {
StringBuilder sb = new StringBuilder();
// Create topological order iterator
for (TopologicalOrderIterator<String, DefaultEdge> iter = new TopologicalOrderIterator<String, DefaultEdge>(
graph); iter.hasNext();) {
String vertex = iter.next();
if (sb.length() > 0) {
sb.append(" > ");
}
sb.append(vertex);
}
System.out.println(sb.toString());
}
Sample output with the cycle disabled.
A > B > D > C > E
I can add my shapes to the stage and get them to do fades and such, but I can't get them to morph - do I need to separate out into lines and curves somehow?
var shape = new Path('M 321.051,510.078 c 0,0-10.126-23.854-19.438-45.792 c -7.927-18.675-15.265-35.961-15.265-35.961 s -17.977-41.111-19.036-77.643 c -1.05-36.243,14.817-67.908,14.817-67.908 s -36.176,73.71-49.086,137.219 c -9.327,45.879,3.426,87.39,3.426,87.39 L 321.051,510.078 z').attr({fillColor: 'red'});
stage.addChild(shape);
var targetPath = new Path('M 321.75,515.816 c 0,0,8.678-42.28,0.604-77.096 c -8.102-34.936-32.956-62.408-32.956-62.408 l -76.102-96.41c0,0-39.866-41.142-45.55-84.785 c -4.992-38.332,24.108-79.856,24.108-79.856 s -55.379,72.451-63.818,141.683 c -6.186,50.745,33.857,104.106,33.857,104.106 s 36.746,38.732,51.061,75.346 c 13.283,33.975,4.212,66.029,4.212,66.029L326.75,515.816 z').attr({fillColor: 'blue'});
shape.addTo(stage);
shape.morphTo(targetPath, '3s');
Bonsai 0.4.1 doesn't support smooth curves (s, S) SVG commands. See related ticket: https://github.com/uxebu/bonsai/issues/191
I guess you get it to work by removing those commands.