Question

In: Computer Science

a path is any sequence of edges that leads from the root to a leaf of...

a path is any sequence of edges that leads from the root to a leaf of a tree. The length of a path is the number of edges it traverses. So a path from A to B to C to D has length 3.

The questions refer to a simple optimization that can be done to improve the performance of mergesort. This is a modification to mergesort that performs the merge operation only if the last element of the first half of the array is greater than the first element of the second half of the array. Thus whenever the last element of the first half is less than or equal to the first element of the second half, we can avoid merging.

Suppose you draw the decision tree for mergesort when applied to an array of 2k elements. What will be the exact length of the longest path in the tree? (Assume that the optimization for mergesort is not being used.)

Suppose you draw the decision tree for mergesort when applied to an array of 2k elements. What will be the exact length of the shortest path in the tree?  (Assume that the optimization for mergesort is not being used.)

Suppose you draw the decision tree for mergesort when applied to an array of 2k elements. What will be the exact length of the shortest path in the tree?  (Assume that the optimization for mergesort is being used.)

Solutions

Expert Solution

// optimization mergesort
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "iostream"

using namespace std;

int main()
{

int elem = 2000; // elements array #2k
int con = 0; // counter division elements
int path; // path counter
double div; // posible division

//1)-- LONGETS PATH without optimization

div = 2000; // inicial div = #elem
while ( div >= 1) // provided that div is greater than 1, the paths can continue to be divided
{
div = div/2;
con++;
}
path = con*2; // the number of longest path are the number of possible divisions by 2
// because it is the longest path that can be taken

cout<<"Longest Path: "<<path<<endl;


//2)-- SHORTEST PATH without optimization

// at some point an odd array remains, which avoids the division and union
// of an element, so those edges of the longest path(without optimization) are subtracted
path = con*2 - 2;

cout<<"Shortest Path: "<<path<<endl;

//3)-- SHORTEST PATH WITH OPTIMIZATION
// Since the array can be divided X times, it means that it can be merged X times.
// In the best case (shortest path) the optimization condition is met,
// and the merged is not necessary, so X times can be avoided,
// except at the last edge.
path = con + 1;
cout<<"Shortest Path with Optimization: "<<path<<endl;
}

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.


Related Solutions

Anatomy. Fully describe the entire path that water travels from the root hair to the hydathode...
Anatomy. Fully describe the entire path that water travels from the root hair to the hydathode as guttation occurs. Correctly use at least the following six anatomical terms in your description: hydathode, stele, tracheid, trichome, vascular bundle, and xylem. Note that you may need to use more terms than this to completely describe this anatomical pathway.
Write a program that implements the A* algorithm to find a path from any two given...
Write a program that implements the A* algorithm to find a path from any two given nodes. You may use any of the following languages: C++, C#, Java, ActionScript. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan...
Describe the sequence of steps for an action potential of one neuronal cell leads to the...
Describe the sequence of steps for an action potential of one neuronal cell leads to the action potential in a postsynaptic cell.
Provide experimental evidence for auxin’s involvement in: a. embryo patterning b. root formation c. cotyledon/leaf formation...
Provide experimental evidence for auxin’s involvement in: a. embryo patterning b. root formation c. cotyledon/leaf formation d. vein formation e. cell elongation f. photo- and gravi-tropism g. apical dominance
A single leaf is taken from 11 different tobacco plants. Each leaf is divided in half,...
A single leaf is taken from 11 different tobacco plants. Each leaf is divided in half, and given one of two preparations of mosaic virus. We wish to examine if there is a difference in the mean number of lesions from the two preparations. Plant Prep1 Prep2 ----------------- 1    18    14 2    20    15 3     9     6   4    14    12   5    38    32   6    26    30   7    15     9   8    10     2   9    25    18 10     7     3 11    13    ...
1.  Prove that for any graph, the sum the degreesPv∈V deg(v) is twice the number of edges...
1.  Prove that for any graph, the sum the degreesPv∈V deg(v) is twice the number of edges |E|. (By “prove” I mean write a few sentences explaining why it is true.) 2. i) At a recent math seminar, 5 mathematicians greeted each other by shaking hands. Is it possible for each mathematician to shake hands with exactly 3 other people? (No one can shake his or her own hand.) To answer the question, please rephrase the problem as a problem about...
DEPTH counting starts at 1 at the root. Q1 [20 pts.]: Given the following key sequence...
DEPTH counting starts at 1 at the root. Q1 [20 pts.]: Given the following key sequence (6,22,9,14,13,1,8) build the dynamic binary search tree WITHOUT BALANCING IT. How many probes (i.e., comparisons) does it take to determine that key 100 is not in the tree? (In this study case, the root is 6; the next element 22 > 6, so it goes to the right, etc).
Research the latest news about the Nissan LEAF in the Canadian market. Have any new features...
Research the latest news about the Nissan LEAF in the Canadian market. Have any new features been added since the car was launched as a new product? Has the style changed? Find out what colors the car is available in. How do you think automobile marketers decide which colors do offer?
The “divide and average” method, an old time method for approximating the square root of any...
The “divide and average” method, an old time method for approximating the square root of any positive number a, can be formulated as x = (x + a/x) / 2 Write a well-structured M-file function based on the while…break loop structure to implement this algorithm. At each step estimate the error in your approximation as ε = abs(( Xnew − Xold )/Xnew Repeat the loop until e is less than or equal to a specified value. Design your program so...
DEPTH counting starts at 1 at the root. What should the loading factor be (on ANY...
DEPTH counting starts at 1 at the root. What should the loading factor be (on ANY HASH TABLE) if you want to have an average of 1.4 comparisons per successful search if LINEAR PROBING?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT