从 QuickGraph 图中获取连接的组件
Getting connected components from a QuickGraph graph
我是图论新手。
我已经用 QuickGraph library 创建了一个邻接图,最终,我想要从图中得到连接的组件。
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
包含图表中的组件,但 FSI 中 x;;
的输出如下所示:
示例 tuple list
中的值表示数据库中的 BillTo
和 ShipTo
客户 ID 值。
QuickGraph 库中的文档很少,特别是对于那些试图 "learn on the fly."
这个问题取代了 。我曾考虑修改我之前的问题,但由于这是一个完全独立的问题,因此决定保留原样。
这是您要找的东西吗?
我会使用 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 的 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()
您也可以只编写某种字典,例如保留 record/count 个引用的数据结构。
原来需要在算法上调用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;}]) ]
我是图论新手。
我已经用 QuickGraph library 创建了一个邻接图,最终,我想要从图中得到连接的组件。
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
包含图表中的组件,但 FSI 中 x;;
的输出如下所示:
示例 tuple list
中的值表示数据库中的 BillTo
和 ShipTo
客户 ID 值。
QuickGraph 库中的文档很少,特别是对于那些试图 "learn on the fly."
这个问题取代了
这是您要找的东西吗?
我会使用 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 的 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()
您也可以只编写某种字典,例如保留 record/count 个引用的数据结构。
原来需要在算法上调用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;}]) ]