Foundations in Data Structure and Algorithms Truong Tuan...
Transcript of Foundations in Data Structure and Algorithms Truong Tuan...
Foundations in Data Structure and Algorithms Truong Tuan Anh
CSE-HCMUT
2
Outline
Basic conceptsRevision
3
What is Data?
4
What is Data?
DataData is information that has been translated into a form that ismore convenient to calculate and analyze.
ExampleNumbers, words, measurements, observations or descriptions
of things.
Qualitative data: descriptive informationQuantitative data: numerical information (numbers).
Discrete data: can only take certain values (like whole numbers)Continuous data: can take any value (within a range)
5
Data Type
Class of data objects that havethe same properties.
Define a data type1. A set of values2. A set of operations on values
Example
6
Data Structure
What is a data structure?1. A combination of elements in which each is either a
data type or another data structure2. A set of associations or relationships (structure)
that holds the data together
ExampleAn array is a number of elements of the same type in a specific order.
7
Abstract Data Type
AimUsers know what a data type can do.How it is done is hidden.
DefinitionAn abstract data type is a data declaration packaged together with the operations that are meaningful for the
data type.
1. Declaration of data2. Declaration of operations3. Encapsulation of data and operations
8
Abstract Data Type
9
Example: List
InterfaceData: sequence of elements of a particular data typeOperations: accessing, insertion, deletion
ImplementationArrayLinked list
10
Algorithm
What is an algorithm?The logical steps to solve a problem
What is a program?Program = Data structures + Algorithms
(Niklaus Wirth)
11
Pseudocode
The most common tool to define algorithmsEnglish-like representation of the algorithm logic
Pseudocode = English + code
English: relaxed syntax being easy to readCode: instructions using basic control structures (sequential, conditional, iterative)
12
Pseudocode
PseudocodeAlgorithm HeaderAlgorithm Body
Algorithm HeaderNameParameters and their typesPurpose: what the algorithm doesPrecondition: precursor requirements for the parametersPostcondition: taken action and status of the parametersReturn condition: returned value
13
Pseudocode
PseudocodeAlgorithm HeaderAlgorithm Body
Algorithm BodyStatementsStatement numbers: decimal notation to express levelsVariables: important dataAlgorithm analysis: comments to explain salient pointsStatement constructs: sequence, selection, iteration
14
Pseudocode: Example
15
Data Structures
16
Data Structures
17
Data StructuresA member of an object can be accessed directly by a dot (.) inserted between the object name and the member name
18
Data Structures
19
Data Structures
20
Exercise
Define a data structure student_t containing a student's name, first name, and age.
Write a code in C++ to take input your data and display it.
21
22
Class
23
Class: Example
24
25
Class
Class ConstructorsAutomatically called whenever a new object of a class is created.Initializing member variables or allocate storage of the object.Declared with a name that matches the class name and without any return type; not even void.
26
27
Class
Member initialization:
28
ArrayAn array is a series of elements of the same type placed in
contiguous memory locations that can be individually referenced by a unique identifier with an index.
29
Class
30
Algorithm Efficiency
A problem often has many algorithms/solutionsComparing two different algorithms
How fast an algorithm is?How much memory does it cost?
Compare by calculate the computational complexity: Measure of the difficulty degree (time and/or space) of an algorithm.
31
Algorithm Efficiency
How to calculate the efficiency
efficiency = f(n)
n is the size of a problem (the key number that determines the size of input data)
32
Linear Loop
for (i = 0; i < 1000; i++)//application code
The number of times the body of the loop is replicated is 1000f(n) = n
for (i = 0; i < 1000; i+=2)//application code
The number of times the body of the loop is replicated is 500f(n) = n/2
33
Linear Loop
34
Logarithmic Loops
The number of times the body of the loop is replicated is f(n) = log2 n
35
Logarithmic Loops
36
Nested Loop
Iterations = Outer loop iterations * Inner loop iterations
The number of times the body of the loop is replicated isf(n) = n*log2 n
37
Nested Loop
38
Quadratic Loop
The number of times the body of the loop is replicated isf(n) = n2
39
Dependent Quadratic Loop
The number of times the body of the loop is replicated isf(n) = 1 + 2 + … + n = n*(n+1)/2
40
Quadratic Loop
41
Asymptotic Complexity
Algorithm efficiency is considered with only big problem sizesWe are not concerned with an exact measurement of an algorithm's efficiencyTerms that do not substantially change the function's magnitude are eliminated
42
Big-O Notation
O(g(n)) = f(n) : ∃ positive constants c and n0,
such that ∀n ≥ n0, we have0 ≤ f(n) ≤ c*g(n)
For function g(n), we define O(g(n)), big-O of n, as the set:
g(n) is an asymptotic upper bound for f(n).
Intuitively: Set of all functions whose rate of growth is the same as or lower than that of g(n).
43
Examplef(n) = 2n + 5g(n) = n
Consider the condition2n + 5 <= n
will this condition ever hold? No!
How about if we tack a constant to n?2n + 5 <= 3n
the condition holds for values of n greater than or equal to 5This means we can select c = 3 and n0 = 5
44
2n+5
n
2n+53n
point where 3n“beats” 2n+5
2n+5 is O(n)
Example
45
Big-O Notation
Set the coefficient of the term to 1Keep the largest term and discard the othersSome example of Big-O:
log2n, n, nlog2n, n2, nk, 2n, n!
46
arrayMax Example
Algorithm arrayMax(A,n):Pre: An array A storing n integers.Post: The maximum element in A.currentMax ← A[0]
for i ← 1 to n - 1 do
if currentMax < A[i] then
currentMax ← A[i]
return currentMax
What is the running time of arrayMax?
47
arrayMax Example
Depending on what operations we decide to count, running time function
f(n) = a*n + bRegardless, f(n) is O(n)Or, equivalently, the running time of algorithm arrayMax is O(n)
48
Standard Measures of Efficiency
Assume instruction speed of 1 microsecond and 10 instructions inloop
n = 10000
49
Standard Measures of Efficiency
50
Big-O Analysis Example
51
Big-O Analysis Example
Nested loop
52
Big Omega Ω-notation
g(n) is an asymptotic lower bound for f(n).
Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n).
Ω(g(n)) = f(n) : ∃ positive constants c and n0,such that ∀n ≥ n0, we have
0 ≤ c*g(n) ≤ f(n)
For function g(n), we define Ω(g(n)), big-Omega of n, as the set:
53
Example
5n2 + 7 is Ω(?)
54
Time Costing Operations
The most time consuming: data movement to/from memory/storageOperations under consideration:
ComparisonsArithmetic operationsAssignments
55
Time Costing
Big-O can be used for categorizing or characterizing functionsFor example, the statements:
2n + 3 is O(n) and 5n is O(n)→ 2n + 3 and 5n are in the same category
→ If the functions are running times of two algorithms, the algorithms are thus comparable
56
Read More
Big-ThetaLittle-OLittle-Omega
57
Takeaways
Basic conceptsBasic data structuresAlgorithm complexity