Difference between subarray, subset & subsequence

女生的网名这么多〃 提交于 2019-11-30 01:35:11
Wilson

In my opinion, if the given pattern is array, the so called subarray means contiguous subsequence.

For example, if given {1, 2, 3, 4}, subarray can be

{1, 2, 3}
{2, 3, 4}
etc.

While the given pattern is a sequence, subsequence contain elements whose subscripts are increasing in the original sequence.

For example, also {1, 2, 3, 4}, subsequence can be

{1, 3}
{1,4}
etc.

While the given pattern is a set, subset contain any possible combinations of original set.

For example, {1, 2, 3, 4}, subset can be

{1}
{2}
{3}
{4}
{1, 2}
{1, 3}
{1, 4}
{2, 3}
etc.

In the context of an array, SubSequence - need not be contigious but needs to maintain the order. But SubArray is contigious and inherently maintains the order.

if you have {1,2,3,4} --- {1,3,4} is a valid SubSequence but its not a subarray.

And subset is no order and no contigious.. So you {1,3,2} is a valid sub set but not a subsequence or subarray.

{1,2} is a valid subarray, subset and subsequence.

All Subarrays are subsequences and all subsequence are subset.

But sometimes subset and subarrays and sub sequences are used interchangably and the word contigious is prefixed to make it more clear.

Sandip Pawar

Consider an array:

 {1,2,3,4}

Subarray: contiguous sequence in an array i.e.

{1,2},{1,2,3}

Subsequence: Need not to be contiguous, but maintains order i.e.

{1,2,4}

Subset: Same as subsequence except it has empty set i.e.

 {1,3},{}

Given an array/sequence of size n, possible

Subarray = n*(n+1)/2
Subseqeunce = (2^n) -1 (non-empty subsequences)
Subset = 2^n

Consider these two properties in collection (array, sequence, set, etc) of elements: Order and Continuity.

Order is when you cannot switch the indices or locations of two or more elements (a collection with a single element has an irrelevant order).

Continuity is that an element must have their neighbors remain with them or be null.

A subarray has Order and Continuity.

A subsequence has Order but not Continuity.

A subset does not Order nor Continuity.

A collection with Continuity but not Order does not exist (to my knowledge)

Per my understanding, for example, we have a list say [3,5,7,8,9]. here

subset doesn’t need to maintain order and has non-contiguous behavior. For example, [9,3] is a subset

subsequence maintain order and has non-contiguous behavior. For example, [5,8,9] is a subsequence

subarray maintains order and has contiguous behavior. For example, [8,9] is a subarray

subarray: some continuous elements in the array

subset: some elements in the collection

subsequence: in most case, some elements in the array maintaining relative order (not necessary to be continuous)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!