October 18, 20011 Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Queues and Linked Lists
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structure Dr. Mohamed Khafagy.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Queues.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Applied Algorithmics - week2
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Important Problem Types and Fundamental Data Structures
Stacks, Queues, and Deques
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Abstract Data Type (ADT) & Stacks
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Algorithms and Data Structures Lecture VI
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
Stacks and Queues MR. Mohammad Alqahtani.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Queues.
ADT description Implementations
Data Structure By Amee Trivedi.
Queues Rem Collier Room A1.02
Linked List Stacks, Linked List Queues, Dequeues
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 1.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks Abstract Data Types (ADTs) Stacks
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
CS210- Lecture 6 Jun 13, 2005 Announcements
Important Problem Types and Fundamental Data Structures
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Stacks and Linked Lists
Presentation transcript:

October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University

October 18, This Lecture Abstract Data Types Dynamic Sets, Dictionaries, Stacks, Queues Linked Lists Linked Data Structures for Trees

October 18, Abstract Data Types (ADTs) 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)

October 18, Abstract Data Types (ADTs) Types of operations: Constructors Access functions Manipulation procedures

October 18, Example - Dynamic Sets We will deal with ADTs, instances of which are sets of some type of elements. Operations are provided that change the set We call such class of ADTs dynamic sets

October 18, Dynamic Sets (2) An example dynamic set ADT Methods: New():ADT Insert(S:ADT, v:element):ADT Delete(S:ADT, v:element):ADT IsIn(S:ADT, v:element):boolean Insert and Delete – manipulation operations IsIn – Access method method

October 18, Dynamic Sets (3) Axioms that define the methods: IsIn(New(), v) = false IsIn(Insert(S, v), v) = true IsIn(Insert(S, u), v) = IsIn(S, v), if v  u IsIn(Delete(S, v), v) = false IsIn(Delete(S, u), v) = IsIn(S, v), if v  u

October 18, Priority Queues A priority queue is an ADT(abstract data type) 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}) Maximum(S) returns the element of S with the largest key Extract-Max(S) returns and removes the element of S with the largest key

October 18, Priority Queues (2) Removal of max takes constant time on top of Heapify

October 18, 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 lg n, running time:

October 18, Priority Queues (4)

October 18, Priority Queues (5) Applications: job scheduling shared computing resources (Unix) Event simulation As a building block for other algorithms A Heap can be used to implement a PQ

October 18, Abstract Data Types Why do we need to talk about ADTs in A&DS course? They serve as specifications of requirements for the building blocks of solutions to algorithmic problems Provides a language to talk on a higher level of abstraction ADTs encapsulate data structures and algorithms that implement them Separate the issues of correctness and efficiency

October 18, Other Examples Simple ADTs: Queue Deque Stack

October 18, Stacks A stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Objects can be inserted at any time, but only the last (the most-recently inserted) object can be removed. Inserting an item is known as “pushing” onto the stack. “Popping” off the stack is synonymous with removing an item.

October 18, Stacks (2) A PEZ ® dispenser as an analogy:

October 18, Stacks(3) A stack is an ADT that supports three main methods: new():ADT – Creates a new stack push(S:ADT, o:element):ADT - Inserts object o onto top of stack S pop(S:ADT):ADT - Removes the top object of stack S; if the stack is empty an error occurs top(S:ADT):element – Returns the top object of the stack, without removing it; if the stack is empty an error occurs

October 18, Stacks(4) The following support methods could also be defined: size(S:ADT):integer - Returns the number of objects in stack S isEmpty(S:ADT): boolean - Indicates if stack S is empty Axioms Pop(Push(S, v)) = S Top(Push(S, v)) = v

October 18, An Array Implementation Create a stack using an array by specifying a maximum size N for our stack. The stack consists of an N-element array S and an integer variable t, the index of the top element in array S. Array indices start at 0, so we initialize t to -1

October 18, An Array Implementation (2) Pseudo code Algorithm size() return t+1 Algorithm isEmpty() return (t<0) Algorithm top() if isEmpty() then return Error return S[t] Algorithm push(o) if size()==N then return Error t=t+1 S[t]=o Algorithm pop() if isEmpty() then return Error S[t]=null t=t-1

October 18, An Array Implementation (3) The array implementation is simple and efficient (methods performed in O(1)). There is an upper bound, N, on the size of the stack. The arbitrary value N may be too small for a given application, or a waste of memory.

October 18, Nodes (data, pointer) connected in a chain by links the head or the tail of the list could serve as the top of the stack Singly Linked List

October 18, Queues A queue differs from a stack in that its insertion and removal routines follows the first-in-first-out (FIFO) principle. Elements may be inserted at any time, but only the element which has been in the queue the longest may be removed. Elements are inserted at the rear (enqueued) and removed from the front (dequeued) Front Rear Queue

October 18, Queues (2) The queue supports three fundamental methods: New():ADT – Creates an empty queue Enqueue(S:ADT, o:element):ADT - Inserts object o at the rear of the queue Dequeue(S:ADT):ADT - Removes the object from the front of the queue; an error occurs if the queue is empty Front(S:ADT):element - Returns, but does not remove, the front element; an error occurs if the queue is empty

October 18, Queues (3) These support methods should also be defined: Size(S:ADT):integer IsEmpty(S:ADT):boolean Axioms: Front(Enqueue(New(), v)) = v Dequeque(Enqueue(New(), v)) = New() Front(Enqueue(Enqueue(Q, w), v)) = Front(Enqueue(Q, w)) Dequeue(Enqueue(Enqueue(Q, w), v)) = Enqueue(Dequeue(Enqueue(Q, w)), v)

October 18, 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 rear one (tail – for enqueue)

October 18, An Array Implementation (2) “wrapped around” configuration what does f=r mean?

October 18, An Array Implementation (3) Pseudo code Algorithm size() return (N-f+r) mod N Algorithm isEmpty() return (f=r) Algorithm front() if isEmpty() then return Error return Q[f] Algorithm dequeue() if isEmpty() then return Error Q[f]=null f=(f+1)modN Algorithm enqueue(o) if size = N - 1 then return Error Q[r]=o r=(r +1)modN

October 18, Dequeue - advance head reference Linked List Implementation

October 18, Enqueue - create a new node at the tail chain it and move the tail reference Linked List Implementation (2)

October 18, Double-Ended Queue A double-ended queue, or deque, supports insertion and deletion from the front and back The deque supports six fundamental methods InsertFirst(S:ADT, o:element):ADT - Inserts e at the beginning of deque InsertLast(S:ADT, o:element):ADT - Inserts e at end of deque RemoveFirst(S:ADT):ADT – Removes the first element RemoveLast(S:ADT):ADT – Removes the last element First(S:ADT):element and Last(S:ADT):element – Returns the first and the last elements

October 18, Stacks with Deques Implementing ADTs using implementations of other ADTs as building blocks Stack MethodDeque Implementation size() isEmpty() top()last() push(o)insertLast(o) pop()removeLast()

October 18, Queues with Deques Queue MethodDeque Implementation size() isEmpty() front()first() enqueue(o)insertLast(o) dequeue()removeFirst()

October 18, Doubly Linked Lists Deletions at the tail of a singly linked list cannot be done in constant time To implement a deque, we use a doubly linked list A node of a doubly linked list has a next and a prev link Then, all the methods of a deque have a constant (that is, O(1)) running time.

October 18, Doubly Linked Lists (2) When implementing a doubly linked lists, we add two special nodes to the ends of the lists: the header and trailer nodes The header node goes before the first list element. It has a valid next link but a null prev link. The trailer node goes after the last element. It has a valid prev reference but a null next reference. The header and trailer nodes are sentinel or “dummy” nodes because they do not store elements

October 18, Circular Lists No end and no beginning of the list, only one pointer as an entry point Circular doubly linked list with a sentinel is an elegant implementation of a stack or a queue

October 18, Trees A rooted tree is a connected, acyclic, undirected graph

October 18, Trees: Definitions A is the root node. B is the parent of D and E. A is ancestor of D and E. D and E are descendants of A. C is the sibling of B D and E are the children of B. D, E, F, G, I are leaves.

October 18, Trees: Definitions (2) A, B, C, H are internal nodes The depth (level) of E is 2 The height of the tree is 3 The degree of node B is 2

October 18, Binary Tree Binary tree: ordered tree with all internal nodes of degree 2

October 18, Representing Rooted Trees BinaryTree: Parent: BinaryTree LeftChild: BinaryTree RightChild: BinaryTree           Root

October 18, Unbounded Branching UnboundedTree: Parent: UnboundedTree LeftChild: UnboundedTree RightSibling: UnboundedTree          Root 

October 18, Next Week Hashing