Dafri
Dafri

Reputation: 11

Displaying directed acyclic graphs in Octave

I'd like to visualize a directed acyclic graph tree in Octave.

In MATLAB, this is done using biograph(). Is there an Octave equivalent?

Also, how do I use Octave's drawgraph()?

Upvotes: 1

Views: 1431

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

drawGraph (from the geometry package) accepts:

  • a "Nodes" matrix, where each row is a set of coordinates representing a single node
  • an "Edges" matrix, where each row represents an edge between two nodes, where the nodes are identified numerically from the corresponding row in the "Nodes" matrix.

Example:

pkg load geometry;

Nodes = [ 0, -1;
          1,  0;
          0,  1;
         -1,  0];

Edges = [1, 2;
         2, 3;
         3, 4;
         1, 3;
         2, 4];

g = drawGraph(Nodes, Edges);
set(g, 'markerfacecolor', 'g', 'markersize', 50, 'linewidth', 5);

However, this is literally just a collection of lines with markers. You could easily replicate this yourself using simple line and plot commands (or quiver if you also wanted arrows) in a simple loop. This may be preferable if you want nodes with different shapes, colours, text, etc.

Here's a manual example:

Nodes(1) = struct('coords', [0, -1], 'shape', 'o', 'text', 'Node 1', 'facecolor', 'k', 'edgecolor', 'r', 'textcolor', 'g', 'size', 75);
Nodes(2) = struct('coords', [1,  0], 'shape', 'd', 'text', 'Node 2', 'facecolor', 'r', 'edgecolor', 'g', 'textcolor', 'b', 'size', 100);
Nodes(3) = struct('coords', [0,  1], 'shape', 's', 'text', 'Node 3', 'facecolor', 'g', 'edgecolor', 'b', 'textcolor', 'k', 'size', 75);
Nodes(4) = struct('coords', [-1, 0], 'shape', 'p', 'text', 'Node 4', 'facecolor', 'b', 'edgecolor', 'k', 'textcolor', 'r', 'size', 150);

NodesLayer = axes();
hold on; 
for i = Nodes
  Node = plot(i.coords(1), i.coords(2)); 
  set (Node, 'marker', i.shape, 'markerfacecolor', i.facecolor, 'markeredgecolor', i.edgecolor', 'markersize', i.size, 'linewidth', 5);
end
hold off; axis off;
set(NodesLayer, 'xlim', [-1.5, 1.5], 'ylim', [-1.5, 1.5]);

TextLayer = axes('color', 'none');
for i = Nodes
  Text = text (i.coords(1)-0.16, i.coords(2), i.text); 
  set (Text, 'color', i.textcolor, 'fontsize', 12, 'fontweight', 'bold');
end
hold off; axis off;
set(TextLayer, 'xlim', [-1.5, 1.5], 'ylim', [-1.5, 1.5]);

Edges = [1,2; 2,3; 3,4; 4,1; 2,1; 4,3; 1,3; 3,1];

EdgesLayer = axes('color', 'none')
hold on;
for E = Edges.'
  i = E(1);   j = E(2); 
  u = [Nodes(j).coords(1) - Nodes(i).coords(1)]; 
  v = [Nodes(j).coords(2) - Nodes(i).coords(2)];
  x = Nodes(i).coords(1) + u * 0.25;
  y = Nodes(i).coords(2) + v * 0.25;
  Q = quiver(x, y, u * 0.5, v * 0.5, 0.1);
  set (Q, 'linewidth', 3, 'color', 'k');
end
hold off; axis off;
set (EdgesLayer, 'xlim', [-1.5, 1.5], 'ylim', [-1.5, 1.5]);

Upvotes: 2

Related Questions