Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Prove (rigorously and by definition, not generally) that the number of operations in the algorithm below is O(n 2 ), where n is the number

Prove (rigorously and by definition, not generally) that the number of operations in the algorithm below is O(n2 ), where n is the number of nodes in the tree.

image text in transcribed

Algorithm 3 Calculate number of pairs of nodes such that one is ancestor of other and the sum of values stored in these nodes is equal to a function PrObleM5 (Node curNode;; array valuesSeen) valuesSeen.append(curNode.value) *Count how many ancestors of current node have value a- curNode. value* for value in valuesSeen do if value -a - curNode.value then answer += 1 end if end for or child in curNode.children do end for remove the last element from valuesSeen answer PROBLEM 5(child, valuesSeen) return answer end function function WrAPPERFUNCTION values:Seen- answer - PROBLEM5(root, valuesSeen) print ("The number of such pairs of nodes found is: answer) end function Note: The remove operation is required if the array is passed by reference. In such a case, if we modify the array in the function (as we do while appending in the first step), then this modification needs to be undone before we return, or else the modification to the array will be preserved. We could have also made the array to be global (declared it outside the function), instead of passing it through the recursion, and in this case as well the append and remove steps would both be required Algorithm 3 Calculate number of pairs of nodes such that one is ancestor of other and the sum of values stored in these nodes is equal to a function PrObleM5 (Node curNode;; array valuesSeen) valuesSeen.append(curNode.value) *Count how many ancestors of current node have value a- curNode. value* for value in valuesSeen do if value -a - curNode.value then answer += 1 end if end for or child in curNode.children do end for remove the last element from valuesSeen answer PROBLEM 5(child, valuesSeen) return answer end function function WrAPPERFUNCTION values:Seen- answer - PROBLEM5(root, valuesSeen) print ("The number of such pairs of nodes found is: answer) end function Note: The remove operation is required if the array is passed by reference. In such a case, if we modify the array in the function (as we do while appending in the first step), then this modification needs to be undone before we return, or else the modification to the array will be preserved. We could have also made the array to be global (declared it outside the function), instead of passing it through the recursion, and in this case as well the append and remove steps would both be required

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

ISBN: B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

=+ Is the giving level high enough?

Answered: 1 week ago

Question

Explain the cost of capital.

Answered: 1 week ago

Question

Define capital structure.

Answered: 1 week ago

Question

List out some inventory management techniques.

Answered: 1 week ago

Question

1. Write down two or three of your greatest strengths.

Answered: 1 week ago