How can I stop a recursive call?

Or is that possible? That is, can I stop the recursion before the stack is empty?
I am trying to test for cycles in a graph, and print out only the edges which make up the cycle in the graph using depth first search. Or perhaps someone has some better ideas of how to do this, without attempting to do that clumsy "stop a recursive call" method?
To explain what I was thinking of: Go trough edges until a visited one is encountered (x). Then return back and terminate when we are back in that (x) edge. The (x) edge has a lable from the edge which called the dfs on the (x) edge, so that (x) is included in the cycle.
tnx in advance!

Or is that possible? That is, can I stop the
recursion before the stack is empty? You can stop recursion whenever you want merely by not doing the recursive call when some condition holds. (Obviously.)
For 'find a cycle', you want to keep the search going until a cycle is found, and then collect the path on the return trip.
Here's a simple example:
Assuming a Node class
class Node {
   public List<Node> children=new ArrayList();
   public boolean visited=false;
   public Node() {
}this code will return a list of nodes on the cyclic path (plus the path from root to the start of the cycle):
  /** returns a node path ending with a cycle, or an empty list if no cycle found */
  List getCycle(Node root) {
    List cycle=new ArrayList();
    findCycle(root, cycle);
    return cycle;
  /** returns true if cycle was found, puts the path in the 'path' argument */
  boolean findCycle(Node n, List path) {
    if (n.visited) { // recursion bottom: cycle found
      path.add(n); // add first element to path
      return true; // terminate recursion
    // base case
    n.visited=true;
    for (Node ch : n.children) { // iterate over children
      // depth-first recursion. returns true if cycle is found through child 'ch'
      if (findCycle(ch, path)) { 
        // if there is a cyclic path going through ch,
        // the path also goes through n
        path.add(n); 
        return true; // terminate recursion
    return false; // no cycle through n
  }This example returns the first cycle found, you may want to return all cycles, the shortest cycle or some variant thereof.
You would probably want to get rid of the path before the start of the cycle.
And by using the method call stack, the longest (non-cyclic) path in the graph cannot be longer than the maximum size of the call stack.
So 'stopping a recursive call' need not be that clumsy. Although it can be tricky to get all the details right, especially at first.

Similar Messages

  • How can I stop the sales calls?

    A little over a week ago, Verizon called me to ask me to upgrade my DSL to FiOS.  I told them I'd review it and contact them if I wanted to proceed.
    Saturday morning, they called me to ask me to upgrade my DSL to FiOS.  I told them I wasn't interested.
    Saturday afternoon, they called me to ask me to upgrade my DSL to FiOS.  I told them again I wasn't interested.
    Saturday afternoon, they called me AGAIN (yes, 3 sales calls in one day) to ask me to upgrade my DSL to FiOS.  I told them AGAIN I wasn't interested, and that they'd called me three times that day.
    I emailed a complaint, got a non-response with an invalid link asking for more information.
    This morning, they called me yet again asking me to upgrade my DSL to FiOS.  The sales rep said he could not connect me to customer service, and I kind of went off on him.
    At this point, the only thing I can think of is to see if I can get my phone and DSL services thorugh another company,  Verizon seems to have been bought out by a consortium of Nigerian bankers.

    beatio wrote:
    how can I stop the program from resizing, zooming in and out, of my photos? I want it to create stop action.
    in the Project Properties (command-j), you can tell iM how to handle the initial photo placement method on import. out-of-the-box, iM is set to Ken Burns, but 'fit' should be your setting.
    keep in mind:
    iMovie doesn't allow a clip-length of 1-frame, which is needed for smooth action for stop-motion.
    specialized tools as Boinx' iStopMotion (AppStore, 40$) or the free FrameByFrame do perhaps a (much) better job

  • How can I bar unwanted / spam calls

    How can I stop/bar unwanted calls which are spam? Phone is a Nokia c3 on Orange 5 contract.

    Hi  Welcome to the EE Community. The Nokia C3 does not have the functionality to block contacts from getting through to you, however here's a helpful page you should take a look at which may be able to help. Cheers,
    Titanium
    Was my post helpful? Please take 2 seconds to hit the 'Kudos' button below

  • How can i stop my phone from vibrating in my ear when i am on the call?

    How can i stop my phone from vibrating in my ear when i am on the call? I usually keeps my phone on vibration mode so i get notified when there is a new mail or message? However, when i am on the call, then also, it vibrates in my ear everytime a mail comes. Very irritating. I use my business phone and mails keep coming every 2-5 mins.
    Please help.

    very impractical . specially when you call A LOT and receive notifications constantly!

  • Phone and my husbands iphone ring at the same time when only one of us is being called. How can I stop this from happening?

    Both my iphone and my husbands iphone ring at the same time when only one of us is being called. How can I stop this from happening?

    This post has already been answered. Why are you plugging your own site with an answer that has already been given? Not to mention that the information you are handing out is incorrect. There is no bug, there is no patch, and no one needs to delete any email addresses. If you take the time to read the posts with the correct answers, you will see that the solution is totally different than the erroneous one you have posted.
    GB

  • When the wife gets a call or facetime request  my phone also get it . how can i stop this ?

    when my wife gets a facetime request or a phone call my phone also rings . how can i stop this ?

    That is a new Continuity feature described in Connect your iPhone, iPad, iPod touch, and Mac using Continuity - Apple Support
    You can turn it off by going to Settings > FaceTime and changing "iPhone Cellular Calls" to off.
    You may also want to consider using separate Apple IDs. That is more flexible now with new Family Sharing features: http://www.apple.com/ios/whats-new/family-sharing/

  • All my apple devices ring with each incoming phone call. How can I stop this?

    All of my apple devices ring with each incoming phone call. This includes an iPhone 6, an iPhone 4s, and an iPad mini (FaceTime). We have 2 different phone numbers, but they now seem interchangeable? How can I direct each phone call to its rightful phone?

    This is part of Apple continuity.  ON all of the devices go to settings - facetime - turn off iphone cellular calls.
    also don't use the same apple id for facetime.

  • How can I stop Address Book (Lion) from printing an extra blank page when I print an envelope

    Whenever I print a mailing envelope in Lion's Address Book, it prints fine but then prints a blank page following. How can I stop this?

    HP printers for years have had a problem printing an extra envelope from the Apple Addressbook Application with the # 10 envelope.
    Solution: Select a #9  envelop.  This prints about the same size and does not have an extra envelop.
    How to: From Address book, select print, Style: Envelopes, Layout: Layout pulldown Official 9 then print
    Interestingly the two smaller and two larger sizes only print one envelope. You can test this by clicking pdf and open pdf in preview.
    Over the years I have called HP and they couldn't solve the problem.
    I have told Apple today when I figures out the workaround.

  • About mackeeper how can i stop it disturbs me

    hi sir i have a problem with my macbook pro 13 mid 2012.
    whenever i open a page it opens another page for mackeeper and i have never dawnload it.
    i try evertime to escape it but now the notebook start to slow down please how can i stop any idea help please
    thank you all.

    You may have installed a variant of the "VSearch" ad-injection malware. Follow Apple Support's instructions to remove it.
    If you have trouble following those instructions, see below.
    Malware is always changing to get around the defenses against it. This procedure works as of now, as far as I know. It may not work in the future. Anyone finding this comment a few days or more after it was posted should look for a more recent discussion, or start a new one.
    The VSearch malware tries to hide itself by varying the names of the files it installs. To remove it, you must first identify the naming pattern.
    Triple-click the line below on this page to select it, then copy the text to the Clipboard by pressing the key combination  command-C:
    /Library/LaunchDaemons
    In the Finder, select
              Go ▹ Go to Folder...
    from the menu bar and paste into the box that opens by pressing command-V. You won't see what you pasted because a line break is included. Press return.
    A folder named "LaunchDaemons" may open. Look inside it for two files with names of the form
              com.something.daemon.plist
    and
               com.something.helper.plist
    Here something is a variable string of characters, which can be different in each case. So far it has always been a string of letters without punctuation, such as "cloud," "dot," "highway," "submarine," or "trusteddownloads." Sometimes it's a meaningless string such as "e8dec5ae7fc75c28" rather than a word. Sometimes the string is "apple," and then you must be especially careful not to delete the wrong files, because many built-in OS X files have similar names.
    If you find these files, leave the LaunchDaemons folder open, and open the following folder in the same way:
    /Library/LaunchAgents
    In this folder, there may be a file named
              com.something.agent.plist
    where the string something is the same as before.
    If you feel confident that you've identified the above files, back up all data, then drag just those three files—nothing else—to the Trash. You may be prompted for your administrator login password. Close the Finder windows and restart the computer.
    Don't delete the "LaunchAgents" or "LaunchDaemons" folder or anything else inside either one.
    The malware is now permanently inactivated, as long as you never reinstall it. You can stop here if you like, or you can remove two remaining components for the sake of completeness.
    Open this folder:
    /Library/Application Support
    If it has a subfolder named just
               something
    where something is the same string you saw before, drag that subfolder to the Trash and close the window.
    Don't delete the "Application Support" folder or anything else inside it.
    Finally, in this folder:
    /System/Library/Frameworks
    there may an item named exactly
                v.framework
    It's actually a folder, though it has a different icon than usual. This item always has the above name; it doesn't vary. Drag it to the Trash and close the window.
    Don't delete the "Frameworks" folder or anything else inside it.
    If you didn't find the files or you're not sure about the identification, post what you found.
    If in doubt, or if you have no backups, change nothing at all.
    The trouble may have started when you downloaded and ran an application called "MPlayerX." That's the name of a legitimate free movie player, but the name is also used fraudulently to distribute VSearch. If there is an item with that name in the Applications folder, delete it, and if you wish, replace it with the genuine article from mplayerx.org.
    This trojan is often found on illegal websites that traffic in pirated content such as movies. If you, or anyone else who uses the computer, visit such sites and follow prompts to install software, you can expect more of the same, and worse, to follow. Never install any software that you downloaded from a bittorrent, or that was downloaded by someone else from an unknown source.
    In the Security & Privacy pane of System Preferences, select the General tab. The radio button marked Anywhere  should not be selected. If it is, click the lock icon to unlock the settings, then select one of the other buttons. After that, don't ignore a warning that you are about to run or install an application from an unknown developer.
    Then, still in System Preferences, open the App Store or Software Update pane and check the box marked
              Install system data files and security updates (OS X 10.10 or later)
    or
              Download updates automatically (OS X 10.9 or earlier)
    if it's not already checked.

  • How can I stop the recent and favorites from show on the screen of my i-phone and i-pad, this is a new feature of iso 8

    How can I stop the recent and favorites from show on the screen of my i-phone and i-pad, this is a new feature of iso 8

    Hi AppleLove78,
    I'm sorry to hear you are having issues with your new iPhone 6. If you continue to have audio or call quality issues, you may find the information and troubleshooting steps outlined in the following article helpful:
    If you can't hear a person on a call or voicemail or if the sound quality is poor on iPhone - Apple Support
    Regards,
    - Brenden

  • How can i stop icloud from downloading all my photos onto my iphone 4s each time I sync. Then, how do I delete them from my phone, there are no delete options. I'm slowly going crazy.

    How can i stop icloud from downloading all my photos onto my iphone 4s each time I sync. Then, how do I delete them from my phone, there are no delete options. I'm slowly going crazy.

    Hi iprover,
      Let me first say that I am NO EXPERT with anything Apple and really have only started looking into this iCloud stuff a few hours ago because I'm trying to help my mom free up some space on her iCloud. But anyway, here's what I'm learning and what I THINK might be happening to you.
    Also, if any Apple support people (do they browse the forums to dish out helpful hints?) or people who probably know infinitely more than me about this stuff, please do chime in and confirm / correct my statements!
    This is what I gather.
    iCloud for Windows is set up with a local folder (let's call it "C:\Users\Administrator\Pictures\iCloud Photos\My Photo Stream") where iCloud will store photos uploaded to the Photo Stream. This will allow you to view your Photo Stream photos on your PC.
    This does not mean that this is a PC backup of your Photo Stream photos, but just a way to view your Photo Stream from your PC. That being said, the general Photo Stream rules apply: 1. No videos are on the photo-stream, and 2. Photos on the Photo Stream will be removed after ~30 days and/or after hitting the Photo Stream limit of 1000 photos.
    My guess is that the iCloud Windows App will delete photos from the local folder that are older than ~30 days and/or delete all but the most recent 1000 items if the 1000-item limit is reached.
    That is a scary thought! I really hope you did not lose all your pictures, but my guess is that the iCloud local folder is NOT meant for backup but instead is the iCloud Windows App's sandbox for updating and managing Photo Stream photos.
    All that being said, I'd say it's important to back up your photos in a separate folder not related to the iCloud local folder.

  • I rented a movie.  It downloaded it wouldn't play.  Now it is constantly tring to download.  How can I stop this?

    I rented a movie.  It downloaded it wouldn't play.  Now it is constantly tring to download.  How can I stop this?

    It should be in the video app.   Or go to the leftmost home screen, called spotlight, and type if the first part of the title.  If it is there, it will show up.

  • My desktop pages keep collapsing into my doc. How can I stop this?

    I have an iMac 500 with the Lion 10.6 OS. When I try to move a page I'm working on to a different part of the desktop my mouse doesn't pick it up and move it. Instead it collapses the page into my dock at the bottom of the screen. How can I stop this collapsing feature?

    Chris, thank you for your response. It is appreciated. What you mentioned was my original problem with the SIM (the fact that it did not mesh well with Apple). After plenty of frustration and money wasted, a technician advised me to de-activate iMessage and FaceTime because I was getting charged 0.30 euro for every call and every text. I followed this suggestion and after testing it out, then the phone seemed to be working fine without the extra charge problems.
    This morning when I woke up, I checked my credits on the phone and I had lost another 1.20 euro during the night. When I called PosteMobile, they told me that I had sent another 4 messages to that same UK number. I'm not sure how this is possible, and when I try to explain my situation to them, they are not the friendliest people. I've used WIND before in Italy as a provider without any problems, and they are not listed on the link that you sent me for Italy, so I don't understand why PosteMobile is giving me so many problems.
    I was also wondering if there is a way to block my phone from sending messages to this number? (Although, I don't even know the last 3 digits of the number). Sorry if I'm rambling, but I don't know how to resolve this problem without starting a new contract with another service provider.

  • My 4s is now copying me on all my texts. How can I stop this?

    My 4s is copying me on all my text messages.....how can I stop this?

    Unfortunately I no longer have an iPhone 4 to check but on my iPhone 5 this works.  Go to Settings->General->Accesibility->Incoming Calls.  Ensure you select "Default" and not "Speaker".
    I hope this helps.

  • TS2755 When i send an imessage to my wife, my mother in law receives it as well. How can i stop this

    When i send an imessage to my wife, my mother in law receives it as well. How can i stop this

    On your MIL's device go to Settings>Messages> Send and Receive and delete the email address you are calling. Add her own email address if she wants to use Messages and here own is not listed.

Maybe you are looking for

  • Upgrading to Acrobat X

    I've purchased a new laptop and would like to upgrade to Acrobat X on it.  I have Acrobat 8 Pro on my old system, but I've misplaced the original disk.  I have the serial number for the original program.  What is the best way to upgrade, without purc

  • How to Disable Tools-- Form settings option..

    Hi Experts, I have created two users User1 and User 2, i want the form settings option to be disabled for the 2nd user. How to Disable it? Any Inbuilt settings in SAP B1. waiting for the earliest reply. Regards, Magesh.

  • Macbook pro still slow after erase

    Hi everyone, I have a slow performance problem on my macbook pro that I cannot solve. I tried already everything I could find here (see below) but nothing worked so far and that´s why I´m posting a new thread. PROBLEM: Macbook got suddenly incredibly

  • Random purchased songs being deleted from my phone?

    I keep noticing a lot of different songs from different albums (purchased from iTunes) keep deleting themselves from my phone? I'm tired of having to go through and redownloading, so how can I solve this? Thanks!

  • Database trigger cannot read the updating table

    I want to create a database trigger : after insert or update on EMP for each row begin select sum(EMP.salary) from EMP WHERE ..... end; However, I got an error is that I cannot read the updating table if the trigger is 'for each row'. Can anyone help