0

I am trying to generate the powerset of a list of node elements in a graph. I have identified and adapted the following code from a previous post (Unique Combination of Set)

Public Function PowerSet(ByVal s As List(Of Node)) As List(Of List(Of Node))

    Dim ini As List(Of List(Of Node)) = New List(Of List(Of Node))
    Return s.Aggregate(ini, Function(a, b) a.Concat(a.Select(Function(x) x.Concat({b}))))

End Function

I am testing the function using

For Each x In G.PowerSet(G.internal_nodes_in_path)
    Console.WriteLine(x)
Next

However, I get an invalid cast exception error pointing at function(a,b) in the PowerSet function:

Additional information: Unable to cast object of type 'WhereSelectListIterator2[System.Collections.Generic.List1[cDAG_with_classes.Node],System.Collections.Generic.IEnumerable1[cDAG_with_classes.Node]]' to type 'System.Collections.Generic.IEnumerable1[System.Collections.Generic.List`1[cDAG_with_classes.Node]]'.

Can anyone offer some advice on where I might be going wrong?

Thanks

Community
  • 1
  • 1
MLD
  • 25
  • 9

1 Answers1

0

SO I got the tumbleweed badge for this one. Don't know if that's a good or a bad thing?! Anyway, I went back to basics, got my books out and came up with a better solution (and more importantly one that worked). For anyone who tumble(-weed)s across this in the future...

Public Function PowerSet(Of T)(ByVal s As IEnumerable(Of T)) As IEnumerable(Of IEnumerable(Of T))
    s = P.nodes_in_path
    Dim result = From m In Enumerable.Range(0, 1 << s.Count)
            Select
               From i In Enumerable.Range(0, s.Count)
               Where (m And (1 << i)) <> 0
                   Select s(i)
    Return result

End Function
MLD
  • 25
  • 9