Overview of Algorithm Analysis: Ms. Andleeb Yousaf Khan Spring 2020
Overview of Algorithm Analysis: Ms. Andleeb Yousaf Khan Spring 2020
Analysis
Ms. Andleeb Yousaf Khan
Spring 2020
Lecture 2
What is an algorithm?
• A high level description of a process.
• Based on some input, an algorithm describes a
method to generate output.
• An algorithm should solve some problem –
captured by the input/output relationship
2
Example algorithm
7
Review: Arrays
• Arrays are linear series of data.
• Operations we assume to be available:
• [i] – access the ith element of the array
• size – how many elements are in the array
8
Review: Trees
• Binary Trees contain a set of
Nodes. Each Node can have a
left or right child.
• Operations we assume to be
available:
• parent – get the parent node
• left – get the left child
• right – get the right child
9
Review: Graphs
• A Graph is defined by a set of Vertices or Nodes, and a set of Edges that
connect pairs of Vertices.
• Operations we assume to be available:
• Vertex::adjacent_vertices
• Vertex::out_edges
• Vertex::in_edges
• Edge::source_vertex
• Edge::dest_vertex
Example: Insertion Sort
12
Insertion Sort
13
Insertion Sort
function insertionSort(A)
for j « 2 to size(A) do
key « A[j]
i « j – 1
while i > 0 and A[i] > key do
A[i + 1] « A[i]
i « i – 1
end while
A[i + 1] « key
end for
end
14
Correctness of Insertion Sort
15
Invariant = never changing.
• In computer science, an invariant is a condition
that can be relied upon to be true during execution
of a program, or during some portion of it. It is a
logical assertion that is held to always be true
during a certain phase of execution.
16
Correctness of Insertion Sort
• We show insertion sort is correct using a construct
called a Loop Invariant.
• Three properties of a Loop Invariant for an
algorithm to be considered correct.
1 Initialization – It is true at the start of the loop.
2 Maintenance – It is true at the end of the loop.
3 Termination – When the loop terminates, the Loop
Invariant should show that the algorithm is correct.
17
Insertion Sort Loop Invariant
• Loop Invariant: At the start of each for loop
iteration, A[1..j-1] contains the items initially in
A[1..j-1] but in sorted order.
function insertionSort(A)
for j « 2 to size(A) do
key « A[j]
i « j – 1
while i > 0 and A[i] > key do
A[i + 1] « A[i]
i « i – 1
end while
A[i + 1] « key
end for
end 18
Insertion Sort Loop Invariant
19
Insertion Sort Loop Invariant
20
Insertion Sort Loop Invariant
21
Insertion Sort – run time
function insertionSort(A) n
for j « 2 to size(A) do n- 1
key « A[j] n- 1
//insert A[j] into A[1…..j-1] n- 1
i « j – 1 å
n
j
j=2
while i > 0 and A[i] > key
do n
å ( j - 1)
A[i + 1] « A[i] j=2
å
n
( j - 1)
i « i – 1 j=2
end while
A[i + 1] « key
n- 1
end for
22
end
Insertion Sort
function insertionSort(A)
for j « 2 to size(A) do n
key « A[j] n- 1
i « j – 1 n- 1
while i > 0 and A[i] > key do n- 1
å
n
A[i + 1] « A[i] j=2
j
i « i – 1 å
n
( j - 1)
end while å
j=2
n
( j - 1)
A[i + 1] « key j=2
end for
n- 1
end
• Total Runtime =
j +å ( j - 1) + å
n n n
n + n - 1+ n - 1+ n - 1+ å ( j - 1) + n - 1
j=2 j=2 j=2
23
Insertion Sort
j +å ( j - 1) + å
n n n
• Total runtime= n + n - 1+ n - 1+ n - 1+ å ( j - 1) + n - 1
j=2 j=2 j=2