Programming Theory
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesProgramming Theory

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old January 11th, 2005, 02:36 AM
SnowStorm SnowStorm is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 374 SnowStorm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Question about recursive programming

Ok when you have two or more recursive calls right next to each other in a program (like the method below), what exactly happens?
php Code:
Original - php Code
  1.  
  2. public void find(int row, int col)
  3.     {
  4.         if(row > size)
  5.         {
  6.             //print solution and exit
  7.             if(sum>current)current = sum;
  8.             System.out.print("CURRENT: " + sum);
  9.             return;
  10.         }
  11.         else
  12.         {
  13.             System.out.print("Row: " + row + "Column: " + col + "  ");
  14.             if(row<size && col<size && rows[row][col] != 0)
  15.             {
  16.                 sum += rows[row][col];
  17.                
  18.                 find(row+1, col);
  19.                 find(row+1, col+1);
  20.             }
  21.         }
  22.        
  23.     }


This is a method I'm writing (in java) to find the largest possible sum in a number triangle. I think I need two recursive calls because there are 2 possible directions it could go at every row in the triangle.

Reply With Quote
  #2  
Old January 13th, 2005, 08:07 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 3
RE: Question about recursive programming

I like recursive functions, when they are written well they are very elegant. Many times an iterative function would run faster though, but sometimes simplicity of code is more desirable and the recursive function is well worth it.

Anyway, this is a case where you can use a vastly simpler function to find the answer, so I assume that the point of this is to teach you about recursion. I find it funny that the lecturers when trying to teach you how to use recursion always use examples where you never would use it.

Let's start with a linked list. If you recall, a linked list contains a number of nodes, each pointing to the next. You can print out the contents of the list using a recursive function, which goes something like this (in pseudocode)

Code:
function display_list(node)
{
  print node->value;
  if (node->next <> null) display_list(node->next);
}


Now in this function it starts at the head of the list, prints the value of that node, and moves to the next node. In other words, it prints out the items of the list from the first item to the last. The interesting thing here is if you put the print statement after the recursive call, the values are printed in reverse order. It is important to understand why that is.

If the function first moves to the next node and only prints that node's value when that recursive call has ended, obviously the last node will be printed first, and so on. The important thing here is that if you move the print statement around the recursive call you change the order the node values are printed.

It is the same with a tree. Your function has two recursive calls, and it forms a tree. If you were to print out each node's value you would easily see the order the nodes are visited. In fact, this is the best way to understand what is going on.

Do that, at each step print the value of that cell. Once you have done that, try changing the location of the print statement. First put the print statement before both recursive calls and see the order of the nodes. Then put the print statement between the recursive calls and view it, and lastly put the print statement after the two recursive calls and view the order.

This is the absolute best way to see the behaviour of the recursion.

Incidentally, the order of the nodes in these three cases is called pre-order, in-order and post-order.

Reply With Quote
  #3  
Old January 13th, 2005, 08:27 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 3
RE: Question about recursive programming

Actually, I spoke too fast. In a triangle of nodes, each having some numeric value, the recursive method is quite a good way to find the sum of the nodes, as good as any other method. There is no vastly simpler algorithm, a recursive function would be the simplest.

Now that I look at your function in more detail, I see you do print the value of every node, I missed that as I glanced over it. Anyhow, your function has a serious problem. By the 3rd row it starts counting some nodes twice. I assume that the triangle you are talking about looks something like this:

1
23
456

As it stands, it will visit 1245356 and you will notice that node 5 was visited twice. This is because in this tree, each row has only one more node than the previous row.

To correct this you would have to say that only on the last column in a row should the function run the second recursive call.

I think I have said enough at this point. You have the information you need.

Actually, I also see that you are calculating a very strange sum indeed. Since I am feeling kind, and it is so grossly wrong, I will give you a hint. Make the function return an int and sum the branches.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesProgramming Theory > Question about recursive programming


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek