0% found this document useful (0 votes)
37 views17 pages

Week01-Lec02-After Class

The document presents an overview of algorithms and data structures, focusing on problem-solving techniques such as the Mutilated Checkerboard Problem and various data structures like arrays, linked lists, stacks, and queues. It discusses their properties, operations, and efficiency, emphasizing the importance of abstraction and encapsulation in programming. The lecture also introduces fundamental concepts of algorithm analysis and prepares for further discussions in subsequent weeks.

Uploaded by

BIR BIRDIE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views17 pages

Week01-Lec02-After Class

The document presents an overview of algorithms and data structures, focusing on problem-solving techniques such as the Mutilated Checkerboard Problem and various data structures like arrays, linked lists, stacks, and queues. It discusses their properties, operations, and efficiency, emphasizing the importance of abstraction and encapsulation in programming. The lecture also introduces fundamental concepts of algorithm analysis and prepares for further discussions in subsequent weeks.

Uploaded by

BIR BIRDIE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

COMP90038 Algorithms and Complexity

Algorithms and Data Structures

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

Can we cover this board with 31 tiles


of the form shown?

This is known as the Mutilated


Checkerboard Problem.

There are only finitely many ways we


can arrange the 31 tiles, so there is a
brute-force (yet very inefficient) way
of solving the problem.

Question: Can you come out with a more efficient solution?

2/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 2 / 17
Transform and Conquer? Use Abstraction?

Can we cover this board with 31 tiles


of the form shown?

Why can we quickly determine that


the answer is NO?

Hint: Using the way the squares are


coloured helps.

3/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 3 / 17
Algorithms and Data Structures

Algorithms: for solving problems, transforming data.

Data structures: for storing data; arranging data in a way that suits an algorithm.

Linear data structures: stacks and queues


Trees and graphs
Dictionaries

Which data structures are you familiar with?

4/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 4 / 17
Exercise: Data Structures

Pick a data structure and describe:

What can this data structure do?


How to insert an item into the data structure
How to find an item
How to handle duplicate items

5/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 5 / 17
Primitive Data Structures: The Array

An array corresponds to a sequence of consecutive cells in memory.

Depending on programming language: A[0] up to A[n-1], or A[1] up to A[n].

Locating a cell, and storing or retrieving data at that cell is very fast.

The downsides of an array are that:

the length n has to be known in advance and is fixed;


maintaining a contiguous bank of cells with information can be difficult and
time-consuming.
42148 6
42150 9
6 9 2 3 7 5 8
42152 2
0 1 2 3 4 5 6 42154 3
42156 7
42158 5
42160 8
6/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 6 / 17
Primitive Data Structures: The Linked List

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.

For example, to locate item x.

function find(A,x,n) function find(head,x)


j := 0 p := head
while j < n while p != null
if A[j] == x if p.val == x
return j return p
j := j+1 p := p.next
return -1 return null

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.

function find(A,x,lo,hi) function find(p,x):


if lo > hi if p == null
return null return p
else if A[lo] == x else if p.val == x
return lo return p
else else
return find(A,x,lo+1,hi) return find(p.next,x)
Initial call: find(A,x,0,n-1)
Initial call: find(head,x)

We will discuss recursion properly in Week 3.

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.

Think of an ADT as a set of promises, or contracts.

We must still implement these promises, but it is an advantage to separate the


implementation of the ADT from the “concept”.

Good programming practice is to support this separation:

Nothing outside of the definitions of the ADT should refer to anything inside,
except through function calls for the basic operations.

Such separation is also known as Encapsulation.

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?

Usually implemented as an ADT.

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:

Question: Is an array an appropriate data structure for implementing a stack? And


why?

13/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 13 / 17
Stack Implementation with a Linked List

With a linked list (push):

elt 3

st 0 1 2

function Push(x, st)


elt ← new node
elt.val ← x
elt.next ← st
st ← elt
return st
14/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 14 / 17
Fundamental Data Structures: The Queue

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

We shall meet many other (abstract) data structures, such as

The priority queue


Various types of “tree”
Various types of “graph”

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

Algorithm analysis—how to reason about an algorithm’s resource consumption.

17/17
Algorithms and Complexity (Sem 1, 2023) Algorithms and Data Structures © University of Melbourne 17 / 17

You might also like