SlideShare a Scribd company logo
1 of 33
Download to read offline
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
More Algorithm
Analysis
More Algorithm Analysis page 2
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Examples of Analyzing Code:
 We now go over many examples of code
fragments
 Each of these methods will be analyzed for their
runtime in terms of the variable n
 Utilizing the idea of Big-O,
 determine the Big-O running time of each
More Algorithm Analysis page 3
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 1:
 Determine the Big O running time of the following
code fragment:
for (k = 1; k <= n/2; k++) {
sum = sum + 5;
}
for (j = 1; j <= n*n; j++) {
delta = delta + 1;
}
More Algorithm Analysis page 4
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 1:
 So look at what’s going on in the code:
 We care about the total number of REPETITIVE
operations.
 Remember, we said we care about the running time for
LARGE values of n
 So in a for loop with n as part of the comparison value
determining when to stop
 Whatever is INSIDE that loop will be executed a LOT of times
 So we examine the code within this loop and see how many
operations we find
 When we say operations, we’re referring to mathematical
operations such as +, -, *, /, etc.
for (k=1; k<=n/2; k++)
More Algorithm Analysis page 5
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 1:
 So look at what’s going on in the code:
 The number of operations executed by these loops is
the sum of the individual loop operations.
 We have 2 loops,
 The first loop runs n/2 times
 Each iteration of the first loop results in one operation
 The + operation in: sum = sum + 5;
 So there are n/2 operations in the first loop
 The second loop runs n2 times
 Each iteration of the second loop results in one operation
 The + operation in: delta = delta + 1;
 So there are n2 operations in the second loop.
More Algorithm Analysis page 6
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 1:
 So look at what’s going on in the code:
 The number of operations executed by these loops is
the sum of the individual loop operations.
 The first loop has n/2 operations
 The second loop has n2 operations
 They are NOT nested loops.
 One loop executes AFTER the other completely finishes
 So we simply ADD their operations
 The total number of operations would be n/2 + n2
 In Big-O terms, we can express the number of
operations as O(n2)
More Algorithm Analysis page 7
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 2:
 Determine the Big O running time of the following
code fragment:
int func1(int n) {
int i, j, x = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
x++;
}
}
return x;
}
More Algorithm Analysis page 8
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 2:
 So look at what’s going on in the code:
 We care about the total number of REPETITIVE
operations
 We have two loops
 AND they are NESTED loops
 The outer loop runs n times
 From i = 1 up through n
 How many operations are performed at each iteration?
 Answer is coming…
 The inner loop runs n times
 From j = 1 up through n
 And only one operation (x++) is performed at each iteration
More Algorithm Analysis page 9
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 2:
 So look at what’s going on in the code:
 Let’s look at a couple of iterations of the OUTER loop:
 When i = 1, what happens?
 The inner loop runs n times
 Resulting in n operations from the inner loop
 Then, i gets incremented and it becomes equal to 2
 When i = 2, what happens?
 Again, the inner loop runs n times
 Again resulting in n operations from the inner loop
 We notice the following:
 For EACH iteration of the OUTER loop,
 The INNER loop runs n times
 Resulting in n operations
More Algorithm Analysis page 10
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 2:
 So look at what’s going on in the code:
 And how many times does the outer loop run?
 n times
 So the outer loop runs n times
 And for each of those n times, the inner loop also runs
n times
 Resulting in n operations
 So we have n operations per iteration of OUTER loop
 And outer loop runs n times
 Finally, we have n*n as the number of operations
 We approximate the running time as O(n2)
More Algorithm Analysis page 11
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 3:
 Determine the Big O running time of the following
code fragment:
int func3(int n) {
int i, x = 0;
for (i = 1; i <= n; i++)
x++;
for (i = 1; i<=n; i++)
x++;
return x;
}
More Algorithm Analysis page 12
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 3:
 So look at what’s going on in the code:
 We care about the total number of REPETITIVE
operations
 We have two loops
 They are NOT nested loops
 The first loop runs n times
 From i = 1 up through n
 only one operation (x++) is performed at each iteration
 How many times does the second loop run?
 Notice that i is indeed reset to 1 at the beginning of the loop
 Thus, the second loop runs n times, from i = 1 up through n
 And only one operation (x++) is performed at each iteration
More Algorithm Analysis page 13
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 3:
 So look at what’s going on in the code:
 Again, the loops are NOT nested
 So they execute sequentially (one after the other)
 Therefore:
 Our total runtime is on the order of n+n
 Which of course equals 2n
 Now, in Big O notation
 We approximate the running time as O(n)
More Algorithm Analysis page 14
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 4:
 Determine the Big O running time of the following
code fragment:
int func4(int n) {
while (n > 0) {
printf(“%d”, n%2);
n = n/2;
}
}
More Algorithm Analysis page 15
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 4:
 So look at what’s going on in the code:
 We have one while loop
 You can’t just look at this loop and say it iterates n times or
n/2 times
 Rather, it continues to execute as long as n is greater than 0
 The question is: how many iterations will that be?
 Within the while loop
 The last line of code divides the input, n, by 2
 So n is halved at each iteration of the while loop
 If you remember, we said this ends up running in log n
time
 Now let’s look at how this works
More Algorithm Analysis page 16
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 4:
 So look at what’s going on in the code:
 For the ease of the analysis, we make a new variable
 originalN:
 originalN refers to the value originally stored in the input, n
 So if n started at 100, originalN will be equal to 100
 The first time through the loop
 n gets set to originalN/2
 If the original n was 100, after one iteration n would be 100/2
 The second time through the loop
 n gets set to originalN/4
 The third time through the loop
 n gets set to originalN/8
Notice:
After three iterations, n
gets set to originalN/23
More Algorithm Analysis page 17
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 4:
 So look at what’s going on in the code:
 In general, after k iterations
 n gets set to originalN/2k
 The algorithm ends when originalN/2k = 1, approximately
 We now solve for k
 Why?
 Because we want to find the total # of iterations
 Multiplying both sides by 2k, we get originalN = 2k
 Now, using the definition of logs, we solve for k
 k = log originalN
 So we approximate the running time as O(log n)
More Algorithm Analysis page 18
© Dr. Jonathan (Yahya) Cazalas
Brief Interlude: Human Stupidity
More Algorithm Analysis page 19
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 5:
 Determine the Big O running time of the following
code fragment:
int func5(int[][] array, int n) {
int i = 0, j = 0;
while (i < n) {
while (j < n && array[i][j] == 1)
j++;
i++;
}
return j;
}
More Algorithm Analysis page 20
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 5:
 So look at what’s going on in the code:
 At first glance, we see two NESTED loops
 This can often indicate an O(n2) algorithm
 But we need to look closer to confirm
 Focus on what’s going on with i and j
int func5(int[][] array, int n) {
int i = 0, j = 0;
while (i < n) {
while (j < n && array[i][j] == 1)
j++;
i++;
}
More Algorithm Analysis page 21
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 5:
 So look at what’s going on in the code:
 Focus on what’s going on with i and j
 i and j clearly increase (from the j++ and i++)
 BUT, they never decrease
 AND, neither ever gets reset to 0
int func5(int[][] array, int n) {
int i = 0, j = 0;
while (i < n) {
while (j < n && array[i][j] == 1)
j++;
i++;
}
More Algorithm Analysis page 22
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 5:
 So look at what’s going on in the code:
 And the OUTER while loop ends once i gets to n
 So, what does this mean?
 The statement i++ can never run more than n times
 And the statement j++ can never run more than n times
int func5(int[][] array, int n) {
int i = 0, j = 0;
while (i < n) {
while (j < n && array[i][j] == 1)
j++;
i++;
}
More Algorithm Analysis page 23
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 5:
 So look at what’s going on in the code:
 The MOST number of times these two statements can
run (combined) is 2n times
 So we approximate the running time as O(n)
int func5(int[][] array, int n) {
int i = 0, j = 0;
while (i < n) {
while (j < n && array[i][j] == 1)
j++;
i++;
}
More Algorithm Analysis page 24
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 6:
 Determine the Big O running time of the following
code fragment:
 What’s the one big difference here???
int func6(int[][] array, int n) {
int i = 0, j;
while (i < n) {
j = 0;
while (j < n && array[i][j] == 1)
j++;
i++;
}
return j;
}
More Algorithm Analysis page 25
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 6:
 So look at what’s going on in the code:
 The difference is that we RESET j to 0 a the beginning
of the OUTER while loop
int func6(int[][] array, int n) {
int i = 0, j;
while (i < n) {
j = 0;
while (j < n && array[i][j] == 1)
j++;
i++;
}
return j;
}
More Algorithm Analysis page 26
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 6:
 So look at what’s going on in the code:
 The difference is that we RESET j to 0 a the beginning
of the OUTER while loop
 How does that change things?
 Now j can iterate from 0 to n for EACH iteration of the
OUTER while loop
 For each value of i
 This is similar to the 2nd example shown
 So we approximate the running time as O(n2)
More Algorithm Analysis page 27
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 7:
 Determine the Big O running time of the following
code fragment:
int func7(int A[], int sizeA, int B[], int sizeB) {
int i, j;
for (i = 0; i < sizeA; i++)
for (j = 0; j < sizeB; j++)
if (A[i] == B[j])
return 1;
return 0;
}
More Algorithm Analysis page 28
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 7:
 So look at what’s going on in the code:
 First notice that the runtime here is NOT in terms of n
 It will be in terms of sizeA and sizeB
 And this is also just like Example 2
 The outer loop runs sizeA times
 For EACH of those times,
 The inner loop runs sizeB times
 So this algorithm runs sizeA*sizeB times
 We approximate the running time as O(sizeA*sizeB)
More Algorithm Analysis page 29
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 8:
 Determine the Big O running time of the following
code fragment:
int func8(int A[], int sizeA, int B[], int sizeB) {
int i, j;
for (i = 0; i < sizeA; i++) {
if (binSearch(B, sizeB, A[i]))
return 1;
}
return 0;
}
More Algorithm Analysis page 30
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
 Example 8:
 So look at what’s going on in the code:
 Note: we see that we are calling the function binSearch
 As discussed previously, a single binary search runs in
O(log n) time
 where n represents the number of items within which you are
searching
 Examining the for loop:
 The for loop will execute sizeA times
 For EACH iteration of this loop
 a binary search will be run
 We approximate the running time as O(sizeA*log(sizeB))
More Algorithm Analysis page 31
© Dr. Jonathan (Yahya) Cazalas
More Algorithm Analysis
WASN’T
THAT
SWEET!
More Algorithm Analysis page 32
© Dr. Jonathan (Yahya) Cazalas
Daily Demotivator
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
More Algorithm
Analysis

More Related Content

What's hot (20)

Recursion
RecursionRecursion
Recursion
 
Lec10
Lec10Lec10
Lec10
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Big o
Big oBig o
Big o
 
Lec10
Lec10Lec10
Lec10
 
5.2 primitive recursive functions
5.2 primitive recursive functions5.2 primitive recursive functions
5.2 primitive recursive functions
 
Lec4
Lec4Lec4
Lec4
 
Primitive Recursive Functions
Primitive Recursive FunctionsPrimitive Recursive Functions
Primitive Recursive Functions
 
Lec1
Lec1Lec1
Lec1
 
Primality
PrimalityPrimality
Primality
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
The Complexity Of Primality Testing
The Complexity Of Primality TestingThe Complexity Of Primality Testing
The Complexity Of Primality Testing
 
An Introduction to ECDSA and it's use in Bitcoin (1)
An Introduction to ECDSA and it's use in Bitcoin (1)An Introduction to ECDSA and it's use in Bitcoin (1)
An Introduction to ECDSA and it's use in Bitcoin (1)
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
 
Rsa encryption
Rsa encryptionRsa encryption
Rsa encryption
 
Big oh Representation Used in Time complexities
Big oh Representation Used in Time complexitiesBig oh Representation Used in Time complexities
Big oh Representation Used in Time complexities
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
Ch10 Recursion
Ch10 RecursionCh10 Recursion
Ch10 Recursion
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 

Similar to algorithm_analysis2

Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptxabcdefgh690537
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptxEnosSalar
 
Lecture 03 algorithm analysis
Lecture 03 algorithm analysisLecture 03 algorithm analysis
Lecture 03 algorithm analysisNurjahan Nipa
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxdickonsondorris
 
18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)AdityaKhandelwal58
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm AnalysisMegha V
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesHaitham El-Ghareeb
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithmsrajatmay1992
 
008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdf008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdfomchoubey297
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithmsNico Ludwig
 

Similar to algorithm_analysis2 (20)

Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptx
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Lecture 03 algorithm analysis
Lecture 03 algorithm analysisLecture 03 algorithm analysis
Lecture 03 algorithm analysis
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
 
18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdf008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdf
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithms
 

More from Mohamed Elsayed (20)

binary_trees3
binary_trees3binary_trees3
binary_trees3
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
binary_trees1
binary_trees1binary_trees1
binary_trees1
 
queues
queuesqueues
queues
 
stacks2
stacks2stacks2
stacks2
 
stacks1
stacks1stacks1
stacks1
 
recursion1
recursion1recursion1
recursion1
 
linked_lists4
linked_lists4linked_lists4
linked_lists4
 
linked_lists3
linked_lists3linked_lists3
linked_lists3
 
Linked_lists2
Linked_lists2Linked_lists2
Linked_lists2
 
linked_lists
linked_listslinked_lists
linked_lists
 
sorted_listmatch
sorted_listmatchsorted_listmatch
sorted_listmatch
 
binary_search
binary_searchbinary_search
binary_search
 
arrays
arraysarrays
arrays
 
introduction
introductionintroduction
introduction
 
heaps2
heaps2heaps2
heaps2
 
heaps
heapsheaps
heaps
 
hash_tables
hash_tableshash_tables
hash_tables
 
graphs
graphsgraphs
graphs
 
quick_sort
quick_sortquick_sort
quick_sort
 

Recently uploaded

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

algorithm_analysis2

  • 1. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I More Algorithm Analysis
  • 2. More Algorithm Analysis page 2 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Examples of Analyzing Code:  We now go over many examples of code fragments  Each of these methods will be analyzed for their runtime in terms of the variable n  Utilizing the idea of Big-O,  determine the Big-O running time of each
  • 3. More Algorithm Analysis page 3 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 1:  Determine the Big O running time of the following code fragment: for (k = 1; k <= n/2; k++) { sum = sum + 5; } for (j = 1; j <= n*n; j++) { delta = delta + 1; }
  • 4. More Algorithm Analysis page 4 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 1:  So look at what’s going on in the code:  We care about the total number of REPETITIVE operations.  Remember, we said we care about the running time for LARGE values of n  So in a for loop with n as part of the comparison value determining when to stop  Whatever is INSIDE that loop will be executed a LOT of times  So we examine the code within this loop and see how many operations we find  When we say operations, we’re referring to mathematical operations such as +, -, *, /, etc. for (k=1; k<=n/2; k++)
  • 5. More Algorithm Analysis page 5 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 1:  So look at what’s going on in the code:  The number of operations executed by these loops is the sum of the individual loop operations.  We have 2 loops,  The first loop runs n/2 times  Each iteration of the first loop results in one operation  The + operation in: sum = sum + 5;  So there are n/2 operations in the first loop  The second loop runs n2 times  Each iteration of the second loop results in one operation  The + operation in: delta = delta + 1;  So there are n2 operations in the second loop.
  • 6. More Algorithm Analysis page 6 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 1:  So look at what’s going on in the code:  The number of operations executed by these loops is the sum of the individual loop operations.  The first loop has n/2 operations  The second loop has n2 operations  They are NOT nested loops.  One loop executes AFTER the other completely finishes  So we simply ADD their operations  The total number of operations would be n/2 + n2  In Big-O terms, we can express the number of operations as O(n2)
  • 7. More Algorithm Analysis page 7 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 2:  Determine the Big O running time of the following code fragment: int func1(int n) { int i, j, x = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { x++; } } return x; }
  • 8. More Algorithm Analysis page 8 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 2:  So look at what’s going on in the code:  We care about the total number of REPETITIVE operations  We have two loops  AND they are NESTED loops  The outer loop runs n times  From i = 1 up through n  How many operations are performed at each iteration?  Answer is coming…  The inner loop runs n times  From j = 1 up through n  And only one operation (x++) is performed at each iteration
  • 9. More Algorithm Analysis page 9 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 2:  So look at what’s going on in the code:  Let’s look at a couple of iterations of the OUTER loop:  When i = 1, what happens?  The inner loop runs n times  Resulting in n operations from the inner loop  Then, i gets incremented and it becomes equal to 2  When i = 2, what happens?  Again, the inner loop runs n times  Again resulting in n operations from the inner loop  We notice the following:  For EACH iteration of the OUTER loop,  The INNER loop runs n times  Resulting in n operations
  • 10. More Algorithm Analysis page 10 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 2:  So look at what’s going on in the code:  And how many times does the outer loop run?  n times  So the outer loop runs n times  And for each of those n times, the inner loop also runs n times  Resulting in n operations  So we have n operations per iteration of OUTER loop  And outer loop runs n times  Finally, we have n*n as the number of operations  We approximate the running time as O(n2)
  • 11. More Algorithm Analysis page 11 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 3:  Determine the Big O running time of the following code fragment: int func3(int n) { int i, x = 0; for (i = 1; i <= n; i++) x++; for (i = 1; i<=n; i++) x++; return x; }
  • 12. More Algorithm Analysis page 12 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 3:  So look at what’s going on in the code:  We care about the total number of REPETITIVE operations  We have two loops  They are NOT nested loops  The first loop runs n times  From i = 1 up through n  only one operation (x++) is performed at each iteration  How many times does the second loop run?  Notice that i is indeed reset to 1 at the beginning of the loop  Thus, the second loop runs n times, from i = 1 up through n  And only one operation (x++) is performed at each iteration
  • 13. More Algorithm Analysis page 13 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 3:  So look at what’s going on in the code:  Again, the loops are NOT nested  So they execute sequentially (one after the other)  Therefore:  Our total runtime is on the order of n+n  Which of course equals 2n  Now, in Big O notation  We approximate the running time as O(n)
  • 14. More Algorithm Analysis page 14 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 4:  Determine the Big O running time of the following code fragment: int func4(int n) { while (n > 0) { printf(“%d”, n%2); n = n/2; } }
  • 15. More Algorithm Analysis page 15 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 4:  So look at what’s going on in the code:  We have one while loop  You can’t just look at this loop and say it iterates n times or n/2 times  Rather, it continues to execute as long as n is greater than 0  The question is: how many iterations will that be?  Within the while loop  The last line of code divides the input, n, by 2  So n is halved at each iteration of the while loop  If you remember, we said this ends up running in log n time  Now let’s look at how this works
  • 16. More Algorithm Analysis page 16 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 4:  So look at what’s going on in the code:  For the ease of the analysis, we make a new variable  originalN:  originalN refers to the value originally stored in the input, n  So if n started at 100, originalN will be equal to 100  The first time through the loop  n gets set to originalN/2  If the original n was 100, after one iteration n would be 100/2  The second time through the loop  n gets set to originalN/4  The third time through the loop  n gets set to originalN/8 Notice: After three iterations, n gets set to originalN/23
  • 17. More Algorithm Analysis page 17 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 4:  So look at what’s going on in the code:  In general, after k iterations  n gets set to originalN/2k  The algorithm ends when originalN/2k = 1, approximately  We now solve for k  Why?  Because we want to find the total # of iterations  Multiplying both sides by 2k, we get originalN = 2k  Now, using the definition of logs, we solve for k  k = log originalN  So we approximate the running time as O(log n)
  • 18. More Algorithm Analysis page 18 © Dr. Jonathan (Yahya) Cazalas Brief Interlude: Human Stupidity
  • 19. More Algorithm Analysis page 19 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 5:  Determine the Big O running time of the following code fragment: int func5(int[][] array, int n) { int i = 0, j = 0; while (i < n) { while (j < n && array[i][j] == 1) j++; i++; } return j; }
  • 20. More Algorithm Analysis page 20 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 5:  So look at what’s going on in the code:  At first glance, we see two NESTED loops  This can often indicate an O(n2) algorithm  But we need to look closer to confirm  Focus on what’s going on with i and j int func5(int[][] array, int n) { int i = 0, j = 0; while (i < n) { while (j < n && array[i][j] == 1) j++; i++; }
  • 21. More Algorithm Analysis page 21 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 5:  So look at what’s going on in the code:  Focus on what’s going on with i and j  i and j clearly increase (from the j++ and i++)  BUT, they never decrease  AND, neither ever gets reset to 0 int func5(int[][] array, int n) { int i = 0, j = 0; while (i < n) { while (j < n && array[i][j] == 1) j++; i++; }
  • 22. More Algorithm Analysis page 22 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 5:  So look at what’s going on in the code:  And the OUTER while loop ends once i gets to n  So, what does this mean?  The statement i++ can never run more than n times  And the statement j++ can never run more than n times int func5(int[][] array, int n) { int i = 0, j = 0; while (i < n) { while (j < n && array[i][j] == 1) j++; i++; }
  • 23. More Algorithm Analysis page 23 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 5:  So look at what’s going on in the code:  The MOST number of times these two statements can run (combined) is 2n times  So we approximate the running time as O(n) int func5(int[][] array, int n) { int i = 0, j = 0; while (i < n) { while (j < n && array[i][j] == 1) j++; i++; }
  • 24. More Algorithm Analysis page 24 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 6:  Determine the Big O running time of the following code fragment:  What’s the one big difference here??? int func6(int[][] array, int n) { int i = 0, j; while (i < n) { j = 0; while (j < n && array[i][j] == 1) j++; i++; } return j; }
  • 25. More Algorithm Analysis page 25 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 6:  So look at what’s going on in the code:  The difference is that we RESET j to 0 a the beginning of the OUTER while loop int func6(int[][] array, int n) { int i = 0, j; while (i < n) { j = 0; while (j < n && array[i][j] == 1) j++; i++; } return j; }
  • 26. More Algorithm Analysis page 26 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 6:  So look at what’s going on in the code:  The difference is that we RESET j to 0 a the beginning of the OUTER while loop  How does that change things?  Now j can iterate from 0 to n for EACH iteration of the OUTER while loop  For each value of i  This is similar to the 2nd example shown  So we approximate the running time as O(n2)
  • 27. More Algorithm Analysis page 27 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 7:  Determine the Big O running time of the following code fragment: int func7(int A[], int sizeA, int B[], int sizeB) { int i, j; for (i = 0; i < sizeA; i++) for (j = 0; j < sizeB; j++) if (A[i] == B[j]) return 1; return 0; }
  • 28. More Algorithm Analysis page 28 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 7:  So look at what’s going on in the code:  First notice that the runtime here is NOT in terms of n  It will be in terms of sizeA and sizeB  And this is also just like Example 2  The outer loop runs sizeA times  For EACH of those times,  The inner loop runs sizeB times  So this algorithm runs sizeA*sizeB times  We approximate the running time as O(sizeA*sizeB)
  • 29. More Algorithm Analysis page 29 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 8:  Determine the Big O running time of the following code fragment: int func8(int A[], int sizeA, int B[], int sizeB) { int i, j; for (i = 0; i < sizeA; i++) { if (binSearch(B, sizeB, A[i])) return 1; } return 0; }
  • 30. More Algorithm Analysis page 30 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis  Example 8:  So look at what’s going on in the code:  Note: we see that we are calling the function binSearch  As discussed previously, a single binary search runs in O(log n) time  where n represents the number of items within which you are searching  Examining the for loop:  The for loop will execute sizeA times  For EACH iteration of this loop  a binary search will be run  We approximate the running time as O(sizeA*log(sizeB))
  • 31. More Algorithm Analysis page 31 © Dr. Jonathan (Yahya) Cazalas More Algorithm Analysis WASN’T THAT SWEET!
  • 32. More Algorithm Analysis page 32 © Dr. Jonathan (Yahya) Cazalas Daily Demotivator
  • 33. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I More Algorithm Analysis