Data Structures and Algorithms Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 7 Data Structures Modeling versus Implementation Example: Abstract.

Slides:



Advertisements
Similar presentations
Lists CS 3358.
Advertisements

COMP171 Fall 2005 Lists.
DATA STRUCTURES USING C++ Chapter 5
Lecture 15. Graph Algorithms
CHP-5 LinkedList.
College of Information Technology & Design
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Data Structures & Algorithms
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
BTrees & Bitmap Indexes
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
Summary of lectures (1 to 11)
Chapter 3: Arrays, Linked Lists, and Recursion
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Important Problem Types and Fundamental Data Structures
Data Structures Using C++ 2E
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSC 211 Data Structures Lecture 13
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Data Structures Using C++1 Chapter 5 Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
CSE 1342 Programming Concepts
Unit – I Lists.
C++ Programming:. Program Design Including
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.
Top 50 Data Structures Interview Questions
Linked Lists Chapter 5 (continued)
Big-O notation Linked lists
Lecture - 6 On Data Structures
Data Structures Interview / VIVA Questions and Answers
LINKED LISTS CSCD Linked Lists.
Linked Lists.
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures and Algorithms Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 7 Data Structures Modeling versus Implementation Example: Abstract Data Type “List” and its implementations Two interesting data structure problems Lecture 7 Data Structures Modeling versus Implementation Example: Abstract Data Type “List” and its implementations Two interesting data structure problems 1

Data Structure Definition: A collection of data elements arranged and connected in a way which can facilitate efficient executions of a (possibly long) sequence of operations. 2 For example, it makes sense to sort the telephone directory only if there is going to be a large number of queries.

Two steps process of designing a Data Structure Step 1: Mathematical Modeling A Formal description of the possible operations of a data structure. Operations can be classified into two categories: Query Operations: Retrieving some information from the data structure Update operations: Making a change in the data structure Step 2: Implementation Explore the ways of organizing the data that facilitates performing each operation efficiently using the existing tools available. 3 Outcome of Mathematical Modeling: an Abstract Data Type Since we don’t specify here the way how each operation of the data structure will be implemented

4

5 Mathematical Modeling of a List ith element of list L

Query Operations on a List 6 The type of this parameter will depend on the implementation

Update Operations on a List 7

8

Array based Implementation Before we proceed, let us know a little bit more about arrays. RAM allows O(1) time to access any memory location. Array is a contiguous chunk of memory kept in RAM. For an array A[] storing n words, the address of element A[i] = “start address of array A” + i 9 RAM Array A This feature supports O(1) time to access A[i] for any i

Array based Implementation Store the elements of List in array A such that A[i] denotes (i+1)th element of the list at each stage (since index starts from 0). (Assumption: The maximum size of list is known in advance.) Keep a integer variable Length to denote the number of elements in the list at each stage. Example: If at any moment of time List is 3,5,1,8,0,2,40,27,44,67, then the array A looks like: Question: How to describe location of an element of the list ? Answer: by the corresponding array index. Location of 5 th element of List is A Length = 10

Time Complexity of each List operation using Array based implementation OperationTime Complexity per operation IsEmpty(L) Search(x,L) Successor(i,L) Predecessor(i,L) CreateEmptyList(L) Insert(x,i,L) Delete(i,L) MakeListEmpty(L) 11 O(1) O(n) O(1) O(n) Homework: Write C Function for each operation with matching complexity. All elements from A[i] to A[n-1] have to be shifted to the right by one place. All elements from A[i+1] to A[n-1] have to be shifted to the left by one place. Arrays are very rigid n: number of elements in list at present

Link based Implementation: 12 Value node Head Address of next (or right) node Singly Linked List Address of previous (left) node Doubly Linked List

Doubly Linked List based Implementation Keep a doubly linked list where elements appear in the order we follow while traversing the list. The location of an element is the address of the node containing it. Example: List 3,9,1 appears as Head

How to perform Insert(x,p,L) ? Insert(x,p,L){ q  new(node); q.value  x; q.right  p; temp  p.left; q.left  temp; temp.right  q; q.right  p; p.left  q; } Head p x Arrays are very flexible O(1) time

How to perform successor(p,L) ? Successor(p,L){ q  p.right; return q.value; } Head p p.right

Time Complexity of each List operation using Doubly Linked List based implementation OperationTime Complexity per operation IsEmpty(L) Search(x,L) Successor(p,L) Predecessor(p,L) CreateEmptyList(L) Insert(x,p,L) Delete(p,L) MakeListEmpty(L) 16 O(1) O(n) O(1) Homework: Write C Function for each operation with matching complexity.

Doubly Linked List based implementation versus array based implementation of “List” OperationTime Complexity per operation for array based implementation Time Complexity per operation for doubly linked list based implementation IsEmpty(L)O(1) Search(x,L)O(n) Successor(p,L)O(1) Predecessor(p,L)O(1) CreateEmptyList(L)O(1) Insert(x,p,L)O(n)O(1) Delete(p,L)O(n)O(1) MakeListEmpty(L)O(1) 17

(If you underestimate the power of lists and arrays, ponder over these problems) 18

Problem 1 Maintain a telephone directory Operations: Search the phone # of a person with name x Insert a new record (name, phone #,…) Delete an existing record (name, phone #,…) 19 Array basedLinked list based O(n) O(1) O(n)O(1) Can we achieve the best of the two data structure simultaneously ? Can we improve it ? Think over it … Yes. Keep the array sorted according to the names and do Binary search for x. Log n

Problem 2 There are n students with roll number from 1 to n appearing for interview. Each student has finite information (name, roll #, section, …) associated with him/her. The interview board calls students in any arbitrary order. The information(record) of each student is destroyed just after the interview. Due to reason best known to the board, while interviewing a student, with roll number, say i, they want to know the information about the student with maximum roll number less than i who has not been interviewed yet. We call such student as pred(i) (a shorthand for predecessor). Likewise we define succ(i) (a shorthand for successor). So design a data structure storing information of n students so that each of the following operations can be performed in O(1) time. (No assumption on the sequence of operations allowed). Access(i): return information about a student with roll number i. Delete(i): delete the record of a student with roll number i. Pred(i): return the roll no. of the predecessor of a student with roll number i. Succ(i): return the successor of an existing student with a roll number i. 20

Important Advice In this lecture, it was assumed that the student has basic knowledge of records and singly linked lists from ESC101. In case, you lack this basic knowledge, you are advised to revise the basic concepts of pointers, records in C from ESC101. This will also be helpful for some programming assignment in future as well. 21