0% found this document useful (0 votes)
44 views

CSC2103 Lecture01

This document outlines a lecture on algorithms and data structures. It will cover the definition of algorithms, where they come from, and why they are important to study. It will also discuss properties of algorithms like correctness, efficiency, and applicability. The lecture will then explain what data structures are and how they are more than just organizing data. Examples of algorithms like recipes, tying shoes, and different multiplication techniques will also be provided.

Uploaded by

Felicia Fam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

CSC2103 Lecture01

This document outlines a lecture on algorithms and data structures. It will cover the definition of algorithms, where they come from, and why they are important to study. It will also discuss properties of algorithms like correctness, efficiency, and applicability. The lecture will then explain what data structures are and how they are more than just organizing data. Examples of algorithms like recipes, tying shoes, and different multiplication techniques will also be provided.

Uploaded by

Felicia Fam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

LECTURE OUTLINE

○ Algorithms
○ Properties of algorithms
○ Data Structures
CSC2103 – Data Structures ○ Examples
○ Some mathematical Revision
& Algorithms ○ Some Programming Revision

CSC2103 - Data Structures & Algorithms

1 2

OVERVIEW ALGORITHMS
○ What is an algorithm?
○ Algorithm
● A procedure or formula for solving a
● What is an algorithm? problem
● Where do they come from?
findMax(Array)
● Why do we need algorithm? {
declare a variable to store
maximum value
max = Array[0]
Start do loop
Compare max with all array
values
if array_value > max
max = array_value
3
End Loop 4
print max value
CSC2103 - Data Structures & Algorithms } CSC2103 - Data Structures & Algorithms

3 4
ALGORITHMS ALGORITHMS

Where do algorithms come from? Why do we study algorithms?


○ To understand how they work
○ Named for the ninth century Persian
○ To be able to know when to use them
mathematician al-Khwarizmi
○ To be able to adapt them to new problems
○ Derived from the need to solve a specific
○ To be able to create new algorithms that
problem
are better than the existing ones (not all
○ Algorithms do not simply come out of thin of us)
air (or out of a textbook) ○ Better? In what way?
● Faster
● Less space used
5 ● More general 6

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

5 6

PROPERTIES OF ALGORITHMS PROPERTIES OF ALGORITHMS

○ Properties of Algorithms Correctness


● Correctness ○ As with a theorem it is easy to show an
● Efficiency algorithm is incorrect, just find a case
where the algorithm fails.
● Applicability
○ Showing that an algorithm is correct is a
much more difficult proposition.
○ Correctness can be shown formally or
informally.

7 8

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

7 8
PROPERTIES OF ALGORITHMS PROPERTIES OF ALGORITHMS

Efficiency Applicability
○ Usually this is a measure of speed, how ○ Generally, what is the domain of the
many basic steps are needed to complete a algorithm?
task.
○ Sometimes it is a measure of space, how
○ More importantly, what sub domain is the
much storage is needed. algorithm efficient in?
○ Rarely, it is a measure of some other
property or combination of properties

9 10

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

9 10

DATA STRUCTURES DATA STRUCTURES

○ A data structure is a systematic way of Data structures – more than just data?
organizing a collection of data.
○ Organized collection of data
○ A static data structure is one whose
capacity is fixed at creation. ○ Represents an abstract data type in an
E.g.: array. efficient way
○ A dynamic data structure is one whose ○ Efficient? In what way?
capacity is variable, so it can expand or
contract at any time. ● Allows for efficient access by the algorithm
E.g.: linked list, binary tree. ● Uses a minimum of storage
○ For each data structure we need algorithms
for insertion, deletion, searching, etc.
12

11 CSC2103 - Data Structures & Algorithms

11 12
ALGORITHMS-EXAMPLE ALGORITHM- EXAMPLE

○ A Recipe ○ Multiplication
○ Tying shoes ● Long multiplication (Grade School)
○ Multiplication ● Multiplication à la russe
● Divide and conquer multiplication
○ Programming in Visual basic

13 14

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

13 14

LONG MULTIPLICATION MULTIPLICATION À LA RUSSE

○ Long Multiplication 931 x ● Write multiplicand and multiplier side by


9 3 1
88 side
8 8 ● Make 2 columns by repeating the following
○ Multiply multiplicand
rule until the left column value is 1:
successively by each digit
7 4 4 8 ● “Divide left column by 2, truncating fractions,
of multiplier from right to
left and double number in right column.”
7 4 4 8 ● Result is addition of right column values
○ Place results one beneath
corresponding to odd valued left column
the other, shifting each 8 1 9 2 8 values
line one place to the left ● Note: only requires addition (*2) and division
○ Add all rows together to by 2
get final result 15 16

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

15 16
MULTIPLICATION À LA RUSSE MULTIPLICATION À LA RUSSE
931 88 931 88

465 176 Double the


previous
Divide the
number.
previous
number by
2 and
truncate if
fraction.

17 18

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

17 18

MULTIPLICATION À LA RUSSE MULTIPLICATION À LA RUSSE

931 88 931 88
Continue
the 465 176 465 176
division 232 352 232 352
and 116 704
doubling
process
until the
left column
number
becomes 1.

19 20

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

19 20
MULTIPLICATION À LA RUSSE MULTIPLICATION À LA RUSSE
931 88 931 88

465 176 465 176

232 352 232 352

116 704 116 704

58 1408 58 1408

29 2816

21 22

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

21 22

MULTIPLICATION À LA RUSSE MULTIPLICATION À LA RUSSE


931 88 931 88

465 176 465 176

232 352 232 352

116 704 116 704

58 1408 58 1408

29 2816 29 2816

14 5632 14 5632

7 11264

23 24

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

23 24
MULTIPLICATION À LA RUSSE MULTIPLICATION À LA RUSSE
931 88 931 88

465 176 465 176

232 352 232 352

116 704 116 704

58 1408 58 1408

29 2816 29 2816

14 5632 14 5632

7 11264 7 11264

3 22528 3 22528

1 45056
25 26

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

25 26

MULTIPLICATION À LA RUSSE MULTIPLICATION À LA RUSSE

Add the right 931 88 931 88 88


column values
465 176 465 176 176
if the left
column value 232 352 232 352
is an odd
value. 116 704 116 704

58 1408 58 1408

29 2816 29 2816 2816

14 5632 14 5632
The result of
7 11264 the addition of 7 11264 11264

3 22528 the chosen 3 22528 22528


right column
1 45056 values is the 1 45056 45056
27 28
answer for the
multiplication. 81928
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

27 28
EXERCISE 1 DIVIDE & CONQUER MULTIPLICATION

○ In divide and conquer multiplication, the


○ Multiply 1980 x 21 using A la Russe numbers can be recursively broken down into
method smaller parts of numbers and multiply the
smaller parts of the numbers to obtain the sub-
instance of the multiplication. These sub-
instances are then added together to obtain the
result of the multiplication.

29 30

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

29 30

DIVIDE & CONQUER MULTIPLICATION DIVIDE & CONQUER MULTIPLICATION

○ For example, to obtain the result of 931 x ○ Let’s first look at how the two numbers are
1234 using divide and conquer method: actually multiplied using some
○ Split each of the numbers into two halves, representational symbols.
the right half and the left half, ○ Let the two numbers as 931 and 1234 as a
○ each with n/2 digits; e.g., and b respectively.
○ Thus, Left-half of a (al) = 09, Right-half of
931 is split into 09 – left half and 31 right half a (ar) = 31, Left-half of b (bl) = 12 and
1234 is split into 12 –left half and 34 right half Right-half of b (br) = 34.

31 32

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

31 32
DIVIDE & CONQUER MULTIPLICATION DIVIDE & CONQUER MULTIPLICATION

a: al ar ○ The positional value of the values al and bl


b: bl br is 10n/2, because al and bl are the left half of
(br x al) (br x ar) their original numbers.
(bl x al) (bl x ar) ○ For example, 0931 = 09 x 104/2 + 31
(bl x al) + (br x al)+(bl x ar) + (br x ar) = 09 x 100 + 31
= 931
1234 = 12 x 104/2 +34= 12 x 100 + 34
○ The above multiplication, is just a
representational. In term of the values of the = 1234
numbers, the al and bl would need to include the
positional values.
33 34

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

33 34

DIVIDE & CONQUER MULTIPLICATION DIVIDE & CONQUER MULTIPLICATION

○ Thus, with positional value, we express the ○ Hence,


multiplication as:
a: 0931 al x 10n/2 ar a × b = (bl x al)10n + [(br x al)+(bl x ar)] x 10n/2 +
b: 1234 bl x 10 n/2 br (br x ar)
(br x al x 10 ) (br x ar)
n/2

bl x 10n/2 x (bl x 10n/2 x ar) ○ Note: The 10n position value can be obtained by
al x 10n/2 shifting to the left the value n position.
(bl x al)10n + [(br x al)+(bl x ar)] x 10n/2 + (br x ar)

35 36

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

35 36
DIVIDE & CONQUER MULTIPLICATION

○ Divide & conquer multiplication 931 x 1234

Multiply Shift Result


(al x bl)
(al x br)
09 12
09 34
4
2
1080000
30600
Some mathematical Revision
(ar x bl) 31 12 2 37200
(ar x br) 31 34 0 1054
1148854
○ Useful in extended precision integer arithmetic 38

Exercise 2 : Multiply 1980 * 2315


37

CSC2103 - Data Structures & Algorithms

37 38

SOME MATHEMATICAL REVISION LAWS OF EXPONENTS

○ Exponents
○ Analysis of algorithms 1.

2.

3.

4.

39 5. 40

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

39 40
ANALYSIS OF ALGORITHMS ANALYSIS OF ALGORITHMS

○ To analyze an algorithm is to determine ○ There are two ways to analyze an


the amount of resources (such as time and algorithm
storage) necessary to execute it
● Empirical analysis
● (Time/Space) Complexity = f(n)
● The running time of an algorithm typically
● Theoretical analyses
grows with the input size
● Average case time is often difficult to
determine

41 42

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

41 42

EMPIRICAL ANALYSIS EMPIRICAL ANALYSIS

○ Empirical analysis is research that Limitations of Experiments


derives its data by means of direct ○ It is necessary to implement the
observation or experiment. algorithm, which may be difficult and
○ The results are based upon actual evidence time consuming
○ Results may not be indicative of the
running time on other inputs not included
in the experiment.
○ In order to compare two algorithms, the
same hardware and software
43 environments must be used 44

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

43 44
THEORETICAL ANALYSIS

○ Uses a high-level description of the


algorithm (Pseudo-code) instead of an
implementation Summary
○ Estimating runtime: Characterizes running
time as a function of the input size, n, in the
worst case scenario.
○ Takes into account all possible inputs
○ Allows us to evaluate the speed of an 46

algorithm independent of the hardware/


software environment 45

CSC2103 - Data Structures & Algorithms

45 46

ELEMENTARY DATA STRUCTURES ARRAY

○ A data structure is a particular ○ An array is a data structure consisting of a


way of storing and organizing data fixed number of data items of the same
in a computer so that the data can be type.
used and manipulated efficiently.
○ For example,
○ Table: array[1..20] of integer
○ Data structures are used in almost
○ Letters: array[1..26] of character
every program or software system.
○ An example of an array A of integer with
10 elements
1 2 3 4 5 6 7 8 9 10
47 18 6 9 34 4 5 1 2 100 35 48

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

47 48
ARRAY ARRAY – OPERATION COMPLEXITY

○ Operation – Array elements are accessed ○ Access an item: O(1)


vie index value or subscript ● Random access in constant time by specifying
its index, i.e. evaluating the expression a[i]
○ Example y = A[5], Initial = A[2]
○ Inserting at the beginning: O(n)
○ Array can be a multi dimensional, where
● All n items must move over one cell to make
more then one index can be used to access room for the added Element
elements in a particular position. ○ Deleting the first element: O(n)
○ Example A[3][3], is an array with a total ● All n items must move forwards one cell to
of 9 values. fill up the empty cell of the removed element

49 50

CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms

49 50

ARRAY EXAMPLE

int a[5]; // declare integer array with size 5


for(int i=0;i<5;i++)
cin>>a[i]; // Read array elements
cout<<"The array elements are: "; Exercise
for(int i=0;i<5;i++)
cout<<a[i]<<" "; //Display array elements

52

51

CSC2103 - Data Structures & Algorithms

51 52
MULTIPLICATION
○ Japanese way
● 22 x 33
● 123 x 231

53

CSC2103 - Data Structures & Algorithms

53

You might also like