Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
84 views

Analysis of Algorithm Chapter 1

The time complexity of heap sort is O(nlogn) in the average and worst cases. Heap sort works by first converting the input list into a max heap using the heapify operation, which takes O(n) time. It then repeatedly extracts the maximum element from the heap, exchanges it with the last element of the heapified portion of the list, and sifts the new root down to re-establish the heap property. Each extraction and sifting takes O(logn) time and there are n extractions, so this sorting phase takes O(nlogn) time. Therefore, the overall time complexity of heap sort is O(n) for the heapification process + O(nlogn

Uploaded by

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

Analysis of Algorithm Chapter 1

The time complexity of heap sort is O(nlogn) in the average and worst cases. Heap sort works by first converting the input list into a max heap using the heapify operation, which takes O(n) time. It then repeatedly extracts the maximum element from the heap, exchanges it with the last element of the heapified portion of the list, and sifts the new root down to re-establish the heap property. Each extraction and sifting takes O(logn) time and there are n extractions, so this sorting phase takes O(nlogn) time. Therefore, the overall time complexity of heap sort is O(n) for the heapification process + O(nlogn

Uploaded by

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

Introduction and elementary data

structures

Chapter 1
Part1

Analysis of Algorithms &


Orders of Growth
Algorithms
-Algorithm
–Characteristics of algorithm
–Algorithm analysis Introduction
Program=Algorithm+Datastructure
•Data Structure: –The way data are organized in a
computers memory.
•Algorithm: –The sequence of computational steps to
solve a problem.
Algorithms
• Algorithm Definition –is a well-defined
computational procedure that takes some value or a
set of values as input and produces some value or a
set of values as output
•An algorithm transforms data structures from one state
to an other state in two ways:
–An algorithm may change the value held by a data
structure
– An algorithm may change the data structure itself
Algorithms
• All algorithms must satisfy the following
criteria:
1) Input. Zero more quantities are externally
supplied.
2) Output. At least one quantity is produced.
3) Definiteness. Each instruction is clear and
unambiguous.
4) Finiteness. the algorithm terminates after a
finite number of steps.
5) Effectiveness. It is not enough that each
operation be definite as in 3) it also must be
feasible (achievable).
Analysis of Algorithm
• The term analysis of algorithms is used to describe
approaches to the study of the performance of
algorithms.
• What is the goal of analysis of algorithms?
– To compare algorithms mainly in terms of running
time but also in terms of other factors (e.g.,
memory requirements, programmer's effort etc.)
• What do we mean by running time analysis?
– Determine how running time increases as the
size of the problem increases.
Cont’d…
• Algorithm analysis refers to:
the process of determining the amount of
computing time and storage space required by
different algorithms. In other words, it’s a
process of predicting the resource requirement of
algorithms in a given environment.
• To classify some data structures and
algorithms as good, we need precise ways of
analyzing them in terms of resource
requirement.
Cont’d…
• The main resources are:
 Running Time
 Memory Usage
 Communication Bandwidth (range of
frequency)
Complexity
• Complexity: A measure of the performance of
algorithm .An algorithm’s performance depends on
Internal and external factors. Complexity measures
the internal factors (usually more interested in time
than space)
Internal The algorithm’s efficiency, in terms of:
 Time required to run
Space(memory storage)required to run
External •Size of the input to the algorithm
• Speed of the computer on which it is run
•Quality of the compiler
Complexity Analysis
Is the systematic study of the cost of
computation, measured either
–in time units or
–in operations performed, or
–in the amount of storage space required
Performance Analysis
• 1. Space Complexity- S(P): The space
complexity of a program is the amount of
memory it needs to run to completion.
Determine the approximate memory required to
solve a problem of size n
Performance Analysis
• 2. Time Complexity-T(P): –Time Complexity:
Determine the approximate number of
operations required to solve a problem of size
n.
How to Measure Algorithm
Performance
What metric should be used to judge algorithms?
Length of the program (lines of code)
Ease of programming(bugs, maintenance)
Memory required
Running time
•Running time is the dominant standard.
Analysis Rules:
1. We assume an arbitrary time unit.
2. Execution of one of the following operations
takes time 1:
 Assignment Operation
 Single Input/output Operation
 Single Boolean Operations
 Single Arithmetic Operations
 Function Return
 Function call
Cont’d…
3. Running time of a selection statement (if, switch) is the time
for the condition evaluation + the maximum of the running times
for the individual clauses in the selection.
4. Loops: Running time for a loop is equal to the running time for
the statements inside the loop * number of iterations. The total
running time of a statement inside a group of nested loops is the
running time of the statements multiplied by the product of the
sizes of all the loops.
How do we analyze algorithms?
(3) Step count
Iterative function to sum a list of numbers
steps/execution
Statement s/e Frequency Total steps
float sum(float list[ ], int n) 0 0 0
{ 0 0 0
float tempsum = 0; 1 1 1
int i; 0 0 0
for(i=0; i <n; i++) 1 n+1 n+1
tempsum += list[i]; 1 n n
return tempsum; 1 1 1
} 0 0 0
Total 2n+3 16
How do we analyze algorithms?
(3) Step count
Step count table for recursive summing function

steps/execution

Statement s/e Frequency Total steps


float rsum(float list[ ], int n) 0 0 0
{ 0 0 0
if (n) 1 n+1 n+1
return rsum(list, n-1)+list[n-1]; 1 n n
return list[0]; 1 1 1
} 0 0 0
Total 2n+2
17
Step count
• Determining step counts help us to compare
the time complexities of two programs and to
predict the growth in run time as the instance
characteristics change.
• But determining exact step counts could be
very difficult. Since the notion of a step count
is itself inexact, it may be worth the effort to
compute the exact step counts.
Computing running time
• Associate a "cost" with each statement and find the
"total cost“ by finding the total number of times
each statement is executed.
• Express running time in terms of the size of the
problem. 
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1 
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
19
Computing running time (cont.)

Cost
  sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N
20
Read more on…..

• Best-case,average-case,and worst-case running time


of algorithm
• Comparing Functions Using
Rate of Growth

21
Part2

Heap Sort
Cont’d…
• Heap sort operates by first converting the list
in to a heap tree.
• Heap tree is a binary tree in which each node
has a value greater than both its children (if
any). It uses a process called "adjust” to
accomplish its task (building a heap tree)
whenever a value is larger than its parent.
Cont’d…
Algorithm:
1. Construct a binary tree
 The root node corresponds to Data[0].
 If we consider the index associated with a particular
node to be i, then the left child of this node
corresponds to the element with index 2*i+1 and the
right child corresponds to the element with index
2*i+2. If any or both of these elements do not exist in
the array, then the corresponding child node does not
exist either.
Cont’d…

2. Construct the heap tree from initial binary tree


using "adjust" process.
3. Sort by swapping the root value with the
lowest, right most value and deleting the lowest,
right most value and inserting the deleted value
in the array in it proper position.
Cont’d…
• Swap the root node with the lowest, right most
nodes and delete the lowest, right most value;
insert the deleted value in the array in its
proper position; adjust the heap tree; and
repeat this process until the tree is empty.
Reading Assignment
• What is the time complexity of heap sort ?
• Explain how?

You might also like