CS 1114: Implementing Search

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Data Structures: A Pseudocode Approach with C
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Linked lists Prof. Noah Snavely CS1114
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 1114: Graphs and Blobs Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
Stacks And Queues Chapter 18.
Linked List by Chapter 5 Linked List by
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.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Breadth-first and depth-first traversal CS1114
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Implementing stacks Prof. Ramin Zabih
Cpt S 122 – Data Structures Abstract Data Types
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
Top 50 Data Structures Interview Questions
September 29 – Stacks and queues
CSCI-255 LinkedList.
Chapter 15 Lists Objectives
Csc 2720 Instructor: Zhuojun Duan
Stacks and Queues.
Queues Queues Queues.
Introduction to Data Structure
Chapter 17 Object-Oriented Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Stacks, Queues, and Deques
Arrays and Linked Lists
Stacks, Queues, and Deques
Lesson Objectives Aims
Data Structures – Stacks and Queus
Linked List Intro CSCE 121 J. Michael Moore.
ITEC 2620M Introduction to Data Structures
Graphs.
Stacks and Queues CSE 373 Data Structures.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Mutators for compound data Stack Queue
Stacks and Queues.
Stacks and Queues CSE 373 Data Structures.
Queues Model Operations Pointer Implementations Array Implementation
CS210- Lecture 6 Jun 13, 2005 Announcements
Linked lists Prof. Noah Snavely CS1114
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
Linked List Intro CSCE 121.
Queues and Priority Queue Implementations
structures and their relationships." - Linus Torvalds
Presentation transcript:

CS 1114: Implementing Search Prof. Graeme Bailey http://cs1114.cs.cornell.edu (notes modified from Noah Snavely, Spring 2009)

Last time Graph traversal Two types of todo lists: Stacks  Depth-first search Queues  Breadth-first search 1 1 3 10 2 2 9 6 5 4 3 5 6 7 8 9 4 8 7 10

Basic algorithms BREADTH-FIRST SEARCH (Graph G) While there is an uncoloured node r Choose a new colour Create an empty queue Q Let r be the root node, colour it, and add it to Q While Q is not empty Dequeue a node v from Q For each of v’s neighbors u If u is not coloured, colour it and add it to Q

Basic algorithms DEPTH-FIRST SEARCH (Graph G) While there is an uncoloured node r Choose a new colour Create an empty stack S Let r be the root node, colour it, and push it on S While S is not empty Pop a node v from S For each of v’s neighbors u If u is not coloured, colour it and push it onto S

Queues and Stacks Examples of Abstract Data Types (ADTs) ADTs fulfill a contract: The contract tells you what the ADT can do, and what the behavior is For instance, with a stack: We can push and pop If we push X onto S and then pop S, we get back X, and S is as before Doesn’t tell you how it fulfills the contract This is a really important technique!!!!

Implementing DFS How can we implement a stack? Needs to support several operations: Push (add an element to the top) Pop (remove the element from the top) IsEmpty 256 42 17

Implementing a stack IsEmpty Push (add an element to the top) function e = IsEmpty(S) e = (length(S) == 0); Push (add an element to the top) function S = push(S, x) S = [ S x ] % appends x to the end of the array S Pop (remove an element from the top) function [S, x] = pop(S) n = length(S); x = S(n); S = S(1:n-1); % abbreviates S % but what happens if n = 0?

Implementing BFS How can we implement a queue? Needs to support several operations: Enqueue (add an element to back) Dequeue (remove an element from front) IsEmpty Not quite as easy as a stack… 256 42 17

Implementing a queue: Take 1 First approach: use an array Add (enqueue) new elements to the end of the array When removing an element (dequeue), shift the entire array left one unit Q = [];

Implementing a queue: Take 1 IsEmpty function e = IsEmpty(Q) e = (length(S) == 0); Enqueue (add an element) function Q = enqueue(Q,x) Q = [ Q x ]; Dequeue (remove an element) function [Q, x] = dequeue(Q) n = length(Q); x = Q(1); for i = 1:n-1 Q(i) = Q(i+1); % everyone steps forward one step But now imagine them all sitting in chairs in the queue!

What is the running time? IsEmpty Enqueue (add an element) Dequeue (remove an element)

Efficiency Ideally, all of the operations (push, pop, enqueue, dequeue, IsEmpty) run in constant (O(1)) time To figure out running time, we need a model of how the computer’s memory works

Computers and arrays Computer memory is a large array We will call it M In constant time, a computer can: Read any element of M (random access) Change any element of M to another element Perform any simple arithmetic operation This is more or less what the hardware manual for an x86 describes

… Computers and arrays M Arrays in Matlab are consecutive subsequences of M … M A = zeros(8)

Memory manipulation How long does it take to: Read A(8)? Set A(7) = A(8)? Copy all the elements of an array (of size n) A to a new part of M? Shift all the elements of A one cell to the left?

Implementing a queue: Take 2 Second approach: use an array AND Keep two pointers for the front and back of the queue Add new elements to the back of the array Take old elements off the front of the array front back Q = zeros(1000000); front = 1; back = 1;

Implementing a queue: Take 2 IsEmpty Enqueue (add an element) Dequeue (remove an element)

Implementing a queue: Take 3 - Linked lists - Alternative to an array Every element (cell) has two parts: A value (as in an array) A link to the next cell

Linked lists Values 8 4 1 3 Links

Linked lists as memory arrays … M We’ll implement linked lists using M A cell will be represented by a pair of adjacent array entries

A few details I will draw odd numbered entries in blue and even ones in red Odd entries are values Number interpreted as list elements Even ones are links Number interpreted as index of the next cell AKA location, address, or pointer The first cell is M(1) and M(2) (for now) The last cell has 0, i.e. pointer to M(0) Also called a “null pointer”

Example 8 4 1 3 8 3 4 1 2 5 7 6 X 9 8 5 1 2 3 7 4 6 X 9

Traversing a linked list Start at the first cell, [M(1),M(2)] Access the first value, M(1) The next cell is at location c = M(2) If c = 0, we’re done Otherwise, access the next value, M(c) The next cell is at location c = M(c+1) Keep going until c = 0

Inserting an element – arrays How can we insert an element x into an array A? Depends where it needs to go: End of the array: A = [A x]; Middle of the array (say, between elements A(5) and A(6))? Beginning of the array?

Inserting an element – linked lists Create a new cell and splice it into the list Splicing depends on where the cell goes: How do we insert: At the end? In the middle? At the beginning? 8 4 1 3 M(1) 5

Adding a header We can represent the linked list just by the initial cell, but this is problematic Problem with inserting at the beginning Instead, we add a header – a few entries that are not cells, but hold information about the list A pointer to the first element A count of the number of elements

Example 8 4 1 3 1 2 3 4 5 6 7 8 9 10 11 3 4 8 5 4 7 1 9 3 X 1 2 3 4 5 6 7 8 9 10 11 7 4 4 5 1 9 8 3 3 X

First element starts at 5 Linked list insertion 5 2 1 3 4 6 X 7 8 9 10 11 12 13 Initial list First element starts at 5 Size of list is 2 5 3 2 1 7 4 6 X 8 9 10 11 12 13 Insert a 5 at end 1 2 3 4 5 6 7 8 9 X 10 11 12 13 Insert an 8 after the 1 11 5 2 1 3 9 4 6 8 7 X 10 12 13 Insert a 6 at the start

Linked list deletion We can also delete cells Simply update the header and change one pointer (to skip over the deleted element) Deleting things is the source of many bugs in computer programs You need to make sure you delete something once, and only once

Linked list deletion X 5 3 2 9 8 X X X 1 2 3 4 5 6 7 8 9 10 11 12 13 X 10 11 12 13 Initial list 5 3 2 1 9 4 6 8 7 X 10 11 12 13 Delete the last cell 1 2 3 4 5 6 7 8 9 X 10 11 12 13 Delete the 8 1 2 3 4 5 6 7 8 9 X 10 11 12 13 Delete the first cell

Linked lists – running time We can insert an item (at the front) in constant (O(1)) time Just manipulating the pointers As long as we know where to allocate the cell If we need to insert an item inside the list, then we must first find the place to put it. We can delete an element (at the front) in constant time If the element isn’t at the front, then we have to find it … how long does that take?

Linked lists – running time What about inserting / deleting from the end of the list? How long does it take to get there?