Week01-Lec02-After Class
Week01-Lec02-After Class
Junhao Gan
(with thanks to Harald Søndergaard, Toby Murray and Olya
Ohrimenko)
Week 1 Lecture 2
Semester 1, 2023
1/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 1 / 17
Approaching a Problem
2/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 2 / 17
Transform and Conquer? Use Abstraction?
3/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 3 / 17
Algorithms and Data Structures
Data structures: for storing data; arranging data in a way that suits an algorithm.
4/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 4 / 17
Exercise: Data Structures
5/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 5 / 17
Primitive Data Structures: The Array
Locating a cell, and storing or retrieving data at that cell is very fast.
x 2 3 5 7
42148 3
A collection of objects with links to one another, 42150 42152
possibly in different parts of the computer’s mem- 42152 5
ory. 42154 42164
42156
Each “node” has two attributes: a “val” which is 42158
its value, and a “next” which points to the rest of 42160 2
the list. 42162 42148
42164 7
Here x corresponds to address 42160. 42166 0
7/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 7 / 17
The Linked List
Often we use a dummy head node that points to the first object, or to a special
null object that represents an empty list. This makes it easier to write functions
that insert or delete elements.
Inserting and deleting elements is very fast: just move a few links around.
th
Finding the i element can be time-consuming.
8/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 8 / 17
Iterative Processing
Walk through the array (of length n), or through the linked list.
9/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 9 / 17
Recursive Processing
Solve the problem for a sub-instance and use the solution to solve the full instance.
10/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 10 / 17
Abstract Data Types (ADT)
A collection of data items, and a family of operations that operate on that data.
Nothing outside of the definitions of the ADT should refer to anything inside,
except through function calls for the basic operations.
11/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 11 / 17
Fundamental Data Structures: The Stack
Last-in-first-out (LIFO).
Operations:
CreateStack
Push
Pop
Top
EmptyStack?
…
12/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 12 / 17
Stack Implementation with an Array
With an array:
st:
13/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 13 / 17
Stack Implementation with a Linked List
elt 3
st 0 1 2
First-in-first-out (FIFO).
Operations:
CreateQueue
Enqueue
Dequeue
Head
EmptyQueue?
…
15/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 15 / 17
Other Data Structures
If you check out algorithm animation tools or advanced algorithm books, you will
meet exotic data structures such as splay trees and skip lists.
16/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 16 / 17
Next Week
17/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 17 / 17