My immediate thought was to use a Depth First Search or a Breadth First Search b
ID: 642791 • Letter: M
Question
My immediate thought was to use a Depth First Search or a Breadth First Search because their time complexity is O(n+m) but I'm not sure that's right at all.
You are given two collections of numbers, each stored in arrays sorted from least to greatest. Give an O(log(n+m)) algorithm that finds the median of the collection made from combining these two. We will define the median as the center element if the collection has an odd length, and the left of the two center elements if even: this corresponds to position [(m + n/2] in an array that ranges from 1 to (m + n). m is the number of elements in one collection, n is the number in the second. First consider how this could be solved if the sets were the same size, then use those insights to solve the problem for unequally-sized sets. Also give a recurrence relation for the time cost of your algorithm, and show that it is O(log(n + m)). My immediate thought was to use a Depth First Search or a Breadth First Search because their time complexity is O(n+m) but I'm not sure that's right at all.Explanation / Answer
Let us consider two arrays of Odd length then the median is Center element there is no doubt. if the array sort eithe greater to lower or lower to greater.
There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n))
Median: In probability theory and statistics, a median is described as the number separating the higher half of a sample, a population, or a probability distribution, from the lower half.
The median of a finite list of numbers can be found by arranging all the numbers from lowest value to highest value and picking the middle one.
For getting the median of input array { 12, 11, 15, 10, 20 }, first sort the array. We get { 10, 11, 12, 15, 20 } after sorting. Median is the middle element of the sorted array which is 12.
There are different conventions to take median of an array with even number of elements, one can take the mean of the two middle values, or first middle value, or second middle value.
Let us see different methods to get the median of two sorted arrays of size n each. Since size of the set for which we are looking for median is even (2n), we are taking average of middle two numbers in all below solutions.
Method 1 (Simply count while Merging)
Use merge procedure of merge sort. Keep track of count while comparing elements of two arrays. If count becomes n(For 2n elements), we have reached the median. Take the average of the elements at indexes n-1 and n in the merged array. See the below implementation.
#include <stdio.h>
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
int getMedian(int ar1[], int ar2[], int n)
{
int i = 0; /* Current index of i/p array ar1[] */
int j = 0; /* Current index of i/p array ar2[] */
int count;
int m1 = -1, m2 = -1;
/* Since there are 2n elements, median will be average
of elements at index n-1 and n in the array obtained after
merging ar1 and ar2 */
for (count = 0; count <= n; count++)
{
/*Below is to handle case where all elements of ar1[] are
smaller than smallest(or first) element of ar2[]*/
if (i == n)
{
m1 = m2;
m2 = ar2[0];
break;
}
/*Below is to handle case where all elements of ar2[] are
smaller than smallest(or first) element of ar1[]*/
else if (j == n)
{
m1 = m2;
m2 = ar1[0];
break;
}
if (ar1[i] < ar2[j])
{
m1 = m2; /* Store the prev median */
m2 = ar1[i];
i++;
}
else
{
m1 = m2; /* Store the prev median */
m2 = ar2[j];
j++;
}
}
return (m1 + m2)/2;
}
/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
int n1 = sizeof(ar1)/sizeof(ar1[0]);
int n2 = sizeof(ar2)/sizeof(ar2[0]);
if (n1 == n2)
printf("Median is %d", getMedian(ar1, ar2, n1));
else
printf("Doesn't work for arrays of unequal size");
getchar();
return 0;
}
O/p is:
Time Complexity: O(n)
Depth First Search Analysis
The general running time for depth first search is as follows. The loops in dfs both run in O(V), not counting what happens in dfsvisit, since they are executed once for each vertex in the graph. Indfsvisit the loop is executed once for each edge in the adjacency list of the current vertex. Sincedfsvisit is only called recursively if the vertex is white, the loop will execute a maximum of once for every edge in the graph or O(E). So, the total time for depth first search is O(V+E).
/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
int n1 = sizeof(ar1)/sizeof(ar1[0]);
int n2 = sizeof(ar2)/sizeof(ar2[0]);
if (n1 == n2)
printf("Median is %d", getMedian(ar1, ar2, n1));
else
printf("Doesn't work for arrays of unequal size");
getchar();
return 0;
}
O/p is:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.