0% found this document useful (0 votes)
10 views40 pages

Lec 01 22 01 17

The document discusses the role of algorithms in computing, defining an algorithm as a well-defined procedure that takes input and produces output, specifically focusing on sorting problems. It emphasizes the importance of algorithm efficiency, comparing Insertion sort and Merge sort in terms of time complexity and resource usage. The goal of the course is to learn algorithm design and analysis techniques to create efficient algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views40 pages

Lec 01 22 01 17

The document discusses the role of algorithms in computing, defining an algorithm as a well-defined procedure that takes input and produces output, specifically focusing on sorting problems. It emphasizes the importance of algorithm efficiency, comparing Insertion sort and Merge sort in terms of time complexity and resource usage. The goal of the course is to learn algorithm design and analysis techniques to create efficient algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

1

Data Structures and Algorithms

BITS-Pilani K. K. Birla Goa Campus

1
Material for the presentation taken from Cormen, Leiserson, Rivest and
Stein, Introduction to Algorithms, Third Edition; 1
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
CS F211: Data Structures and Algorithms

Handout Discussion

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
� An algorithm solves a computational problem.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
� An algorithm solves a computational problem.
� Sorting Problem

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
� An algorithm solves a computational problem.
� Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
� An algorithm solves a computational problem.
� Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation < a1� , a2� , . . . , an� > such that
a1� ≤ a2� ≤ · · · ≤ an�

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
� An algorithm solves a computational problem.
� Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation < a1� , a2� , . . . , an� > such that
a1� ≤ a2� ≤ · · · ≤ an�
� E.g., Input sequence : < 31, 41, 59, 26, 41, 58 >

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
� An algorithm solves a computational problem.
� Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation < a1� , a2� , . . . , an� > such that
a1� ≤ a2� ≤ · · · ≤ an�
� E.g., Input sequence : < 31, 41, 59, 26, 41, 58 >
� Sorting algorithm should give the output:
< 26, 31, 41, 41, 58, 59 >

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.
� We can have algorithms for a wide range of computational
problems.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.
� We can have algorithms for a wide range of computational
problems.
� Initially, we will focus on the sorting problem to understand
different algorithm design techniques and algorithm analysis
tools.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.
� We can have algorithms for a wide range of computational
problems.
� Initially, we will focus on the sorting problem to understand
different algorithm design techniques and algorithm analysis
tools.
� What is a Data Structure?

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.
� We can have algorithms for a wide range of computational
problems.
� Initially, we will focus on the sorting problem to understand
different algorithm design techniques and algorithm analysis
tools.
� What is a Data Structure?
A data structure is a way to store and organize data in order
to facilitate access and modification.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.
� We can have algorithms for a wide range of computational
problems.
� Initially, we will focus on the sorting problem to understand
different algorithm design techniques and algorithm analysis
tools.
� What is a Data Structure?
A data structure is a way to store and organize data in order
to facilitate access and modification.
� Goal of this course:

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

� An algorithm is correct if it halts for every input with the


correct output.
� We can have algorithms for a wide range of computational
problems.
� Initially, we will focus on the sorting problem to understand
different algorithm design techniques and algorithm analysis
tools.
� What is a Data Structure?
A data structure is a way to store and organize data in order
to facilitate access and modification.
� Goal of this course:
Learn techniques of algorithm design and analysis so that we
can design algorithms on our own, show that the algorithm is
correct, and understand how efficient the algorithm is.
4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

� There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

� There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
� If computers were infinitely fast and computer memory was
unlimited, then any correct algorithm would do.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

� There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
� If computers were infinitely fast and computer memory was
unlimited, then any correct algorithm would do.
� However, computers are not infinitely fast and memory is
limited.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

� There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
� If computers were infinitely fast and computer memory was
unlimited, then any correct algorithm would do.
� However, computers are not infinitely fast and memory is
limited.
� We should prefer an algorithm that uses these two resources
wisely.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

� There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
� If computers were infinitely fast and computer memory was
unlimited, then any correct algorithm would do.
� However, computers are not infinitely fast and memory is
limited.
� We should prefer an algorithm that uses these two resources
wisely.
� A good algorithm would be efficient in terms of computing
time and memory that is used.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.
� c1 is usually much smaller than c2 .

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.
� c1 is usually much smaller than c2 .
� The time taken by the two algorithms for small values of n
will be similar.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.
� c1 is usually much smaller than c2 .
� The time taken by the two algorithms for small values of n
will be similar.
� As n increases, time taken by insertion sort is increasing faster
(n × n) compared to the time taken by merge sort (n × lg n).

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.
� c1 is usually much smaller than c2 .
� The time taken by the two algorithms for small values of n
will be similar.
� As n increases, time taken by insertion sort is increasing faster
(n × n) compared to the time taken by merge sort (n × lg n).
� If n is 1 million, lg n is approximately 20.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.
� c1 is usually much smaller than c2 .
� The time taken by the two algorithms for small values of n
will be similar.
� As n increases, time taken by insertion sort is increasing faster
(n × n) compared to the time taken by merge sort (n × lg n).
� If n is 1 million, lg n is approximately 20.
� So, for smaller values of n insertion sort might be better
(because c1 is much smaller than c2 ).

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

� Two algorithms for the sorting problem: Insertion sort and


Merge sort.
� Insertion sort takes roughly c1 n2 time. Merge sort takes
roughly c2 n lg n.
� c1 is usually much smaller than c2 .
� The time taken by the two algorithms for small values of n
will be similar.
� As n increases, time taken by insertion sort is increasing faster
(n × n) compared to the time taken by merge sort (n × lg n).
� If n is 1 million, lg n is approximately 20.
� So, for smaller values of n insertion sort might be better
(because c1 is much smaller than c2 ).
� As n becomes larger and larger, time taken by merge sort will
be very less compared to insertion sort.
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).
� Let n = 10 million ( So, memory needed
10 × 106 × 8 (8-byte integer) = 80 MB )

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).
� Let n = 10 million ( So, memory needed
10 × 106 × 8 (8-byte integer) = 80 MB )
� Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).
� Let n = 10 million ( So, memory needed
10 × 106 × 8 (8-byte integer) = 80 MB )
� Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
� Let c1 = 2 and c2 = 50.

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).
� Let n = 10 million ( So, memory needed
10 × 106 × 8 (8-byte integer) = 80 MB )
� Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
� Let c1 = 2 and c2 = 50.
2 × (107 )2
� Time taken by faster computer A:
1010 instructions/second

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).
� Let n = 10 million ( So, memory needed
10 × 106 × 8 (8-byte integer) = 80 MB )
� Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
� Let c1 = 2 and c2 = 50.
2 × (107 )2
� Time taken by faster computer A:
1010 instructions/second
50 × (107 ) lg 107
� Time taken by slower computer B:
107 instructions/second

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

� Does all this matter? Maybe we should use a faster computer?


� Let us compare a faster computer A (running insertion sort)
with a slower computer B (running merge sort).
� Let n = 10 million ( So, memory needed
10 × 106 × 8 (8-byte integer) = 80 MB )
� Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
� Let c1 = 2 and c2 = 50.
2 × (107 )2
� Time taken by faster computer A:
1010 instructions/second
50 × (107 ) lg 107
� Time taken by slower computer B:
107 instructions/second
� Faster computer A takes more than 20,000 seconds (5.5
hours), whereas slower computer B takes around 1163 seconds
(< 20 minutes).
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparison of running times

� For each function f (n) and time t in the following table,


determine the largest size n of a problem that can be solved in
time t, assuming that the algorithm to solve the problem
takes f (n) microseconds.

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparison of running times

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms

You might also like