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

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

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Week 6. Data structures

Stacks and Queues

Basic Data Structures

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

Standard ADTs. Lecture 19 CS2110 Summer 2009

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

Basic Data Structures 1 / 24

12 Abstract Data Types

CS3110 Spring 2016 Lecture 6: Modules

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

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

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

+ Abstract Data Types

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

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

Queue ADT. CMPT 125 Feb. 28

ITI Introduction to Computing II

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

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

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

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

Algorithm Design and Analysis

Topic 2. Collections

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

csci 210: Data Structures Stacks and Queues

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

Chapter 9 STACK, QUEUE

Algorithms and Data Structures

OOPLs Semantic Foundations. Data Abstraction Foundation of OOPLs

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

INTERFACES IN JAVA. Prof. Chris Jermaine

Interfaces & Generics

G Programming Languages - Fall 2012

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

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

INTERFACES IN JAVA. Prof. Chris Jermaine

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

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

8. Fundamental Data Structures

CS24 Week 4 Lecture 2

Stacks and Queues. CSE 373 Data Structures Lecture 6

Lecture 9 Stacks & Queues

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

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

Introduction to Programming Using Java (98-388)

CS 671, Automated Reasoning

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

CSC148 Week 2. Larry Zhang

Data Structures and Algorithms

Algorithms and Data Structures

3. Fundamental Data Structures

PA3 Design Specification

Software Architecture. Abstract Data Types


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

STACKS AND QUEUES. Problem Solving with Computers-II

.:: UNIT 4 ::. STACK AND QUEUE

Some major graph problems

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

data structures and algorithms lecture 6

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

182 review 1. Course Goals

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

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

Lecture 4 Stack and Queue

Control flow in Eiffel

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Data Structures And Algorithms

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Trees. Carlos Moreno uwaterloo.ca EIT

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

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

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

Data Abstraction and Specification of ADTs

Linked Structures. See Section 3.2 of the text.

Unit-3 Software Design (Lecture Notes)

LECTURE 17 GRAPH TRAVERSALS

Data Structures -- Introduction

Lecture 3: Stacks & Queues

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

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.

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Today s lecture. CS 314 fall 01 C++ 1, page 1

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

AJAX: Automating an Informal Formal Method for Systematic JUnit Test Suite Generation

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

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

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

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Implementing Dynamic Data Structures

Ashish Gupta, Data JUET, Guna

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Transcription:

Algorithms and Data Structures Data Types Marius Kloft

Content of this Lecture Example Abstract Data Types Lists, Stacks, and Queues Realization in Java Marius Kloft: Alg&DS, Summer Semester 2016 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? Marius Kloft: Alg&DS, Summer Semester 2016 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) Marius Kloft: Alg&DS, Summer Semester 2016 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; Marius Kloft: Alg&DS, Summer Semester 2016 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; 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 Marius Kloft: Alg&DS, Summer Semester 2016 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 dist(( x y 2 2 1, y1),( x2, y2)) ( x1 x2) ( y1 2) Marius Kloft: Alg&DS, Summer Semester 2016 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 Marius Kloft: Alg&DS, Summer Semester 2016 8

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 may be ignored Performed before searching starts Assuming that S does not change Marius Kloft: Alg&DS, Summer Semester 2016 9

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 This is not easy, but can be achieved in O( S *log( S ) time Nearest-neighbor search using Voronoi diagrams is O(log( S )) Conclusion: Finding a proper data structure does pay off Marius Kloft: Alg&DS, Summer Semester 2016 10

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 Thus, T must support (at least) two operations T.init (S: list_of_coordinate) T.nearestNeighbor (c: coordinate) T apparently uses another data type: coordinate Such combinations of object sets and operations on these sets are called a data type If we abstract from the implementation and only look at the sets and operations, we call this an abstract data type Marius Kloft: Alg&DS, Summer Semester 2016 11

Content of this Lecture Example Abstract Data Types Lists, Stacks, and Queues Realization in Java Marius Kloft: Alg&DS, Summer Semester 2016 12

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 Marius Kloft: Alg&DS, Summer Semester 2016 13

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 primitive data types to be given (string, int ) Not the only way Marius Kloft: Alg&DS, Summer Semester 2016 14

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 Design of ADT is a modeling decision Shop owner? Laptop models being repaired? Opening hours? Depends on requirements, ease-of-use, extensibility, personal preferences, existing ADTs, Marius Kloft: Alg&DS, Summer Semester 2016 15

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 be 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; Marius Kloft: Alg&DS, Summer Semester 2016 16

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); Marius Kloft: Alg&DS, Summer Semester 2016 17

List versus Points type points import coordinate, bool, list( coordinates); 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? Marius Kloft: Alg&DS, Summer Semester 2016 18

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)}; Marius Kloft: Alg&DS, Summer Semester 2016 19

We Stop Here There are various ways to formally specify the behavior of operations of an ADT In this lecture, we only look at signature No semantics (except parameters of operations) Supported by most programming languages (e.g. Java) Algebraic specification 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 Marius Kloft: Alg&DS, Summer Semester 2016 20

Content of this Lecture Data Structures Again Abstract Data Types Example: Lists, Stacks, and Queues Realization in Java Marius Kloft: Alg&DS, Summer Semester 2016 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 Marius Kloft: Alg&DS, Summer Semester 2016 22

Queues IN OUT Operations: enqueue, dequeue Special semantic: First in, first out (FIFO) Breadth-first traversal, shortest paths, BucketSort, Marius Kloft: Alg&DS, Summer Semester 2016 23

Stacks IN OUT Operations: push, pop Special semantic: Last in, first out (LIFO) Call stacks, backtracking, Kellerautomaten, Marius Kloft: Alg&DS, Summer Semester 2016 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? Marius Kloft: Alg&DS, Summer Semester 2016 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 Marius Kloft: Alg&DS, Summer Semester 2016 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 s:stack, t:t top( push( s, t)) = t; pop( push( s, t)) = 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); Marius Kloft: Alg&DS, Summer Semester 2016 27

Content of this Lecture Data Structures Again Abstract Data Types Lists, Stacks, and Queues Realization in Java Marius Kloft: Alg&DS, Summer Semester 2016 28

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 Marius Kloft: Alg&DS, Summer Semester 2016 29

Remarks Java does not support axioms on interfaces Some other languages do, e.g. contracts in Eiffel Java adds functionality we mostly ignore Inheritance (syntactic sugar), different levels of visibility (public, protected, private, ), overloading, Historically, ADTs are a predecessor of classes in programming languages ADTs can be realized at least in all OO languages Critical: encapsulation you must not see anything of an object / do anything with an object that is not represented in its (public) interface Marius Kloft: Alg&DS, Summer Semester 2016 30

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 (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 Marius Kloft: Alg&DS, Summer Semester 2016 31

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? Marius Kloft: Alg&DS, Summer Semester 2016 32