You are on page 1of 103

Translated from Turkish to English - www.onlinedoctranslator.

com

YMT219
Data Structures

Assist.Prof.Dr. Erkan TANYILDIZI

one
2nd

Textbooks and Supplementary Resources

-Data Structures and Algorithms


- Dr. Rifat ÇÖLKESEN, Papatya Publishing

-Data Structures and Problem Solving Using Java


- Mark Allen Weiss, Pearson International Edition

-Ahmet Yesevi University Lecture Notes

- You can also access many resources on the internet.


3

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

Lesson Processing Rules


- Attendance is mandatory. A maximum of 4 weeks of absence can be made.
- Pay attention to lesson start times. Students who arrive
late to the class will wait until the break.
- Each lesson will be signed.
- Assignments will be submitted on time. Assignments submitted
after the given date will not be accepted.
- Please do not talk to each other (or to yourself) during the
lesson, do not whisper, do not text etc.
- Please respect the lecturers and listeners.
- Do not be afraid to ask questions about everything that is told or not explained, or on any subject you
are curious about.

- Your mobile phone etc. personal portable communication devices.


Please keep it closed during the lesson and do not carry it with you
during the exam.
Data Structures and

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.

- Thealgorithm precisely lays out the steps and


conditions to be followed in succession to achieve
the outcome of a particular problem. For any input
data, it is necessary to obtain output data. Other
cases are not algorithms.
7

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…).

- In order for an algorithm to be effective, understandable


and accurate, the data that the algorithm will process
must be organized.
9

Data Structure and Data Model


- datastructure (Data Structure) indicates the way or arrangement in
which data or information is held in memory.

- All programming languages, in general, have basic data


structures for storing integers, fractions, characters, and words.
Even a program variablea simple data structure can be
accepted as

- data model (Data Model) shows the relational or ordinal state


of the data with each other; It can be said that it is a
conceptual approach method for solving the problem.
- All mathematical and engineering problems to be applied in the
computer environment can be solved by approximating a data
model or by defining new data models.
10

Why Do We Need Data Structures?


- Computer software is becoming more and more complex day by day.
is taking place. Programming and management of software
becomes difficult.
- For example indexing 8 billion pages (Google)
- Data structures allow for more efficient and accurate
programming.
- Requirements for good software:
-a clean design
- Easy maintenance and management

- Trustworthy

- easy to use
- Fast algorithms
11th

Why Do We Need Data Structures?

- Sample

- Let's say there are 3000 text collections, each with an


average of 10 words per line and again an average
of 20 lines.
- →600,000 words
- Let's try to find all the words that will match
the word "world" in these texts.
-1 second of the comparison to be made for the correct match.
Let's assume it's on.
- What should be done?
12

Why Do We Need Data Structures?

- Sample

- Solution. one:

- Sequential matchmaking: 1 sec. x 600,000 words =166 hours


- Solution. 2nd:
- Binary searching:
-‐ words are ordered
-‐ only one half search is made
- total number of steps log 2nd N = log 2nd 600000 about 20 steps
(cycle) 20 sec.

-20 seconds or 166 hours!


13

Classification of Data Structures

- data structures,
- Basic Data Structures
- Defined (Composite) Data Structures

- Basicdata structures are mostly used by


programming languages by directly declaring
variables or constants.
- Defineddata structures build on predecessor
defined or basic data structures; that is, they are
subsequently defined using previously valid data
structures.
14

Basic 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

Defined Data Structures

- Defineddata structure is the creation of new data structures by using basic


or previously defined data structures. It can be done in three different
ways:
- Creating a Community (Struct): It is to bring together multiple data
structures and create a new data structure. (classes in Java language)
- Partnership (Union) Creation: A data structure definition that allows multiple
variables to use the same memory space. Whichever data structure occupies
the most space in the partnership, all variables in the partnership share it.

- Bitwise Access: It offers the ability to operate on each bit of


the data independently of the others.

- 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

Defined Data Structures

- Below is an example of defined data structures in C language.


17

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.

- Data models, in general, can be given as:


- Link List
- Tree (Tree)
- Graph
- State Machine
- Database-Relational
- Network Connection
Balancing Speed and Memory
Amount
18

List and Linked List Data Model

- 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

List and Linked List Data Model

- The simplest list data model is one held over a one-


dimensional array. Adding elements to such a list is fairly
easy; In general, newly arrived elements are added to the
end of the list. The next element in the plain list is
immediately after the memory space occupied by that
element.

-A linked list, on the other hand, is provided by using a link


information in addition to the values of the elements
themselves; connection information is the address of the
next element.
20

Tree Data Model


- The tree data model consists of nodes and branches; Nodes hold the
data themselves or some of it, while branches show the link
relationships to other nodes. The tree data model offers an effective
solution, especially in applications where the cluster is large and the
search operation is used a lot.
- The top node is called the root, the node that has no connection from
below is called the leaf, and the others are called the intermediate node
(internal node). Nodes connected to a node from below are called
children, and nodes connected from above are called parents of that
node.
21

Graph Data Model


- The graph data model consists of nodes, edges (edges) and their
combinations, as seen in the figure, of data belonging to the same cluster.
Nodes separate the junction point and show the connection relationship of
the nodes. The data themselves or some of them can be kept both in the
nodes and in the information part of the edges.
- Graphs have an important place in the software world. For example, problems
such as providing the highest flow from a city's traffic infrastructure, the most
efficient mode of transport of the transport company, or achieving high
performance in network connections.
22

State Machine Data Model


-Astate machine data model is an approach used to
describe and reveal the behavior of a system; In
operating systems, compilers and interpreters,
control software, it reduces the behavior of the
system to states and reveals the system with
transition conditions between states.
- Thestate machine can be used in many areas in software
application. For example, the movement of a robot arm is
the solution in cases of decoding, process control in real-
time operating systems, and in general, the control
subsystems successfully finalize the application with
software.
23

State Machine Data Model


- The state machine data model is similar in shape to a
directed graph, except that the junction points are not
nodes as in graphs. situation, the edges transition curves
is called. The transitions between states depend on the
state of the system up to that point and the input
parameters.
24

Relational Data Model in Database


- Thedatabase relational data model is one of four or five classes that
exist in database applications; data is based on relationships
established through tables as shown in the figure.
25

Relational Data Model in Database

- Querycan be made on the database using SQL (Structured Query


Language), query language. Many database management
systems such as Access, Microsoft SQL, ORACLE, SYBASE, Ingres
support the relational data model.

- Database management systems provide the successful completion


of tasks such as creating databases, creating tables, defining
fields, and database management in general.
26

Network Data Model

- In layered network architectures, the network data model reveals the


slice, packet, and frame structures that enable data exchange
between computers at the level of peer layers and defines the
behavior required for communication. Almost all architectures in data
communication are layered. All architectures are instance
representative OSI 1, reference model 7, TCP/IP (TransmissionNS
control Protocol / NSinternet Protocol) protocol set has 4 layers.
27

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

Data Model pros Cons


NS
Data Y ap lari:
add suck and very fast
Soap opera Slow search, slow delete and fixed size.
access(index known rsa). Faster
Hfast
search than unsorted array.
Sequence Slow erasing and fixed size.

Provides last-in, first-out


Stack Slow access to other items.
access.
Provides first-in, first-out
Tail Slow access to other items.
access.
Linked List Quick insertion and deletion. Slow search.
Quick insertion and very fast Slow delete, slow access when key is
Hash Table
access when the key is known. unknown, and inefficient memory usage.
Slow access to other items. Access to the
Cluster(Heap) Quick insertion and deletion.
largest item first.
Quick search, insert and delete(if the
Binary Tree The deletion algorithm is complex.
tree remained stable).
Ability to model real- Some algorithms are slow and
graph
world problems. complex.
algorithmic
Program Design
and Analysis
part 2

29
30

What is Algorithmic Program Design?


- It is the process of revealing a given problem step by
step to be solved in a computer environment and
coding it with any programming tool.
- The actions to be taken for the solution are expressed verbally
without allowing any alternative interpretation.
- It can also be defined as verbal expression of the data,
from the unit
which environment to enter into the computer, how to
which
solve the problem, how to get results by going through
the steps, how and where to write the result.
31

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

Coarse-Code (Pseudo Code)


- Rough-codeis the semi-programming rule, semi-
speech-oriented presentation, definition, and
expression of an algorithm.

- Rough-codeoften helps to design the algorithm in


general without relying on a data structure.
36

Actual Code
- It is the implementation of the algorithm on a specific data
structure with any programming language.

- The actual code of an algorithm only works on the data


structure for which it was designed.

- If an algorithm is given with a rough-code, it is easier to understand


than if it is given a real code.
37

Rough-code: basic notation


- one.Usually to assign a value " := " used, "=" sign is used for
equality check.
- 2nd.Method, function, procedure names:
Algorithm Name ({parameter list})
- 3. The program structure is defined as:
- Decision structures: if ... then ... else ...
- while loops: while ... do {loop body}
- Repeat loops: repeat {loop body} until ...
- for loops: for ... do {loop body}
- Array indexes: A[i]
- 4. Calling methods: Method name ({variable list})
- 5. Return from methods: return value
38

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

Brute-code and Real-Code


40

Algorithm Analysis
- Estimating the amount of resources that the algorithm will hear
during execution Algorithm Analysis is called.

- When the resource is mentioned, memory, communication bandwidth, logic


gates may come to mind, but the most important resource is the estimation
of the time required to execute the algorithm.

- Algorithm analysis is based on finding the mathematical expression of the


designed program or function according to a certain operation.

- 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

- Why do we analyze the algorithm?


- To measure the performance of the algorithm
- To compare with different algorithms
- Is better possible? Is it the best you can be?

- How do we analyze the algorithm?


- Runtime (Running Time)-T(n)
- Complexity -Notations
42

Runtime Analysis (Running Time)

- Working time; It is a relation that gives the


number of executions of functions considered
essential for a program or algorithm to perform
its function, and is denoted by T(n).

- As the basic unit of account, expressions in the programming


language can be selected, as well as the number of cycles, the
number of aggregation operations, the number of assignments,
the number of file accesses, as well as the basic unit of account.
43

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

one thousand Input data N


45

Summary of Working Times


N T1 T2

10 10-2nd sec 10-4 sec

one hundred 10-one sec 10-2nd sec

one thousand 1 sec 1 sec

10000 10 sec 100 sec

100000 100 sec 10000 sec

When n is less than 1000, the runtime between the two


algorithms is negligible.
46

Analysis

- Working time is difficult to pinpoint

- Best case dependent on input data (best case)


- average situation (average case); difficult to
calculate
- Worst case compared to others (worst
case); easy to calculate
- Various notations are used for this.
47

For Arithmetic Average T(n) account


- Example 1: Below is the one that finds the arithmetic mean of an array
and sends the result to the caller. findMiddle() The code of the
function is given. Showing the execution time of this functionT(n)
Determine the relation according to the discrete C language
statements.
find floatMiddle(float A[], int n) { {

float mean, sum=0; int k ;

one- for(k=0;k<n;k++)
2nd- sum+=A[k];
3- mean=total/n
4- return average;
}
48

For Arithmetic Average T(n) account


- Solution 1:
Unit Time Frequency(Repeat) Total
Basic Unit of Account
(Unit-Time) (frequency) (Total)

find floatMiddle(float A[], int n) { - - -


- - -
float mean, sum=0; int k ; - - -
- - -
one- for(k=0;k<n;k++) 1,1,1 1, (n+1), n 2n+2
2nd- sum+=A[k]; one n n
3- mean=total/n one one one
4- return average; one one one
} - - -
T(n)=3n+4
49

For Finding the Smallest Element T(n) account

- Example 2: Find the smallest element in an array belowfindEnLittle() The code


for a function named is shown. Showing the execution time of this function
T(n) Determine the relation according to the discrete C language statements.

- float findEnSmall(float A[]) {

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

For Finding the Smallest Element T(n) account


- Solution 2:
Unit Time Frequency(Repeat) Total
Basic Unit of Account (Unit-Time) (frequency) (Total)

find floatEnSmall(float A[]) { - - -

float size; - - -

int k ; - - -

one- size=A[0]; one one one

2nd- for(k=1;k<n;k++) 1,1,1 1, n, (n-1) 2n

3- if (A[k]<ink) one n-1 n-1

4- size=A[k]; one n-1 n-1

5- return enkucuk; one one one

} - - -

T(n)=4n
51

For Matrix Addition T(n) account


Unit Time Frequency(Repeat) Total
Basic Unit of Account
(Unit-Time) (frequency) (Total)

void sumMatrix (A,B,C) { - - -


int A[n][m], B[n][m], C[n][m]; - - -
int i,j ; - - -
one- for(i=0;i<n;i++) 1,1,1 1,(n+1),n 2n+2
2nd-
for(j=0;j<m;j++) 1,1,1 n(1,(m+1),m) n(2m+2)

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

T(n) General Account

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.

- Ingeneral, complexity is not of interest for a small number of


parameters; number of elementsnIt is used to see the behavior
of the algorithm's cost calculation if ' goes to infinity, or to
compare it with other algorithms doing similar work.

- complexity expression to be able to for asymptotic expressions

is used.

- Forthis purpose, definitions such as O(n) (O notation), -(n) (Omega


notation), θ(n) (Theta notation) are used.
Translated from Turkish to English - www.onlinedoctranslator.com

54

Complexity

-Strategy: Finding lower and upper limits

Upper limit – O(n)

The real function of the algorithm

Lower limit- - (n)


55

Common Functions Encountered


Big Oh Type of Change

O(1) Fixed, the command is run one or more times. Invincible!

Logarithmic, A large problem is solved by breaking down the original

O(logn) problem into smaller parts by constant fractions at each step. Typical time of

well-crafted search algorithms

Linear is done for each element in small problems. It is a fast


Front)
algorithm. The time required to enter N data. Logarithmic with linear

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

Front3) data is low (N<1000)

O(2n) Exponential in base two. Suitable when the amount of data is very small (N<=20)

O(10n) Exponential in base ten

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.

- T(n) - cf(n) and n - n0 providing the conditions NS and n0if


they have values T(n) - cf(n) statement is true.
- f(n) is called the asymptotic upper limit of T(n).
- T(n)=O(f(n))
Value of the function -

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.

- 2n+5 for n>=4 <= 1*n2nd


- c = 1, no = 4

- 2n+5 for n>=3 <= 2*n2nd


- c = 2, no = 3

- Other NS and n0 values can also be found.


59

Big-Oh(Big-Oh) Notation:
Asymptotic Upper Limit
- Example: T(n) = n(n+1)/2 - HE(?)

- T(n) = n2nd/2 + n/2 - FRONT2nd). Why?

-n when n >= 12nd/2 + n/2 <= n2nd/2 + n2nd/2 <= n2nd


- Thus, T(n)=n*(n+1)/2 <= one*n2nd for all n >=one
- c=1, no=1

- Note: T(n) is also O(n)3) type.


60

Importance of O Notation

- When comparing two algorithms, the time order is


discussed. It is easy to understand that the one with the
higher order is slower.

- Machines between difference, coefficients aspect


considering O(7n2nd) in its place Front2nd) statement is
valid. Therefore, the constants can be neglected and
gets rid of
61

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.

10 n2nd = 3n2nd + 2n2nd + 5n2nd

- 3n2nd n - 1 for + 2n + 5
NS = 10, n0 = one

How many provide the solution set n0 and NS It doesn't matter if


it's a couple. A single pair is sufficient for the accuracy of the
notation.
63

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

- T(n) = O(7n)2nd+5n+4) =O(n)2nd)


64

O notation- HEexample 3
- Writethe time spent by the functions in O
notation.

- f1(n) = 10 n + 25 n2nd • O(n)2nd)


- f2(n) = 20 n log n + 5 n • O(n log n)
- f3(n) = 12 n log n + 0.05 n2nd • O(n)2nd)
- f4(n) = n1/2 + 3 n log n • O(n log n)
65

Is the condition true?


66

- Notation - Asymptotic Lower Limit


(Best case analysis)
- It is the opposite of that notation.
- In every situation T(n) - cf(n) and n - n0 positive, constant NS and
providing
n0 if the values can be found T(n)=-(f(n))statement is true.

- f(n) is called the asymptotic lower limit of T(n).

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

– T(n) = 5*n2nd - 3*n - -(n2nd). Why?


– 4*n2nd <= 5*n2nd - 3*n <= 5*n2nd, for all n >=
4
71

It is impossible to arbitrarily determine


the value of N. because c2nd is fixed.
72
73

Big-Oh, Theta, Omega


- where n is large and constants are eliminated
- Considering O(f(n)) "less than or equal to" f(n)
- Top bound: "grows slowly or at the same rate" with f(n)

- Ω(If we consider f(n)) "equal or greater" with f(n)


- Bottom bound: "grows as fast or faster" as f(n)

- Θ(If we consider f(n)) "equal" with f(n)


- Bottom and Top border: growth rates are equal
74

Common Mistakes
- Don't just count loops to find complexity.

-1 to n of 2 nested loops2nd The complexity is O(n) if we consider that it


rotates by4) It is possible.

- O(2n2nd) or O(n2ndDo not use expressions like +n).


- Only the dominant term is used.
- The leading constants are removed.

- Nestedloops directly affect complexity, while


consecutive loops do not.
75

best time (Tbest)


- For an algorithm, the state in which the best result is
achieved in calculations of execution time, cost or
complexity is called the "best time".

- For example, the best case of a search in an array is when the


element being searched is the first element of the array.
76

Best time (Tbest)


Unit Time Frequency Total
(Unit-Time) (frequency) (Total)

i=1 one one one

while (a[i] ≠key) and (i≤ N) 2nd one 2nd

i++ one 0 0

if (a[i]=key) print found(or return i) 2nd one 2nd


else print not found(or return 0)

Tbest (N) = 5 = Ө(1)


77

worst time (Tworst)


- The worst time is the computation time required
for the algorithm to produce a solution if all
adverse conditions occur.

- For example, in the case of a search in an array, the worst


case is that the searched element is not in the array.
Because in order to understand that the searched element
is not in the array, it is necessary to look at all the
elements one by one.
78

worst time (Tworst)


Unit Time Frequency Total
(Unit-Time) (frequency) (Total)

i=1 one one one

while (a[i] ≠key) and (i≤ N) 2nd N+1 2N+2

i++ one N N

if (a[i]=key) print found(or return i) 2nd one 2nd


else print not found(or return 0)

Tworst (N) = 3N+5 = Ө(N)


T(N) ≠ Ө(N)
T(N) = O(N)
79

Average time (Taverage)

- Average time is the time spent in the resulting state when the
input parameters fall between the best and worst case.

- Thisrun time is the average of all inputs in each input size.


Assuming that each of the n elements has an equal probability of
being searched and no element will be searched outside the list,
the average run time is (n+1)/2. With the second assumption
removed, the average run time is in the range [(n+1)/2,n]
(depending on the tendency of the searched elements to be in
the list). Mean-state analysis is difficult even when simple
assumptions are made, and these assumptions can lead to poor
estimates of actual performance.
80

Average time (Taverage)

Unit Time Frequency Total


(Unit-Time) (frequency) (Total)

i=1 one one one

while (a[i] ≠key) and (i≤ N) i+ 2nd k+1 2k+2

+ one k k

if (a[i]=key) print found(or return i)


2nd one 2nd
else print not found(or return 0)

=3k+5
81

Average time (Taverage)

N -one one
one
Taverage (N ) - - (3k - 5) + (3N - 5)
k -0 2ndN 2nd

one one one one one


Taverage (N ) - 5 - (3 - 5) - (3.2 - 5) - ... (3(N -1) - 5) - (3N - 5)
2ndN 2ndN 2ndN 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

Some Rules in Algorithm Analysis

- 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

Some Rules in Algorithm Analysis


- For Loop:

T(n)=c1*N+ (c2+c3) *N3


84

Some Rules in Algorithm Analysis


- For Loop:
Consecutive

your idioms
total execution
your time
to find
just collecting
makes.
85

Some Rules in Algorithm Analysis


86

An Example in Algorithm Analysis:


Binary Search Algorithm
- The binary search algorithm is a search method used
in ordered arrays. The algorithm can be iterative or
recursive.
- Iteration statements (loops) allow given instructions to
be executed until a certain condition is met. The
specified condition can be predetermined, as in the
"for" loop, or it may be undefined and open-ended, as
in the "while-do" loop.
- Belowis an example of an iteratively
designed binary search algorithm.
87
Example: Binary Search

- 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

0 one 2nd 3 4 6 7 8 11th 15


3 8 10 11th 20 50 55 60 65 70 72 90 91 94 96 99

left middle right Eliminated


88
Binary search (continued)

0 one 2nd 3 4 5 6 7 8 11th 15


3 8 10 11th 20 50 55 60 65 70 72 90 91 94 96 99

Eliminated
left middle

0 one 2nd 3 4 5 6 7 8 11th 15


3 8 10 11th 20 50 55 60 65 70 72 90 91 94 96 99

Eliminated

middle

- We found 55- Successful search


- When we call 57, it will terminate unsuccessfully on the next
transaction.
89
Binary Search (continued)

< target ? > target

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

from the search area.


Binary Search - Algorithm 90

// Returns the index of the searched number and returns -1 if the searched number is not found.int

BinarySearch(int A[], int N, int number){

left = 0;
right = N-1;

while (left <= right){


int middle = (left+right)/2; // Index of the number to be tested

if (A[middle] == number) return middle; // Searched number found. Return index else if

(number < A[middle]) right = middle – 1; // Take the right

else left = side // Take the left side

middle+1; } //done-while

return -1; // The searched number was not found

} //done-binarySearch

• Worst runtime: T(n) = 3 + 5*log2ndn. Why?


91

An Example in Algorithm Analysis: Binary Search


Algorithm
92

An Example in Algorithm Analysis: Binary


Search Algorithm
- There are N elements in the first run.
N N
- at the 2nd N - 3.also -... - 1- 0
2ndone 2nd3 2ndk
- From the above expression N=2k is understood to
be. In this case, it is determined as k=logN. Since k
will be 0 for N=1,
- Tworst will be =logN+1.
93

Homework

- Do research on other notations and apply them to the


examples in the lecture notes.
- Apply all notations to different algorithms (T(n) calculation)
and interpret the results. (At least 5 algorithms)
- Assignments will arrive next week as Word documents, slides and
printouts. Assignments submitted outside the deadline will not be
accepted.

- In the homework report, the person who prepared it, the working time, the resources he

found and his contribution will be written.

- Assignment will be received with signature.


94

Data Structures - Chapter Summary


- Datamodels and their data structures are fundamental points of software
development; It is close to the nature of the algorithmic expression in
order to solve the problems in the most effective way. In short, data
structures show the pattern of storing data, and data models show the
relationship between data.

- Data models that are known and frequently used in


solutions can be given as link list, tree, graph, state
machine, network and database-relation. .

- 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

int Gather(int A[], int N) { - This function


what is the execution time
int total = 0; how much?

for (i=0; i < N; i++){


total += A[i];
} //done-for

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;

for (i=0; i < N; i++){ N


sum += A[i];
N
} //done-for

return collect; one


} //Done-Collect
--------

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

while (i < N){ 1<=L<=N


if (A[i] == number) break; i++;
1<=L<=N
} //done-while 0<=L<=N

if (i < N) return i; else one


return -1; one
} //done-Search
---------

Total: 1+3*L+1+1 = 3L+3


99

Example II:
Searching for an element in an array

- What is the best runtime?


- The loop only ran once=>T(n) = 6

- What is the average (expected) uptime?


- Loop N/2 times it worked =>T(n)=3*n/2+3 = 1.5n+3

- What is the worst runtime?


- Loop N times worked =>T(n) = 3n+3
one hundred

Example III: Matrix Multiplication

/* Two-dimensional array A, B, Calculate C = A*B */


C. for (i=0; i<N; i++) {
for (j=0; j<N; j++) {
C[i][j] = 0;
for (int k=0; k<N; k++){
C[i][j] += A[i][k]*B[k]
[j]; } //done-inner for } //
done-inner for
} //done-outer for
N -one N -one

T (N ) - --(one--one) - N 3 - N 2nd
N -one

NS-0 j-0 k -0
101

Some Mathematical Expressions


N
N (N -one)
S(N) = 1 + 2 + 3 + 4 + … N =
- NS -
2nd
NS-one

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

Some Mathematical Expressions

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

Some Mathematical Expressions


- The sum of the numbers between the two borders:

b b a-one

- f (NS) -- f (NS) -- f (NS)


NS-a NS-0 NS-0

n n n

- (4NS 2nd - 6NS) - 4-NS 2nd - 6-NS


NS-one NS-one NS-one

You might also like