Data Structures: Assist - Prof.Dr. Erkan TANYILDIZI
Data Structures: Assist - Prof.Dr. Erkan TANYILDIZI
com
YMT219
Data Structures
one
2nd
Course Requirements
- It is assumed that students in this course know one of the
Object-oriented programming languages (Java, C++, C#)
or one of the procedural programming languages (C,
Pascal).
- Topics to know:
- Basic data types (int, float)
- Control structure (if else structure)
- Loops
- Functions(Methods)
- Entry and exit procedures
- Simple arrays and classes
4
models
Episode 1
5
6
algorithm
- algorithm, means the way to be followed in solving a problem.
When the algorithm is expressed in a programming language
(such as Java, C++, C#)program takes its name.
algorithm
- In computer application, many algorithms are needed
when developing a software. For example,
- search algorithm
- sorting algorithm
- matrix or vector processing algorithm
- graph algorithm
- algorithm for solving a mathematical model
- There are many types of algorithms, and when
developing applications, one or more of them is
always used.
8
Data
- Data, are the most basic elements processed by
algorithms (numeric information, textual
information, pictures, sounds and other information
used as input, output or intermediate calculations…).
- Trustworthy
- easy to use
- Fast algorithms
11th
- Sample
- Sample
- Solution. one:
- data structures,
- Basic Data Structures
- Defined (Composite) Data Structures
- All
programming languages, in general, have basic data structures
for storing characters, integers, fractions, and words (strings).
The data structure is essentially the formatting order that
determines the interpretation of raw 1's and 0's data. For
example, the binary equivalent of 62 is stored in memory as
111110.
- Basic data structures can be classified as follows:
15
- The purpose of use of each is different and one or all of them can be
used together according to the application. In general, the most used
is community building; thus, multiple data structures are brought
together/packaged and a new data structure/type is created.
16
Data Models
- Data models ensure that the program to be designed is in the most appropriate and
efficient way and gives information about the running speed and memory requirement
of the program from the beginning. Most of the time, it can be said that the amount of
memory requirement is directly proportional to the running speed of the program.
- The list data model is based on the principle that data belonging to the
same cluster are kept sequentially in memory. Data may or may not be
in a particular order (ordered, etc.); The important thing is to keep all
the data in sequential order.
19
Data Models
- In this course, the following data models will be discussed in detail;
- List
- consisting of a finite number of elements and in linear order
embedded data model. There is no restriction on accessing any
element.
- Stack or Stack
- List-aware data model (Last In First Out-LIFO list) with restricted
access to its elements.
- Tail
-A list-aware data model with access restrictions to its elements. (First
In First Out-FIFO list).
- Tree
- nonlinear data model with specific attributes
- Graph
- It consists of nodes called vertices and links that connect vertices
called edges. nonlinear data model
28
29
30
Features of Algorithms
- There are five basic properties that an algorithm must
have.
- 1. Input
- An algorithm has zero or more input variables. Input variables
are the set of values given to the algorithm before the
algorithm starts processing, or the memory area given to
save the value.
- 2. Definiteness
- For each step of an algorithm, exactly what it will do should be
determined and there should be no ambiguity. Whatever action is to
be taken for each case should be clearly defined.
32
Features of Algorithms
- 3.Output)
- Each algorithm has one or more output values. There are
correlations between output values and input values.
- 4.Efficiency
- Naturally,
every algorithm is expected to operate effectively. It
should run as fast as possible and use as little memory as
possible.
- This means that the transactions will be sufficiently basic and must be
processed and finished in a limited time.
33
Features of Algorithms
- 5. Boundedness
- Each algorithm must finish after a limited number of run steps.
- Limitation is very important for an algorithm. Let's say that two algorithms that
do the same thing, one finishes in a billion steps and the other finishes in a
hundred steps. In this case, the algorithm that ends in a hundred steps is
always better. The meaning of this is to finish the process with the least
possible number of steps, which is meant to be explained with the concept of
limitation. Some other criteria are the ability of the algorithm to be
transferred to the computer environment, its simplicity, etc. such features.
34
Algorithm Process
- design
- Validation
- Analysis (analysis)
- Implementation
- Test
35
Actual Code
- It is the implementation of the algorithm on a specific data
structure with any programming language.
Expressing algorithms as
brute-code
- Example: Define the algorithm that calculates the sum and
product of the elements in an array, using rough-code.
- Calculate Sum and Product (sequence, sum, product)
- Entry: array of n numbers.
- Output: sum and product of array elements
- for i- 1 to n do
- sum- sum + array[i]
- product- product* array[i]
- end for
39
Algorithm Analysis
- Estimating the amount of resources that the algorithm will hear
during execution Algorithm Analysis is called.
- Here, the basic unit of calculation is selected, and a relation is calculated to find
out how many of these transactions need to be done in order for the program
to fulfill its task.
- If this relationship is related to the speed of working with time, if it is related to the memory
requirement, it reveals the memory requirement.
41
Algorithm Analysis
Runtime Analysis
- Algorithm 1, Tone ( N ) = 1000*N
- Algorithm 2, T2nd (N) = N2nd
Runtime Tone(n)
N input data
Algorithm 1
100010101010001111100011000111010
101010101010100100010101010001000
00000000011110101000111010
runtimeT2nd(n)
Algorithm 2
44
Runtime Analysis
Working time T(N)
Algorithm 2
Algorithm 1
Analysis
one- for(k=0;k<n;k++)
2nd- sum+=A[k];
3- mean=total/n
4- return average;
}
48
float size;
int k ;
one- size=A[0];
2nd- for(k=1;k<n;k++)
3- if (A[k]<ink)
4- size=A[k];
5- return enkucuk;
}
50
float size; - - -
int k ; - - -
} - - -
T(n)=4n
51
3- C[i][j]=A[i][j]+B[i][j]; one nm nm
} - - -
T(n,m)=3nm+4n+2
if n=m T(n)=3n2nd+4n+2
N N N
T (N ) - --one - - N - N * N - N 2nd
NS-one j-one NS-one
52
Cost Frequency(Repeat)
Basic Unit of Account
(Cost) (frequency)
int i, j, x=0; c1 one
for(i=n; i>=1; i=i-1) c2 n
for(j=1; j<=i; j=j+1) c3 n*(n-1)/2
x=i+j; c4 n*(n-1)/2
T(n)=c1*1+c2*n+(c3+c4)*n*(n-1)/2
T(n)=c1*1+c2*n+(c3+c4)*(n2nd/2-n/2)
T(n)=c1*1+ (c2-(c3+c4)/2) *n+(c3+c4)*(n2nd/2nd)
53
Complexity
- complexity;
They are expressions that show the variation of an
algorithm against a large number of parameters. They are used to
more accurately find the Run (Execute) time.
is used.
54
Complexity
O(logn) problem into smaller parts by constant fractions at each step. Typical time of
O(nlogn) multiplier. Most sorting algorithms are Terrestrial. Suitable when the
Front2nd) amount of data is low (N<1000) Cubic. Suitable when the amount of
O(2n) Exponential in base two. Suitable when the amount of data is very small (N<=20)
Front!) factorial
56
Complexity
57
Big-Oh(Big-Oh) Notation:
Asymptotic Upper Limit (Worst case analysis)
- running time of an algorithm,
- T(n)=HE(f(n)) HEis not a function, just a representation.
c*f(n)
T(n)
n0 Growing n -
58
Big-Oh(Big-Oh) Notation:
Asymptotic Upper Limit
- Example: T(n) = 2n+5 is O(n)2nd) Why?
- n>=n0 for
all numbers satisfying the conditionT(n) = 2n+5
<= c*n2nd satisfying the condition NS and n0looking for
your values.
Big-Oh(Big-Oh) Notation:
Asymptotic Upper Limit
- Example: T(n) = n(n+1)/2 - HE(?)
Importance of O Notation
O Notation
- When we write in O notation, we write in the simplest way.
- E.g
-3n2nd+2n+5 = O(n2nd)
- The following notations are correct but not used.
-3n2nd+2n+5 = O(3n2nd+2n+5)
-3n2nd+2n+5 = O(n2nd+n)
-3n2nd+2n+5 = O(3n2nd)
62
O Notation-Example 1
- 3n2nd+2n+5 = O(n2ndProve whether ) is true.
- 3n2nd n - 1 for + 2n + 5
NS = 10, n0 = one
O Notation-Example 2
- T(n)=O(7n)2ndIfit can be expressed as +5n+4) then
the function T(n) can be any of the following.
- T(n)=n2nd
- T(n)=4n+7
- T(n)=1000n2nd+2n+300
O notation- HEexample 3
- Writethe time spent by the functions in O
notation.
T(n)
Value of the function -
c*f(n)
n0 Growing n -
67
- notation-Example
– T(n) = 2n + 5 - -(n). Why?
– 2n+5 >= 2ndn, all n >= one for
– T(n) = 5*n2nd - 3*n - -(n2nd). Why?
– 5*n2nd - 3*n >= 4*n2nd, all n >= 4 for
- 7n2nd+3n+5 = O(n4)
- 7n2nd+3n+5 = O(n3)
- 7n2nd+3n+5 = O(n2nd)
- 7n2nd+3n+5 = -(n2nd)
- 7n2nd+3n+5 = -(n)
- 7n2nd+3n+5 = -(1)
68
69
- Notation
(Average situation analysis)
- In every situation NSonef(n) - T(n) - c2nd f(n) and n - n0
positive, stable NSone,NS2nd and n0if the
values can be found T(n)= -(f(n)) statement
is true.
NS2nd*f(n)
Value of the function -
T(n)
NSone*f(n)
n0 Growing n -
70
- notation- HEexample
– T(n) = 2n + 5 - -(n). Why?2ndn <=
2n+5 <= 3n, all n >= 5 for
Common Mistakes
- Don't just count loops to find complexity.
i++ one 0 0
i++ one N N
- Average time is the time spent in the resulting state when the
input parameters fall between the best and worst case.
+ one k k
=3k+5
81
N -one one
one
Taverage (N ) - - (3k - 5) + (3N - 5)
k -0 2ndN 2nd
one N (N -one) 3 5
Taverage (N ) - (3 - 5N ) - N -
2ndN 2nd 2nd 2nd
3 353 5
Taverage (N ) - N - - - N - - 2ndN - 4 - - (N )
44222
82
- For Loop:
-A for Maximum execution time for loop forthe number
of times the statements (including test) inside the
loop are repeated.
- Nested Loops
- In nested loops, the total execution time of the statement
inside the group is the total execution time of the statements.
for is the product of the dimensions of the loops. In this case,
the analysis is done from the inside out.
N N N
T (N ) - --one - - N - N * N - N 2nd
NS-one j-one NS-one
83
your idioms
total execution
your time
to find
just collecting
makes.
85
- Since the array is sorted, the number in the middle of the array
and the searched number is reduced to half, and
so on.
- Example: Let's call 55
0 one 2nd 3 4 7 8 11th 15
3 8 10 11th 20 50 55 60 65 70 72 90 91 94 96 99
(left - right)
left middle - right
2nd
Eliminated
left middle
Eliminated
middle
left right
- At any stage while searching for the target, we can use our search area. "right"
with"left" We restrict the space between
- “to the left of "left" the remaining area is smaller than the target and this area is subtracted
from the search area.
- “on the right of "right" the remaining area is greater than the target and this area is excluded
// Returns the index of the searched number and returns -1 if the searched number is not found.int
left = 0;
right = N-1;
if (A[middle] == number) return middle; // Searched number found. Return index else if
middle+1; } //done-while
} //done-binarySearch
Homework
- In the homework report, the person who prepared it, the working time, the resources he
- Eachdata model has different processing time costs and memory requirements,
depending on the underlying data structure. While the program is being
developed, it is tried to find a solution that will balance the time and memory
space costs. In general, it can be said that the cost and the memory
requirement are inversely proportional to the memory where sequential
accesses can be made.
95
Examples
96
Example I:
Finding the sum of the numbers in the array
return total;
} //Done-Collect
97
Example I:
Finding the sum of the numbers in the array
Process
number
int Gather(int A[], int N) {
one
int sum = 0;
Total: 1 + N + N + 1 = 2N + 2
• Working time: T(N) = 2N+2
– N is the number of numbers in the array
98
Example II:
Searching for an element in an array
Process
int Call(int A[], int N,
number
int number) {
int i = 0; one
Example II:
Searching for an element in an array
T (N ) - --(one--one) - N 3 - N 2nd
N -one
NS-0 j-0 k -0
101
N
2nd N * (N -12 n - one) N 3
Sum of Squares: - NS -
6
-
3
NS-one
N
A
-
N -one -one
Geometric Series: ANS - A>1
NS-0 A -one
N A<1
one- AN -one
- ANS -
one- A
- - (one)
NS-0
102
Linear Geometric
n (
(n -one)x n- - nxn - x
one)
series: -ix NS - x - 2ndx 2nd
3
- 3x - . . . - nx n -
NS-0 (x -one)2nd
11th
one
one
Harmonic series: H n - - - 1- - - ... - - (ln n) -HE(one)
n
NS 23
NS-one
n
log AB - B * log A
Logarithm: log( A* B) - log A - log B
A
log( ) - log A - log B
B
103
b b a-one
n n n