473,749 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is there problem that "has to" use recursion

Hi

Please give problems that "HAS TO" to use recursion (recursive calls to
itself.)
Preferrably real world examples, not knights tour.

I'm thinking about eliminating the use of stack...

Thanks.

Nov 14 '05 #1
10 2519
[F'ups set to comp.programmin g]

p...@mmail.ath. cx wrote:

Please give problems that "HAS TO" to use recursion (recursive
calls to itself.)


This is not a C language question and, as such, it is off-topic
in clc.

The C language does provide recursion, for those who want to use
it. However, it is subject to practical and pragmatic caveats that
are also largely off topic in clc since the C language standards
provide no minimal implementation requirements on either depth or
efficieny of recursion.

--
Peter

Nov 14 '05 #2
pa***@mmail.ath .cx wrote:

Please give problems that "HAS TO" to use recursion (recursive
calls to itself.) Preferrably real world examples, not knights tour.

I'm thinking about eliminating the use of stack...


Can't. There are known standard means of eliminating recursion,
which may involve the use of an auxiliary stack. Since this can
always be done, no problem actually relies on recursion.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #3
pa***@mmail.ath .cx wrote:
Please give problems that "HAS TO" to use recursion (recursive calls to
itself.) Preferrably real world examples, not knights tour.

I'm thinking about eliminating the use of stack...


None, if you're merely thinking about eliminating the use of the call
stack; you can always flatten the algorithm into an iterative one,
possibly with the use of your own stack ADT. Many, if you mean
eliminating any stack at all; quicksort, for example, uses recursion on
both halves of the array, and AFAIK only the second half can be
eliminated without the use of a hand-written stack.

Richard
Nov 14 '05 #4
<pa***@mmail.at h.cx> wrote:
Please give problems that "HAS TO" to use recursion (recursive calls to
itself.)
Preferrably real world examples, not knights tour.

I'm thinking about eliminating the use of stack...


The first paragraph is theoretical (as used in ordinary conversation) and
the second is practical. I suggest rephrasing the first paragraph and ask
"What is an example of a problem where it would be impractical to use an
iterative solution?" This will, of course, open a discussion on the meaning
of "impractica l" by the purists. But you might by chance get a meaningful or
useful answer too, mixed in with all the noise. But this is the wrong group
too, try going "up a notch". programming, compiling, math, ....
Nov 14 '05 #5
jxh
pa***@mmail.ath .cx wrote:
Please give problems that "HAS TO" to use recursion (recursive
calls to itself.)
Preferrably real world examples, not knights tour.
Implementing a sort in scheme. Well, implementing anything in
scheme that involves iterating an indeterminate number of
times.

Defining a grammar in BNF for a regular language that contains
strings of indeterminate size.

Proving Goedel's Theorem.

What was your C question?
I'm thinking about eliminating the use of stack...


Well C doesn't really define a stack. You can define functions,
and you can call functions. Inside a function, you can define
automatic variables. Functions can be defined to take arguments
which are treated like automatic variables that are initialized
by the caller of the function. C supports recursion in that the
caller can call any function whose name is in scope.

So if we replace "stack" with "function call", then you have old
style BASIC.

-- James

Nov 14 '05 #6
pa***@mmail.ath .cx wrote:
...
Please give problems that "HAS TO" to use recursion (recursive calls to
itself.)
Preferrably real world examples, not knights tour.

I'm thinking about eliminating the use of stack...
...


The answer depends heavily on what you mean under "recursion" . A
syntactic recursion (when one function in a program calls itself
directly or indirectly) is very easy to eliminate in all cases.

The algorithmic recursion (if defined as a LIFO-based application of
Divide&Conquer approach) cannot be eliminated in general case. A simple
real-world example is a depth-first tree traversal of a tree that only
has parent->child links, but no child->parent links (assuming that the
input tree is non-modifiable and that the required complexity is O(n),
where 'n' is the number of nodes).

Informally speaking, if the input data structure is inherently and
essentially recursive, the processing algorithms will always be
recursive and there's no way around it. The implementations of these
algorithms, of course, will not necessarily employ syntactic recursion.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7
pa***@mmail.ath .cx wrote:
Please give problems that [must] use recursion
(recursive calls to itself.)
Since there are none,
I have given all of them.
Preferrably real world examples, not knights tour.

I'm thinking about eliminating the use of stack.


Why? Recursion is your friend.
A good optimizing C compiler can convert a recursive function
into the equivalent iterative function automatically
if there is indeed any advantage to doing so.

I used Google

http://www.google.com/

to search for

+"tail-recursion optimization" +"C program"

and I found lots of stuff.
Nov 14 '05 #8
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
jxh <jx*@despammed. com> wrote:
pa***@mmail.at h.cx wrote:
Please give problems that "HAS TO" to use recursion (recursive
calls to itself.)
Defining a grammar in BNF for a regular language that contains
strings of indeterminate size.


<word> ::= <letter> | <letter> <word>

That does not use recursion at the -syntatic- level, and it does not
*need* recursion in order to do semantic analysis of a potential word.
At the semantic analysis level, it becomes just a simple regular
expression, processable by a DFA (Deterministic Finite Automata) with
nothing more than a "current state" variable if you are just trying to
recognize <word>.
:Proving Goedel's Theorem.

Which of Goedel's Theorem's? His Completeness Theorem? His
Incompleteness Theorem? There is a particular point in the usual
proof of the Incompleteness Theorem in which a number representing
a function with one free variable is applied to itself in order to
go from the step "Some statements are false" to "This particular
statement is false", but in one case the number is being
"used" and in the other case the number is being "mentioned" ,
with there being no location that I can recall in the theorem
proof in which a number is used upon itself multiple times --
no recursion, just a simple one-step evaluation.
There is no finitely computable result which cannot be represented
as a Turing Machine; Turing Machines have no "recursion" as we
commonly think of it (but they do have a stack of indefinite length.)
Every finite recursive function programmable in C has a maximum
number of calls, N, which can be simultaneously logically active; e..g,

g(x-1) * g(x+1) - g(x+1) * g(x-1)

has 4 in that fragment. This can be converted into an interative
structure which uses an indefinitely-sized block of memory each
element of which is
N by the size of the returned value
plus 1 times the size of the argument
plus something large enough to hold 0 .. N to indicate what has
been finished

A linked list of structures could be used to hold this.

Once this is in place, the code can proceed iteratively: the top
element of the stack / linked list / "work list" tells you which
value has to be processed next by looking at the counter,
and the value slots are used to save intermediate results.
What is the difference between this and calling the function
recursively? Not much, really -- automatic stack storage
by repeated calling, plus the return PC automatically stored
on the stack are equivilent to the above storage structure
and a linked list. But when the question arises as to
what *must* be done recursively, this transformation can
be trotted out to show that there is no finitely computable
result which is programmable in C that *must* be programmed
with recursion.
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
Nov 14 '05 #9
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
pa***@mmail.ath .cx wrote:
Please give problems that [must] use recursion
(recursive calls to itself.)


Since there are none,
I have given all of them.
Preferrably real world examples, not knights tour.
I'm thinking about eliminating the use of stack.


Why? Recursion is your friend.
A good optimizing C compiler can convert a recursive function
into the equivalent iterative function automatically
if there is indeed any advantage to doing so.

I used Google

http://www.google.com/

to search for

+"tail-recursion optimization" +"C program"

and I found lots of stuff.


Sure, but tail-recursion optimization doesn't cover all cases of
recursion.

Tail-recursive functions are particularly easy to convert to iterative
functions, but all recursive functions can be converted to an
iterative form (possibly with the use of an explicit stack).

Doing so might make sense on a small embedded system that might limit
the depth of the call stack, or where avoiding the overhead of nested
function calls can be worthwhile.

In most cases, though, if a problem is inherently recursive, recursion
is the best way to implement the solution.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

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

Similar topics

16
3412
by: Maciej Kalisiak | last post by:
I use this simple test in Python: def foo(i): print i foo(i+1) import sys sys.setrecursionlimit(1000000) foo(0) Now, my understanding is that I get the segfault when Python overruns the C
12
3733
by: Mikito Harakiri | last post by:
I wonder if WITH RECURSIVE MaryAncestor(anc,desc) AS ( (SELECT parent as anc, child as desc FROM ParentOf WHERE desc = "Mary") UNION (SELECT A1.anc, A2.desc FROM MaryAncestor A1, MaryAncestor A2 WHERE A1.desc = A2.anc) ) SELECT anc FROM MaryAncestor
40
5711
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but welcome any improvements: i = 0; if (g && 1) i++; if (g && 2) i++;
3
2117
by: kiplring | last post by:
Suppose a function which has Sleep() method in it. And I want to recycle it. I made two buttons which call "Resume()" and "Suspend()". But It doesn't work. The state of thread "t" will be "WaitSleepJoin" when it runs because the function it calls - "GenerateEvents()" has "Sleep()" function. Can I solve this problem with Thread? I don't want add nasty boll flags on it.
71
3171
by: Greg | last post by:
Is it your general opinion that C# is generally designed/intended/ready to replace C++? I know the answer is not black and white but please repond YES or NO (add any comments if you would like) I just thought it might be interesting to get a tally. This is related to a previous thread. So far we have two NOs from "aa" and Bo Persson. -- Greg McPherran www.McPherran.com
2
1341
by: Leo | last post by:
Dear All, Can I do this: class MyClass() { public: doStuff(); private: int iNum;
10
11333
by: Angel | last post by:
I have the following code var thisDoc=document.getElementById("myTable"); // myTable is the name of the table alert(thisDoc.childNodes.children.length) How do I change the second line of code for it to work in firefox? Regards, Angel
2
4686
by: rakesh kumawat | last post by:
I am facing a problem while reading the result which is loaded in DOMDocument. In which I am sending a request to web service and getting a record of Single Order. This is my VB Code which is i am using.... ......................... Dim Connector As SoapConnector30 ' To connect to webservice Dim Serializer As SoapSerializer30 ' To serialize the XML data Dim Reader As SoapReader30 ' To read the Webservice response...
1
1206
by: Mateusz | last post by:
Hello, I new on this forum :-) I've got some problem with my script. I'm writing some script, when FF say "document.forms.google_bot has no properties" look at this code : <script language="JavaScript"> function adres()
0
8997
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
8833
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
9256
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
8257
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...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
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();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.