Hash (3)-infers whether an array is a subset of an array

Source: Internet
Author: User

given two arrays: Arr1[0..m-1] and arr2[0..n-1]. Infer arr2[] is a subset of arr1[].

Both of these arrays are unordered.

Like what:
Input: arr1[] = {One, 1, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: ARR2 is a subset of the arr1.

Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: ARR2 is a subset of the arr1.

Input: arr1[] = {Ten, 5, 2, Max, +}, arr2[] = {19, 5, 3}
Output: ARR2 is not a subset of arr1, because element 3 in ARR2 does not exist in arr1.

Method 1 (Simple method):Use two loops to process: The outer loop iterates through each element of the arr2.

The inner loop is compared to the elements passed in from the outer layer. Assume that all elements match successfully. Returns 1, otherwise 0 is returned.

#include <iostream>//assumes that ARR2 is a subset of arr1, it returns 1.bool issubset (int arr1[], int arr2[], int numArr1, int numArr2) {  int i = 0;  int j = 0;  for (i = 0; i < numArr2; i++)  {for    (j = 0; J < NumArr1; J + +)    {      if (arr2[i] = = Arr1[j])        break;
     }    //Assuming that the inner loop above does not break, the ARR2 is not a subset of arr1    if (j = = numArr1)      return 0;  }  Assuming run here, the ARR2 is a subset of arr1  return 1;} int main () {  int arr1[] = {One, 1, 3, 7};  int arr2[] = {One, 3, 7, 1};  int numArr1 = sizeof (ARR1)/sizeof (arr1[0]);  int numArr2 = sizeof (ARR2)/sizeof (arr2[0]);  if (Issubset (arr1, arr2, NUMARR1, NUMARR2))    std::cout<< "arr2[] is subset of arr1[]";  else    std::cout<< "arr2[" is not a subset of arr1[] ";  return 0;}
complexity of Time: O (m*n)
Method 2 (use sort and binary search)1) Sort arr1[], average O (MLOGM)
2) for each element in arr2[], binary search in sorted arr1[].
A) If this element is not found, then 0 is returned.
3) Assume that all elements are found. Then 1 is returned.
#include the <iostream>//function declaration.

Two auxiliary functions to infer a subset.

void QuickSort (int *arr, int si, int ei), int binarysearch (int arr[], int low, int high, int x);//assuming arr2[] is a subset of arr1[], return 1bool issubset (int arr1[], int arr2[], int numArr1, int numArr2) {int i = 0; QuickSort (arr1, 0, numarr1-1); for (i=0; i<numarr2; i++) {if (BinarySearch (arr1, 0, Numarr1-1, arr2[i]) = =-1) return 0; }//Assume that it's running here. Description ARR2 is a subset of arr1 return 1;} ---------Auxiliary function begin--------//Standard binary lookup function int binarysearch (int arr[], int low, int high, int x) {if (high >= low) {in T mid = (low + high)/2; or Low + (high-low)/2; /* * Detect Arr[mid] is the first time you encounter X. * When X meets one of the following conditions, it proves to be the first encounter with X: (1) (Mid = = 0) && (arr[mid] = = x) (2) (Arr[mid-1] < x) && (Arr[mid] = = x) */if ((Mid = = 0 | | arr[mid-1] < x) && (Arr[mid] = = x)) return mid; else if (x > Arr[mid]) return BinarySearch (arr, (mid + 1), high, X); else return BinarySearch (arr, Low, (mid-1), x); } return-1;} Template<typenameType>void Exchange (Type *a, type *b) {type temp; temp = *a; *a = *b; *b = temp;} int partition (int a[], int si, int ei) {int x = A[ei]; int i = (si-1); Int J; for (j = si; J <= Ei-1; j + +) {if (A[j] <= x) {i++; Exchange (&a[i], &a[j]); }} Exchange (&a[i + 1], &a[ei]); return (i + 1);} /* Implement high-speed sorting function a[]: Array to be sorted si:starting indexei:ending index*/void quickSort (int a[], int si, int ei) {int pi; Partitioning index if (Si < ei) {pi = partition (A, si, ei); QuickSort (A, si, pi-1); QuickSort (A, pi + 1, ei); }}//---------auxiliary function End--------int main () {int arr1[] = {11, 1, 13, 21, 3, 7}; int arr2[] = {11, 3, 7, 1}; int numArr1 = sizeof (ARR1)/sizeof (arr1[0]); int numArr2 = sizeof (ARR2)/sizeof (arr2[0]); if (Issubset (arr1, arr2, NUMARR1, NUMARR2)) std::cout<< "arr2[] is subset of arr1[]"; else std::cout<< "arr2[" is not A subset of arr1[] "; return 0;}

Time complexity: O (MLOGM + NLOGM). Among them, MLOGM is the average complexity of the sorting algorithm. Since the above uses a high-speed sort, assuming the worst case scenario, the complexity becomes O (m^2).
Method 3 (use sort and merge)1) sort the two arrays arr1 and ARR2 respectively.

O (MLOGM + nlogn)
2) Use the merge process to detect if the ordered array arr2 exists in the arr1.


Suppose Arr2 is a subset of arr1. Then return 1bool issubset (int arr1[], int arr2[], int m, int n) {    int i = 0, j = 0;    if (M < n)       return 0;    QuickSort (arr1, 0, m-1);    QuickSort (arr2, 0, n-1);    while (I < n && J < m)    {        if (Arr1[j] <arr2[i])            j + +;        else if (arr1[j] = = Arr2[i])        {            j + +;            i++;        }        else if (Arr1[j] > Arr2[i])            return 0;    }    if (i < n)        return 0;    else        return 1;}
Time complexity: O (MLOGM + nlogn). For example, Law 2 is better.
Method 4 (use hash)1) Create a hash table for all elements of the array arr1.
2) iterate through the array arr2. and detects whether each element exists in a hash table. If no element is found in the hash table, 0 is returned.
3) Assume that all elements have been found. Then 1 is returned.
Note: The method 1,2,4 does not handle the case where there are repeated elements in the ARR2 array. For example, {1, 4, 4, 2} are not actually subsets of {1, 4, 2}, but these methods are thought to be subsets.

Hash (3)-infers whether an array is a subset of an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.