473,657 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to determine existence of infinate recursion in Access 2003

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.

E.g.:

Master Slave
1 2
2 1

Would be a simple example of infinite recursion in this context. This I
can easily detect with a simple self-join. However:

Master Slave
1 2
2 3
3 1

Is a bit harder. Of course the chain can be much longer. I am not as
concerned about the time for the query to work as I am with the accuracy
thereof. My readings on queries and infinite recursion have left me a
bit discouraged.

I don't believe I need the code spelled out for me as much as a pointer
or two.

Thanks.
Mar 27 '06 #1
8 1826
No bother <do**@email.m e> wrote in
news:sR******** *******@fe08.lg a:
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.

E.g.:

Master Slave
1 2
2 1

Would be a simple example of infinite recursion in this
context. This I can easily detect with a simple self-join.
However:

Master Slave
1 2
2 3
3 1

Is a bit harder. Of course the chain can be much longer. I
am not as concerned about the time for the query to work as I
am with the accuracy thereof. My readings on queries and
infinite recursion have left me a bit discouraged.

I don't believe I need the code spelled out for me as much as
a pointer or two.

Thanks.


I coded a BOM explosion module that took an array of recordsets
whch held the slave items for the current master. if the slave
wa a master, increment the array index and load the new
recordset with the slaves of the now master.

Each time you increment the child level, loop through the
array's master field, to see if the new slave is already there.
If it is there you have infinite recursion.

basically:
Dim childs(99) as recordset
Dim pointer as integer

getchilds 1,0

getchilds ( master as integer, level as integer)
childs(level)= currentdb.openr ecordset("selec t slave from table
Where `Master = " & master & ",dbopensnapsho t)
with childs(level)
do until .eof
'check for recursion
for pointer = level to 0 step -1
if childs(pointer) .master = .slave then
msgbox "We have recursion"
'back out of the loop
end if
next
getchilds slave, (level +1)
loop
end with
childs(level).c lose
end sub

Ill try to post some of the real code tomorrow from the office.

--
Bob Quintal

PA is y I've altered my email address.
Mar 28 '06 #2
No bother <do**@email.m e> wrote:
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.

E.g.:

Master Slave
1 2
2 1

Would be a simple example of infinite recursion in this context. This I
can easily detect with a simple self-join. However:

Master Slave
1 2
2 3
3 1

Is a bit harder. Of course the chain can be much longer. I am not as
concerned about the time for the query to work as I am with the accuracy
thereof. My readings on queries and infinite recursion have left me a
bit discouraged.

I don't believe I need the code spelled out for me as much as a pointer
or two.

Thanks.


I don't know if you can solve this problem in general with a query. If
you want to check out, google for "CELKO trees BOM" (BOM = Bill of
Materials).

If you want to use code, google for "directed graph strongly connected
component" or "Kosaraju's algorithm".

HTH
Matthias Kläy
--
www.kcc.ch

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 28 '06 #3
No bother <do**@email.m e> wrote in news:sR******** *******@fe08.lg a:
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.Exec ute( _
"SELECT Master, Slave, " _
& "IsRecursive(Ma ster, Master) FROM MasterSlave")
Debug.Print
With r
While Not .EOF
Debug.Print .Fields(0).Valu e, _
.Fields(1).Valu e, _
CBool(.Fields(2 ).Value)
.MoveNext
Wend
End With
End Sub

Public Function IsRecursive(Mas ter As Long, Optional Begin As Long) As
Boolean
Static Iterations As Long
Static MaximumIteratio ns As Long
Static Start As Long
If Begin = Master Then
Iterations = 0
MaximumIteratio ns = CurrentProject. Connection.Exec ute( _
"SELECT COUNT(*) FROM MasterSlave") _
.Fields(0).Valu e
Start = Master
End If
If Iterations > MaximumIteratio ns Then Exit Function
Master = CurrentProject. Connection.Exec ute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master).Fields( 0).Value
If Master = Start Then
IsRecursive = True
Else
Iterations = Iterations + 1
IsRecursive = IsRecursive(Mas ter)
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
Mar 28 '06 #4
Thanks for the help!

I ended up making a little change from:
Master = CurrentProject. Connection.Exec ute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master).Fields( 0).Value
to
dim r as recordset
r = CurrentProject. Connection.Exec ute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master)
if r.recordcount > 0 then
master = r.fields(0).val ue
....

I also converted this to DAO, but I won't bore the group with the details.

I also found that when executing this that after about five minutes I
would get a message complaining about not being able to open more
databases. I may try again when:
1. I get access (no pun intended) to a Cray or a big iron
2. Access is ported to one of these, either when XP is ported or Access
is ported directly to an OS that runs on one of these

I'm not holding my breath.

On the other hand, maybe Cobol would be up to the task...
Lyle Fairfield wrote:
No bother <do**@email.m e> wrote in news:sR******** *******@fe08.lg a:
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.Exec ute( _
"SELECT Master, Slave, " _
& "IsRecursive(Ma ster, Master) FROM MasterSlave")
Debug.Print
With r
While Not .EOF
Debug.Print .Fields(0).Valu e, _
.Fields(1).Valu e, _
CBool(.Fields(2 ).Value)
.MoveNext
Wend
End With
End Sub

Public Function IsRecursive(Mas ter As Long, Optional Begin As Long) As
Boolean
Static Iterations As Long
Static MaximumIteratio ns As Long
Static Start As Long
If Begin = Master Then
Iterations = 0
MaximumIteratio ns = CurrentProject. Connection.Exec ute( _
"SELECT COUNT(*) FROM MasterSlave") _
.Fields(0).Valu e
Start = Master
End If
If Iterations > MaximumIteratio ns Then Exit Function
Master = CurrentProject. Connection.Exec ute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master).Fields( 0).Value
If Master = Start Then
IsRecursive = True
Else
Iterations = Iterations + 1
IsRecursive = IsRecursive(Mas ter)
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.

Mar 30 '06 #5
No bother <do**@email.m e> wrote in news:iq******** ****@fe10.lga:
Thanks for the help!

I ended up making a little change from:
Master = CurrentProject. Connection.Exec ute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master).Fields( 0).Value
to
dim r as recordset
r = CurrentProject. Connection.Exec ute( _
"SELECT Slave FROM MasterSlave " _
& "WHERE Master = " & Master)
if r.recordcount > 0 then
master = r.fields(0).val ue
...

I also converted this to DAO, but I won't bore the group with the
details.

I also found that when executing this that after about five minutes I
would get a message complaining about not being able to open more
databases.


I wonder if ADO uses enough of JET and DAO that it too opens Databases?
Perhaps it does not.

If both DAO and ADO open too many databases then one could pretty easily
map the recordset to an array and do the recursion with it. Probably I
would want to do this with some language which allowed for sparse arrays.
VBA isn't it.

Of course, like everything, too many records would be too much.
Mar 30 '06 #6
"No bother" <do**@email.m e> schreef in bericht
news:sR******** *******@fe08.lg a...
I don't believe I need the code spelled out for me as much as a pointer or
two.


If the recursive version is impracatical, you may try the
traditional iterative way of proving a graph is acyclic, basically:

1. count the number of masters for each node
2. remove all nodes with count = 0
3. decrement count for each slave of each removed node
4. repeat from 2. until al nodes are removed (graph is acyclic)
or no node has count = 0 (graph has at least one cycle)

--
Paul

Mar 30 '06 #7
Kaniest schreef:
If the recursive version is impracatical, you may try the
traditional iterative way of proving a graph is acyclic, basically:

1. count the number of masters for each node
2. remove all nodes with count = 0
3. decrement count for each slave of each removed node
4. repeat from 2. until al nodes are removed (graph is acyclic)
or no node has count = 0 (graph has at least one cycle)


For nitpickers: of course this applies to directed graphs.
Mar 30 '06 #8
No bother wrote:
On the other hand, maybe Cobol would be up to the task...


This approach works nicely in Cobol (VBA/DAO version):

'Check the graph in table or query Arrows(tail,hea d) for cycles
'strategy: remove sinks (work head to tail)
With CurrentDb 'use temp table H1(head)

'make H1 the subset of distinct tails
.Execute _
"SELECT DISTINCT tail AS head INTO H1 FROM Arrows;"

Do While .RecordsAffecte d
'remove sinks from subgraph A1 with heads in H1
#If Not JET_antics Then
.Execute _
"DELETE * FROM H1 WHERE head NOT IN " & _
"(SELECT tail FROM Arrows WHERE head IN " & _
"(SELECT head FROM H1));"
#Else
.Execute _
"DELETE DISTINCTROW H1.* FROM H1 LEFT JOIN " & _
"[SELECT tail FROM Arrows INNER JOIN " & _
"H1 ON Arrows.head = H1.head]. AS T1 ON H1.head = T1.tail " & _
"WHERE tail Is Null;"
#End If
Loop
'leaves subgraph A1 without sinks (empty or having cycles)

Debug.Print "Cycle(s) present:"; .TableDefs!H1.R ecordCount > 0

.Execute _
"DROP TABLE H1;"
End With
Apr 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1907
by: Steven D.Arnold | last post by:
I have a query which does not use column indexes that it should use. I have discovered some interesting behaviors of Postgres which may indicate a bug in the database's query planning. Take a look at the query below. There is a btree index on both m.account_id and a.account_id. Query (1) does not use the index on the messages table, instead opting for a full table scan, thus killing performance. The messages table can contain...
10
2971
by: Kenneth | last post by:
I have a Query that consist of a lot of different sales data, and one of the colums are different date. The date goes from 1jan2003 til 31jan2003. in this Query I only want the salesdata for 1jan2003. How do I remove the dates , 2jan2003 til 31jan2003 without removing them from the table, from the Query? (Because I want to use the data for 2jan2003 etc later in other queries) -kenneth
2
1857
by: Jerry | last post by:
Hi, Access 2003. I would like to query specific dates or years. I need to be able to selct the dates in a query, ie aug 12 2004, aug 11th 2004, or 2001, 2002 and 2003. Any help is most appreciated
11
3465
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I tend to iterate over subsets of these arrays by walking a pointer and an offset per dimension across their dimensions. I've found that this tends to result in more performance in a portable manner than doing explicit indexing calculations and...
4
2064
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating premiums are dependant on the country in which the client is in. Therefore, we have a Country table, with its list of rates, a client table and then the property table. Getting this is great, works fine, easy! Problem is, now I need to work out a...
4
7180
by: Br | last post by:
We're using an Access2000 ADP with an SQL2000 back-end. Because SQL2000 was released after Access2000 you need to be running Access2000 SP1 (2 or 3) for it to work properly. Is there an easy way for the ADP to determine programatically wether the correct service pack is installed (or if they are using a newer version such as 2002/2003)? Ta.
13
3244
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own ReadElementBodyAsXml the code works, but is less efficent. ReadElementBodyAsXml is required by my application with .Net Framework 1.1. The code breaks on the second call to ReadElementBodyAsXml with the inner xml: </EGDConfigExtension>...
22
31187
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
11
2645
by: lenygold via DBMonster.com | last post by:
Hi everybody! This query is supposed to count consecutive years from the current year without OLAP. Input Table: ID DateCol 1 02/01/2006 1 01/01/2006 1 01/01/2005
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.