Answered step by step
Verified Expert Solution
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.
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started