简体   繁体   English

QuickGraph查找顶点度

[英]QuickGraph finding indegree of Vertices

I am using QuickGraph to create a directed acyclic graph. 我正在使用QuickGraph创建有向无环图。 I need to find all vertices whose indegree is zero. 我需要找到所有度数为零的顶点。 I don't see this support on a graph's Vertices collection, or a way to filter using LINQ. 我在图形的Vertices集合上看不到这种支持,也没有使用LINQ进行过滤的方法。

Here is a sample data structure I'm composing: 这是我正在编写的样本数据结构:

var componentGraph = new AdjacencyGraph<Component, Edge<Component>>();

var serverOnMachineA = new TalismaServerComponent("CLTDEPAPI10");
var serverOnMachineB = new TalismaServerComponent("CLTDEPAPI11");
var appServerOnMachineB = new TalismaAppServerComponent("CLTDEPAPI11");
var webComponentsOnMachineC = new TalismaWebComponentsComponent("CLTDEPFE1");

componentGraph.AddVertex(serverOnMachineA);
componentGraph.AddVertex(serverOnMachineB);
componentGraph.AddVertex(appServerOnMachineB);
componentGraph.AddVertex(webComponentsOnMachineC);

componentGraph.AddEdge(new Edge<Component>(appServerOnMachineB, serverOnMachineA));
componentGraph.AddEdge(new Edge<Component>(webComponentsOnMachineC, appServerOnMachineB));
componentGraph.AddEdge(new Edge<Component>(webComponentsOnMachineC, serverOnMachineB));

I simply need a list of the vertices in this graph that have no "in" edges (indegree=0). 我只需要此图中没有“入”边(indegree = 0)的顶点的列表即可。

You might need a different graph type. 您可能需要其他图形类型。 Diving a bit into the forums and source of QuickGraph I found the BidirectionalGraph class which is 深入讨论一下QuickGraph的论坛和源代码,我发现了BidirectionalGraph类,它是

A mutable directed graph data structure efficient for sparse graph representation where out-edge and in-edges need to be enumerated. 一种可变的有向图数据结构,对于需要枚举边缘和边缘的稀疏图形表示有效。 Requires twice as much memory as the adjacency graph. 需要的内存是邻接图的两倍。

The method to retrieve the in-degree seems to be found on the IBidirectionalIncidenceGraph as this discussion implies. 正如本次讨论所暗示的IBidirectionalIncidenceGraph ,似乎可以在IBidirectionalIncidenceGraph上找到检索度数的方法。

The data structure you use does not do book keeping on incoming edges, thus you would have to retrieve the in-degree of a vertex by iterating through all edges and looking at their target, which could be an expensive operation for big graphs. 您使用的数据结构不会保留输入边的预订,因此您必须通过遍历所有边并查看其目标来检索顶点的入度,这对于大图形而言可能是一项昂贵的操作。

The BidirectionalGraph is quicker for that but takes twice as much memory space for the book-keeping. 为此, BidirectionalGraph更快,但用于簿记的内存空间是原来的两倍。 Using it you could do something like: 使用它,您可以执行以下操作:

var orphans = graph.Vertices.Where(v => graph.InDegree(v) == 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM