CMSC Introduction to Algorithms Spring 2012 Lecture 7

Similar documents
1 P age DS & OOPS / UNIT II

data structures and algorithms lecture 6

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

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

3. Fundamental Data Structures

if (Q.head = Q.tail + 1) or (Q.head = 1 and Q.tail = Q.length) then throw F ullqueueexception if (Q.head = Q.tail) then throw EmptyQueueException

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

.:: UNIT 4 ::. STACK AND QUEUE

CS24 Week 4 Lecture 2

Algorithms and Data Structures

Algorithms and Data Structures for Mathematicians

Chapter 9 STACK, QUEUE

8. Fundamental Data Structures

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Data Structure. IBPS SO (IT- Officer) Exam 2017

Algorithms and Data Structures

Matriculation number:

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

Ashish Gupta, Data JUET, Guna

Stack and Queue. Stack:

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

CSC 1052 Algorithms & Data Structures II: Linked Queues

Lecture 3: Stacks & Queues


LIFO : Last In First Out

CMSC 132: Object-Oriented Programming II. Stack and Queue

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

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

CMSC Introduction to Algorithms Spring 2012 Lecture 16

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Linked Lists, Stacks, and Queues

Chapter 8: Data Abstractions

Stacks and Queues

DATA STRUCTURE UNIT I

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

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

Week 6. Data structures

12 Abstract Data Types

Abstract Data Types. Abstract Data Types

csci 210: Data Structures Stacks and Queues

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

Design and Analysis of Algorithms

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

UNIT 6A Organizing Data: Lists Principles of Computing, Carnegie Mellon University

16. Dynamic Data Structures

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

DEEPIKA KAMBOJ UNIT 2. What is Stack?

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

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

Programming. Lists, Stacks, Queues

Matriculation number:

Algorithms. 演算法 Data Structures (focus on Trees)

UNIT 6A Organizing Data: Lists. Last Two Weeks

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

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

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Fun facts about recursion

CS 206 Introduction to Computer Science II

III Data Structures. Dynamic sets

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

CS350 - Exam 1 (100 Points)

Cpt S 122 Data Structures. Data Structures

Lecture Data Structure Stack

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Linked Structures. See Section 3.2 of the text.

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

Sequence Abstract Data Type

Linked Lists and Abstract Data Structures A brief comparison

Data Structures and Algorithms. Roberto Sebastiani

Name CPTR246 Spring '17 (100 total points) Exam 3

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Data structure and algorithm in Python

Data Abstraction and Specification of ADTs

Dynamic Storage Allocation

The time and space are the two measure for efficiency of an algorithm.

PA3 Design Specification

Data Structures and Algorithms for Engineers

SYSC 2006 CD. Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

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

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

a) State the need of data structure. Write the operations performed using data structures.

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

Basic Data Structures

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

Linked Lists in C and C++

! 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)

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

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)

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

Lesson 3: Understanding General Software Development

Algorithm Design and Analysis

DNHI Homework 3 Solutions List, Stacs and Queues

Transcription:

CMSC 351 - Introduction to Algorithms Spring 2012 Lecture 7 Instructor: MohammadTaghi Hajiaghayi Scribe: Rajesh Chitnis 1 Introduction In this lecture we give an introduction to Data Structures like arrays, linked lists, etc. 2 A Brief Introduction to Data Structures Data structures are the building blocks of computer algorithms. Here we consider abstract data type (ADT), i.e., we do not specify the data types like integers, reals, characters, etc. It is more convenient and more general to design algorithms for these operations without specifying the data type of the items. Modern object-oriented languages such as C++ or Java support a form of abstract data. When a class is used as a type, it is an abstract type that refers to hidden representation. An ADT is typically implemented as a class and each instance of the ADT is an object of that class. 3 Elementary Data Structures 3.1 Elements It is a generic name for an unspecified data type. An element could be an integer, a set of integers, a file of text, or another data structure. We use this term whenever the discussion is independent of the type of data. For example, in sorting we only care about comparisons between element which can be integers, names (strings of characters), etc. Usually we assume we can compare elements for equality (and some times for less than relation) and we can copy them. Each of these operations is counted as one unit of time. 1

Lecture 7 3 ELEMENTARY DATA STRUCTURES 3.2 Arrays An array is a row of elements of the same type. The size of an array is the number of elements in that array. The size of the array should be fixed and allocated in memory in advance. The elements can be bytes, integers, strings or more generally any elements. Arrays are very efficient and very common data structures and each element of it can be accessed in constant time. Arrays are good except they store elements of the same type and their size is fixed. We represent them as a[12... n] and access element i as a[i] for 1 i n. We can also have a two-dimensional array a[12... n, 12... m] and access a general element as a[i, j]. 3.3 Records Records are similar to arrays, except that we do not assume all elements are of the same type. A records thus is a list of elements of different types. However like arrays the exact combination of types and thus the storage size is known (since the sizes of all the elements in the records are known). Each element in a record can be accessed in constant time. It is also possible to compute the location of each element in constant time. Let us now see an example of a Record: begin end Int 1: integer; Ar1: array[1 2... 20] of integers; Name1: array[1 2... 20] of characters; Structures in C and classes in C++ (more general) can be used to implement records. 3.4 Linked Lists There are many applications in which the number of elements is changing dynamically as the algorithm progresses (we can have large arrays to overcome this, but it is often not a good solution. Also due to consecutive representation of arrays doing some operations like insertion and deletion in the middle of an array is very inefficient). Linked lists are the simplest form of dynamic data structures. Here each element is represented separately and all elements are connected through the use of pointers. A pointer is simply a variable that holds as its value the address of another element. It exists in C, C++ but not explicitly in Java (although it can be simulated). A linked list is a list of pairs, say in a record, each containing an element and a pointer, such that each pointer contains the address of the next pair. It is 2

Lecture 7 3 ELEMENTARY DATA STRUCTURES not possible to access each element directly in a linked list, instead one needs to scan it by following the addresses in the pointer. This is called as a linear scan. The end of the list will be denoted by nil or null, a pointer which points to nowhere. An example is given in Figure 1. Drawbacks of Linked Lists 1. Requires more space: one additional pointer per element (not a huge problem though). 2. Need a linear scan to find say the 30th element. Advantages of Linked Lists 1. With only 2 operations we can insert an element in the middle. 2. With only 1 change we can delete an element. Nil or Null Figure 1: Example of a linked list 3.5 Stacks Stacks are dynamic data structures in which we delete (pop) an element which was most recently inserted (push). A stack implements a last-in-first-out (LIFO) policy. We have also an operation top which gives the element at the top of the stack. We can implement it with both arrays (if we know the maximum size) or linked lists. We now show how to implement the Push, Top and Pop functions using arrays. As an exercise, you can implement them with linked lists. Each of Push, Pop and Top operations takes O(1) time. Algorithm 1 Pop(s) 1: if t=0 then 2: return error stack underflow 3: else 4: t t 1 5: return s[t + 1] 3

Lecture 7 REFERENCES Algorithm 2 Push(s, x) 1: t t + 1 2: s[t] x Algorithm 3 Top(s) 1: if t=0 then 2: return error stack underflow 3: else 4: return s[t] 5: end if 3.6 Queues A queue is a dynamic data structure in which the element deleted (dequeue) is always the one that has been inserted (enqueue) for the longest time. A queue implements a first-in-first-out (FIFO) policy like in the registrar s office. The queue has a head and a tail. We insert at the tail and delete from the head. It can be implemented by array or linked lists (like stacks). We now show how to implement the Enqueue and Dequeue functions using arrays as if the queue is circular. As an exercise, you can implement them with linked lists. Each of Enqueue and Dequeue operations takes O(1) time. Algorithm 4 Enqueue(Q, x) 1: Q[tail] x 2: if tail = lengthqueue then 3: tail 1 4: else 5: tail tail +1 References [1] Udi Manber, Introduction to Algorithms - A Creative Approach 4

Lecture 7 REFERENCES 5 2 3 4 1 Top Figure 2: Example of a stack Algorithm 5 Dequeue(Q, x) 1: x Q[head] 2: if head = lengthqueue then 3: head 1 4: else 5: head head +1 7: return x Head Tail Figure 3: Example of a queue 5