在绘制图形之前删除边数为 0 的节点(dot、graphviz)

Remove nodes with 0 edges before drawing the graph (dot, graphviz)

如何在使用 dot -Tpng mcve.gv -o mcve.png 绘图之前从图表中删除没有边的节点?[​​=19=]

当前结果: 想要的结果:

阅读文档并搜索网络后,我发现了以下 answer 乍一看似乎不错。 (删除所有边为 0 的节点)

vpr -c 'N[$.degree==0]{delete($);}' mvce.gv | dot ...

不幸的是,这个命令给出了以下错误:

gvpr: "<command line>", line 2: delete($)<<< 
 -- cannot convert node_t to graph_t

从我读到的内容来看这似乎很奇怪 N[predicate]{action} 应该只在节点上执行 action

我的图表的一个最小示例:

graph main_graph {
  node1;
  node2;
  node3;
  node4; // not used

  subgraph graph1 {
    edge [color=red,penwidth=2]
    node1 -- node2;
  }

  subgraph graph2 {
    edge [color="blue",penwidth=2]
    node2 -- node3;
    node1 -- node3;
  }
}

gvpr doc 表示 delete 实际上有 2 个参数 - 首先是要删除的图形,然后是要删除的对象。

您只传递了一个节点,这似乎可以解释转换错误。

由于没有对可用图表的引用,因此可以将 NULLroot(参见同一文档)作为参数传递:

gvpr -c "N[$.degree==0]{delete(root, $)}" mygraph.gv
gvpr -c "N[$.degree==0]{delete(NULL, $)}" mygraph.gv

将得到 int

graph main_graph {
    subgraph graph1 {
        edge [color=red,
            penwidth=2
        ];
        node1 -- node2       [color=red,
            penwidth=2];
    }
    subgraph graph2 {
        edge [color=blue,
            penwidth=2
        ];
        node1 -- node3       [color=blue,
            penwidth=2];
        node2 -- node3       [color=blue,
            penwidth=2];
    }
}