Upload
andres-mendez-vazquez
View
28
Download
1
Embed Size (px)
Citation preview
Data StructuresLinear List Array Representation
Andres Mendez-Vazquez
May 6, 2015
1 / 24
Images/cinvestav-1.jpg
Outline
1 Linear List Array RepresentationOperations in Array List
2 Dynamic ArraysHow much we need to expand it...A Strategy for itAnalysis
2 / 24
Images/cinvestav-1.jpg
How do we implement the ADT List?
In our first representation will use an arrayUse a one-dimensional array element[]:
a b c d e - - - -0 1 2 3 4 5 6 7 8
The previous arrayA representation of L = (a, b, c, d , e) using position i in element[i ].
3 / 24
Images/cinvestav-1.jpg
How do we implement the ADT List?
In our first representation will use an arrayUse a one-dimensional array element[]:
a b c d e - - - -0 1 2 3 4 5 6 7 8
The previous arrayA representation of L = (a, b, c, d , e) using position i in element[i ].
3 / 24
Images/cinvestav-1.jpg
Where to map in the array
Right To Left Mapping- - - - e d c b a
Mapping That Skips Every Other Positiona - b - c - d - e - -
Wrap Around Mappingd e - - - - - - a b c
4 / 24
Images/cinvestav-1.jpg
Where to map in the array
Right To Left Mapping- - - - e d c b a
Mapping That Skips Every Other Positiona - b - c - d - e - -
Wrap Around Mappingd e - - - - - - a b c
4 / 24
Images/cinvestav-1.jpg
Where to map in the array
Right To Left Mapping- - - - e d c b a
Mapping That Skips Every Other Positiona - b - c - d - e - -
Wrap Around Mappingd e - - - - - - a b c
4 / 24
Images/cinvestav-1.jpg
Representation Used In Text
Something Notablea b c d e - - - -0 1 2 3 4 5 6 7 8
Size=5
Thus1 Put element i of list in element[i ].2 Use a variable size to record current number of elements
5 / 24
Images/cinvestav-1.jpg
Representation Used In Text
Something Notablea b c d e - - - -0 1 2 3 4 5 6 7 8
Size=5
Thus1 Put element i of list in element[i ].2 Use a variable size to record current number of elements
5 / 24
Images/cinvestav-1.jpg
CodeOur Implementation
p u b l i c c l a s s S imp l eA r r a y L i s t <Item> implementsL i n e a r L i s t <Item>{
// p r i v a t e e l ement s o f imp l ementa t i onp r o t e c t e d Item e lement [ ] ;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 sp u b l i c S imp l eA r r a y L i s t ( ){
t h i s . s i z e = 0 ;t h i s . e l ement = ( Item [ ] ) new Object [ t h i s . DEFAULT_SIZE ] ;
}
p u b l i c S imp l eA 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 . e l ement = ( Item [ ] ) new Object [ NewSize ] ;
}
6 / 24
Images/cinvestav-1.jpg
Data Type Of Array element[]
First than anythingData type of list elements is unknown.
Thus, we used the genericity of Object1 Then, we use element[] to be of data type Object.2 Then, we cast to the new “Item.”
HoweverYou cannot put elements of primitive data types (int, float, double, char,etc.) into our linear lists.
7 / 24
Images/cinvestav-1.jpg
Data Type Of Array element[]
First than anythingData type of list elements is unknown.
Thus, we used the genericity of Object1 Then, we use element[] to be of data type Object.2 Then, we cast to the new “Item.”
HoweverYou cannot put elements of primitive data types (int, float, double, char,etc.) into our linear lists.
7 / 24
Images/cinvestav-1.jpg
Data Type Of Array element[]
First than anythingData type of list elements is unknown.
Thus, we used the genericity of Object1 Then, we use element[] to be of data type Object.2 Then, we cast to the new “Item.”
HoweverYou cannot put elements of primitive data types (int, float, double, char,etc.) into our linear lists.
7 / 24
Images/cinvestav-1.jpg
Data Type Of Array element[]
First than anythingData type of list elements is unknown.
Thus, we used the genericity of Object1 Then, we use element[] to be of data type Object.2 Then, we cast to the new “Item.”
HoweverYou cannot put elements of primitive data types (int, float, double, char,etc.) into our linear lists.
7 / 24
Images/cinvestav-1.jpg
Outline
1 Linear List Array RepresentationOperations in Array List
2 Dynamic ArraysHow much we need to expand it...A Strategy for itAnalysis
8 / 24
Images/cinvestav-1.jpg
Operations: Add
Add/Remove an elementa 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-1.jpg
Operations: Add
Add/Remove an elementa 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-1.jpg
CodeOur Implementation
p u b l i c vo i d add ( i n t index , I tem myobject ){// I n i t i a l V a r i a b l e si n t i ;// Always check f o r p o s s i b l e e r r o r si f ( t h i s . s i z e == element . l e n gh t ){
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 ( " I ndex ␣ out ␣ o f ␣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 yf o r ( i = t h i s . s i z e ; i>i ndex ; i −−)
e l ement [ i +1]=e lement [ i ] ;// copy e l ement i n t o c o n t a i n e re l ement [ i +1]=myobject ;
}
10 / 24
Images/cinvestav-1.jpg
Operations: Get
HereWe 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-1.jpg
Operations: Get
HereWe 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-1.jpg
Operations: GetHereWe 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 simplep u b l i c I tem get ( i n t i nd e x ){
// Check a lwaysi 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 ( " I ndex ␣ out ␣ o f ␣bound " ) ;System . e x i t ( 0 ) ;
}r e t u r n e l ement [ i nd e x ]
}
11 / 24
Images/cinvestav-1.jpg
You can implement the rest
YesPart of your homework!!!
12 / 24
Images/cinvestav-1.jpg
Now, we have a common problem
BecauseWe do not know how many elements will be stored at the list.
We useAn initial length and dynamically increase the size as needed.
13 / 24
Images/cinvestav-1.jpg
Now, we have a common problem
BecauseWe do not know how many elements will be stored at the list.
We useAn initial length and dynamically increase the size as needed.
13 / 24
Images/cinvestav-1.jpg
Example
ExampleLength of array element[] is 6:
a b c d e f
First create a new and larger arrayNewArray = (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-1.jpg
Example
ExampleLength of array element[] is 6:
a b c d e f
First create a new and larger arrayNewArray = (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-1.jpg
Example
ExampleLength of array element[] is 6:
a b c d e f
First create a new and larger arrayNewArray = (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-1.jpg
Example
Finally, rename new arrayelement = NewArray;
element[0]−→ a b c d e f - - - - - -
15 / 24
Images/cinvestav-1.jpg
Outline
1 Linear List Array RepresentationOperations in Array List
2 Dynamic ArraysHow much we need to expand it...A Strategy for itAnalysis
16 / 24
Images/cinvestav-1.jpg
First Attempt
What if you use the following policy?At least 1 more than current array length.
ThusWhat 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-1.jpg
First Attempt
What if you use the following policy?At least 1 more than current array length.
ThusWhat 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-1.jpg
First Attempt
What if you use the following policy?At least 1 more than current array length.
ThusWhat 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-1.jpg
First Attempt
What if you use the following policy?At least 1 more than current array length.
ThusWhat 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-1.jpg
First Attempt
What if you use the following policy?At least 1 more than current array length.
ThusWhat 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-1.jpg
What if we do n insertions?
We finish with something like this1 First Insertion: Creation of the List ⇒ Cost = 1.2 Second Insertion Cost = 2.3 Third Insertion Cost = 34 etc!!!
Thus, we have
OkNot a good idea!!!
18 / 24
Images/cinvestav-1.jpg
What if we do n insertions?
We finish with something like this1 First Insertion: Creation of the List ⇒ Cost = 1.2 Second Insertion Cost = 2.3 Third Insertion Cost = 34 etc!!!
Thus, we have
OkNot a good idea!!!
18 / 24
Images/cinvestav-1.jpg
What if we do n insertions?
We finish with something like this1 First Insertion: Creation of the List ⇒ Cost = 1.2 Second Insertion Cost = 2.3 Third Insertion Cost = 34 etc!!!
Thus, we have
OkNot a good idea!!!
18 / 24
Images/cinvestav-1.jpg
What if we do n insertions?
We finish with something like this1 First Insertion: Creation of the List ⇒ Cost = 1.2 Second Insertion Cost = 2.3 Third Insertion Cost = 34 etc!!!
Thus, we have
OkNot a good idea!!!
18 / 24
Images/cinvestav-1.jpg
What if we do n insertions?
We finish with something like this1 First Insertion: Creation of the List ⇒ Cost = 1.2 Second Insertion Cost = 2.3 Third Insertion Cost = 34 etc!!!
Thus, we have
1 + 2 + 3 + · · ·+ n = n (n + 1)2 = O
(n2
)(1)
OkNot a good idea!!!
18 / 24
Images/cinvestav-1.jpg
What if we do n insertions?
We finish with something like this1 First Insertion: Creation of the List ⇒ Cost = 1.2 Second Insertion Cost = 2.3 Third Insertion Cost = 34 etc!!!
Thus, we have
1 + 2 + 3 + · · ·+ n = n (n + 1)2 = O
(n2
)(1)
OkNot a good idea!!!
18 / 24
Images/cinvestav-1.jpg
Outline
1 Linear List Array RepresentationOperations in Array List
2 Dynamic ArraysHow much we need to expand it...A Strategy for itAnalysis
19 / 24
Images/cinvestav-1.jpg
A better strategy
Dynamic ArrayTo avoid incurring the cost of resizing many times, dynamic arrays resizeby an amount a.
In our example we double the size, a = 2
20 / 24
Images/cinvestav-1.jpg
A better strategy
Dynamic ArrayTo avoid incurring the cost of resizing many times, dynamic arrays resizeby an amount a.
In our example we double the size, a = 2
I tem NewArray [ ] ;i f ( t h i s . s i z e == element . l e n gh t ){
// R e s i z e the c a p a c i t yNewArray = ( Item [ ] ) new Object [ 2*this.size ]f o r ( i n t i =0; i < s i z e ; i ++){
NewArray [ i ]= e lement [ i ] ;}e l ement = NewArray ;
}
20 / 24
Images/cinvestav-1.jpg
Space Complexity
Every time an insertion triggers a doubling of the arrayThus, 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-1.jpg
Space Complexity
Every time an insertion triggers a doubling of the arrayThus, 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-1.jpg
For example, we have the followingA trade-off between time and space
You have and average time for insertion is 22−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, aThus, we have:
Average time for insertion is aa−1 .
An upper bound for the wasted cells in the array is(a − 1) n − 1 = an − n − 1.
Different languages use different valuesJava, a = 3
2 .Python, a = 9
8
22 / 24
Images/cinvestav-1.jpg
For example, we have the followingA trade-off between time and space
You have and average time for insertion is 22−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, aThus, we have:
Average time for insertion is aa−1 .
An upper bound for the wasted cells in the array is(a − 1) n − 1 = an − n − 1.
Different languages use different valuesJava, a = 3
2 .Python, a = 9
8
22 / 24
Images/cinvestav-1.jpg
For example, we have the followingA trade-off between time and space
You have and average time for insertion is 22−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, aThus, we have:
Average time for insertion is aa−1 .
An upper bound for the wasted cells in the array is(a − 1) n − 1 = an − n − 1.
Different languages use different valuesJava, a = 3
2 .Python, a = 9
8
22 / 24
Images/cinvestav-1.jpg
Outline
1 Linear List Array RepresentationOperations in Array List
2 Dynamic ArraysHow much we need to expand it...A Strategy for itAnalysis
23 / 24
Images/cinvestav-1.jpg
We have the following final analysis
Amortized Analysis Vs. ClassicArray Classic Analysis Dynamic Array
Amortized AnalysisIndexing O (1) O (1)Search O (n) O (n)
Add/Remove O (n) O (n)Space Complexity O (n) O (n)
24 / 24