Breadth First Vs Depth First
When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great. 4 Answers 4
When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great. 4 Answers 4
I am working on a script to recursively go through subfolders in a mainfolder and build a list off a certain file type. I am having an issue with the script. It’s currently set as follows: for root, subFolder, files in os.walk(PATH): for item in files: if item.endswith(“.txt”) : fileNamePath = str(os.path.join(root,subFolder,item)) the problem is … Read more
A reddit thread brought up an apparently interesting question: Tail recursive functions can trivially be converted into iterative functions. Other ones, can be transformed by using an explicit stack. Can every recursion be transformed into iteration? The (counter?)example in the post is the pair: (define (num-ways x y) (case ((= x 0) 1) ((= y … Read more
I am creating a quick backup script that will dump some databases into a nice/neat directory structure and I realized that I need to test to make sure that the directories exist before I create them. The code I have works, but is there a better way to do it? [ -d “$BACKUP_DIR” ] || … Read more
Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn’t pass in the function name. $factorial = function( $n ) use ( $factorial ) { if( $n <= 1 ) return 1; return $factorial( $n – 1 ) * … Read more
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for … Read more
Most of the times, the definition of reentrance is quoted from Wikipedia: A computer program or routine is described as reentrant if it can be safely called again before its previous invocation has been completed (i.e it can be safely executed concurrently). To be reentrant, a computer program or routine: Must hold no static (or … Read more
This is similar to this question, but I want to include the path relative to the current directory in unix. If I do the following: ls -LR | grep .txt It doesn’t include the full paths. For example, I have the following directory structure: test1/file.txt test2/file1.txt test2/file2.txt The code above will return: file.txt file1.txt file2.txt … Read more
I am to show that log(n!) = Θ(n·log(n)). A hint was given that I should show the upper bound with nn and show the lower bound with (n/2)(n/2). This does not seem all that intuitive to me. Why would that be the case? I can definitely see how to convert nn to n·log(n) (i.e. log … Read more
Is there a performance hit if we use a loop instead of recursion or vice versa in algorithms where both can serve the same purpose? Eg: Check if the given string is a palindrome. I have seen many programmers using recursion as a means to show off when a simple iteration algorithm can fit the … Read more