Data Structures and Algorithms. Roberto Sebastiani

Similar documents
Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Data Structures and Algorithms. Roberto Sebastiani

Linked Structures. See Section 3.2 of the text.

Basic Data Structures and Heaps

DS L9: Queues

Data Structures and Algorithms. Roberto Sebastiani

Cpt S 122 Data Structures. Data Structures


Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Linked List. April 2, 2007 Programming and Data Structure 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

1 P age DS & OOPS / UNIT II

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Dynamic Data Structures

Sequence Abstract Data Type

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

8. Fundamental Data Structures

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Matriculation number:

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Solution for Data Structure

.:: UNIT 4 ::. STACK AND QUEUE

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Algorithms and Data Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

3137 Data Structures and Algorithms in C++

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Stacks, Queues and Hierarchical Collections

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Module 2: Priority Queues

Module 2: Priority Queues

CSC 1600 Memory Layout for Unix Processes"

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Topic: Heaps and priority queues

3. Fundamental Data Structures

Algorithms, Data Structures, and Problem Solving

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

CS24 Week 4 Lecture 2

Basic Data Structures (Version 7) Name:

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

12 Abstract Data Types

Abstract Data Types. Definitions, Implementations, and Uses

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CS301 - Data Structures Glossary By

III Data Structures. Dynamic sets

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CP2 Revision. theme: dynamic datatypes & data structures

CS 216 Exam 1 Fall SOLUTION

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

DATA STRUCTURE UNIT I

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Matriculation number:

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

LIFO : Last In First Out

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

Ashish Gupta, Data JUET, Guna

Data Structures Question Bank Multiple Choice

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Lecture Notes on Queues

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Basic Data Structures

Lecture 10 Notes Linked Lists

Heapsort. Heap data structure

Programming in C. Lecture Tiina Niklander. Faculty of Science

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

CS W3134: Data Structures in Java

Introduction. Reference

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Week 4 Stacks & Queues

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Design and Analysis of Algorithms

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Basic Data Structures 1 / 24

Lecture 10 Notes Linked Lists

Arrays and Linked Lists

PROGRAMMAZIONE I A.A. 2017/2018

Programming. Lists, Stacks, Queues

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Summer Final Exam Review Session August 5, 2009

Introduction to Algorithms 3 rd edition

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

+ Abstract Data Types

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Transcription:

Data Structures and Algorithms Roberto Sebastiani roberto.sebastiani@disi.unitn.it http://www.disi.unitn.it/~rseba - Week 05 - B.S. In Applied Computer Science Free University of Bozen/Bolzano academic year 2010-2011 1

Acknowledgements & Copyright Notice These slides are built on top of slides developed by Michael Boehlen. Moreover, some material (text, figures, examples) displayed in these slides is courtesy of Kurt Ranalter. Some examples displayed in these slides are taken from [Cormen, Leiserson, Rivest and Stein, ``Introduction to Algorithms'', MIT Press], and their copyright is detained by the authors. All the other material is copyrighted by Roberto Sebastiani. Every commercial use of this material is strictly forbidden by the copyright laws without the authorization of the authors. No copy of these slides can be displayed in public or be publicly distributed without containing this copyright notice. 2

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 3

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 4

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 5

Records Records are used to group a number of (different) fields. A person record may group name, age, city, nationality, ssn. The grouping of fields is a basic and often used technique. It is available in all programming languages. 6

Records in C In C a struct is used to group fields: struct rec rec { int int a; a; int int b; b; }; }; struct rec rec r; r; int int main() { r.a r.a = 5; 5; r.b r.b = 8; 8; printf( The sum sum of of a and and b is is %d\n, r.a r.a + r.b); } // // gcc gcc -o -o dummy dummy.c ;./dummy 7

Records in Java In java a class is used to group fields: class rec rec { int int a; a; int int b; b; }; }; public class dummy { static rec rec r; r; public static void main(string args[]) { r = new new rec(); r.a r.a = 15; 15; r.b r.b = 8; 8; System.out.print( Adding a and and b yields ); ); System.out.println(r.a + r.b); } } 8

Recursive Data Structures The counterpart of recursive functions are recursively defined data structures. Example: list of integers list= { integer } integer,list In C: struct list { int value; Struct list * tail; }; 9

Recursive Data Structures/2 The storage space of recursive data structures is not known in advance. It is determined by the number of elements that will be stored in the list. This is only known during runtime (program execution). The list can grow and shrink during program execution. 10

Recursive Data Structures/3 There must be a mechanism to constrain the initial storage space of recursive data structures (it is potentially infinite). There must be a mechanism to grow and shrink the storage space of a recursive data structures during program execution. 11

Pointers A common technique is to allocate the storage space (memory) dynamically. That means the storage space is allocated when the program executes. The compiler only reserves space for an address to these dynamic parts. These addresses are called pointers. 12

Pointers/2 integer i pointer p to an integer (55) record r with integer components a (17) and b (24) pointer s that points to r Address Variable 1af782 i 1af783 p 1af784 r 1af785 1af786 s 1af787 1af788 1af789 1af78a Memory 23 1af789 17 24 1af784 55 1 13

Pointers in C 1. To follow (chase, dereference) a pointer variable we write *p *p = 12 2. To get the address of a variable i we write &i p = &i 3. To allocate memory we use malloc(sizeof(type)) p = malloc(sizeof(int)) 4. To free storage space pointed to by a pointer p we use free free(p) 14

Pointers in C/2 To declare a pointer to type T we write T* int* p Note that * is used for two purposes: Declaring a pointer variable int* p Following a pointer *p = 15 In other languages these are syntactically different. 15

Pointers in C/3 2 int i i = 23 Address 1af782 Variable i Memory 23 int* p p = malloc(sizeof(int)) *p = 55 1af783 1af784 1af785 p r 1af789 17 24 struct rec r rec.a = 17 rec.b = 24 struct rec* s; s = &r 1af786 1af787 1af788 1af789 1af78a s 1af784 55 16

Pointers in C/4 Alternative notation: Address Variable Memory 1af782 i 23 1af783 p 1af789 1af784 r 17 1af785 24 1af786 s 1af784 1af787 1af788 1af789 55 1af78a Variable i p r s Memory 23 17 24 55 17

Pointers/3 Pointers are only one mechanism to implement recursive data structures. The programmer does not have to be aware of their existence. The storage space can be managed automatically. In C the storage space has to be managed explicitly. In Java an object is implemented as a pointer. creation of objects (new) automatically allocates storage space. accessing an object will automatically follow the pointer. deallocation is done automatically (garbage collection). 18

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 19

Lists A list of integers: root 88 52 11 12 Corresponding declaration in C: struct node { int int val; struct node* next; } struct node* root; Accessing a field: (*p).a = p->a 20

Lists/2 A list of integers: root 88 52 11 12 Corresponding declaration in java: class node { int int val; node next; } node root; Accessing a field: p.a 21

Lists/3 Populating the list with integers (C): root 88 52 12 root = malloc(sizeof(struct node)); root->val = 88; root->next = malloc(sizeof(struct node)); p = root->next; p.val = 52; p->next = malloc(sizeof(struct node)); p = p->next; p->val = 12; p->next = NULL; 22

Lists/4 Populating the list with integers (java): root 88 52 12 root = new node(); root.val = 88; root.next = new node(); p = root.next; p.val = 52; p.next = new node(); p = p.next; p.val = 12; p.next = null; 23

List Traversal Print all elements of a list (C): root 88 52 12 p = root; while (p (p!=!= null) { printf( %d,, p->val); p = p->next } printf( \n ); 24

List Traversal 6 5 Print all elements of a list (java): root 88 52 12 p = root; while (p (p!=!= null) { System.out.printf( %d,, p.val); p = p.next } System.out.printf( \n ); 25

List Insertion Insert 43 at the beginning (C): root 88 12 p = malloc(sizeof(struct node)); p->val = 43 43 p->next = root; root = p; p; root 43 88 12 26

List Insertion Insert 43 at the beginning (java): root 88 12 p = new node(); p.val = 43 43 p.next = root; root = p; p; root 43 88 12 27

List Insertion/2 Insert 43 at end (C): root 88 12 if if (root == == null) { root = malloc(sizeof(struct node)); root->val = 43; 43; root->next = null; } else { q = root; while (q->next!=!= null) { q = q->next; } q->next = malloc(sizeof(struct node)); q->next->val = 43; 43; q->next->next = null; } 28

List Insertion/2 Insert 43 at end (java): root 88 12 if if (root == == null) { root = new new node(); root.val = 43; 43; root.next = null; } else { q = root; while (q.next!=!= null) { q = q.next; } q.next = new new node(); q.next.val = 43; 43; q.next.next = null; } 29

List Deletion Delete element x from a non empty list (C): p = root; if if (p->val == == x) x) { root = p->next; free(p); } else { while (p->next!=!= null && && p->next->val!=!= x) x) { p = p->next; } tmp tmp = p->next; p->next = tmp->next free(tmp); } 30

List Deletion Delete element x from a non empty list (java): p = root; if if (p.val == == x) x) { root = p.next; } // // no no need of of freeing in in java else { while (p.next!=!= null && && p.next.val!=!= x) x) { p = p.next; } tmp tmp = p.next; p.next = tmp.next; } 31

List Cost of operations: Insertion at beginning: O(1) Insert at end: O(n) Check isempty: O(1) Delete from the beginning: O(1) Search: O(n) Delete: O(n) Print: O(n) 32

Suggested exercises Implement a linked list with the following functionalities: isempty, insertfirst, insertlast, search, deletefirst, delete, print As before, with a recursive version of: insertlast, search, delete, print are recursive versions simpler? Implement an efficient version of print which prints the list in reverse order 33

Variants of linked lists Linked lists with explicit head/tail Doubly linked lists 34

List with Explicit Head/Tail Instead of a root we can have a head and tail: 35

Doubly Linked Lists To be able to quickly navigate back and forth in a list we use doubly linked lists. A node of a doubly linked list has a next and a prev link. 36

37

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 38

Abstract Data Types (ADTs) An ADT is a mathematically specified entity that defines a set of its instances, with: a specific interface a collection of signatures of operations that can be invoked on an instance. a set of axioms (preconditions and postconditions) that define the semantics of the operations (i.e., what the operations do to instances of the ADT, but not how). 39

ADTs/2 Why ADTs? ADTs allows to break work into pieces that can be worked on independently without compromising correctness. They serve as specifications of requirements for the building blocks of solutions to algorithmic problems. ADTs encapsulate data structures and algorithms that implement them. 40

ADTs/3 Provides a language to talk on a higher level of abstraction. Allows to separate the concerns of correctness and the performance analysis 1. Design the algorithm using an ADT 2. Count how often different ADT operations are used 3. Choose implementations of ADT operations ADT = Instance variables + procedures (Class = Instance variables + methods) 41

ADTs/4 We discuss a number of popular ADTs: Stacks, Queues Ordered Lists Priority Queues Trees (next chapter) They illustrate the use of lists and arrays. 42

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 43

Stacks In a stack, insertions and deletions follow the last infirst out (LIFO) principle. Thus, the element that has been in the queue for the shortest time are deleted. Example: OS stack, Solution: Elements are inserted at the beginning (push) and removed from the beginning (pop). Beginning Stack 44

Stacks/2 Appropriate data structure: Linked list, one root: good Array: fastest, limited in size Doubly linked list: unnecessary 45

An Array Implementation Create a stack using an array A maximum size N is specified. The stack consists of an N element array S and one integer variable count: count: index of the front element (head) count represents the position where to insert next element, and the number of elements in the stack 46

An Array Implementation/2 int int size() return count int int isempty() return (count == == 0) 0) Element pop() if if isempty() then Error x = S[count-1] count--; return x void push(element x) x) if if count==n then Error; S[count] = x; x; count++; 47

A Linked List implementation A list of integers: root 88 52 11 12 Insert from the top of the list push(element x): x): node p = new new node(); p.info = x; x; p.next = root; root = p; p; Constant time operation! 48

A Linked List implementation A list of integers: root 88 52 11 12 Extract from the top of the list Element pop(): x = root.info; root = root.next; Return x; x; Constant time operation! 49

Queues In a queue insertions and deletions follow the firstin first out (FIFO) principle. Thus, the element that has been in the queue for the longest time are deleted. Example: Printer queue, Solution: Elements are inserted at the end (enqueue) and removed from the beginning (dequeue). Beginning Queue End 50

Queues/2 Appropriate data structure: Linked list, root: inefficient insertions Linked list, head/tail: good Array: fastest, limited in size Doubly linked list: unnecessary 51

An Array Implementation Create a queue using an array in a circular fashion A maximum size N is specified. The queue consists of an N element array Q and two integer variables: f, index of the front element (head, for dequeue) r, index of the element after the last one (tail, for enqueue) 52

An Array Implementation/2 wrapped around configuration what does f=r mean? 53

An Array Implementation/3 int int size() return (N-f+r) mod mod N int int isempty() return size() == == 0 Element dequeue() if if isempty() then Error x = Q[f] f = (f+1) mod mod N return x void enqueue() if if siz size()==n-1 then Error Q[r] = x r = (r+1) mod mod N 54

A Linked List Implementation Use linked list with head and tail Insert in tail, extract from head 55

A Linked List implementation/2 Insert at the end of the list: O(1) enqueue(element x): x): node p = new new node(); p.info = x; x; p.next = null; tail.next=p; tail=tail.next; 56

A Linked List implementation/3 Extract from the top of the list: O(1) Element dequeue(): x = root.info; root = root.next; Return x; x; 57

Suggested exercises Implement stack and queue as arrays Implement stack and queue as linked lists, with the same interface as the array implementation 58

Suggested exercises/2 Suppose a queue of integers is implemented with an array of 8 elements: draw the outputs and status of such array after the following operations: enqueue 2,4,3,1,7,6,9 dequeue 3 times Enqueue 2,3,4 Can we enqueue any more element? Try the same with a stack Try similar examples (also with a stack) 59

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Queue Ordered Lists Priority Queue 60

Ordered List In an ordered list elements are ordered according to a key. Example functions on ordered list: init() isempty() Search(element x) delete(element x) print() insert(element x) 61

Ordered List/2 Declaration of an ordered list identical to unordered list Some operations (search, and hence insert and delete) are slightly different 62

Ordered List/3 Insertion into an ordered list (C): void insert(struct node* l, l, int int x) x) { struct node* p; p; struct node* q; q; if if (root == == NULL root->val > x) x) { p = malloc(sizeof(struct node)); p->val = x; x; p->next = root; root = p; p; } else { 63

Ordered List/4 Insertion into an ordered list (C): void insert(struct node* l, l, int int x) x) { } else { p = root; while (p->next!=!= NULL && && p->next->val < x) x) p = p->next; q = malloc(sizeof(struct node)); q->val = x; x; q->next = p->next; p->next = q; q; } } 64

Ordered List/5 Insertion into an ordered list (java): void insert(node l, l, int int x) x) { node p; p; node q; q; if if (root == == NULL root.val > x) x) { p = new new node(); p.val = x; x; p.next = root; root = p; p; } else { 65

Ordered List/5 Insertion into an ordered list (java): void insert(node l, l, int int x) x) { } else { p = root; while (p.next!=!= NULL && && p.next.val < x) x) p = p.next; q = new new node(); q.val = x; x; q.next = p.next; p.next = q; q; } } 66

Ordered List Cost of operations: Insertion: O(n) Check isempty: O(1) Search: O(n) Delete: O(n) Print: O(n) 67

Suggested exercises Implement an ordered list with the following functionalities: isempty, insert search, delete, print Implement also deletealloccurrences() As before, with a recursive version of: insert, search, delete, print are recursive versions simpler? Implement an efficient version of print which prints the list in reverse order 68

Data Structures and Algorithms Week 5 Dynamic Data Structures Records, Pointers Lists Abstract Data Types Stack, Queue Ordered Lists Priority Queue 69

Priority Queues A priority queue is an ADT for maintaining a set S of elements, each with an associated value called key. A PQ supports the following operations Insert(S,x) insert element x in set S (S := S {x}) ExtractMax(S) returns and removes the element of S with the largest key One way of implementing it: a heap 70

Priority Queues/2 Removal of max takes constant time on top of Heapify Θ(log n) ExtractMax(A) // // removes & returns largest elem of of A max := := A[1] A[1] := := A[n] n := := n-1 Heapify(A, 1, 1, n) n) return max 71

Priority Queues/3 Insertion of a new element enlarge the PQ and propagate the new element from last place up the PQ tree is of height log n, running time: Θ(log n) Insert(A,key) n := := n+1; i := := n; n; while i > 1 and and A[parent(i)] < key key A[i] := := A[parent(i)] i := := parent(i) A[i] := := key key 72

Priority Queues/4 16 a) b) 14 10 14 8 7 9 3 8 7 16 10 9 3 2 4 1 2 4 1 c) 16 10 d) 15 16 10 8 14 9 3 8 14 9 3 2 4 1 7 2 4 1 7 73

Priority Queues/5 Applications: job scheduling shared computing resources (Unix) Event simulation As a building block for other algorithms We used a heap and an array to implement PQ. Other implementations are possible. 74

Suggested exercises Implement a priority queue Consider the PQ of previous slides. Draw the status of the PQ after each of the following operations: Insert 17,18,18,19 Extract four numbers Insert again 17,18,18,19 Build a PQ from scratch, adding and inserting elements at will, and draw the status of the PQ after each operation 75

Summary Records, Pointers Dynamic Data Structures Lists (root, head/tail, doubly linked) Abstract Data Types Type + Functions Stack, Queue Ordered Lists Priority Queues 76

Next Week Binary Search Trees Red Black Trees 77