Algorithms and Data Structures

Similar documents
Algorithms and Data Structures

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

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

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

Stacks and Queues

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

Standard ADTs. Lecture 19 CS2110 Summer 2009

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

Algorithm Design and Analysis

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

+ Abstract Data Types

12 Abstract Data Types

Week 6. Data structures

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

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

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Basic Data Structures

Topic 2. Collections

Basic Data Structures 1 / 24

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

csci 210: Data Structures Stacks and Queues

Chapter 9 STACK, QUEUE

CS3110 Spring 2016 Lecture 6: Modules

COMP 182 Algorithmic Thinking. Breadth-first Search. Luay Nakhleh Computer Science Rice University

8. Fundamental Data Structures

data structures and algorithms lecture 6

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

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

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

ITI Introduction to Computing II

Stacks and Queues. CSE 373 Data Structures Lecture 6

OOPLs Semantic Foundations. Data Abstraction Foundation of OOPLs

Lecture 9 Stacks & Queues

Queues. Queue ADT. Queue Specification. An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements.

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

INTERFACES IN JAVA. Prof. Chris Jermaine

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)

CSIS 10B Lab 2 Bags and Stacks

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

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

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(); }

PA3 Design Specification

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

INTERFACES IN JAVA. Prof. Chris Jermaine

3. Fundamental Data Structures


STACKS AND QUEUES. Problem Solving with Computers-II

Software Architecture. Abstract Data Types

Interfaces & Generics

Some major graph problems

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

Object-Oriented Design Lecture 23 CS 3500 Fall 2009 (Pucella) Tuesday, Dec 8, 2009

182 review 1. Course Goals

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Lecture 4 Stack and Queue

Queue ADT. CMPT 125 Feb. 28

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Trees. Carlos Moreno uwaterloo.ca EIT

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

Assignment 8 CSCE 156/156H/RAIK 184H Spring 2017

Problem with Scanning an Infix Expression

Linked Structures. See Section 3.2 of the text.

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Lecture 3: Stacks & Queues

Data Abstraction and Specification of ADTs

CS24 Week 4 Lecture 2

Algorithms and Data Structures

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

Data Structures -- Introduction

LECTURE 17 GRAPH TRAVERSALS

Ashish Gupta, Data JUET, Guna

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

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Foundations of Data Structures

CSE 100: GRAPH SEARCH

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

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

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

CSC148 Week 2. Larry Zhang

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

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Algorithms and Data Structures

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

CSCI-142 Exam 2 Review November 2, 2018 Presented by the RIT Computer Science Community

DATA STRUCTURES AND ALGORITHMS

Lecture 9 Stacks & Queues

Lecture 3: Queues, Testing, and Working with Code

Reasoning About Programs Panagiotis Manolios

Procedural Programming & Fundamentals of Programming

Lecture 15 Notes Binary Search Trees

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Lecture 15 Binary Search Trees

LIFO : Last In First Out

Midterm Exam (REGULAR SECTION)

15-110: Principles of Computing, Spring Problem Set 6 (PS6) Due: Friday, March 2 by 2:30PM via Gradescope Hand-in

Transcription:

Algorithms and Data Structures Data Types Ulf Leser

Content of this Lecture Example Abstract Data Types Lists, Stacks, and Queues Realization in Java Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 2

Problem Suppose you are in the centre of Hamburg and are looking for the next (i.e., closest) laptop repair shop Fortunately, your mobile knows your position and has a list of laptop repair shops in Hamburg How does your mobile find the closest shop? Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 3

Classical Post Box Problem Suppose a city with n boxes located at arbitrary positions You wake up in the middle of the city with a letter in your hand; the letter should be thrown in the closest post box How do you find the closest post box? You have a list with locations of all post boxes Looking at a map is not the answer Devise an algorithm S: set_of_coordinates; c: coordinate (x,y) Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 4

Simple Solution How much work? Input S: set_of_coordinates; c: coordinate (x,y); # your loc t: coordinate; # closest box m: real := MAXREAL; # smal. dist for each c S do if m > distance(c,c ) then m := distance(c,c ); t := c ; end if; end for; return t; Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 5

Simple Solution Input S: set_of_coordinates; c: coordinate (x,y); # your loc t: coordinate; # closest box m: real := MAXREAL; # smal. dist for each c S do if m > distance(c,c ) then m := distance(c,c ); t := c ; end if; end for; return t; Clearly, we can save the second call to distance Thus, we need to compute S distances, make S comparisons, and perform at most 2* S assignments We perform O( S ) operations, which are O(1) or distance computations Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 6

Simple Solution How much work? Clearly, we can save the second call to distance Thus, we need to compute S distances, make S comparisons, and perform at most 2* S assignments Euclidian distance 6 arithmetic ops per distance 2 2 1, y1),( x2, y2)) = ( x1 x2) + ( y1 2) dist(( x y Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 7

Not the only Option How much work? Clearly, we can save the second call to distance Thus, we need to compute S distances, make S comparisons, and perform at most 2* S assignments Manhattan distance 5 operations, and different ones dist(( x1, y1),( x2, y2)) = x1 x2 + y1 y2 Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 8

Complexity How much work? Clearly, we can save the second call to distance Thus, we need to compute S distances, make S comparisons, and perform at most 2* S assignments Both cases: O( S *dim(s)) dim(s): Number of dimensions of every point in S Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 9

Data Structure Point of View input S: set_of_coordinates; c: coordinate (x,y); t: coordinate; m: real := MAXREAL; For each c S do if m > dist(c,c ) then m := dist(c,c ); t := c ; end if; end for; return t; Data structures We need a list of coordinates The algorithm must iterate over the elements of this list A linked list would suffice Now assume we need to perform such searches very often Can we represent S in another way (S ), such that searching requires less work? Note: Time for computing S from S will be ignored Performed before searching starts Assuming that S does not change Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 10

Voronoi Diagrams Pre-processing: Compute for every point s S its Voronoi area, i.e., the area in which all points have s as nearest point from S Can be achieved in O( S *log( S )) time (no details here) Nearest-neighbor search using Voronoi diagrams is O(log( S )) Conclusion: Finding a proper data structure does pay off Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 11

More Abstract We want a piece of software T that can store a list of coordinates can compute the nearest point to a given point c Piece of software: (1) algorithm and (2) data structure Thus, T must support (at least) two operations T.init (S: list_of_coordinates) T.nearestNeighbor(c: coordinate): coordinate T apparently uses another data structure: coordinate Such combinations of object sets and operations on these sets are called a data type If only look at sets and operations: Abstract data type No details on algorithms / implementation Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 12

Content of this Lecture Example Abstract Data Types Lists, Stacks, and Queues Realization in Java Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 13

Abstract Data Types (ADT) An ADT defines a set of operations over a set of objects of a certain (more basic) type Or over multiple sets of objects of different or same types The set of operations and types is called signature An ADT is independent of an implementation Different data structures to represent the objects Different algorithms to implement the operations An ADT is independent of any programming language Encapsulation: Objects are accessed only through the ops An implementation of a ADT is called a concrete (or physical) data type Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 14

Example type points import coordinate; operators add: points x coordinate points; neighbor: points x coordinate coordinate; ADT that we could use for our app for searching shops We only need two operations A way to insert shops (with their coordinates) A way to get the nearest shop with respect to a given coordinate We assume basic data types to be given (string, int ) Not the only way Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 15

Modeling More Details type shop import coordinate; string; operators getname: shop string; getcoor: shop coordinate; type shops import shop; operators add: shops x shop shops; neighborc: shops x coordinate coordinate; neighborn: shops x coordinate string; neighbors: shops x coordinate shop; An ADT defines what is necessary Designing a ADT is a modeling process Shop owner? Laptop models being repaired? Opening hours? Depends on requirements, ease-of-use, extensibility, personal preferences, existing ADTs, Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 16

Reusing Existing ADTs For implementing points (or shops), it would be helpful to import something that can hold a set of coordinates We need a list an ADT in itself A parameterized ADT a list of elements of an arbitrary ADT T For our ADT points, T will use objects of type coordinate type list( T) import integer, bool; operators isempty: list bool; add: list x T list; delete: list x T list; contains: list x T bool; length: list integer; Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 17

Axioms: What we Know about an ADT We expect operations on lists to have a certain semantic Adding an element increases length by one If we assume bag semantics Deleting an element that doesn t exist creates an error If a list is empty, its length is 0 type list( T) import integer, bool; operators isempty: list bool; add: list x T list; contains: list x T bool; delete: list x T list; length: list integer; axioms: l: list, t: T length( add(l, t)) = length( l) + 1; length( l)=0 isempty(l); Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 18

List versus Points type points import coordinate, bool, list( coordinate); Operators contains: points x coordinate bool; # Implement as list.contains add: points x coordinate points; # Implement as list.add neighbor: points x coordinate coordinate; # Not implemented in list! axioms neighbor(p,c) = {x contains(p,x) x :contains(p, x )=> distance(x,c) distance(x,c)}; points uses a list and adds further functionality What s wrong? What happens if multiple x have the same distance to c? Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 19

List versus Points type points import coordinate, bool, 2Dspace; Operators contains: points x coordinate bool; add: points x coordinate points; neighbor: points x coordinate points; axioms neighbor(p,c) = {x contains(p,x) x : contains(p,x ): distance(x,c) distance(x,c)}; Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 20

Content of this Lecture Data Structures Again Abstract Data Types Example: Lists, Stacks, and Queues Parameterized ADTs Realization in Java Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 21

Lists, Stacks, Queues We looked at a data type (points, shops) which essentially is a list with one special operation: nearestneighboor Canonical list operations: insert, search, delete, update, length There are many ways to implement the general ADT list Array, linked lists, double-linked lists, trees, Two types of lists are of exceptional importance in computer science: Stacks and Queues Both support mostly two operations These suffice for surprisingly many problems and applications Can be implemented very efficiently Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 22

Queues IN OUT Operations: enqueue, dequeue Special semantic: First in, first out (FIFO) Breadth-first traversal, shortest paths, BucketSort, Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 23

Stacks IN OUT Operations: push, pop Special semantic: Last in, first out (LIFO) Call stacks, backtracking, Kellerautomaten, Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 24

As Abstract Data Types type stack( T) import bool; operators isempty: stack bool; push: stack x T stack; pop: stack stack; top: stack T; type queue( T) import bool; operators isempty: queue bool; enqueue: queue x T queue; dequeue: queue queue; head: queue T; Where s the difference? Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 25

Signature does not Suffice type a( T) import bool; operators isempty: a bool; add: a x T a; remove: a a; give: a T; type a( T) import bool; operators isempty: a bool; add: a x T a; remove: a a; give: a T; Where s the difference? From the signature alone, there is no difference Yet we expect a different behavior Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 26

Defining the Difference type stack( T) import bool; operators isempty: stack bool; push: stack x T stack; pop: stack stack; top: stack T; axioms q:stack, t:t top( push( s, t)) = t; pop( push( s, t)) = s; Long version: push(s,t) o top(s)=t => t=t push(s,t) o pop(s)=s => s=s type queue( T) import bool; operators isempty: queue bool; enqueue: queue x T queue; dequeue: queue queue; head: queue T; axioms q:queue, t:t head( enqueue(q, t)) = if isempty( q): t else head( q); dequeue( enqueue( q, t)) = if isempty( q): q else enqueue( dequeue(q), t); Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 27

Example type queue( T)... dequeue( enqueue( q, t)) = if isempty( q): q else enqueue( dequeue(q), t); d( e( <3,2>, 5)) = e( d( <3,2>), 5) = e( d( e( <3>, 2)), 5) = e( e( d( <3>), 2), 5) = e( e( d( e(<>), 3), 2), 5) = e( e( <>, 2), 5) = <2,5> Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 28

We Stop Here There are various ways to formally specify the behavior of operations of an ADT For instance: Algebraic specifications Define an algebra over the object sets of the ADT Includes axioms defining the semantics of operations Axioms are essential to prove aspects of a system s behavior Ideally, one only specifies and never programs See lecture on Modellierung und Spezifikation In this lecture, we only look at signatures No axioms Supported by most programming languages (e.g. Java interfaces) Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 29

Content of this Lecture Data Structures Again Abstract Data Types Lists, Stacks, and Queues Realization in Java Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 30

ADTs in Java Recall An ADT summarizes the essential operations on a set of objects An ADT is independent of a realization/implementation Any implementation of a ADT is called a concrete data type Realization in Java? Interfaces Only exhibit the essential operations on a class of objects Can have different implementations Can be implemented by a concrete class Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 31

Remarks Java does not support axioms on interfaces Some other languages do, e.g. contracts in Eiffel Java adds important functionality for practical work which we ignore in this lecture Inheritance, visibility (public, protected, ), overloading, Critical: Encapsulation you must not see anything of an object / do anything with an object that is not represented in its interface See lectures on Software Engineering Historically, ADTs are a predecessor of classes in programming languages Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 32

Summary ADT s specify the possible operations on a data structure ADT s are free of implementation details We often discuss pros/contras of different ways to implement a given ADT We will often assume certain data types to be given Always: Strings, integers, reals, We make implicit assumptions on cost of operations: UCM (Formal) ADTs can be used for much more Proving properties of a data type Proving that a concrete data type implements a ADT Proving that an implementation does not hurt axioms Program verification Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 33

Exemplary Questions What is an abstract data type, what is a physical data type? What are typical operations of a list? Of a stack? Imagine a class storing rectangles in a plane. We want to add and remove rectangles, test if there are any rectangles, and find all rectangles intersection of given one. Define the ADT. What could be possible axioms? Ulf Leser: Algorithms and Data Structures, Summer Semester 2017 34