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

Week 1 - Complexity Analysis

This document provides an overview of data structures and complexity analysis for a class on data structures. It outlines the class rules, marking scheme, schedule, and topics to be covered. Some key topics include complexity analysis, common data structures like arrays, stacks, queues, and trees. It discusses measuring performance using asymptotic analysis and big-O notation. Examples are provided to demonstrate calculating time complexity of algorithms that use different data structures like searching in an unsorted vs sorted array. The overall goal is for students to learn techniques for solving problems efficiently by applying appropriate data structures and analyzing time complexity.

Uploaded by

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

Week 1 - Complexity Analysis

This document provides an overview of data structures and complexity analysis for a class on data structures. It outlines the class rules, marking scheme, schedule, and topics to be covered. Some key topics include complexity analysis, common data structures like arrays, stacks, queues, and trees. It discusses measuring performance using asymptotic analysis and big-O notation. Examples are provided to demonstrate calculating time complexity of algorithms that use different data structures like searching in an unsorted vs sorted array. The overall goal is for students to learn techniques for solving problems efficiently by applying appropriate data structures and analyzing time complexity.

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Data Structure

Complexity Analysis

Week 1
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Syllabus and Class Rules
Class Rules

• All slides can be downloaded from ULS


• Don’t be late. The important explanation sometimes start
in the beginning of the lesson
• Plagiarism is a very BIG NO. If you give your work to
your friend and it is copied by your friend, and we know
that both works are the same, then BOTH of you will get
0 in ALL of your mark (you will FAIL in Data Structure).
Marking

• NTS = 20% Exercise + 30% Quiz + 50% Mid Exam


(UTS)
• NAS = 20% Exercise + 30% Quiz + 50% Final Exam
(UAS)

• Remember!!! The minimum passing grade for all courses


in the 2021 Curriculum is C.
Our Schedule
• Before Mid Exam • Before Final Exam
➔ Week 1 : Complexity Analysis ➔ Week 8 : Linked List
➔ Week 2 : Array, ArrayList, Binary ➔ Week 9 : Dictionary and
Search Recursive
➔ Week 3 : Queue ➔ Week 10 : Binary Search Tree
➔ Week 4 : Quiz ➔ Week 11 : Quiz
➔ Week 5 : Stack ➔ Week 12 : AVL Tree
➔ Week 6 : Hashing and Collision ➔ Week 13 : Sorting Algorithm
Resolution ➔ Week 14 : Final Exam
➔ Week 7 : Mid Exam Preparation Preparation
Let’s Start the Course
A GOOD PROGRAM MUST

• Run Correctly (Alpro)


• Easy to be read and understood (OOP)
• Easy to debug (OOP)
• Easy to modify (OOP)
• Run Efficiently (Data Structure)
WHAT WILL YOU LEARN?

• Techniques to solve problems efficiently by applying the


appropriate DATA STRUCTURES by analyzing time
complexity
Data structure

• A data structure is a specialized format for organizing


and storing data.
• Data structures are classes that are used to organize data
and provide various operations upon their data.
• Probably the most common and well-known data
structure is the array and list, which contains a
contiguous collection of data items that can be accessed
by an ordinal index.
SOME DATA STRUCTURES

• Linear list (array)


• Stack (Last In, First Out – LIFO)
• Queue (First In, First Out – FIFO)
• Linked List
• Tree
• Hash Table
SOME ALGORITHMS
• Recursive
• Sorting
– Insertion Sort
– Selection Sort
– Exchange Sort
– Tree Sort
• Searching
– Linear Search
– Binary Search
COMPLEXITY ANALYSIS

• Data structures used for a particular algorithms can


greatly impact the program performance
• Performance of data structures can differ significantly if
it’s used for a large number of data
• In real world, a program is commonly deal with large
number of data
• Can you guarantee that your program can run as fast as
you thought for 1 millions or 1 billions data?
EXAMPLE: SEARCH FOR AN ITEM

• Example: find an element in a certain collection


• What is the performance of your program if you use array
(unsorted vs. sorted) / tree / hash table to store the data?
• Let’s see the performance of a program using array
SEARCH FOR AN ITEM IN AN ARRAY

int SearchItem(string[] myArray, int n, string key)


{
// n = number of data in the array
for (int i =0; i<n; i++)
{
if (myArray[i] == key)
{
//return the idx location where item found
return (i);
}
}
// the item cannot be found.
return -1;
}
SEARCH FOR AN ITEM IN AN ARRAY
Example:
myArray = {‘Orange’,’Grape’,’Apple’,’Mango’}
n = 4
key = ‘Apple’

Unsorted Array Sorted Array


i=0 Orange Not Found i=0 Apple Found
i=1 Grape Not Found
i=2 Apple Found Imagine if there are 100,000
records and "Apple" is in the last
index, how many iterations does
the program have to do if the
data is unsorted?
MEASURING DS PERFORMANCE
• Data structures performance can be measured by
calculating how many access need to be done to the
data structure (in this example, how many access need to
be performed to the array)
• This kind of analysis is called asymptotic analysis, with
a notation that is commonly called “Big-Oh notation”
• The performance of searching data in the previous array
is O(n) linear growth .
• This means that for a very large number of n data in the
array, there will be n number of steps required to search
the array.
MEASURING DS PERFORMANCE

• Asymptotic analysis is a method of describing limiting


behavior. The simplest example is, when considering a
function f(n), there is a need to describe its properties
when n becomes very large. Thus, if f(n) = n2+3n, the
term 3n becomes insignificant compared to n2 when n is
very large. The function "f(n) is said to be asymptotically
equivalent to n2 as n → ∞", and this is written
symbolically as f(n) ~ n2.
FINDING THE RUNNING TIME IN

• Determine the steps that constitute the algorithm’s


running time (with the array, the steps considered are the
read and write accessed to the array)
• Put a number to each of line(s) that perform the steps you
are interested in counting
• Check any loop for each line and calculate the number of
maximum number of repetition
• The largest single term that have been written down is the
running time
The item can be found in index i=0

Line Code Repetition

1 for (int i =0; i<n; i++) 1

2 if (myArray[i] == key) 1

3 return (i); 1
4 return (-1); 0

Total = 1+1+1+0 = 3 => Best case


The item can be found in last index

Example: n=3

Line Code Repetition

1 for (int i = 0; i<n; i++) 3 => n

2 if (myArray[i] == key) 3 => n

3 return (i); 1
4 return (-1); 0

Total = n+n+1+0 = 2n+1 => O(n)


The item can’t be found
Example: n=3

Line Code Repetition

1 for (int i = 0; i<n; i++) 4 => n+1

2 if (myArray[i] == key) 3 => n

3 return (i); 0
4 return (-1); 1

Total = n+1+n+0+1 = 2n+2 => O(n)worst case


Another Example
• Find Big-Oh from the piece of code below:

for ( int count= 2 ; count <=n ; count++ )


{
int factor = 0;
for ( int c = 1 ; c <= n ; c++ )
{
if ( c > 0 ) //assume always true
{
factor++;
}
}
}
Answer : Line Command Repetition
1 for ( int count = 2 ; count <=n ; n
count++ )
2 int factor = 0; n-1
3 for ( int c = 1 ; c <= n ; c++ ) (n-1) (n+1) = n2-1

Because this command is in code line 1, this


command is repeated (n-1) times. In addition, line 3 if
it stands alone will be repeated (n + 1) times.
4 if ( c > 0 ) (n-1) n = n2-n

Because this command is in code line 1, this


command is repeated (n-1) times. In addition, this
command is in code line 3, so it is repeated (n + 1) -1
or n times.
5 factor++; (n-1) n = n2-n

Because this command is in code line 1, this


command is repeated (n-1) times. In addition, this
command is in code line 3, so it is repeated (n + 1) -1
or n times. Even though this code is executed if line 4
is true, we assume line 4 will always true.
Answer :

Total time complexity


= n + (n-1) + (n2-1) + (n2-n) + (n2-n)
= 3 n2– 2

Big-Oh = O (n2)
RUNNING TIME IN ORDER

• O (1) : constant
– Real world case : Checks whether a number is odd or
even.
– Programming case : Retrieve the value in an array
• O (log n) : logarithmic
– Real world case : Look up the translation of the word "car"
in the dictionary
– Programming case : Binary search
RUNNING TIME IN ORDER

• O (n): linear
– Real world case : Move items one by one.
– Programming case : make a program that prints numbers
from 1-10.
• O (n log n): Linearithmic
– Real world case : Assemble puzzles.
– Programming case : Merge sort.
RUNNING TIME IN ORDER

• O (nk) : polynomial
– Real world case : Separating apples and pears, then
separating which fruits are unripe, ripe, and rotten.
– Programming case : Selection sort.
• O (2n) : exponential
– Real world case : Looking for a combination of the several
options available.
– Programming case : Searches for a subset of the array.
TIME NEEDED TO FINISH THE PROGRAM

• Looking at Big-Oh, one can estimate how long the program


will run to finish it’s task.
• For example, a certain program with an O(n) running time will
be run at a CPU with clock-speed 4 GHz (4x109
cycle/second).
• Each statement in the program can only be run in each cycle
of the CPU (therefore, this CPU can execute 4x109
statements/second).
TIME NEEDED TO FINISH THE PROGRAM

• Find the approximate time required to finish the program if


there are 1012 data in the array.
Answer
• O (n) for 1012 is 1012
• Time = 1012 / (4x109) = 1000 / 4 = 250 s = 4.17 m
Questions??
EXERCISE
• Find time complexity and Big-Oh from the piece of code below:
EXERCISE

• Write your answer on a paper (tulisan tangan sendiri).


• For those who have finished working, please collect your
answer to your lecturer and you may leave this class.

You might also like