• No results found

Programming and Data Structures with Python

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures with Python"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

Programming and Data Structures with Python

Madhavan Mukund

https://www.cmi.ac.in/~madhavan

(2)

Priority queues as trees

Maintain a special kind of binary tree called a

heap

Balanced

: N node tree has height log N

Both insert( ) and delete_max( ) take O(log N)

Processing N jobs takes time O(N log N)

Truly flexible, need not fix upper bound for N in

advance

(3)

Heaps

Binary tree filled level

by level, left to right

At each node, value

stored is bigger than

both children

(Max) Heap

PropertyBinary tree

filled level by level,

left to right

(4)

Summary

Heaps are a tree implementation of priority queues

insert( ) and delete_max( ) are both O(log N)

heapify( ) builds a heap in O(N)

Tree can be manipulated easily using an array

Can invert the heap condition

Each node is

smaller

than its children

Min-heap

, for insert( ), delete_min( )

Max

heaps

min

heaps

(5)

Heap sort

Start with an unordered list

Build a heap — O(n)

Call delete_max( ) n times to extract elements in

descending order — O(n log n)

After each delete_max( ), heap shrinks by 1

Store maximum value at the end of current heap

In place O(n log n) sort

Unsorted

list

d

Old

Heap

I

Repeated

delete

max

/

min

1066gal

Sorted

lost

(6)

Data structures

Behaviour defined through

interface

Allowed set of operations

Stack:

push()

and

pop()

Queue:

addq()

and

removeq()

Heap:

insert()

and

delete_max()

Heap implemented as a list

h

, does not mean

h.append(7)

is legal

(7)

Abstract datatype

Define behaviour in terms of operations

(s.push(v)).pop() == v

If

q.isempty()

then

((q.addq(u)).addq(v)).removeq() == u

No reference to implementation details

Implementation can be optimized without affecting

functionality

push

stack -pop e

:p

:p

" re s'

Il

=

=

APIO

- -

Application

Pm

grainy

µ

Interface

(8)

Black box view

Imagine the data

structure as a black

box

Designated buttons to

interact

Slot for input

Display for output

No other manipulation

allowed

23

pop

push

Display

Input

0

(9)

Built in datatypes

l = []

List operations

l.append()

,

l.extend()

permitted

… but not dictionary operations like

l.keys()

Likewise, after

d = {}

,

d.values()

is OK

… but not

d.append()

Can we do this for stacks, queues, heaps, …?

O

EM

--v1 , k u

}

, -

-O

=

(10)

-Object Oriented

programming

Data type definition with

Public interface

Operations allowed on the data

Private implementation

(11)

Classes and objects

Class

Template for a data type

How data is stored

How public functions manipulate data

Object

Concrete instance of template

°

'

is

(12)

Classes and objects

class Heap:
 def __init__(self,l):
 # Create heap
 # from list l
 
 def insert(self,x):
 # insert x into heap


def delete_max(self):
 # return max element

# Create object,
 # calls __init__()
 l = [14,32,15]
 h = Heap(l) # Apply operation
 h.insert(17) h.insert(28) v = h.delete_max()

def

Hii

. . .

f-

list

(

range

-

-|

°

Ill

T

- create

object

a

-M

(13)

-Points on a plane


 
 class Point:
 def __init__(self,a,b):
 self.x = a
 self.y = b def translate(self,deltax,deltay):
 # shift (x,y) to (x+deltax,y+deltay)
 self.x += deltax # same as selfx =


# self.x + deltax
 self.y += deltay

y

n

pi

i

-_

in

A.

Laid

¥

;

't

:

s

2=2*7

F

-g.

yyxctoxiytoi

))

f.

no

object

.name

-17=1

-

-pl

p2

xx -- 5 -

-PpYgRp

y

-

=6

-z*=7

(14)

Points on a plane


 
 class Point:
 def __init__(self,a,b):
 self.x = a
 self.y = b def translate(self,deltax,deltay):
 # shift (x,y) to (x+deltax,y+deltay)
 self.x += deltax # same as selfx =


# self.x + deltax
 self.y += deltay p = Point(3,2)

(3,2)

b

b

- -

init

-

-O

(15)

Points on a plane


 
 class Point:
 def __init__(self,a,b):
 self.x = a
 self.y = b def translate(self,deltax,deltay):
 # shift (x,y) to (x+deltax,y+deltay)
 self.x += deltax # same as selfx =


# self.x + deltax
 self.y += deltay p = Point(3,2) p.translate(2,1)

(5,3)

n

3.

5

y 2-73

-17

(

3,27

(16)

Points on a plane

class Point:


. . .


def odistance(self):
 # Distance from (0,0)
 # from math import *
 return(
 sqrt(
 (self.x*self.x) + (self.y*self.y)
 ))

Can

d

=

pl

. o

distance

C§µµy

x

-ftp.xf-ottcu-o#

(17)

Polar coordinates

Recall polar coordinates

Instead of (x,y), use (r,!)

x = r cos !

y = r sin !

r = √(x

2

+ y

2

) — same as distance

! = tan

-1

(y/x)

(18)

Points on a plane

class Point:
 def __init__(self,a,b):
 self.r = sqrt(a*a + b*b)
 if a == 0:
 self.theta = 0
 else:
 self.theta = atan(b/a) def odistance(self):
 return(self.r) def translate(self,deltax,deltay):
 # Convert (r,theta) to (x,y) and back!

Private

implementation has

changed

Functionality of

public interface

remains same

pl

--

Point

-

(

3,2

)

#

y

pl

-

translate

(

2,1

)

O

0

tank

)

- =

I

11

- a ←

¥

no

→ x.

y

xtoxiyto

, → r

:O

'

(19)

A

--

up

. .

array

(

Cl

,

2,37

)

n

p

-

pi

pl

.

x

S

=

pd

.

series

(

-

)

t

--

pd

.

dataf.name

-

)

(20)

Default arguments

class Point:
 def __init(self,a=0,b=0):
 self.x = a
 self.y = b . . . # Point at (3,4)
 p1 = Point(3,4) # Point at (0,0)
 p2 = Point() -- run A -_

-p3=

Point

(5)

slow

Last

x

(21)

Special functions

__init__()

Constructor, called when object is created

__str__()

Return string representation of object

str(o) == o.__str__()

Implicitly invoked by print()

def __str__(self): # For Point()


return('('+str(self.x)+','+str(self.y)+')')

Int

(

"76"

)

srtr

(

767

t

O

pnntfe

)

'' 76 " -

-h

--

Heap

L

-

7

*

putted

pnhtlpl

)

daisy

- ' --

)

(22)

Special functions

__add__()

Invoked implicitly by +

p1 + p2 == p1.__add__(p2)

def __add__(self,p): # For Point()
 return(Point(self.x+p.x,self.y+p.y)
 
 p1 = Point(1,2)
 p2 = Point(2,5)
 p3 = p1 + p2 # p3 is now (3,7)

-"

m

-

O

- =

(23)

Special functions

__mult__()

Called implicitly by

*

__lt__(), __gt__(), __le__(), . . .

Called implicitly by

<

,

>

,

<=

References

Related documents

A binary tree can be implemented where each node has left and right pointer elds, an (optional) parent pointer, and a data eld.... Right: A heap but not a binary

Morality is one of these traits. Morality is an important tool for forming larger, more solidary social groups because it alleviates the problem of within-group conflict. Larger

If every non leaf node in a binary tree has non empty left and right sub trees then such tree is termed as a strictly binary tree.. • Complete

Warner/Chappell Music Limited (40%)/Windswept Music (London) Limited (35%)/Sony/ATV Music Publishing (UK) Limited.. All

 Revenues from product sales. Upfront payments are received from licensee pharmaceutical companies when the agreement is signed, while milestone payments are generated in

As predicted, relatively lower-class individuals primed with the working class label exhibited enhanced well-being as measured in terms of including one’s social class in the self

University of Sydney Business School, Australia, Institute of Advanced Studies, Yokohama National University, Japan, Econometric Institute, Erasmus School of Economics,