CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
CSCE 3110 Data Structures & Algorithm Analysis
DATA STRUCTURES USING C++ Chapter 5
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
CSCE 3110 Data Structures & Algorithm Analysis
Review Learn about linked lists
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Array pair C++ array requires the index set to be a set of consecutive integers starting at 0 C++ does not check an array index to ensure that it belongs.
Data Abstraction and Encapsulation
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
CHAPTER 21 ARRAYS AND STRUCTURES. CHAPTER 22 Arrays Array: a set of index and value data structure For each index, there is a value associated with that.
Chapter 2 1. Arrays Array: a set of index and value Data structure For each index, there is a value associated with that index. Eg. int list[5]: list[0],
Pointers OVERVIEW.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
LINKED LISTS.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Structure Sang Yong Han
Lists CS 3358.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CHAPTER 2 ARRAYS AND STRUCTURES
CSCE 210 Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Lists CSE 373 Data Structures.
Vectors, Lists and Sequences
Vectors, Lists, and Sequences
Lists CSE 373 Data Structures.
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists

Arrays Array: a set of pairs (index and value) data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory.

Arrays in C++ int list[5], *plist[5]; list[5]: five integers list[0], list[1], list[2], list[3], list[4] *plist[5]: five pointers to integers plist[0], plist[1], plist[2], plist[3], plist[4] implementation of 1-D array list[0]base address =  list[1]  + sizeof(int) list[2]  + 2*sizeof(int) list[3]  + 3*sizeof(int) list[4]  + 4*size(int)

Arrays in C++ (cont’d) Compare int *list1 and int list2[5] in C++. Same:list1 and list2 are pointers. Difference:list2 reserves five locations. Notations: list2 - a pointer to list2[0] (list2 + i) - a pointer to list2[i] (&list2[i]) *(list2 + i) - list2[i]

#include void print1(int *ptr, int rows) { int i; cout << "Address Contents" << endl; for (i=0; i < rows; i++) cout << ptr+i << " " << *(ptr+i) << endl; } void main(){ int one[] = {0, 1, 2, 3, 4}; //Goal: print out address and value print1(one, 5); } Example Address Contents 0xbffffbb4 0 0xbffffbb8 1 0xbffffbbc 2 0xbffffbc0 3 0xbffffbc4 4

Objects: A set of pairs where for each value of index there is a value from the set item. Index is a finite ordered set of one or more dimensions, for example, {0, …, n-1} for one dimension, {(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for two dimensions, etc. Methods: for all A  Array, i  index, x  item, j, size  integer Array Create(j, list) ::= return an array of j dimensions where list is a j-tuple whose kth element is the size of the kth dimension. Items are undefined. Item Retrieve(A, i) ::= if (i  index) return the item associated with index value i in array A else return error Array Store(A, i, x) ::= if (i in index) return an array that is identical to array A except the new pair has been inserted else return error The Array ADT

Questions What is the complexity of “retrieve” in an array? What is the complexity of “store” in an array? What about insertion and deletion for ordered elements in an arrary?

Polynomials A(X)=3X 20 +2X 5 +4, B(X)=X 4 +10X 3 +3X 2 +1 Other Data Structures Based on Arrays Arrays: Basic data structure May store any type of elements Polynomials: defined by a list of coefficients and exponents - degree of polynomial = the largest exponent in a polynomial

Polynomial ADT Objects: a set of ordered pairs of where a i in Coefficients and e i in Exponents, e i are integers >= 0 Methods: for all poly, poly1, poly2  Polynomial, coef  Coefficients, expon  Exponents Polynomial Zero( ) ::= return the polynomial p(0) Boolean IsZero(poly) ::= if (poly) return FALSE else return TRUE Coefficient Coef(poly, expon) ::= if (expon  poly) return its coefficient else return Zero Exponent Lead_Exp(poly) ::= return the largest exponent in poly Polynomial Attach(poly,coef, expon) ::= if (expon  poly) return error else return the polynomial poly with the term inserted

Polyomial ADT (cont’d) Polynomial Remove(poly, expon) ::= if (expon  poly) return the polynomial poly with the term whose exponent is expon deleted else return error Polynomial SingleMult(poly, coef, expon)::= return the polynomial poly coef x expon Polynomial Add(poly1, poly2) ::= return the polynomial poly1 +poly2 Polynomial Mult(poly1, poly2) ::= return the polynomial poly1 poly2

Polynomial Addition (1) Use an array to keep track of the coefficients for all exponents advantage: easy implementation disadvantage: waste space when sparse Running time? A(X)=2X B(X)=X 4 +10X 3 +3X 2 +1 A B

Store pairs of exponent and coefficient Polynomial Addition (2) coef exp starta finisha startb finishb avail A(X)=2X B(X)=X 4 +10X 3 +3X 2 +1 advantage: less space disadvantage: longer code Running time?

col1 col2 col3 col4 col5 col6 row0 row1 row2 row3 row4 row5 8/36 6*65*3 15/15 sparse matrix data structure? Sparse Matrices

Sparse Matrix ADT Objects: a set of triples,, where row and column are integers and form a unique combination, and value comes from the set item. Methods: for all a, b  Sparse_Matrix, x  item, i, j, max_col, max_row  index Sparse_Marix Create(max_row, max_col) ::= return a Sparse_matrix that can hold up to max_items = max _row  max_col and whose maximum row size is max_row and whose maximum column size is max_col.

Sparse Matrix ADT (cont’d) Sparse_Matrix Transpose(a) ::= return the matrix produced by interchanging the row and column value of every triple. Sparse_Matrix Add(a, b) ::= if the dimensions of a and b are the same return the matrix produced by adding corresponding items, namely those with identical row and column values. else return error Sparse_Matrix Multiply(a, b) ::= if number of columns in a equals number of rows in b return the matrix d produced by multiplying a by b according to the formula: d [i] [j] =  (a[i][k]b[k][j]) where d (i, j) is the (i,j)th element else return error.

(1)Represented by a two-dimensional array. Sparse matrix wastes space. (2)Each element is characterized by. Sparse Matrix Representation The terms in A should be ordered based on

Sparse Matrix Operations Transpose of a sparse matrix. What is the transpose of a matrix? row col value row col value a[0] b[0] [1] [1] [2] [2] [3] [3] [4] [4] [5] [5] [6] [6] [7] [7] [8] [8] transpose

Transpose a Sparse Matrix Write Pseudo codes for transposing sparse matrix. Analyze its complexity. Example: (0, 0, 15) ====> (0, 0, 15) (0, 3, 22) ====> (3, 0, 22) (0, 5, -15) ====> (5, 0, -15) (1, 1, 11) ====> (1, 1, 11) Note: your array is one dimensional

Linked Lists Avoid the drawbacks of fixed size arrays with Growable arrays Linked lists

Growable arrays Avoid the problem of fixed-size arrays Increase the size of the array when needed (I.e. when capacity is exceeded) Two strategies: tight strategy (add a constant): f(N) = N + c growth strategy (double up): f(N) = 2N

Tight Strategy Add a number k (k = constant) of elements every time the capacity is exceeded C0 + (C0+k) + … (C0+(S-1)k) = S = (N – C0) / k Running time? C0 * S + S*(S+1) / 2  O(N 2 ) S: number of times array capacity is exceeded

Tight Strategy void insertLast(int rear, element o) { if ( size == rear) { capacity += k; element* B = new element[capacity]; for(int i=0; i<size; i++) { B[i] = A[i]; } A = B; } A[rear] = o; rear++; size++; }

Growth Strategy Double the size of the array every time is needed (I.e. capacity exceeded) C0 + (C0 * 2) + (C0*4) + … + (C0*2 s-1 ) = s = log (N / C0) Running time? C0 [ … + 2 log(N/C0)-1 ]  O(N) How does the previous code change? S: number of times array capacity is exceeded

Linked Lists Avoid the drawbacks of fixed size arrays with Growable arrays Linked lists

int *pi = new int; float *pf=new float; *pi =1024; *pf =3.14; cout << “an integer “ << *pi << “ a float = ” << *pf; free(pi); free(pf); request memory return memory Using Dynamically Allocated Memory (review)

bat  cat  sat  vat NULL Linked Lists

Singly Linked List A singly linked list is a concrete data structure consisting of a series of nodes Each node stores Data item Link to the next node next Data itemNODE ABCD  HEADCURRENTTAIL

Insertion ABC X  AB X C  ABC  X

Deletion C ABD  2 1 ABD  C X ABD 

Implement a Linked List Single Linked List (.h,.cpp, test program)hcpptest program Double Linked List (.h,.cpp, test program)hcpptest program

Linked List vs. Array Linked list can be easily expanded or reduced; Array needs a contiguous memory space and may not even be possible to resize. Insertion and deletion in linked list are O(1) while it takes O(n) for array. Array allow random access and the indexing of an array takes O(1). The sequential access with linked list makes it more expensive in indexing, which takes O(n)

Can we do insertion BEFORE a node in singly linked list? Can we do deletion BEFORE a node in singly linked list?

List ADT The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: size(), isEmpty() Query methods: isFirst(p), isLast(p) Accessor methods: first(), last() before(p), after(p) Update methods: replaceElement(p, o), swapElements(p, q) insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o) remove(p)

Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes prevnext elem trailer header nodes/positions elements node

Insertion We visualize operation insertAfter(p, X), which returns position q ABXC ABC p ABC p X q pq

Deletion We visualize remove(p), where p = last() A BCD p A BC D p A BC

Running Time Analysis insertAfter O(?) deleteAfter O(?) deleteBeforeO(?) deleteLastO(?) insertFirst O(?) insertLast O(?)