0% found this document useful (0 votes)
13 views51 pages

Session

The document provides guidance on preparing for a programming exam, including reviewing common topics like algorithms and data structures. It discusses array and matrix representations and traversal, as well as examples of algorithm design like a coin change problem solved with a greedy approach.

Uploaded by

ramsses
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)
13 views51 pages

Session

The document provides guidance on preparing for a programming exam, including reviewing common topics like algorithms and data structures. It discusses array and matrix representations and traversal, as well as examples of algorithm design like a coin change problem solved with a greedy approach.

Uploaded by

ramsses
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/ 51

A brief review on algorithms and data structures

Jean-Bernard Hayet

CIMAT, A.C.

1/ 51
Preparing the programming admission exam

2/ 51
The programming exam

What we want to evaluate in the exam the logic behind your


solution, not that much the prociency in some language!

You may answer in any programming language you know, or


even with a pseudo-code.

3/ 51
The programming exam: Topics

https://pcc.cimat.mx/es/maestria-pcc/admision

4/ 51
Advice: preparation

You do not need to learn a new language (yet).

Be sure to be able to describe an algorithm, either with a


programming language or with pseudo code.

Review the concepts that are mentioned in


https://www.cimat.mx/sites/default/files/
Programas_docentes/Temario-MCC.pdf.

5/ 51
Advice: exam

Focus your answers in explaining well the logic of your problem


solving (do not lose time on superuous things).

Analyze the given examples to be sure to understand the


question.

Be as clear as possible in your answers.

Read all the exam once, rst.

Be pragmatic.

Manage your time well.

6/ 51
Resources

Lots of material here:


https://pcc.cimat.mx/Material_Preparacion_PCC

Websites to practice programming:

https://www.hackerrank.com/
https://leetcode.com/

7/ 51
Handling matrices/arrays

8/ 51
Arrays/matrices

Because linear algebra is a fundamental mathematical element in


computer science, the exam nearly always includes problems
involving arrays or matrices.

Be sure to dominate the representation and access of arrays,


including multi-dimensional arrays.

9/ 51
Arrays

C++ int weights [] = {79, 58, 65, 89, 56};

C# int [] weights = {79, 58, 65, 89, 56};

Java int [] weights = {79, 58, 65, 89, 56};

JavaScript var weights = [79, 58, 65, 89, 56];

Python weights = [79, 58, 65, 89, 56]

Swift var weights :[ Int ] = [79, 58, 65, 89, 56]

10/ 51
Arrays/matrices

In most languages: indices starting at 0.

Visit by incrementing the indices.

Use for/while loops.

Access with [] operator.

Be sure to dominate i/o access to arrays in your favorite


language

11/ 51
Arrays/matrices: index-based access

Most common way to access the data is with indices.


In C++:

int A[]={1,3,4};
for ( int i =0;i<3;i++)
cout << A[i] << endl;
vector <int> B;
for ( int i =0;i<B.size (); i ++)
cout << B[i] << endl;

In Python:

A=[1,3,4]
for i in range( len (A)):
print (A[ i ])

12/ 51
Arrays/matrices: range-based access

In C++:

for (auto& elem: v) {


cout << elem << endl;
}

In Python:

for elem in A:
print (elem)

13/ 51
Arrays/matrices: multiple dimensions

In general, matrices (or higher level tensors) are formed by arrays of


arrays of arrays of...

Use one [] operator for each dimension.

Typically, you can have access to lower dimensional arrays (e.g.,


rows of a matrix)

14/ 51
Arrays/matrices: multiple dimensions

In C:

int A[ROWS][COLS];
for ( int i =0;i<ROWS;i++) {
for ( int j =0;j<COLS;j++)
print ("%d ",A[i ][ j ]);
print ("\n");
}

In Python:

A = [[1,2,3],[4,5,6]]
for i in range (2):
for j in range (3):
print (A[ i ][ j ])

15/ 51
Arrays/matrices: linear search

The arrays allow the simplest form of search: linear search.

Use a loop to scan all the elements of the array.

Check if the element is the one you are searching for.

16/ 51
Practice: 2019 exam

Se suele representar una imagen numéricamente por una matriz de valores I tal que
I [x][y ] indique el valor de nivel de gris en la posición indicada por los índices x y y . Se
supondrá aquí que estos valores son enteros positivos encodicados en 8 bits (entre 0
y 255). Escribir una función que tome de entrada una matriz-imagen I cuadrada de
tamaño m × m y que le apliqué una rotación de 90 grados hacia la derecha (como
cuando uno rota la imagen en su celular).
Ejemplo: ante la ejecución de la función con la siguiente imagen
10 50 70 0
 
200 200 170 200
I=
 0 45 120 156

2 78 89 20
la función debe devolver
2 0 200 10
 
78 45 200 50
I′ = 
89 120 170 70

20 156 200 0

17/ 51
Algorithms

18/ 51
Algorithmic choices

Being given a problem to solve through a computer, you design the


step-by-step solution (the recipe) so that one can implement the
solution by following these steps, with any programming language.

That's the algorithm.

To solve a problem, many algorithms can be used.

19/ 51
Algorithmic choices

Should be your top concern when solving problems.

Does not need a programming language: paper/pencil.

Denes the maths/logic behind your resolution.

Will be in great part the main explanation for the


computational eciency of your program.

Focus on explaining the logic; do not spend time on


technicalities (reading inputs...)

20/ 51
Algorithmic choices

When designing an algorithm, two main concerns:

Is my algorithm working? Can I make a mathematical proof


that it will be working?

Is my algorithm ecient? How to quantify eciency? How to


compare two algorithms?

21/ 51
Algorithmic choices: Example

Problem
Given the Mexican peso coins set (1,2,5,10), design an algo-
rithm to give the minimal number of coins for any amount of
change.

Example: Give the change for 49 pesos. The best option is


4 × 10 + 5 + 2 × 2.

An algorithm for determining this optimal choice?

22/ 51
Algorithmic choices: Example

Require: N ≥ 0 the change to be given


Require: The ordered coin values c1 = 1, ..., c4 = 10
Ensure: The minimal number of coins to be used γ ∗
γ∗ ⇐ 0
i ⇐4
while i > 0 do ▷ While we have unseen coins
while N ≥ ci do ▷ While we can use coin ci ...
N ⇐ N − ci
γ∗ ⇐ γ∗ + 1
end while
i ⇐i −1
end while

You may have questions about some piece of code. Be


sure to understand well the logic.

23/ 51
Algorithmic choices: Example

Be sure to:

understand the use of a variable and assignment of values to


variables (⇐);

understand the use of while (condition);

understand what these nested loops are doing.

Example of greedy algorithm: To determine an optimal solution to a


problem involving a set of smaller decisions, you express the global
solution as a sequence of simple choices on the smaller decisions.

24/ 51
Algorithmic choices: Example

Correctness
Since c1 = 1, you are sure there is always a solution.
We scan the coin values from the largest to the smallest and lls the
change with as much as possible of this coin.
Proof of correctness: You can check the property for N < 10 exhaustively
and prove that n1 ≤ 1, n2 ≤ 2, n5 ≤ 1, n1 + n2 ≤ 2 (by contradiction). Sup-
pose N ≥ 10. By contradiction, if we were not to use a 10 coin, then we
should be able to ll the change optimally with n1 , n2 , n5 . This is impossible
for N ≥ 10 because the maximum sum with n1 , n2 , n5 is 9.
Important: This argument works for this set of coins but not for any set of
coins (ex: {1, 5, 12} and N = 15).

You are not expected to give correctness proofs, just the


algorithm.

25/ 51
Algorithmic choices: Example

Eciency

The number of times we will go through the double cycles is

N N mod 10 N mod 5
+ + +N mod 2
10 5 2

When N is large, this is close to linear in N.

(Simplication?)

26/ 51
Gentle Review of Data structures

27/ 51
Algorithms and data structures

When implementing an algorithm, you typically handle data in


some clever way adapted to the kind of algorithm you need.

This is the realm of data structures, among which we will review


the simplest ones: Linear structures.

You are expected to know about arrays, linked lists,


stacks and queues.

28/ 51
Linear structures: Linked lists/arrays

Structures designed to hold a collection of elements (generally


with the same type) that are typically accessed linearly
(consecutively).

Arrays allow an access at any element in constant time.


Implementation as blocks of contiguous memory.

A linked list is a collection of nodes that allow the access,


through a link, to the neighboring node in the list.

They are the basic implementation for collection of data within


other, higher-level structures.

29/ 51
Linear structures: Insertion/deletions

Edition of the structure at the extremities or inside:

Easy with a linked list (removing/adding links).

More dicult with an array (why?).

Computational complexities for these structures?

You are not expected to know about the big O notation.

30/ 51
Linear structures: Memory

The maximal size (capacity) of an array is xed.

A linked list is by denition expandable.

Typically, elements in arrays stored contiguously, while those in


a linked list may be dispersed.

Overhead in the case of linked list.

More exibility of the linked lists at the moment of


moving/swapping elements (e.g. for sorting)

31/ 51
Linear structures: Memory

Example: Imagine you apply a simple sorting algorithm


(BubbleSort) to very large objects.

The traditional swap operation may be very heavy. Easier with


linked lists?

You are expected to know at least one sorting algorithm.

32/ 51
Simply linked lists

Implementation as a sequence of nodes.

Each node contains a unique link (in C, through a pointer) to


its successor.

The link in the last node is marked in a special way (NULL in


C, None in Python) (termination).

33/ 51
Simply linked lists: Nodes

In Python:

class NodeStruct:
def __init__(self, data):
self . data = data
self . next = None

# Insert a node after another


def insert (prev , data ):
newNode = NodeStruct(data)
newNode.next = prev.next
prev . next = newNode

34/ 51
Simply linked lists

All the pointers/references to generated nodes are kept in the


'nex't elds.

Typically, one keeps a pointer/reference to the rst node as a


"head" of the list.

Cannot be used to loop backwards, or to insert 'before'.

In Python:

head = NodeStruct(0)
tail = head
for i in range (1,20):
insert ( tail , i )
tail = tail . next

35/ 51
Simply linked lists: Visit the list

In Python:

n = head
while n is not None:
print (n.data)
n = n.next

What is the eect of the following?

n.next = n.next.next

36/ 51
Simply linked lists: deletion

In C:

// Delete a node after another


void delete (Node* prev) {
if (prev==NULL || prev=>next==NULL) return;
Node *tmp = prev=>next;
prev =>next = tmp=>next;
free (tmp);
}

In Python:

# Delete a node after another


def delete (node):
if (node is not None) and (node.next is not None):
n = node.next.next
node.next = n

37/ 51
Simply linked lists

Example: In the Josephus problem, n soldiers in dire straits are


arranged along a circle and decide to commit group suicide. The
rule is the following: We start at soldier 1 and make k steps, while
cycling over the circle. The k -th soldier is killed.

Implement a simulation of this process to determine who will be


standing last.

[See code]

38/ 51
Abstract data types

Some structures are not dened through a full implementation,


but as a list of specications.

A list of functions and/or guarantees w.r.t the computational


complexity may be given.

Close to a mathematical, descriptive model.

Implementation choices open to programmers.

39/ 51
Abstract data types: examples

Stack, queue.

Priority queue.

Set, multi-set.

Tree, Graph.

40/ 51
The stack collection

Linear data collection that works under the Last In First Out
(LIFO) principle.

Useful in may kinds of problems: Collection of visited


webpages; Collection of actions realized with a text editor.

Specication: it should allow push/pop at the same end of the


collection; several implementations are possible.

41/ 51
The stack collection

push()
top
pop()

42/ 51
The stack collection

In Python, the interface should follow:

def push( self , data : object )


def pop( self )=>object
def top( self )=>object
def isEmpty( self )=>bool
def size ( self )=>int

43/ 51
Linked list implementation

# Linked list =based stack structure


class stack :
def __init__(self):
self . head=None
# Is the stack empty?
def isEmpty( self )=>bool:
return self . head==None
# Stack size
def size ( self )=>int:
if self . isEmpty():
return 0
count = 0
n = self . head
while n!=None:
count= count+1
n = n.next
return count

44/ 51
Linked list implementation

# Push a new data


def push( self , data : object ):
tmp = self . head
self . head = NodeStruct(data)
self . head.next= tmp
# Pop data from the top
def pop( self )=>object:
if self . isEmpty():
return None
tmp = self . head
self . head= tmp.next
return tmp.data
# Returns the element at the top
def top( self )=>object:
if self . isEmpty():
return None
return self . head.data

45/ 51[See code]


The queue collection

Linear data collection that works under the First In First Out
(FIFO) principle.

Useful in may kinds of problems: Collection of clients to be


processed.

Specication: should allow enqueue/dequeue operations at


opposite ends of the linear structure (rear/front); several
implementations are possible.

46/ 51
The queue collection

enqueue()

rear front
dequeue()

47/ 51
The queue collection

The interface should follow (in Python):

def isEmpty( self ) => bool


def size ( self ) => int
def enqueue( self , data : object )
def dequeue( self )=>object
def rear ( self )=>object
def front ( self )=>object

48/ 51
Fixed-capacity array implementation

# Array=based queue structure


class queue:
def __init__(self,capacity: int ):
self . capacity = capacity
self . rear = =1
self . front =0
self .data = capacity *[None]

# Is the queue empty?


def isEmpty( self ) => bool:
return self . rear = self . front ===1

# Stack size
def size ( self ) => int:
return self . rear = self . front +1

# Enqueue a new data


def enqueue( self ,data: object ):
if ( self . rear ==self.capacity =1):
return
self . rear = self . rear +1
self .data[ self . rear ] = data
49/ 51
Fixed-capacity array implementation

# Dequeue data from the front


def dequeue( self )=>object:
if self .isEmpty():
return None
data = self .data[ self . front ]
self . front = self . front +1
return data

# Get the value at rear


def rear ( self )=>object:
if self .isEmpty():
return None
return self .data[ self . rear ]

# Get the value at front


def front ( self )=>object:
if self .isEmpty():
return None
return self .data[ self . front ]

[See code]
50/ 51
Contact

[email protected]

51/ 51

You might also like