2. Weeks 2, 3 - sessions 3, 4 - Data structures, Algorithm Analysis Growth Rates, Model, Run Time Calculation, and Application for students
2. Weeks 2, 3 - sessions 3, 4 - Data structures, Algorithm Analysis Growth Rates, Model, Run Time Calculation, and Application for students
Algorithms
What Are Data
Structures?
What Are Data Structures?
• A data structure is a way of organizing and storing
data to perform operations efficiently.
• Data: This refers to the raw facts and figures that are
processed by a computer. It can be numbers,
characters, images, or any other form of information.
• Primitive data types: These are the basic data types, such
as integers, floats, characters, etc.
• Examples:
• Arrays, Linked Lists, Stacks, and Queues are common linear data
structures.
• In an array, elements are stored in contiguous memory locations,
and their indices define the order.
• In linked lists, each element points to the next one, forming a
chain.
• Traversal:
• Elements are accessed and processed in a linear fashion,
typically through iteration.
• The order of elements is predetermined.
Linear Data Structure vs Nonlinear Data Structure
• Examples:
• Trees and Graphs are common nonlinear data structures.
• In trees, each element (node) can have multiple child nodes,
forming a hierarchical structure.
• Graphs allow arbitrary connections between nodes, creating
complex relationships.
• Traversal:
• Traversal in nonlinear structures may involve more complex
algorithms.
•
Algorithm
What is an Algorithm?
• An algorithm is any well-defined computational procedure
that takes some value, or set of values, as input and produces
some value, or set of values, as output.
• An algorithm is said to be effi cient and fast, if it takes less time to execute and
consumes less memory space.
• Different algorithms devised to solve the same problem often differ dramatically
in their effi ciency.
• These differences can be much more significant than differences due to hardware
and software.
Algorithm Efficiency
• Effi ciency
• Let's consider a simple example of searching for a specific
element in an array.
• F(n)=F(n−1)+F(n−2)
• with initial conditions F(0)=0 and F(1)=1.
Some key aspects of algorithm analysis:
Dynamic Programming:
• The recursive solution to solve the Fibonacci leads to an
exponential number of recursive calls (exponential time
complexity) due to redundant computations, making it highly
ineffi cient for larger values of n.
Examples:
• array: linear search, traversing, find minimum
• ArrayList: contains method // find an element if it exists
• queue: contains method
Calculating Time Complexity
• If there is a single iteration, and the iterative variable is
incrementing linearly (size increases +1) then it's O(n) e.g.
Example:
Binary search
Quadratic Time: O(n2)
• selection sort
• insertion sort
Nested loop
If there is nested loop, where one has a complexity of
O(n) and the other O(logn), then overall complexity is
O(nlogn).
//Overall O(nlogn)
The Execution Time of Algorithms
A sequence of operations:
Total Cost = c1 + c2
The Execution Time of Algorithms (cont.)
Example: Simple If-Statement
Cost Times
if (n < 0) c1 1
absVal = -n c2 1
else
absVal = n; c3 1
63
The Execution Time of Algorithms (cont.)
Example: Simple Loop
Cost Times
i = 1; c1
sum = 0; c2
while (i <= n) { c3
i = i + 1; c4
sum = sum + i; c5
}
= (c3+c4+c5)*n + (c1+c2+c3)
= a*n + b
So, the growth-rate function for this algorithm is O(n) // linear time
The Execution Time of Algorithms (cont.)
Cost Times
i=1; c1
sum = 0; c2
while (i <= n) { c3
j=1; c4
while (j <= n) { c5
sum = sum + i; c6
j = j + 1; c7
}
i = i +1; c8
}
So, the growth-rate function for this algorithm is
Cost Times
i=1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
j=1; c4 n
while (j <= n) { c5 n*(n+1)
sum = sum + i; c6 n*n
j = j + 1; c7 n*n
}
i = i +1; c8 n
}
T(n) = c1 + c2 + (n+1)*c3 + n*c4 + n*(n+1)*c5+n*n*c6+n*n*c7+n*c8
int recursiveFunction(int n) {
if (n <= 1) {
return 1;
}
return recursiveFunction(n - 1) + recursiveFunction(n -
2);
}
Exercise 6 solution
• Howto improve the time complexity of Fibonacci
recursive function?