No bother <do**@email.me> wrote in news:sR***************@fe08.lga:
I have a table with two columns, one named master, the other slave.
Each column has a set of numbers. A number in one column can appear in
the other. I am trying to see if there is any infinite recursion in the
table.
I'd start with something like this. I haven't tested it extensively and
don't know it it will always succeed. I'm not sure about a lot of things.
Would it be faster if the data is loaded into an array? Don't know. If I
were serious about it I would probably use some language which had sparse
arrays, that it where all the elements don't have to contain something
(without any memory being reserved for those empty elements).
Public Sub FindRecursives()
Dim r As ADODB.Recordset
Set r = CurrentProject.Connection.Execute( _
"SELECT Master, Slave, " _
& "IsRecursive(Master, Master) FROM MasterSlave")
Debug.Print
With r
While Not .EOF
Debug.Print .Fields(0).Value, _
.Fields(1).Value, _
CBool(.Fields(2).Value)
.MoveNext
Wend
End With
End Sub
Public Function IsRecursive(Master As Long, Optional Begin As Long) As
Boolean
Static Iterations As Long
Static MaximumIterations As Long
Static Start As Long
If Begin = Master Then
Iterations = 0
MaximumIterations = CurrentProject.Connection.Execute( _
"SELECT COUNT(*) FROM MasterSlave") _
.Fields(0).Value
Start = Master
End If
If Iterations > MaximumIterations Then Exit Function
Master = CurrentProject.Connection.Execute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master).Fields(0).Value
If Master = Start Then
IsRecursive = True
Else
Iterations = Iterations + 1
IsRecursive = IsRecursive(Master)
End If
End Function
Three runs on slightly different data are given here:
---
1 2 True
2 3 True
3 1 True
---
1 2 True
2 3 True
3 1 True
4 3 False
---
1 3 True
2 3 False
3 1 True
4 3 False
When would this finish for 100 000 records? Probably not in my lifetime.
Interesting Problem. Thanks for sharing it.
And yes, one could use DAO just as well.
--
Lyle Fairfield