You are on page 1of 55

Advanced Programming and Algorithms I

Winter 2022/2023

Anja Rey

7th November 2022


Recap Quiz

Questions:

• Algorithms and Programming


• Efficiency
• Correctness and Code Quality I

todo detail: quiz

36/69 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Data Structures
Data Structures Intro

Data: temperature distance


Key values name rating volume time

Now: data structure that contains multiple elements


Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

0
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


Data Structures Intro

Data: temperature distance


Key values name rating volume time

Now: data structure that contains multiple elements


Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

0
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


Data Structures Intro

kehr Key
Data: key's
µ
Now: data structure that contains multiple elements kein
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

0
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


Data Structures Intro

assume a

Data:
key complete
Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

g
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


Data Structures Intro

assume a

Data:
key complete
Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

g
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


Data Structures Intro

assume a

Data:
key complete
Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

g
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


Data Structures Intro

assume a

Data:
45 key complete
order wrt
key Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)

Required methods for a data struture:

initialise: create a new collection


search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection

37/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

0 1 2 4 d

initialise: array of length n: A Ω new array A[0..n ≠ 1]


search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

length n

z
affixed

initialise: array of length n: A Ω new array A[0..n ≠ 1]


search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

index length n

z
affixed

initialise: array of length n: A Ω new array A[0..n ≠ 1]


search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

index length n

z
WE affixed

Key Ali

initialise: array of length n: A Ω new array A[0..n ≠ 1]


search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

index length n

z idk affixed

su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

index length n

z idk affixed

su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

index length n

z idk affixed

su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Arrays

index length n

z idk affixed

su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten

38/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Lists

Simple linked list: HEAD


I I NIL

variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:

Initialise empty list:


init_simple_list(L):
1 L.head Ω nil

Teaser: object oriented programming in more detail in Part II

39/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Lists

Simple linked list: HEAD


I I NIL

variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:

Initialise empty list:


init_simple_list(L):
1 L.head Ω nil

Teaser: object oriented programming in more detail in Part II

39/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Lists

Simple linked list: HEAD


I I NIL

variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:

Initialise empty list:


key next
init_simple_list(L):
1 L.head Ω nil

Teaser: object oriented programming in more detail in Part II

39/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Lists

Simple linked list: HEAD


I I NIL

variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:

Initialise empty list:


key next
init_simple_list(L):
1 L.head Ω nil

Teaser: object oriented programming in more detail in Part II

39/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Lists

Simple linked list: HEAD


I I NIL

variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:

Initialise empty list:


key next
init_simple_list(L):
1 L.head Ω nil

Teaser: object oriented programming in more detail in Part II

39/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Insert

Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure

HEAD
X
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L

40/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Insert

Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure

HEAD

insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L

40/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Insert

Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure

HEAD

X
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L

40/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Insert

Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure

HEAD
X D
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L

40/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Insert

Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure

HEAD

I I I
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L

40/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Insert

Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure

HEAD

I I I
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L

40/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Decision: Is Key Contained?


input: data structure D, key x
output: Does D contain an element with key x ? True or False

HEAD
4 3 77 74 72 1

41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Find Key
input: data structure D, key x
output: An element with key x

HEAD
4 3 77 74 72

search_simple_list(L, x ):
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e

41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Find Key
input: data structure D, key x
output: An element with key x

HEAD
3 77 74 72

search_simple_list(L, x ):
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e

41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Find Key
input: data structure D, key x
output: An element with key x

HEAD
3 77 74 72
4
search_simple_list(L, x ):
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e

41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Find Key
input: data structure D, key x
output: An element with key x

HEAD
3 77 74 72

search_simple_list(L, x ):
4
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e

41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Find Key
input: data structure D, key x
output: An element with key x

IET 7 34 72

search_simple_list(L, x ):
4
1 e Ω L.head
2 while e ”= nil and e.key ”= x do

m
3 e Ω e.next
4 return e

41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Search

Find Key
input: data structure D, key x
output: An element with key x

IET 7 34 72

search_simple_list(L, x ):
4
1 e Ω L.head
2 while e ”= nil and e.key ”= x do

Un
3 e Ω e.next
return e
4
an
41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD

77 74 72
need to remember the previous element

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD

It 2
need to remember the previous element

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD

3 77 74 72
need to remember the previous element

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x

Ä P
7 4 72 -
7
search_including_predecessor_simple_list(L, x ):
1 if L.head.key = x then
2 return nil, L.head
3 p Ω L.head
4 e Ω p.next
5 while e ”= nil and e.key ”= x do
6 pΩe
7 e Ω e.next
8 return e, p
42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x

HEAD
P
37 4 72
search_including_predecessor_simple_list(L, x ):
1 if L.head.key = x then
2 return nil, L.head
3 p Ω L.head
4 e Ω p.next
5 while e ”= nil and e.key ”= x do
6 pΩe
7 e Ω e.next
8 return e, p
42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x

HEAD

77 74 72
search_including_predecessor_simple_list(L, x ):
1 if L.head.key = x then
2 return nil, L.head
3 p Ω L.head
4 e Ω p.next
5 while e ”= nil and e.key ”= x do
6 pΩe
7 e Ω e.next
8 return e, p
42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x

HEAD
P
3 77 4 72
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x

HEAD

77 2 7
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x

HEAD

77 2 7
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
4
HEAD

3 77 2
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Linked List – Delete

Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
4
HEAD

3 77 2 I
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x ) Ola
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L

42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Doubly Linked List

HEAD
S S S SNIL

one element:

43/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Doubly Linked List

HEAD
Nils
- - Ä SN

one element:

43/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Doubly Linked List

HEAD
NIL S SNIL
c

one element:

prä hext
key

43/69 Advanced Programming and Algorithms I – Data Structures Anja Rey


In Theory: Doubly Linked List

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD
Nils 3 7 S
4 si 2 SNIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Doubly Linked List

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD
NIL -
3 - -
7 I 2 NIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Doubly Linked List

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD
NIL -
3 - -
7 I 2 NIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Doubly Linked List

Delete Element
input: data structure D, element e
output: the same data structure as D without element e

HEAD
NIL -
3 - -
7 i 2 NIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L a
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
What’s next?

Next two lectures:

• data structures continued


• multielement types in Python: tuple, list, set, dict
• numpy

45/69 Advanced Programming and Algorithms I – Data Structures Anja Rey

You might also like