SlideShare a Scribd company logo
1 of 50
Download to read offline
Data Structures
Linear List Array Representation
Andres Mendez-Vazquez
May 6, 2015
1 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
2 / 24
Images/cinvestav-
How do we implement the ADT List?
In our first representation will use an array
Use a one-dimensional array element[]:
a b c d e - - - -
0 1 2 3 4 5 6 7 8
The previous array
A representation of L = (a, b, c, d, e) using position i in element[i].
3 / 24
Images/cinvestav-
How do we implement the ADT List?
In our first representation will use an array
Use a one-dimensional array element[]:
a b c d e - - - -
0 1 2 3 4 5 6 7 8
The previous array
A representation of L = (a, b, c, d, e) using position i in element[i].
3 / 24
Images/cinvestav-
Where to map in the array
Right To Left Mapping
- - - - e d c b a
Mapping That Skips Every Other Position
a - b - c - d - e - -
Wrap Around Mapping
d e - - - - - - a b c
4 / 24
Images/cinvestav-
Where to map in the array
Right To Left Mapping
- - - - e d c b a
Mapping That Skips Every Other Position
a - b - c - d - e - -
Wrap Around Mapping
d e - - - - - - a b c
4 / 24
Images/cinvestav-
Where to map in the array
Right To Left Mapping
- - - - e d c b a
Mapping That Skips Every Other Position
a - b - c - d - e - -
Wrap Around Mapping
d e - - - - - - a b c
4 / 24
Images/cinvestav-
Representation Used In Text
Something Notable
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
Thus
1 Put element i of list in element[i].
2 Use a variable size to record current number of elements
5 / 24
Images/cinvestav-
Representation Used In Text
Something Notable
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
Thus
1 Put element i of list in element[i].
2 Use a variable size to record current number of elements
5 / 24
Images/cinvestav-
Code
Our Implementation
p u b l i c c l a s s S i m p l e A r r a y L i s t <Item> implements
L i n e a r L i s t <Item >{
// p r i v a t e elements of implementation
p r o t e c t e d Item element [ ] ;
p r o t e c t e d i n t s i z e ;
p r o t e c t e d f i n a l s t a t i c i n t DEFAULT_SIZE = 10;
// C o n s t r u c t o r s
p u b l i c S i m p l e A r r a y L i s t (){
t h i s . s i z e = 0;
t h i s . element = ( Item [ ] ) new Object [ t h i s . DEFAULT_SIZE ] ;
}
p u b l i c S i m p l e A r r a y L i s t ( i n t NewSize ){
t h i s . s i z e = 0;
t h i s . element = ( Item [ ] ) new Object [ NewSize ] ;
}
6 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
8 / 24
Images/cinvestav-
Operations: Add
Add/Remove an element
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
add(1,g)
a g b c d e - - -
0 1 2 3 4 5 6 7 8
Size=6
9 / 24
Images/cinvestav-
Operations: Add
Add/Remove an element
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
add(1,g)
a g b c d e - - -
0 1 2 3 4 5 6 7 8
Size=6
9 / 24
Images/cinvestav-
Code
Our Implementation
p u b l i c void add ( i n t index , Item myobject ){
// I n i t i a l V a r i a b l e s
i n t i ;
// Always check f o r p o s s i b l e e r r o r s
i f ( t h i s . s i z e == element . len ght ){
System . out . p r i n t l n ( " L i s t ␣ does ␣ not ␣ have ␣ space " ) ;
System . e x i t ( 0 ) ;
}
i f ( index <0 | | index >t h i s . s i z e ){
System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ;
System . e x i t ( 0 ) ;
}
// S h i f t p o s t i i o n s as n e c e s s a r y
f o r ( i = t h i s . s i z e ; i >index ; i −−)
element [ i +1]=element [ i ] ;
// copy element i n t o c o n t a i n e r
element [ i +1]=myobject ;
}
10 / 24
Images/cinvestav-
Operations: Get
Here
We have
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
get(3)
It will return “d”.
The code is simple
11 / 24
Images/cinvestav-
Operations: Get
Here
We have
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
get(3)
It will return “d”.
The code is simple
11 / 24
Images/cinvestav-
Operations: Get
Here
We have
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
get(3)
It will return “d”.
The code is simple
p u b l i c Item get ( i n t index ){
// Check always
i f ( t h i s . s i z e == 0) r e t u r n n u l l ;
i f ( index <0 | | index >t h i s . s i z e −1){
System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ;
System . e x i t ( 0 ) ;
}
r e t u r n element [ index ]
}
11 / 24
Images/cinvestav-
You can implement the rest
Yes
Part of your homework!!!
12 / 24
Images/cinvestav-
Now, we have a common problem
Because
We do not know how many elements will be stored at the list.
We use
An initial length and dynamically increase the size as needed.
13 / 24
Images/cinvestav-
Now, we have a common problem
Because
We do not know how many elements will be stored at the list.
We use
An initial length and dynamically increase the size as needed.
13 / 24
Images/cinvestav-
Example
Example
Length of array element[] is 6:
a b c d e f
First create a new and larger array
NewArray = (Item[]) new Object[12]
- - - - - - - - - - - -
Now copy the new elements into the new array!!!
System.arraycopy(element, 0, newArray, 0, element.length);
a b c d e f - - - - - -
14 / 24
Images/cinvestav-
Example
Example
Length of array element[] is 6:
a b c d e f
First create a new and larger array
NewArray = (Item[]) new Object[12]
- - - - - - - - - - - -
Now copy the new elements into the new array!!!
System.arraycopy(element, 0, newArray, 0, element.length);
a b c d e f - - - - - -
14 / 24
Images/cinvestav-
Example
Example
Length of array element[] is 6:
a b c d e f
First create a new and larger array
NewArray = (Item[]) new Object[12]
- - - - - - - - - - - -
Now copy the new elements into the new array!!!
System.arraycopy(element, 0, newArray, 0, element.length);
a b c d e f - - - - - -
14 / 24
Images/cinvestav-
Example
Finally, rename new array
element = NewArray;
element[0]−→ a b c d e f - - - - - -
15 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
16 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
1 + 2 + 3 + · · · + n =
n (n + 1)
2
= O n2
(1)
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
1 + 2 + 3 + · · · + n =
n (n + 1)
2
= O n2
(1)
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
19 / 24
Images/cinvestav-
A better strategy
Dynamic Array
To avoid incurring the cost of resizing many times, dynamic arrays resize
by an amount a.
In our example we double the size, a = 2
20 / 24
Images/cinvestav-
A better strategy
Dynamic Array
To avoid incurring the cost of resizing many times, dynamic arrays resize
by an amount a.
In our example we double the size, a = 2
Item NewArray [ ] ;
i f ( t h i s . s i z e == element . len ght ){
// Resize the c a p a c i t y
NewArray = ( Item [ ] ) new Object [ 2*this.size ]
f o r ( i n t i =0; i < s i z e ; i ++){
NewArray [ i ]= element [ i ] ;
}
element = NewArray ;
}
20 / 24
Images/cinvestav-
Space Complexity
Every time an insertion triggers a doubling of the array
Thus, space wasted in the new array:
Space Wasted = Old Lenght − 1 (2)
Remember: We double the array and insert!!!
Thus, the average space wasted is
Θ (n) (3)
21 / 24
Images/cinvestav-
Space Complexity
Every time an insertion triggers a doubling of the array
Thus, space wasted in the new array:
Space Wasted = Old Lenght − 1 (2)
Remember: We double the array and insert!!!
Thus, the average space wasted is
Θ (n) (3)
21 / 24
Images/cinvestav-
For example, we have the following
A trade-off between time and space
You have and average time for insertion is 2
2−1 .
In addition, an upper bound for the wasted cells in the array is
(2 − 1) n − 1 = n − 1.
Actually a more general term of expansion, a
Thus, we have:
Average time for insertion is a
a−1 .
An upper bound for the wasted cells in the array is
(a − 1) n − 1 = an − n − 1.
Different languages use different values
Java, a = 3
2 .
Python, a = 9
8
22 / 24
Images/cinvestav-
For example, we have the following
A trade-off between time and space
You have and average time for insertion is 2
2−1 .
In addition, an upper bound for the wasted cells in the array is
(2 − 1) n − 1 = n − 1.
Actually a more general term of expansion, a
Thus, we have:
Average time for insertion is a
a−1 .
An upper bound for the wasted cells in the array is
(a − 1) n − 1 = an − n − 1.
Different languages use different values
Java, a = 3
2 .
Python, a = 9
8
22 / 24
Images/cinvestav-
For example, we have the following
A trade-off between time and space
You have and average time for insertion is 2
2−1 .
In addition, an upper bound for the wasted cells in the array is
(2 − 1) n − 1 = n − 1.
Actually a more general term of expansion, a
Thus, we have:
Average time for insertion is a
a−1 .
An upper bound for the wasted cells in the array is
(a − 1) n − 1 = an − n − 1.
Different languages use different values
Java, a = 3
2 .
Python, a = 9
8
22 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
23 / 24
Images/cinvestav-
We have the following final analysis
Amortized Analysis Vs. Classic
Array Classic Analysis Dynamic Array
Amortized Analysis
Indexing O (1) O (1)
Search O (n) O (n)
Add/Remove O (n) O (n)
Space Complexity O (n) O (n)
24 / 24

More Related Content

What's hot

What's hot (20)

Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
7array in c#
7array in c#7array in c#
7array in c#
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Arrays
ArraysArrays
Arrays
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays
ArraysArrays
Arrays
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Set data structure
Set data structure Set data structure
Set data structure
 
Array data structure
Array data structureArray data structure
Array data structure
 

Similar to Preparation Data Structures 04 array linear_list

Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
CS301-lec01.ppt
CS301-lec01.pptCS301-lec01.ppt
CS301-lec01.pptomair31
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01shaziabibi5
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.pptMahyuddin8
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfarihantcomp1008
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programmingchandankumar364348
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPeterlqELawrenceb
 

Similar to Preparation Data Structures 04 array linear_list (20)

Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Data structures
Data structuresData structures
Data structures
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
CS301-lec01.ppt
CS301-lec01.pptCS301-lec01.ppt
CS301-lec01.ppt
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Data Structure Lec #1
Data Structure Lec #1Data Structure Lec #1
Data Structure Lec #1
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docx
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Preparation Data Structures 04 array linear_list

  • 1. Data Structures Linear List Array Representation Andres Mendez-Vazquez May 6, 2015 1 / 24
  • 2. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 2 / 24
  • 3. Images/cinvestav- How do we implement the ADT List? In our first representation will use an array Use a one-dimensional array element[]: a b c d e - - - - 0 1 2 3 4 5 6 7 8 The previous array A representation of L = (a, b, c, d, e) using position i in element[i]. 3 / 24
  • 4. Images/cinvestav- How do we implement the ADT List? In our first representation will use an array Use a one-dimensional array element[]: a b c d e - - - - 0 1 2 3 4 5 6 7 8 The previous array A representation of L = (a, b, c, d, e) using position i in element[i]. 3 / 24
  • 5. Images/cinvestav- Where to map in the array Right To Left Mapping - - - - e d c b a Mapping That Skips Every Other Position a - b - c - d - e - - Wrap Around Mapping d e - - - - - - a b c 4 / 24
  • 6. Images/cinvestav- Where to map in the array Right To Left Mapping - - - - e d c b a Mapping That Skips Every Other Position a - b - c - d - e - - Wrap Around Mapping d e - - - - - - a b c 4 / 24
  • 7. Images/cinvestav- Where to map in the array Right To Left Mapping - - - - e d c b a Mapping That Skips Every Other Position a - b - c - d - e - - Wrap Around Mapping d e - - - - - - a b c 4 / 24
  • 8. Images/cinvestav- Representation Used In Text Something Notable a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 Thus 1 Put element i of list in element[i]. 2 Use a variable size to record current number of elements 5 / 24
  • 9. Images/cinvestav- Representation Used In Text Something Notable a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 Thus 1 Put element i of list in element[i]. 2 Use a variable size to record current number of elements 5 / 24
  • 10. Images/cinvestav- Code Our Implementation p u b l i c c l a s s S i m p l e A r r a y L i s t <Item> implements L i n e a r L i s t <Item >{ // p r i v a t e elements of implementation p r o t e c t e d Item element [ ] ; p r o t e c t e d i n t s i z e ; p r o t e c t e d f i n a l s t a t i c i n t DEFAULT_SIZE = 10; // C o n s t r u c t o r s p u b l i c S i m p l e A r r a y L i s t (){ t h i s . s i z e = 0; t h i s . element = ( Item [ ] ) new Object [ t h i s . DEFAULT_SIZE ] ; } p u b l i c S i m p l e A r r a y L i s t ( i n t NewSize ){ t h i s . s i z e = 0; t h i s . element = ( Item [ ] ) new Object [ NewSize ] ; } 6 / 24
  • 11. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 12. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 13. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 14. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 15. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 8 / 24
  • 16. Images/cinvestav- Operations: Add Add/Remove an element a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 add(1,g) a g b c d e - - - 0 1 2 3 4 5 6 7 8 Size=6 9 / 24
  • 17. Images/cinvestav- Operations: Add Add/Remove an element a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 add(1,g) a g b c d e - - - 0 1 2 3 4 5 6 7 8 Size=6 9 / 24
  • 18. Images/cinvestav- Code Our Implementation p u b l i c void add ( i n t index , Item myobject ){ // I n i t i a l V a r i a b l e s i n t i ; // Always check f o r p o s s i b l e e r r o r s i f ( t h i s . s i z e == element . len ght ){ System . out . p r i n t l n ( " L i s t ␣ does ␣ not ␣ have ␣ space " ) ; System . e x i t ( 0 ) ; } i f ( index <0 | | index >t h i s . s i z e ){ System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ; System . e x i t ( 0 ) ; } // S h i f t p o s t i i o n s as n e c e s s a r y f o r ( i = t h i s . s i z e ; i >index ; i −−) element [ i +1]=element [ i ] ; // copy element i n t o c o n t a i n e r element [ i +1]=myobject ; } 10 / 24
  • 19. Images/cinvestav- Operations: Get Here We have a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 get(3) It will return “d”. The code is simple 11 / 24
  • 20. Images/cinvestav- Operations: Get Here We have a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 get(3) It will return “d”. The code is simple 11 / 24
  • 21. Images/cinvestav- Operations: Get Here We have a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 get(3) It will return “d”. The code is simple p u b l i c Item get ( i n t index ){ // Check always i f ( t h i s . s i z e == 0) r e t u r n n u l l ; i f ( index <0 | | index >t h i s . s i z e −1){ System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ; System . e x i t ( 0 ) ; } r e t u r n element [ index ] } 11 / 24
  • 22. Images/cinvestav- You can implement the rest Yes Part of your homework!!! 12 / 24
  • 23. Images/cinvestav- Now, we have a common problem Because We do not know how many elements will be stored at the list. We use An initial length and dynamically increase the size as needed. 13 / 24
  • 24. Images/cinvestav- Now, we have a common problem Because We do not know how many elements will be stored at the list. We use An initial length and dynamically increase the size as needed. 13 / 24
  • 25. Images/cinvestav- Example Example Length of array element[] is 6: a b c d e f First create a new and larger array NewArray = (Item[]) new Object[12] - - - - - - - - - - - - Now copy the new elements into the new array!!! System.arraycopy(element, 0, newArray, 0, element.length); a b c d e f - - - - - - 14 / 24
  • 26. Images/cinvestav- Example Example Length of array element[] is 6: a b c d e f First create a new and larger array NewArray = (Item[]) new Object[12] - - - - - - - - - - - - Now copy the new elements into the new array!!! System.arraycopy(element, 0, newArray, 0, element.length); a b c d e f - - - - - - 14 / 24
  • 27. Images/cinvestav- Example Example Length of array element[] is 6: a b c d e f First create a new and larger array NewArray = (Item[]) new Object[12] - - - - - - - - - - - - Now copy the new elements into the new array!!! System.arraycopy(element, 0, newArray, 0, element.length); a b c d e f - - - - - - 14 / 24
  • 28. Images/cinvestav- Example Finally, rename new array element = NewArray; element[0]−→ a b c d e f - - - - - - 15 / 24
  • 29. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 16 / 24
  • 30. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 31. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 32. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 33. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 34. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 35. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 36. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 37. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 38. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 39. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have 1 + 2 + 3 + · · · + n = n (n + 1) 2 = O n2 (1) Ok Not a good idea!!! 18 / 24
  • 40. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have 1 + 2 + 3 + · · · + n = n (n + 1) 2 = O n2 (1) Ok Not a good idea!!! 18 / 24
  • 41. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 19 / 24
  • 42. Images/cinvestav- A better strategy Dynamic Array To avoid incurring the cost of resizing many times, dynamic arrays resize by an amount a. In our example we double the size, a = 2 20 / 24
  • 43. Images/cinvestav- A better strategy Dynamic Array To avoid incurring the cost of resizing many times, dynamic arrays resize by an amount a. In our example we double the size, a = 2 Item NewArray [ ] ; i f ( t h i s . s i z e == element . len ght ){ // Resize the c a p a c i t y NewArray = ( Item [ ] ) new Object [ 2*this.size ] f o r ( i n t i =0; i < s i z e ; i ++){ NewArray [ i ]= element [ i ] ; } element = NewArray ; } 20 / 24
  • 44. Images/cinvestav- Space Complexity Every time an insertion triggers a doubling of the array Thus, space wasted in the new array: Space Wasted = Old Lenght − 1 (2) Remember: We double the array and insert!!! Thus, the average space wasted is Θ (n) (3) 21 / 24
  • 45. Images/cinvestav- Space Complexity Every time an insertion triggers a doubling of the array Thus, space wasted in the new array: Space Wasted = Old Lenght − 1 (2) Remember: We double the array and insert!!! Thus, the average space wasted is Θ (n) (3) 21 / 24
  • 46. Images/cinvestav- For example, we have the following A trade-off between time and space You have and average time for insertion is 2 2−1 . In addition, an upper bound for the wasted cells in the array is (2 − 1) n − 1 = n − 1. Actually a more general term of expansion, a Thus, we have: Average time for insertion is a a−1 . An upper bound for the wasted cells in the array is (a − 1) n − 1 = an − n − 1. Different languages use different values Java, a = 3 2 . Python, a = 9 8 22 / 24
  • 47. Images/cinvestav- For example, we have the following A trade-off between time and space You have and average time for insertion is 2 2−1 . In addition, an upper bound for the wasted cells in the array is (2 − 1) n − 1 = n − 1. Actually a more general term of expansion, a Thus, we have: Average time for insertion is a a−1 . An upper bound for the wasted cells in the array is (a − 1) n − 1 = an − n − 1. Different languages use different values Java, a = 3 2 . Python, a = 9 8 22 / 24
  • 48. Images/cinvestav- For example, we have the following A trade-off between time and space You have and average time for insertion is 2 2−1 . In addition, an upper bound for the wasted cells in the array is (2 − 1) n − 1 = n − 1. Actually a more general term of expansion, a Thus, we have: Average time for insertion is a a−1 . An upper bound for the wasted cells in the array is (a − 1) n − 1 = an − n − 1. Different languages use different values Java, a = 3 2 . Python, a = 9 8 22 / 24
  • 49. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 23 / 24
  • 50. Images/cinvestav- We have the following final analysis Amortized Analysis Vs. Classic Array Classic Analysis Dynamic Array Amortized Analysis Indexing O (1) O (1) Search O (n) O (n) Add/Remove O (n) O (n) Space Complexity O (n) O (n) 24 / 24