Algorithmic and advanced
Programming in Python
Eric Benhamou eric.benhamou@dauphine.eu
Remy Belmonte remy.belmonte@dauphine.eu
Masterclass 1
Electronic copy available at: https://ssrn.com/abstract=3953589
Outline
1. Introduction, motivations, Linked list
2. Stacks
3. Queues, Trees
4. Priority Queues and heaps, Disjoint Sets ADT
5. Graph algorithms,
6. Sorting
7. Searching, Selection algorithms
8. Symbol tables, Hashing
9. String algorithms, Algorithm designs
10. Greedy Algorithms, Divide and Conquer
11. Dynamic programming, Complexity
Algorithmic and advanced Programming in Python 2
Electronic copy available at: https://ssrn.com/abstract=3953589
Evaluation principle
• 30% project to be finalized before 22 Dec 2021
• 70% written test to be done during week of January 10 2022
• For exceptional project, bonus of +20%
Algorithmic and advanced Programming in Python 3
Electronic copy available at: https://ssrn.com/abstract=3953589
Algorithms and
programming are at the
center of all software
applications, all fields
combined
Algorithmic and advanced Programming in Python 4
Electronic copy available at: https://ssrn.com/abstract=3953589
Algorithm &
programming
Algorithmic and advanced Programming in Python 5
Electronic copy available at: https://ssrn.com/abstract=3953589
Prerequisite
• Know how to write a simple algorithm in algorithmic terms:
• handle Boolean, integer, real, character variables
• handle arrays and character strings
• know the control structures (tests, loops, ...)
• know how to divide a program into functions and procedures
• be familiar with the organization of memory
• Know how to implement all this in Python
Algorithmic and advanced Programming in Python 6
Electronic copy available at: https://ssrn.com/abstract=3953589
What is a program?
• A program contains data, variables and instructions
• Data structure should be adapted to the goal
• System defined data types: in python, additional complexity as this is a language with
no types -> so implicit typing
• User defined data types (classes in python) or list
• Abstract data types (ADTs)
• Instructions are the core of the algorithm
• They are structured by the algorithm
• They describe the actions to do on the data to process them and produce an output
Algorithmic and advanced Programming in Python 7
Electronic copy available at: https://ssrn.com/abstract=3953589
What is an algorithm?
• Assume you want to prepare an omelette. What do you need?
1. Get a frying pan
2. Get the oil
a) Do we have the oil?
i. If yes, put in the pan
ii. If no, do we want to buy the oil?
i. If yes, then go and buy
ii. If no, terminate
3. Turn on the stove, etc…
-> So an algorithm is the step by step instructions to solve a given
problem
Algorithmic and advanced Programming in Python 8
Electronic copy available at: https://ssrn.com/abstract=3953589
Goal of the analysis of Algorithms
• Rate of growth of an algorithm:
n!
Time Name Example
complexity
1 Constant Adding an element to the front of a linked list kn
Log n Logarithmic Finding an element in a sorted array
n Linear Finding an element in an unsorted array n2
N log n Linear log Sorting n items by divide and conquer
n2 Quadratic Shortest path between 2 nodes in a graph
n
n3 Cubic Matrix multiplication
kn Exponential Towers of Hanoi problem
logkn
Log log n
Algorithmic and advanced Programming in Python
Electronic copy available at: https://ssrn.com/abstract=3953589
Type of analysis
• Scenario analysis:
• Worst case
• Best case
• Average case
• Asymptotic notation
• O(n), … O(n2), etc…
Algorithmic and advanced Programming in Python 10
Electronic copy available at: https://ssrn.com/abstract=3953589
Golden Rules for complexity
Algorithmic and advanced Programming in Python 11
Electronic copy available at: https://ssrn.com/abstract=3953589
Rules
Algorithmic and advanced Programming in Python 12
Electronic copy available at: https://ssrn.com/abstract=3953589
Logarithmic complexity
Algorithmic and advanced Programming in Python 13
Electronic copy available at: https://ssrn.com/abstract=3953589
Omega Ω Notation
Algorithmic and advanced Programming in Python 14
Electronic copy available at: https://ssrn.com/abstract=3953589
Example
Algorithmic and advanced Programming in Python 15
Electronic copy available at: https://ssrn.com/abstract=3953589
Master theorem for divide and conquer
Algorithmic and advanced Programming in Python 16
Electronic copy available at: https://ssrn.com/abstract=3953589
Divide and conquer master theorem example
Algorithmic and advanced Programming in Python 17
Electronic copy available at: https://ssrn.com/abstract=3953589
Linked list
Algorithmic and advanced Programming in Python 18
Electronic copy available at: https://ssrn.com/abstract=3953589
Why linked lists?
• Difference with array
Algorithmic and advanced Programming in Python 19
Electronic copy available at: https://ssrn.com/abstract=3953589
Advantage of array
Algorithmic and advanced Programming in Python 20
Electronic copy available at: https://ssrn.com/abstract=3953589
Pro and cons for linked list
Algorithmic and advanced Programming in Python 21
Electronic copy available at: https://ssrn.com/abstract=3953589
Cons
Algorithmic and advanced Programming in Python 22
Electronic copy available at: https://ssrn.com/abstract=3953589
Comparison linked list, array and dynamic array
Algorithmic and advanced Programming in Python 23
Electronic copy available at: https://ssrn.com/abstract=3953589
In python
Algorithmic and advanced Programming in Python 24
Electronic copy available at: https://ssrn.com/abstract=3953589
Linked list insertion
Algorithmic and advanced Programming in Python 25
Electronic copy available at: https://ssrn.com/abstract=3953589
Algorithm: insertion
Algorithmic and advanced Programming in Python 26
Electronic copy available at: https://ssrn.com/abstract=3953589
Code
Algorithmic and advanced Programming in Python 27
Electronic copy available at: https://ssrn.com/abstract=3953589
At the end
Algorithmic and advanced Programming in Python 28
Electronic copy available at: https://ssrn.com/abstract=3953589
Code
Algorithmic and advanced Programming in Python 29
Electronic copy available at: https://ssrn.com/abstract=3953589
In the middle
Algorithmic and advanced Programming in Python 30
Electronic copy available at: https://ssrn.com/abstract=3953589
Algorithmic and advanced Programming in Python 31
Electronic copy available at: https://ssrn.com/abstract=3953589
Code
Algorithmic and advanced Programming in Python 32
Electronic copy available at: https://ssrn.com/abstract=3953589
Deleting in linked list
Algorithmic and advanced Programming in Python 33
Electronic copy available at: https://ssrn.com/abstract=3953589
Deleting the first node
Algorithmic and advanced Programming in Python 34
Electronic copy available at: https://ssrn.com/abstract=3953589
Code
Algorithmic and advanced Programming in Python 35
Electronic copy available at: https://ssrn.com/abstract=3953589
Deleting an intermediate node in singly listed list
Algorithmic and advanced Programming in Python 36
Electronic copy available at: https://ssrn.com/abstract=3953589
Code
Algorithmic and advanced Programming in Python 37
Electronic copy available at: https://ssrn.com/abstract=3953589
Code
Algorithmic and advanced Programming in Python 38
Electronic copy available at: https://ssrn.com/abstract=3953589
Delete at position
Algorithmic and advanced Programming in Python 39
Electronic copy available at: https://ssrn.com/abstract=3953589
In Lab session
• You will play with the concepts and starts getting more and more
familiar with how this works in real life
• This will be useful for your project
• Lab are done by Remy Belmonte
Algorithmic and advanced Programming in Python 40
Electronic copy available at: https://ssrn.com/abstract=3953589