![](/img/trans.png)
[英]Creating a undirected graph and traversing it using BFS in QuickGraph
[英]Getting connected components from a QuickGraph graph
我是图论的新手。
我已经使用QuickGraph库创建了一个邻接图,最终,我希望从图中获得连接的组件。
open QuickGraph
let tup = [(1M,1M); (2M, 18M); (3M, 3M); (4M, 5M); (5M, 24M); (24M, 6M); (7M, 6M); (8M, 9M); (10M, 9M)]
type Vertex = {decimal: decimal}
let edges =
tup
|> List.map (fun x -> ({decimal = fst x}, {decimal = snd x}))
|> List.map (fun x -> Edge<Vertex> x)
//Undirected Graph
let undirGraph = edges.ToUndirectedGraph()
undirGraph.Edges
undirGraph.Vertices
let x = QuickGraph.Algorithms.ConnectedComponents.ConnectedComponentsAlgorithm(undirGraph)
undirGraph.Edges
输出:
val it : Collections.Generic.IEnumerable<Edge<Vertex>> =
seq
[FSI_0227+Vertex->FSI_0227+Vertex {Source = {decimal = 1M;};
Target = {decimal = 1M;};};
FSI_0227+Vertex->FSI_0227+Vertex {Source = {decimal = 2M;};
Target = {decimal = 18M;};};
FSI_0227+Vertex->FSI_0227+Vertex {Source = {decimal = 3M;};
Target = {decimal = 3M;};};
FSI_0227+Vertex->FSI_0227+Vertex {Source = {decimal = 4M;};
Target = {decimal = 5M;};}; ...]
并且来自undirGraph.Vertices
:
val it : Collections.Generic.IEnumerable<Vertex> =
seq
[{decimal = 1M;}; {decimal = 2M;}; {decimal = 18M;}; {decimal = 3M;}; ...]
正如所料。
无向图已成功创建,但现在我被卡住了。 从这里开始,我不知道如何获取图形的连通组件,或者坦率地说,如果我使用正确的图形结构。
我希望x
包含图中的组件但是从x;;
输出x;;
在FSI看起来像这样:
示例tuple list
的值表示BillTo
和ShipTo
客户ID值。
QuickGraph库中的文档很少,特别是对于那些试图“即时学习”的人。
这是你要找的东西吗?
我会使用RProvider将代码发送到R并生成它,然后在必要时将其包装在dll中。 然后,您可以使用components
, clusters
, groups
等来提取连接。
# In R:
g1 <- graph( edges=c( "1","1", "2", "18", "3", "3", "4", "5", "5", "24", "24", "6", "7", "6", "8", "9", "10", "9"),n=9,directed=T)
plot(g1)
comp1 <- components(g1)
comp1
groups(comp1)
cl <- clusters(g1)
lapply(seq_along(cl$csize)[cl$csize > 1], function(x)
V(g1)$name[cl$membership %in% x])
如果您决定仍然坚持使用QuickGraph,您在FSI中看到的是因为您正在定义一个名为Vertex
的记录类型,其中有一个名为decimal的十进制成员。 这有点让人困惑,所以最初我建议你坚持使用int
并按以下方式生成图形:
let tup = [(1,1); (2, 18); (3, 3); (4, 5); (5, 24); (24, 6); (7, 6); (8, 9); (10, 9)]
let edges =
tup |> List.map (fun x -> SEdge<int>(fst x, snd x))
let graph = edges.ToAdjacencyGraph()
let uniGraph = edges.ToUndirectedGraph()
你也可以写一些像数据结构这样的字典来保存引用的记录/数量。
事实证明,你需要在算法上调用Compute
方法来实际运行它!
我拿了你的示例代码,只是添加了对Compute
调用:
let x = QuickGraph.Algorithms.ConnectedComponents.
ConnectedComponentsAlgorithm(undirGraph)
x.Compute()
一旦你这样做, x.Components
包含一个字典,为每个顶点分配一个组件的索引,所以如果你想要顶点组(代表组件),你可以只用Value
(它是组件索引)对结果进行分组:
x.Components
|> Seq.groupBy (fun kv -> kv.Value)
|> Seq.map (fun (comp, vertices) ->
comp, vertices |> Seq.map (fun kv -> kv.Key))
这给出了以下内容:
[ (0, [{decimal = 1M;}]);
(1, [{decimal = 2M;}; {decimal = 18M;}]);
(2, [{decimal = 3M;}]);
(3, [{decimal = 4M;}; {decimal = 5M;}; {decimal = 24M;};
{decimal = 6M;}; {decimal = 7M;}]);
(4, [{decimal = 8M;}; {decimal = 9M;}; {decimal = 10M;}]) ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.