CS 221 Analysis of Algorithms Data Structures Vectors, Lists.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node 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 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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 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 Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Elementary Data Structures Stacks, Queues, Lists, Vectors, Sequences, Trees, Priority Queues, Heaps, Dictionaries & Hash Tables.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
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.
© 2004 Goodrich, Tamassia Stacks1 Abstract Data Types (ADTs) An abstract data type (ADT) is a contract between the user of a data structure and its implementor.
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.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 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.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
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)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
© 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.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
CSCE , SPRING 2016 INSTRUCTOR: DR. NANCY M. AMATO 1.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Elementary Data Structures
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Sequences and Iterators
Array Lists, Node Lists & Sequences
Elementary Data Structures
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
Doubly Linked Lists or Two-way Linked Lists
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
Linked Lists & Iterators
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Presentation transcript:

CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002  and  Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book

Data Structures  We have seen data structures Stacks – LIFO Queues – FIFO These were very efficient where  we need back most recently stored data – Stack  we need to retrieve data in the order that is was stored How efficient?

Data Structures  Stacks and Queue are not so efficient When? We need to handle data somewhere other than the end of the structure

Vectors  Suppose we have a linear sequence of data What does linear sequence means? Call sequence S S contains n elements Rank = number of elements before a given element in the sequence First element in S has rank = 0 Last element in S has rank = n-1

Vectors  An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it)  An exception is thrown if an incorrect rank is specified (e.g., a negative rank, rank > n)  Can be implemented using array construct

Vectors  Main vector operations: elemAtRank(integer r): returns the element at rank r without removing it replaceAtRank(integer r, object o): replace the element at rank with o and return the old element insertAtRank(integer r, object o): insert a new element o to have rank r removeAtRank(integer r): removes and returns the element

Vectors  Auxiliary vector operations: Size(): how many elements are in S isEmpty(): return boolean for whether vector has no elements

Vectors - applications  How would you use this type of structure?

Vectors – array implementation  Use an array V of size N  A variable n keeps track of the size of the vector (number of elements stored) V 012n r from: Goodrich and Tamassia, 2002

Vectors – array implementation  elementAtRank(r) run-time? O(1) V 012n r from: Goodrich and Tamassia, 2002

Vectors – array implementation  replaceAtRank(r) run-time? O(1) V 012n r from: Goodrich and Tamassia, 2002

Vectors – array implementation  insertAtRank(r,o) keep in mind- we need to make room for the new element by shifting forward the n  r elements V[r], …, V[n  1] Worst case? run-time? O(n) from: Goodrich and Tamassia, 2002 V 012n r V 012n r V 012n o r

Vectors – array implementation  removeAtRank(r,o) We need to shift every element after removed element to fill hole.. V[r]=V[r+1], V[r+1]=V[r+2],… Worst case? run-time? O(n) from: Goodrich and Tamassia, 2002 V 012n r V 012n o r V 012n r

Vectors - performance OperationT(n) size()O(1) isEmpty()O(1) elementAtRank()O(1) replaceAtRank()O(1) insertAtRank()O(n) removeAtRank()O(n)

Lists  Vectors (or the algorithms to use them)are pretty efficient data structures  …but not so efficient if we have to reorganize them (insertAtRank, removeAtRank)  What if we want to access data with respect to its position in structure, particularly with respect to other elements?

Lists  Consider the concept of an element in a structure as a node  Node contains value of element location of its neighbor node - link next elem node

Lists  As a single linked list we maintain sequence if we have an element we know next element ABCD …

Lists  How about a Stack (LIFO) as a list  Queue (FIFO)as a list ABCD …

Double LinkedLists  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

Position Abstract Data Type  The Position ADT models the notion of place within a data structure where a single object is stored  It gives a unified view of diverse ways of storing data, such as a cell of an array a node of a linked list  Just one method: object element(): returns the element stored at the position

List Abstract Data Type  It establishes a before/after relation between positions  Generic methods:  size(), isEmpty()  Query methods:  isFirst(p), isLast(p)

List Abstract Data Type 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)

Element Insertion  insertAfter(p, X), which returns position q ABXC ABC ABC p X q pq

Element Deletion  remove(p), where p = last() ABCD p ABC D p ABC

List Performance  Consider a double linked list  Space?  run-time insertAfter(p) remove(p)  What about growth?