0% found this document useful (0 votes)
16 views14 pages

5 Array Based Sequences

DSA TOPIC ARRAY IITD COL106

Uploaded by

atqonxy5wb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

5 Array Based Sequences

DSA TOPIC ARRAY IITD COL106

Uploaded by

atqonxy5wb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structures and

Algorithms in Python
Michael T. Goodrich/ Roberto
Tamassia/ Michael H. Goldwasser
Adapted by Subhrakanta Panda

Chapter 5
Array-Based Sequences

© 2021 Goodrich, Tamassia, Goldwasser


Array-Based Sequences

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 2


Python Sequence Classes
❑ Python has built-in types, list, tuple, and str.
❑ Each of these sequence types supports indexing to
access an individual element of a sequence, using a
syntax such as A[i]
❑ Each of these types uses an array to represent the
sequence.
■ An array is a set of memory locations that can be
addressed using consecutive indices, which, in Python,
start with index 0.

A
0 1 2 i n
© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 3
Arrays of Characters or
Object References
❑ An array can store primitive elements, such as
characters, giving us a compact array.

❑ An array can also store references to objects.

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 4


Compact Arrays
❑ Primary support for compact arrays is in a
module named array.
■ That module defines a class, also named array,
providing compact storage for arrays of primitive
data types.
❑ The constructor for the array class requires a
type code as a first parameter, which is a
character that designates the type of data that
will be stored in the array.

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 5


Type Codes in the array Class
❑ Python’s array class has the following
type codes:

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 6


Insertion
❑ In an operation add(i, o), we need to make room
for the new element by shifting forward the n − i
elements A[i], …, A[n − 1]
❑ In the worst case (i = 0), this takes O(n) time

A
0 1 2 i n
A
0 1 2 i n
A o
0 1 2 i n
© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 7
Element Removal
❑ In an operation remove(i), we need to fill the hole left by
the removed element by shifting backward the n − i − 1
elements A[i + 1], …, A[n − 1]
❑ In the worst case (i = 0), this takes O(n) time

A o
0 1 2 i n
A
0 1 2 i n
A
0 1 2 i n
© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 8
Performance
❑ In an array based implementation of a
dynamic list:
■ The space used by the data structure is O(n)
■ Indexing the element at I takes O(1) time
■ add and remove run in O(n) time in worst case
❑ In an add operation, when the array is full,
instead of throwing an exception, we can
replace the array with a larger one…

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 9


Growable Array-based Array List
❑ In an add(o) operation
(without an index), we could Algorithm add(o)
always add at the end if t = S.length − 1
then
❑ When the array is full, we A ← new array
replace the array with a of
larger one size
❑ How large should the new …
array be? for i ← 0 to n− 1
■ Incremental strategy: increase
do
the size by a constant c A[i] ← S[i]
■ Doubling strategy: double the
S←A
size n←n+1
© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences S[n− 1] ← o 10
Comparison of the Strategies
❑ We compare the incremental strategy and
the doubling strategy by analyzing the total
time T(n) needed to perform a series of n
add(o) operations
❑ We assume that we start with an empty
stack represented by an array of size 1
❑ We call amortized time of an add operation
the average time taken by an add over the
series of operations, i.e., T(n)/n

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 11


Incremental Strategy Analysis
❑ We replace the array k = n/c times
❑ The total time T(n) of a series of n add
operations is proportional to
n + c + 2c + 3c + 4c + … + kc =
n + c(1 + 2 + 3 + … + k) =
n + ck(k + 1)/2
❑ Since c is a constant, T(n) is O(n + k2), i.e.,
O(n2)
❑ The amortized time of an add operation is O(n)

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 12


Doubling Strategy Analysis
❑ We replace the array k = log2 n
times
geometric series
❑ The total time T(n) of a series of n
add operations is proportional to 2
n + 1 + 2 + 4 + 8 + …+ 2k = 4
1 1
n + 2k + 1 − 1 =
3n − 1
8
❑ T(n) is O(n)
❑ The amortized time of an add
operation is O(1)

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 13


Python Implementation

© 2021 Goodrich, Tamassia, Goldwasser Array-Based Sequences 14

You might also like