This document provides an overview and introduction to the concepts taught in a data structures and algorithms course. It discusses the goals of reinforcing that every data structure has costs and benefits, learning commonly used data structures, and understanding how to analyze the efficiency of algorithms. Key topics covered include abstract data types, common data structures, algorithm analysis techniques like best/worst/average cases and asymptotic notation, and examples of analyzing the time complexity of various algorithms. The document emphasizes that problems can have multiple potential algorithms and that problems should be carefully defined in terms of inputs, outputs, and resource constraints.
Linear search examines each element of a list sequentially, one by one, and checks if it is the target value. It has a time complexity of O(n) as it requires searching through each element in the worst case. While simple to implement, linear search is inefficient for large lists as other algorithms like binary search require fewer comparisons.
The document discusses the analysis of algorithms, including time and space complexity analysis. It covers key aspects of analyzing algorithms such as determining the basic operation, input size, and analyzing best-case, worst-case, and average-case time complexities. Specific examples are provided, such as analyzing the space needed to store real numbers and analyzing the time complexity of sequential search. Order of growth and asymptotic analysis techniques like Big-O, Big-Omega, and Big-Theta notation are also explained.
The document discusses algorithms and data structures. It defines an algorithm as a well-defined procedure that takes input and produces output. Algorithms are used for calculation, data processing, and automated reasoning. The document discusses different ways of describing algorithms including natural language, flowcharts, and pseudo code. It also discusses analyzing the time complexity of algorithms using asymptotic notation such as Big-O, Omega, and Theta notation. Recursive algorithms and solving recurrences are also covered.
The document discusses algorithms and algorithm analysis. It provides examples to illustrate key concepts in algorithm analysis including worst-case, average-case, and best-case running times. The document also introduces asymptotic notation such as Big-O, Big-Omega, and Big-Theta to analyze the growth rates of algorithms. Common growth rates like constant, logarithmic, linear, quadratic, and exponential functions are discussed. Rules for analyzing loops and consecutive statements are provided. Finally, algorithms for two problems - selection and maximum subsequence sum - are analyzed to demonstrate algorithm analysis techniques.
This document discusses algorithms and their analysis. It begins with definitions of algorithms and their characteristics. Different methods for specifying algorithms are described, including pseudocode. The goal of algorithm analysis is introduced as comparing algorithms based on running time and other factors. Common algorithm analysis methods like worst-case, best-case, and average-case are defined. Big-O notation and time/space complexity are explained. Common algorithm design strategies like divide-and-conquer and randomized algorithms are summarized. Specific algorithms like repeated element detection and primality testing are mentioned.
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
This 90-minute discussion covers algorithms, sorting algorithms, and analyzing algorithm performance. It discusses measuring algorithm effectiveness, common sorting algorithms like insertion sort and merge sort, and running time analysis. Insertion sort runs in O(n^2) time in the worst case, while merge sort runs faster in O(n log n) time by dividing the problem into smaller subproblems. Real-world examples of sorting include cable provider personalized services and alphabetical item lists.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, recursion, stacks and common stack operations like push and pop. Examples are provided to illustrate factorial calculation using recursion and implementation of a stack.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, and recursion. It provides examples of algorithms and data structures like stacks and using recursion to calculate factorials. The document covers fundamental topics in data structures and algorithms.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, and recursion. It provides examples of algorithms and data structures like arrays, stacks and the factorial function to illustrate recursive and iterative implementations. Problem solving techniques like defining the problem, designing algorithms, analyzing and testing solutions are also covered.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, and recursion. It provides examples of algorithms and data structures like stacks and using recursion to calculate factorials. The document covers fundamental topics in data structures and algorithms.
This document discusses data structures and algorithms. It begins by defining data structures as the logical organization of data and primitive data types like integers that hold single pieces of data. It then discusses static versus dynamic data structures and abstract data types. The document outlines the main steps in problem solving as defining the problem, designing algorithms, analyzing algorithms, implementing, testing, and maintaining solutions. It provides examples of space and time complexity analysis and discusses analyzing recursive algorithms through repeated substitution and telescoping methods.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm analysis including time and space complexity, and common algorithm design techniques like recursion. It provides examples of algorithms and data structures like stacks and using recursion to calculate factorials. The document covers fundamental topics in data structures and algorithms.
This document discusses algorithm analysis concepts such as time complexity, space complexity, and big-O notation. It explains how to analyze the complexity of algorithms using techniques like analyzing loops, nested loops, and sequences of statements. Common complexities like O(1), O(n), and O(n^2) are explained. Recurrence relations and solving them using iteration methods are also covered. The document aims to teach how to measure and classify the computational efficiency of algorithms.
Analysis of Algorithm full version 2024.pptxrajesshs31r
This document discusses algorithms and their analysis. It begins by defining an algorithm as a sequence of unambiguous instructions to solve a problem in a finite amount of time. Euclid's algorithm for computing the greatest common divisor is provided as an example. The document then covers fundamentals of algorithmic problem solving, including understanding the problem, choosing exact or approximate solutions, and algorithm design techniques. It also discusses analyzing algorithms based on time and space complexity, as well as worst-case, best-case, and average-case efficiencies. Common problem types like sorting, searching, and graph problems are briefly outlined.
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
This document discusses algorithms and their analysis. It begins by defining an algorithm as a sequence of unambiguous instructions to solve a problem in a finite amount of time. It then provides examples of Euclid's algorithm for computing the greatest common divisor. The document goes on to discuss the fundamentals of algorithmic problem solving, including understanding the problem, choosing exact or approximate solutions, and algorithm design techniques. It also covers analyzing algorithms by measuring time and space complexity using asymptotic notations.
Algorithm Class at KPHB C, C++ Course Training Institute in KPHB, Kukatpally, Hyderabad.
https://sites.google.com/site/algorithmclass
Algorithm Class at KPHB C, C++ Course Training Institute in KPHB, Kukatpally, Hyderabad.
https://sites.google.com/site/algorithmclass
The main purpose of the current study was to formulate an empirical expression for predicting the axial compression capacity and axial strain of concrete-filled plastic tubular specimens (CFPT) using the artificial neural network (ANN). A total of seventy-two experimental test data of CFPT and unconfined concrete were used for training, testing, and validating the ANN models. The ANN axial strength and strain predictions were compared with the experimental data and predictions from several existing strength models for fiber-reinforced polymer (FRP)-confined concrete. Five statistical indices were used to determine the performance of all models considered in the present study. The statistical evaluation showed that the ANN model was more effective and precise than the other models in predicting the compressive strength, with 2.8% AA error, and strain at peak stress, with 6.58% AA error, of concrete-filled plastic tube tested under axial compression load. Similar lower values were obtained for the NRMSE index.
The document discusses algorithms and algorithm analysis. It provides examples to illustrate key concepts in algorithm analysis including worst-case, average-case, and best-case running times. The document also introduces asymptotic notation such as Big-O, Big-Omega, and Big-Theta to analyze the growth rates of algorithms. Common growth rates like constant, logarithmic, linear, quadratic, and exponential functions are discussed. Rules for analyzing loops and consecutive statements are provided. Finally, algorithms for two problems - selection and maximum subsequence sum - are analyzed to demonstrate algorithm analysis techniques.
This document discusses algorithms and their analysis. It begins with definitions of algorithms and their characteristics. Different methods for specifying algorithms are described, including pseudocode. The goal of algorithm analysis is introduced as comparing algorithms based on running time and other factors. Common algorithm analysis methods like worst-case, best-case, and average-case are defined. Big-O notation and time/space complexity are explained. Common algorithm design strategies like divide-and-conquer and randomized algorithms are summarized. Specific algorithms like repeated element detection and primality testing are mentioned.
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
This 90-minute discussion covers algorithms, sorting algorithms, and analyzing algorithm performance. It discusses measuring algorithm effectiveness, common sorting algorithms like insertion sort and merge sort, and running time analysis. Insertion sort runs in O(n^2) time in the worst case, while merge sort runs faster in O(n log n) time by dividing the problem into smaller subproblems. Real-world examples of sorting include cable provider personalized services and alphabetical item lists.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, recursion, stacks and common stack operations like push and pop. Examples are provided to illustrate factorial calculation using recursion and implementation of a stack.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, and recursion. It provides examples of algorithms and data structures like stacks and using recursion to calculate factorials. The document covers fundamental topics in data structures and algorithms.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, and recursion. It provides examples of algorithms and data structures like arrays, stacks and the factorial function to illustrate recursive and iterative implementations. Problem solving techniques like defining the problem, designing algorithms, analyzing and testing solutions are also covered.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm design, analysis of time and space complexity, and recursion. It provides examples of algorithms and data structures like stacks and using recursion to calculate factorials. The document covers fundamental topics in data structures and algorithms.
This document discusses data structures and algorithms. It begins by defining data structures as the logical organization of data and primitive data types like integers that hold single pieces of data. It then discusses static versus dynamic data structures and abstract data types. The document outlines the main steps in problem solving as defining the problem, designing algorithms, analyzing algorithms, implementing, testing, and maintaining solutions. It provides examples of space and time complexity analysis and discusses analyzing recursive algorithms through repeated substitution and telescoping methods.
The document discusses data structures and algorithms. It defines key concepts like primitive data types, data structures, static vs dynamic structures, abstract data types, algorithm analysis including time and space complexity, and common algorithm design techniques like recursion. It provides examples of algorithms and data structures like stacks and using recursion to calculate factorials. The document covers fundamental topics in data structures and algorithms.
This document discusses algorithm analysis concepts such as time complexity, space complexity, and big-O notation. It explains how to analyze the complexity of algorithms using techniques like analyzing loops, nested loops, and sequences of statements. Common complexities like O(1), O(n), and O(n^2) are explained. Recurrence relations and solving them using iteration methods are also covered. The document aims to teach how to measure and classify the computational efficiency of algorithms.
Analysis of Algorithm full version 2024.pptxrajesshs31r
This document discusses algorithms and their analysis. It begins by defining an algorithm as a sequence of unambiguous instructions to solve a problem in a finite amount of time. Euclid's algorithm for computing the greatest common divisor is provided as an example. The document then covers fundamentals of algorithmic problem solving, including understanding the problem, choosing exact or approximate solutions, and algorithm design techniques. It also discusses analyzing algorithms based on time and space complexity, as well as worst-case, best-case, and average-case efficiencies. Common problem types like sorting, searching, and graph problems are briefly outlined.
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
This document discusses algorithms and their analysis. It begins by defining an algorithm as a sequence of unambiguous instructions to solve a problem in a finite amount of time. It then provides examples of Euclid's algorithm for computing the greatest common divisor. The document goes on to discuss the fundamentals of algorithmic problem solving, including understanding the problem, choosing exact or approximate solutions, and algorithm design techniques. It also covers analyzing algorithms by measuring time and space complexity using asymptotic notations.
Algorithm Class at KPHB C, C++ Course Training Institute in KPHB, Kukatpally, Hyderabad.
https://sites.google.com/site/algorithmclass
Algorithm Class at KPHB C, C++ Course Training Institute in KPHB, Kukatpally, Hyderabad.
https://sites.google.com/site/algorithmclass
The main purpose of the current study was to formulate an empirical expression for predicting the axial compression capacity and axial strain of concrete-filled plastic tubular specimens (CFPT) using the artificial neural network (ANN). A total of seventy-two experimental test data of CFPT and unconfined concrete were used for training, testing, and validating the ANN models. The ANN axial strength and strain predictions were compared with the experimental data and predictions from several existing strength models for fiber-reinforced polymer (FRP)-confined concrete. Five statistical indices were used to determine the performance of all models considered in the present study. The statistical evaluation showed that the ANN model was more effective and precise than the other models in predicting the compressive strength, with 2.8% AA error, and strain at peak stress, with 6.58% AA error, of concrete-filled plastic tube tested under axial compression load. Similar lower values were obtained for the NRMSE index.
Construction Materials (Paints) in Civil EngineeringLavish Kashyap
This file will provide you information about various types of Paints in Civil Engineering field under Construction Materials.
It will be very useful for all Civil Engineering students who wants to search about various Construction Materials used in Civil Engineering field.
Paint is a vital construction material used for protecting surfaces and enhancing the aesthetic appeal of buildings and structures. It consists of several components, including pigments (for color), binders (to hold the pigment together), solvents or thinners (to adjust viscosity), and additives (to improve properties like durability and drying time).
Paint is one of the material used in Civil Engineering field. It is especially used in final stages of construction project.
Paint plays a dual role in construction: it protects building materials and contributes to the overall appearance and ambiance of a space.
この資料は、Roy FieldingのREST論文(第5章)を振り返り、現代Webで誤解されがちなRESTの本質を解説しています。特に、ハイパーメディア制御やアプリケーション状態の管理に関する重要なポイントをわかりやすく紹介しています。
This presentation revisits Chapter 5 of Roy Fielding's PhD dissertation on REST, clarifying concepts that are often misunderstood in modern web design—such as hypermedia controls within representations and the role of hypermedia in managing application state.
This research presents the optimization techniques for reinforced concrete waffle slab design because the EC2 code cannot provide an efficient and optimum design. Waffle slab is mostly used where there is necessity to avoid column interfering the spaces or for a slab with large span or as an aesthetic purpose. Design optimization has been carried out here with MATLAB, using genetic algorithm. The objective function include the overall cost of reinforcement, concrete and formwork while the variables comprise of the depth of the rib including the topping thickness, rib width, and ribs spacing. The optimization constraints are the minimum and maximum areas of steel, flexural moment capacity, shear capacity and the geometry. The optimized cost and slab dimensions are obtained through genetic algorithm in MATLAB. The optimum steel ratio is 2.2% with minimum slab dimensions. The outcomes indicate that the design of reinforced concrete waffle slabs can be effectively carried out using the optimization process of genetic algorithm.
AI-Powered Data Management and Governance in RetailIJDKP
Artificial intelligence (AI) is transforming the retail industry’s approach to data management and decisionmaking. This journal explores how AI-powered techniques enhance data governance in retail, ensuring data quality, security, and compliance in an era of big data and real-time analytics. We review the current landscape of AI adoption in retail, underscoring the need for robust data governance frameworks to handle the influx of data and support AI initiatives. Drawing on literature and industry examples, we examine established data governance frameworks and how AI technologies (such as machine learning and automation) are augmenting traditional data management practices. Key applications are identified, including AI-driven data quality improvement, automated metadata management, and intelligent data lineage tracking, illustrating how these innovations streamline operations and maintain data integrity. Ethical considerations including customer privacy, bias mitigation, transparency, and regulatory compliance are discussed to address the challenges of deploying AI in data governance responsibly.
The TRB AJE35 RIIM Coordination and Collaboration Subcommittee has organized a series of webinars focused on building coordination, collaboration, and cooperation across multiple groups. All webinars have been recorded and copies of the recording, transcripts, and slides are below. These resources are open-access following creative commons licensing agreements. The files may be found, organized by webinar date, below. The committee co-chairs would welcome any suggestions for future webinars. The support of the AASHTO RAC Coordination and Collaboration Task Force, the Council of University Transportation Centers, and AUTRI’s Alabama Transportation Assistance Program is gratefully acknowledged.
This webinar overviews proven methods for collaborating with USDOT University Transportation Centers (UTCs), emphasizing state departments of transportation and other stakeholders. It will cover partnerships at all UTC stages, from the Notice of Funding Opportunity (NOFO) release through proposal development, research and implementation. Successful USDOT UTC research, education, workforce development, and technology transfer best practices will be highlighted. Dr. Larry Rilett, Director of the Auburn University Transportation Research Institute will moderate.
For more information, visit: https://aub.ie/trbwebinars
Welcome to MIND UP: a special presentation for Cloudvirga, a Stewart Title company. In this session, we’ll explore how you can “mind up” and unlock your potential by using generative AI chatbot tools at work.
Curious about the rise of AI chatbots? Unsure how to use them-or how to use them safely and effectively in your workplace? You’re not alone. This presentation will walk you through the practical benefits of generative AI chatbots, highlight best practices for safe and responsible use, and show how these tools can help boost your productivity, streamline tasks, and enhance your workday.
Whether you’re new to AI or looking to take your skills to the next level, you’ll find actionable insights to help you and your team make the most of these powerful tools-while keeping security, compliance, and employee well-being front and center.
1. Course: Data Structures
Khushil Saini
Lecturer, Division of Computer Engineering
Netaji Subhas Institute of Technology, New Delhi
2. What this course is about ?
Data structures: Ways to organize data for
efficient storage and efficient manipulation
Employment of these data structures in the
design of efficient algorithms
3. Why do we need them ?
More powerful computers more complex
applications.
Example: Index of billions of pages by Search
Engines (Google) !
Number of features in Mobile Phones increasing
day-by-day
Computers take on more and more complex
tasks Demand more calculations
Data Structures organize data more efficient
programs.
4. Organizing Data
Any organization for a collection of records
can be searched, processed in any order, or
modified.
The choice of data structure and algorithm
can make the difference between a program
running in a few seconds or many days.
Efficient data structures
Efficient algorithms
5. Data Structure Example
Say, we need to read some topic in a book
having 1500 pages. We looked for this topic in
the index and got the information about the
page number of its occurrence in the book.
Say, it is at page number 867. We need to go
to that page.
What to do?
6. Example (cont’d)
What to do?
Sol. 1 Sequential : Start from page 1 and keep on moving from
one page to next till we reach page 867. How many steps ?
or
Sol. 2 Binary Searching:
- Pages are in Sorted order.
- Look only half at a time.
Another Ex. Search 25 in : 5 8 12 14 15 18 23 25 27
25 ? 15 15 18 23 25 27
25 ? 23 23 25 27
25 ? 25
How many steps?
8. What will you learn?
What are some of the common data structures
What are some ways to implement them
How to analyze their efficiency
How to use them to solve practical problems
9. What do you need ?
Some Programming experience with C / C++
Textbook
Data Structures Using C and C++ : Tenenbaum A. M.,
Langsam Y., Augenstein M. J. , Pearson Education
A Computer to write, compile and run your C/C++
programs
Windows/Linux Operating System
C/C++ Compiler (gcc)
Debugger (gdb)
10. Major Topics
• Algorithm Analysis
• ADTs
• Queues and Lists
• Stacks
• Trees
• Heaps / Priority Queues
• Binary Search Trees
• Hashing / Dictionaries
• Sorting
• Graphs and graph algorithms
11. Algorithm Analysis
What is Algorithm?
a clearly specified set of simple instructions to be followed to
solve a problem
• Takes a set of values, as input and
• produces a value, or set of values, as output
May be specified
• In English
• As a computer program
• As a pseudo-code
Program = algorithms + data structures
12. Need for Algorithm Analysis
Writing a working program is not good
enough
The program may be inefficient!
If the program is run on a large data set, then
the running time becomes an issue
Lets refer to previous example of Searching a Page
in a book
13. Example: Selection Problem
Given a list of N numbers, determine the kth
largest, where k N.
Algorithm 1:
(1) Read N numbers into an array
(2) Sort the array in decreasing order by some
simple algorithm ( Bubble Sort)
(3) Return the element in position k
14. Example: Selection Problem…
Algorithm 2:
(1) Read the first k elements into an array and sort
them in decreasing order
(2) Each remaining element is read one by one
• If smaller than the kth element, then it is ignored
• Otherwise, it is placed in its correct spot in the array,
bumping one element out of the array.
(3) The element in the kth position is returned as
the answer.
15. Example: Selection Problem…
Which algorithm is better when
N =100 and k = 100?
N =100 and k = 1?
What happens when N = 1,000,000 and k =
500,000?
There exist better algorithms
16. Algorithm Analysis
We only analyze correct algorithms
An algorithm is correct
If, for every input instance, it halts with the correct output
Incorrect algorithms
Might not halt at all on some input instances
Might halt with other than the desired answer
Analyzing an algorithm
Predicting the resources that the algorithm requires
Resources include
• Memory
• Computational time (usually most important)
• Communication bandwidth
17. Algorithm Analysis…
Factors affecting the running time
computer
compiler
algorithm used
input to the algorithm
• The content of the input affects the running time
• typically, the input size (number of items in the input) is the
main consideration
– E.g. sorting problem the number of items to be sorted
– E.g. multiply two matrices together the total number of
elements in the two matrices
Machine model assumed
Instructions are executed one after another, with no
concurrent operations Not parallel computers
18. Example
N
i
i
1
3
Calculate
Lines 1 and 4 count for one unit each
Line 3: executed N times, each time four units
Line 2: (1 for initialization, N+1 for all the tests, N for
all the increments) total 2N + 2
total cost: 6N + 4 O(N)
1
2
3
4
1
2
3
4
1
2N+2
4N
1
19. Worst- / average- / best-case
Worst-case running time of an algorithm
The longest running time for any input of size n
An upper bound on the running time for any input
guarantee that the algorithm will never take longer
Example: Sort a set of numbers in increasing order; and the
data is in decreasing order
The worst case can occur fairly often
• E.g. in searching a database for a particular piece of
information
Best-case running time
sort a set of numbers in increasing order; and the data is
already in increasing order
Average-case running time
May be difficult to define what “average” means
20. Running-time of algorithms
Bounds are for the algorithms, rather than
programs
programs are just implementations of an
algorithm, and almost always the details of the
program do not affect the bounds
Bounds are for algorithms, rather than
problems
A problem can be solved with several algorithms,
some are more efficient than others
21. Asymptotic notation: Big-Oh
f(N) = O(g(N))
There are positive constants c and n0 such
that
f(N) c g(N) when N n0
The growth rate of f(N) is less than or equal
to the growth rate of g(N)
g(N) is an upper bound on f(N)
22. Big-Oh: example
Let f(N) = 2N2. Then
f(N) = O(N4)
f(N) = O(N3)
f(N) = O(N2) (best answer, asymptotically tight)
O(N2): reads “order N-squared” or “Big-Oh N-
squared”
23. Some rules
When considering the growth rate of a function using
Big-Oh :- Ignore the lower order terms and the
coefficients of the highest-order term
If T1(N) = O(f(N) and T2(N) = O(g(N)), then
T1(N) + T2(N) = max(O(f(N)), O(g(N))),
T1(N) * T2(N) = O(f(N) * g(N))
24. Big Oh: more examples
N2 / 2 – 3N = O(N2)
1 + 4N = O(N)
7N2 + 10N + 3 = O(N2) = O(N3)
log10 N = log2 N / log2 10 = O(log2 N) = O(log N)
sin N = O(1); 10 = O(1), 1010 = O(1)
log N + N = O(N)
N = O(2N), but 2N is not O(N)
210N is not O(2N)
25. Big-Omega
• f(N) = (g(N))
• There are positive constants c and n0 such
that
f(N) c g(N) when N n0
• The growth rate of f(N) is greater than or
equal to the growth rate of g(N).
27. Big-Theta
f(N) = (g(N)) iff
f(N) = O(g(N)) and f(N) = (g(N))
The growth rate of f(N) equals the growth rate
of g(N)
Example: Let f(N)=N2 , g(N)=2N2
Since f(N) = O(g(N)) and f(N) = (g(N)),
thus f(N) = (g(N)).
Big-Theta means the bound is the tightest
possible.
28. Growth rates …
Doubling the input size
f(N) = c f(2N) = f(N) = c
f(N) = log N f(2N) = f(N) + log 2
f(N) = N f(2N) = 2 f(N)
f(N) = N2 f(2N) = 4 f(N)
f(N) = N3 f(2N) = 8 f(N)
f(N) = 2N f(2N) = f2(N)
Advantages of algorithm analysis
To eliminate bad algorithms early
pinpoints the bottlenecks, which are worth coding
carefully
29. General Rules
For loops
at most the running time of the statements inside
the for-loop (including tests) times the number of
iterations.
Nested for loops
the running time of the statement multiplied by the
product of the sizes of all the for-loops.
O(N2)
31. Other Control Statements
while loop: Analyze like a for loop.
if statement: Take greater complexity of
then/else clauses.
switch statement: Take complexity of most
expensive case.
Subroutine call: Complexity of the subroutine.
32. Example
sum1 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
sum1++;
O(n2)
Loop index depends on outer loop index
sum2 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
sum2++;
N(N+1)/2 = O(n2)
34. Run-time for Recursive Algorithms
T(n) is defined recursively in terms of T(k),
k<n
The recurrence relations allow T(n) to be
“unwound” recursively into some base cases
(e.g., T(0) or T(1)).
Examples:
Factorial
Fibonacci
36. 36
Example: Factorial (Iterative)
int factorial1(int n) {
if (n<=1) return 1;
else {
fact = 1;
for (k=2;k<=n;k++)
fact *= k;
return fact;
}
}
Both algorithms are O(n).
)
1
(
O
)
1
(
O
)
(n
O
37. Recursive v/s Iterative
Overhead required to manage the stack and
the relative slowness of function calls
Some types of problems whose solutions are
inherently recursive: Tree Traversal, Divide
and Conquer, Depth First Search etc.
Solution would be much simpler using recursion