0% found this document useful (0 votes)
144 views

BHASIN HARSH_PROBLEM SOLVING AND PYTHON PROGRAMMING

Uploaded by

2122ba18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

BHASIN HARSH_PROBLEM SOLVING AND PYTHON PROGRAMMING

Uploaded by

2122ba18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

NEW AGE

PROBLEM SOLVING
AND
PYTHON PROGRAMMING
Fundamentals and Applications: NumPy, Pandas and Matplotlib

HARSH BHASIN
Get free access to e-books for 3 Months
Best Selling Textbooks in COMPUTER SCIENCE ENGINEERING
by
Renowned Authors from Prestigious Publishers

PHI
... & many more

Compliments from to all Faculty Members


already LIVE @ Download the App to access e-books:

Indian Institute of Technology Kanpur, Uttar Pradesh

Visvesvaraya Technological University (Consortium), Belgaum, Karnataka

National Institute of Technology, Kurukshetra, Haryana

LLEGE (DEEM
CO ED
NG T
RI

O
EE

BE
NJAB ENGIN

UNI
VERSITY)

Punjab Engineering College, Chandigarh


PU

Rajiv Gandhi Proudyogiki Vishwavidyalaya (Consortium), Bhopal, Madhya Pradesh

Bennett University (Times of India Group), Greater Noida, Uttar Pradesh

Jamia Millia Islamia - A Central University, New Delhi Username: COMP2021


.... & MANY OTHER REPUTED UNIVERSITIES Password: COMP2021

CONTACT US TO KEEP LEARNING!


Ms. Atika KhannaMr. Ranjan RoyMr. Manish Gupta
9999709935 93159052989315905295
PROBLEM SOLVING
AND
PYTHON PROGRAMMING
Fundamentals and Applications: NumPy, Pandas and Matplotlib

HARSH BHASIN
Machine Learning Consultant
Former Assistant Professor
Department of Computer Science
FMIT, Jamia Hamdard
Delhi

Committed to Educate the Nation

IN INDIA
NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS
LONDON • NEW DELHI

Copyright © 2022, New Age International (P) Ltd., Publishers
Published by New Age International (P) Ltd., Publishers
First Edition: 2022

All rights reserved.


No part of this book may be reproduced in any form, by photostat, microfilm, xerography, or any other means, or
incorporated into any information retrieval system, electronic or mechanical, without the written permission of
the publisher.

GLOBAL OFFICES
• New Delhi NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS
7/30 A, Daryaganj, New Delhi-110002, (INDIA), Tel.: (011) 23253472, 23253771
E-mail: [email protected] • Visit us at www.newagepublishers.com

• London NEW AGE INTERNATIONAL (UK) LTD.


27 Old Gloucester Street, London, WC1N 3AX, UK
E-mail: [email protected] • Visit us at www.newacademicscience.co.uk
BRANCHES
• Bangalore 37/10, 8th Cross (Near Hanuman Temple), Azad Nagar, Chamarajpet, Bangalore- 560 018
Tel.: (080) 26756823, E-mail: [email protected]

• Chennai 26, Damodaran Street, T. Nagar, Chennai-600 017, Tel.: (044) 24353401
E-mail: [email protected]

• Cochin CC-39/1016, Carrier Station Road, Ernakulam South, Cochin-682 016


Tel.: (0484) 4051304, E-mail: [email protected]

• Guwahati Hemsen Complex, Mohd. Shah Road, Paltan Bazar, Near Starline Hotel
Guwahati-781 008, Tel.: (0361) 2513881, E-mail: [email protected]

• Hyderabad 105, 1st Floor, Madhiray Kaveri Tower, 3-2-19, Azam Jahi Road, Near Kumar Theater
Nimboliadda Kachiguda, Hyderabad-500 027, Tel.: (040) 24652456
E-mail: [email protected]

• Kolkata RDB Chambers (Formerly Lotus Cinema) 106A, 1st Floor, S N Banerjee Road
Kolkata-700 014, Tel.: (033) 22273773, E-mail: [email protected]

• Mumbai 142C, Victor House, Ground Floor, N.M. Joshi Marg, Lower Parel, Mumbai-400 013
Tel.: (022) 24927869, 24915415, E-mail: [email protected]

• New Delhi 22, Golden House, Daryaganj, New Delhi-110 002, Tel.: (011) 23262368, 23262370
E-mail: [email protected]

ISBN: 978-81-951755-0-5
C-21-07-12777
Printed in India at Ajit Printing Press, New Delhi.
Typeset at J.R. Enterprises, New Delhi.

NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS


7/30 A, Daryaganj, New Delhi - 110002
Visit us at www.newagepublishers.com
(CIN: U74899DL1966PTC004618)
Preface

Python is a robust, Procedural, Object-Oriented, Functional language. The features


of the language make it tremendously valuable for web development, gaming, and
scientific programming. Lately, the language has become incredibly popular. The
popularity of the language can be gauged from the fact that it is currently being used
by Google, YouTube, Bit Torrent and many other giants.
This book deals with problem-solving and programming in Python. Programming
is the soul of Computer Science and designing a program requires involved know-how
of the paradigms along with the ability to use the standard procedures. So, to become
a virtuous programmer one must, therefore, not only learn the syntax of the language
but also develop an ability to apply the concepts learned to solve problems. It may
be stated here that the programming languages and their syntax that a professional
learns are of no use until the algorithms that are to be implemented are well designed.
So a basic knowledge of Data Structures and Algorithms is also essential. This is the
reason that the first section of this book has been dedicated to problem-solving.
One of the most important goals of this book is to make the readers understand
Python’s discriminative features. The ability of Python to deal with multi-dimensional
arrays via numpy has been included in the book. Python also helps in visualization via
matplotlib. The topic has also been presented in section IV of this book.

Harsh Bhasin

vii
Contents

Preface vii
Acknowledgements ix

Section I

Algorithmic Problem Solving and Python Fundamentals


1. Algorithmic Problem Solving 3–23
1.1 Introduction 3
1.2 Definition and Characteristics 4
1.3 Notations: Pseudocode, Flow Chart 5
1.4 Strategies for Problem Solving: Recursion versus Iteration 7
1.5 Asymptotic Notation 10
1.6 Complexity 11
1.7 Illustrations 11
1.8 Conclusion 19
Glossary
20
Points to Remember
20
Exercises
20
Multiple Choice questions
20
Theory
22
Application
22
2. Introduction to Python 24–42
2.1 Introduction 24
2.2 Features of Python 26
2.3 The Paradigms 27
2.4 Chronology and Uses 28
2.5 Installation of Anaconda 31

xi
xii Problem Solving and Python Programming

2.6 Implementation of an Algorithm: Statement, State, Control Blocks and Functions 35


2.7 Conclusion 38
Glossary
39
Points to Remember
39
Resources
39
Exercises
40
Multiple Choice Questions 40
Theory
41
3. Fundamentals 43–70
3.1 Introduction 43
3.2 Basic Input Output 45
3.3 Running a Program 47
3.4 The Jupyter Notebook 50
3.5 Value Type and Reference Type 52
3.6 Tokens, Keywords and Identifiers 53
3.7 Statements 55
3.8 Comments 56
3.9 Operators 57
3.10 Types and Examples of Operators 58
3.11 Basic Data Types 65
3.12 Conclusion 67
Exercises
68
Multiple Choice Questions 68
Theory
69
Explore
70

Section II

Procedural Programming
4. Conditional Statements 73–95
4.1 Introduction 73
4.2 ‘If’, if-else and if-elif-else Constructs 74
4.3 The if-elif-else Ladder 81
4.4 Logical Operators 82
4.5 The Ternary Operator 83
4.6 The Get Construct 84
4.7 Examples 85
4.8 Summary 90
Glossary
91
Points to Remember
91
Contents xiii
Exercises
92
Multiple Choice Questions
92
Programming Exercise
95
5. Looping 96–115
5.1 Introduction 96
5.2 While 98
5.3 Patterns 102
5.4 Nesting and Applications of Loops in Lists 107
5.5 Conclusion 111
Glossary
112
Points to Remember
112
Exercises
113
Multiple Choice Questions
113
Programming
115
6. Functions 116–137
6.1 Introduction 116
6.2 Features of a Function 117
6.3 Basic Terminology 118
6.4 Definition and Invocation 118
6.5 Types of Function 122
6.6 Implementing Search 124
6.7 Scope 125
6.8 Recursion 128
6.9 Conclusion 131
Glossary
132
Points to Remember
132
Exercises
132
Multiple Choice Questions
132
Programming Exercise
133
Questions Based on Recursion
134
Theory
134
Extra Questions
134
7. File Handling 138–158
7.1 Introduction 138
7.2 The File Handling Mechanism 139
7.3 The Open Function and File Access Modes 140
7.4 Python Functions for File Handling 142
7.5 Command Line Arguments 145
7.6 Implementation and Illustrations 147
xiv Problem Solving and Python Programming

7.7 Conclusion 154


Points to Remember
154
Exercises
154
Multiple Choice Questions
154
Theory
157
Programming
157
8. Lists, Tuple and Dictionary 159–180
8.1 Introduction 159
8.2 Lists 160
8.3 Tuple 166
8.4 Associate Arrays and Dictionaries 172
8.5 Conclusion 177
Glossary
177
Points to Remember
177
Exercises
178
Multiple Choice Questions
178
Theory
179
Programming Exercise 180
9. Iterations, Generators and Comprehensions 181–200
9.1 Introduction 181
9.2 The Power of ‘for’ 182
9.3 Iterator 184
9.4 Defining an Iterable Object 187
9.5 Generators 188
9.6 Comprehensions 193
9.7 Conclusion 197
Glossary
197
Points to Remember
197
Exercises
198
Multiple Choice Questions
198
Theory
199
Programming Exercise
199
10. Strings 201–217
10.1 Introduction 201
10.2 Loops Revised 202
10.3 String Operators 205
10.4 In-built Functions 207
10.5 Conclusion 214
Glossary
214
Contents xv
Points to Remember
214
Exercises
215
Multiple Choice Questions
215
Theory
217

Section III

Object Oriented Programming


11. Introduction to Object-Oriented Paradigm 221–236
11.1 Introduction 221
11.2 Creating New Types 223
11.3 Attributes and Functions 223
11.4 Elements of Object-Oriented Programming 228
11.5 Conclusion 232
Glossary
233
Points to Remember
233
Exercises
233
Multiple Choice Questions
233
Theory
236
Explore and Design
236
12. Classes and Objects 237–256
12.1 Introduction to Classes 237
12.2 Defining a Class 238
12.3 Creating an Object 238
12.4 Scope of Data Members 240
12.5 Nesting 243
12.6 Constructor 244
12.7 Multiple __init__(s) 246
12.8 Destructors 248
12.9 Conclusion 251
Glossary
251
Points to Remember
252
Exercises
252
Multiple Choice Questions
252
Theory
254
Programming Exercise 255
13. Inheritance 257–296
13.1 Introduction to Inheritance and Composition 257
13.2 Inheritance: Importance and Types 267
xvi Problem Solving and Python Programming

13.3 Methods 280


13.4 Search in Inheritance Tree 286
13.5 Class Interface and Abstract Classes 288
13.6 Conclusion 291
Glossary
291
Points to Remember
291
Exercises
292
Multiple Choice Questions
292
Theory
294
Programming Exercise
295
14. Operator Overloading 297–318
14.1 Introduction 297
14.2 __init__ Revisited 298
14.3 Methods for Overloading Binary Operators 301
14.4 Overloading Binary Operators: The Fraction Example 302
14.5 Overloading the += Operator 308
14.6 Overloading the > and < Operators 310
14.7 Overloading the __bool__ Operator: Precedence of __bool__ over __len__ 311
14.8 Conclusion 314
Glossary
314
Points to Remember
314
Exercises
314
Multiple Choice Questions
314
Theory
316
Programming
316
15. Exception Handling 319–337
15.1 Introduction 319
15.2 Importance and Mechanism 321
15.3 Build-in Exceptions in Python 323
15.4 The Process 325
15.5 Crafting User Defined Exceptions 328
15.6 An Example of Exception Handling 329
15.7 Conclusion 334
Glossary
334
Points to Remember
335
Exercises
335
Multiple Choice Questions
335
Theory
336
Programming
337
Contents xvii

Section IV

NumPy, Pandas and Matplotlib


16. NumPy–I 341–393
16.1 Introduction 341
16.2 Fundamentals 342
16.3 Functions for Generating Sequences 345
16.4 Aggregate Functions 347
16.5 Generating Random Numbers Using NumPy 350
16.6 Zeros, Ones, Eyes and Full 352
16.7 Indexing 354
16.8 Slicing 355
16.9 Operations: Scalar with an Array 364
16.10 Operations: Array with an Array 375
16.11 Conclusion 391
Exercises
391
Multiple Choice Questions
391
Theory
393
17. NumPy–II 394–419
17.1 Introduction 394
17.2 Joining Arrays 394
17.3 Splitting Arrays 401
17.4 Variance 414
17.5 Covariance 415
17.6 Correlation 417
17.7 Conclusion 417
Exercises
418
Multiple Choice Questions
418
Theory
419
18. Data Visualization-I 420–450
18.1 Introduction 420
18.2 The Plot Function 422
18.3 Plotting Lines and Curves 424
18.4 Additional Arguments 431
18.5 The Bar Chart 442
18.6 Conclusion 448
Exercises
448
Multiple Choice Questions
448
Theory
450
xviii Problem Solving and Python Programming

19. Data Visualization–II 451–469


19.1 Introduction 451
19.2 Box Plot 451
19.3 Frequency Plots and Histogram 454
19.4 The Pie Chart 464
19.5 Conclusion 468
Exercises
469
Multiple Choice Questions
469
Theory
469
20. Pandas–I 470–499
20.1 Introduction 470
20.2 Creating Pandas Series 472
20.3 Indexing, iloc, slicing and Boolean Index 475
20.4 Sorting, Statistical Analysis and String Functions 480
20.5 Creating a Data Frame 482
20.6 Operations on Rows and Columns of a Data Frame 486
20.7 Dealing with Rows 489
20.8 Iterating a Pandas Data Frame 491
20.9 Conclusion 497
Exercises
497
Multiple Choice Questions
497
Theory
498
21. Pandas–II 500–518
21.1 Introduction 500
21.2 Data Frame Methods: Head, Tail, Describe 500
21.3 Boolean Index 503
21.4 Sorting, Descriptive Statistics and Applying String Functions 504
21.5 Reading from a csv File: pandas.read_csv 507
21.6 Missing Values 510
21.7 Conclusion 516
Exercises
516
Multiple Choice Questions
516
Theory
518
Appendix 1: Problems for Practice: Programming Questions 519–528
Appendix 2: Answers to MCQs 529–531
References 532
Web Resources 533–534
Index 535–538
Chapter 1
Algorithmic Problem
Solving

Objectives
After reading the chapter, the reader should be able to
• Understand the importance of algorithms
• Understand the features of a good algorithm
• Understand the ways of writing an algorithm
• Understand asymptotic notations
• Differentiate between recursive and iterative algorithms

1.1 INTRODUCTION
This chapter introduces problem solving and algorithms. Let us begin our discussion
by understanding the term algorithm. The word algorithm comes from algorithmi,
from the title “Algoritmi De Numero Indorum”, a book written by Muhammad Ibn
Musa Al-Khwarizmi, who was a Persian mathematician. The word was corrupted and
became “Algorism”. Finally, in the 19th century, it became algorithm. Interestingly, the
book stated above was on Indian numerals. Lately, the word algorithm is identified
with any procedure applied to accomplish a given computing task.
Algorithm directs how to solve a problem and there can be many algorithms to
solve the same problem. However, not all of them are effective and efficient. Also, it is
desirable that in the sequence of steps for accomplishing a task, each step should be as
basic as possible. The task should be completed in a finite number of steps. So, a good
algorithm should be finite and each instruction should be unambiguous.
Algorithms are implemented using programming languages. However, designing
an algorithm cannot be automated as it is, an art [1]. Art cannot be automated,but
you can at least learn approaches like Divide and Conquer, Backtracking, Branch and
Bound, Dynamic programming, Greedy approaches etc. Learning these approaches

3
4 Problem Solving and Python Programming

would not only help you in Computer Science but also help in other disciplines like
Computational Biology, Finance etc.
Algorithms are used everywhere, right from your set-top box to the machine that
gathers biometric data. The advancements in the field of algorithms has changed the
life of millions. The page rank algorithm of Larry Page has helped in the creation
of Google, which is a part of our life. The routing algorithms allowed packets to
be transferred from one computer to another via shortest paths, and helped in the
advancement of communication. Likewise, the pre-processing algorithms for MRI
(Magnetic Resonance Imaging) has helped scientists to develop computer aided
techniques for the diagnosis of diseases. The conventional techniques clubbed
together with the latest advancements like Deep Learning have been able to solve
many problems of the society.

1.2 DEFINITION AND CHARACTERISTICS


Having seen the importance of algorithms, let us now move to its formal definition and
understand its features. Algorithm is a sequence of steps used to accomplish a given
task. It processes the input and generates some output. The most essential elements of
an algorithm are: input, output, correctness, efficiency and definiteness.
The number of input arguments can even be zero. For example, some of the pseudo
random number generators, do not take any argument to generate a random number.
However, there must be at least one output. The first thing that should be taken care
while designing an algorithm is its correctness. An algorithm which is not correct, is
of no use. No amount of fancy controls or sophisticated techniques can replace the
correctness. Also, there can be numerous ways to solve a given problem but not all of
them are equally efficient.
The efficiency of an algorithm is also important. The algorithm should be efficient
both in terms of time and space. That is, it should take minimum possible time and
space. For example, linear search and binary search are the two most important
techniques of searching. The first takes O(n) time, while the second takes O(log n)
time. That is, a list having 1024 element would take time of order of 1024 units, in the
case of linear search and would take time proportional to 10 units, in the case of Binary
Search. Hence, the choice of efficient algorithm is immensely important to make an
effective model. The meaning of O has been explained in section 1.5.
The above discussion can be summarised as follows,
• An algorithm takes zero or more input,
• It produces at least one output,
• It must be correct,
• It should be efficient, both in terms of memory and space, and
• It should not be ambiguous.
Algorithmic Problem Solving 5

1.3 NOTATIONS: PSEUDOCODE, FLOW CHART


In order to appreciate the discussion, let us come back to the example of Linear Search. In
Linear Search, a list is searched for an item by looking for the item iteratively, that is at each
position of the list. The algorithm for Linear Search can, be stated as follows (Algorithm 1).
Table 1.1 shows the conventions used for writing Algorithms, in this book. The
algorithm can also be represented as a flow chart as shown in Figure 1.1. Pseudocode
and flow chart are the two most commonly used ways of representing an algorithm.
The third way of writing an algorithm can be English like. However, this is not
preferred, as it can be ambiguous.

Algorithm 1: Linear Search

Input:
List: L
Length of the list: n
Item to be searched: item
Algorithm: Linear Search
Set i=0;
Set Flag=0;
While (i<n)
{
if (L[i]==item)
{
Print(“Item found at “,i);
Flag=1;
}
i++;
}
if(Flag==0)
{
Print(“Not found”);
}
}
6 Problem Solving and Python Programming

Linear Search: Flow Chart

Start

Input: List: L, Number of Items: n,


Item to be searched: Item

Set Flag=0
Set i=0

Yes No
i<n L[i]==item

No Yes

Flag=1
If Flag==0 Print(‘Found at’,i)
i++

Yes

No
Not found

Stop

Figure 1.1: Flow chart for Linear Search


Linear Search: English Like
1. Set Flag=0
2. Set the value of i to 0 and start scanning the items of the list. If the item to be
searched is found, set Flag to 1.
3. If Flag==0, then print “Not Found”.
Algorithmic Problem Solving 7

Table 1.1: Conventions used in Algorithms

Convention Description
// Single line comment
/*…*/ Multiple line comment
{} Block
<variable name> = <value> Assignment
a<b Less than operator
a>b Greater than operator
a <= b Less than or equal to operator
a >= b Greater than or equal to operator
a == b Checking equality
a != b Checking of the values of the two variables are not
equal
&& AND operator
|| OR operator

1.4 STRATEGIES FOR PROBLEM SOLVING: RECURSION VERSUS


ITERATION
Algorithms can be recursive or iterative. Recursion is the invocation of a function
inside that function. In order to develop a code using recursion, one must express a
function in terms of itself (with reduced values of the parameters) and should specify
the base condition(s) to serve as the stopping criteria. For example, the nth Fibonacci
term can be expressed as the sum of the (n-1)th and (n-2)th Fibonacci terms. Since, the
evaluation of nth Fibonacci term requires two previous results of evaluation, two base
conditions must be specified. The first and the second terms of this sequences are 1
and 1. Therefore the function can be written as follows.
fib(n) = fib(n – 1) + fib(n – 2)
fib(1) = 1
fib(2) = 1
That is, to find the 5 Fibonacci term, we need to find the sum of the 4th and the 3rd
th

term. The 4th Fibonacci term can be found by adding the 3rd and the 2nd term. The 3rd
Fibonacci term can be found by adding the 2nd and the first term, both of which are 1.
The process has been depicted in Figure 1.2.
8 Problem Solving and Python Programming

Figure 1.2: The evaluation of the 5th Fibonacci term.


Though recursion provides graceful solutions and a way to define a function
mathematically, it is hard to get hold of the way problem is solved using recursion.
As a matter of fact, debugging an intricate program, that uses recursion, is difficult.
Moreover, it is inefficient as it uses function calls.
However, some problems like the in-order, the pre-order and the post-order
traversals of trees; the Depth First Search (DFS) and Breadth First Search (BFS) of
graphs; Binary Search, Quicksort and Merge sort etc., can be easily solved using
recursion. It may be stated here that a problem which can be solved using recursion
can also be solved iteratively.
Recursion uses run-time-stack, which provides a Last-In-First-Out (LIFO) access.
In order to understand this, let us consider the example of calculating factorial using
recursion. The factorial of a number can be calculated using the following formula.
fac(n) = n * fac(n – 1)
fac(1) = 1
Evaluating fac(3) would require multiplying fac(2) with 3. The evaluation of fac(2)
would require multiplying fac(1) with 2. The value of fac(1) is 1. So, in the above
example, fac(3) calls fac(2) which calls fac(1). When fac(1) ends, the run-time-stack stores
the location of fac(2) to return to fac(3). Likewise, when fac(2) ends the run-time-stack
stores the location of fac(3) to return to fac(4). The run-time-stack makes backtracking
easy. Chapter 7 deals with recursion in detail.
Another example of recursion is the calculation of power of a given number. The
power of a number can be calculated using iterative algorithms as shown in program 1.
Algorithmic Problem Solving 9

In the program the variable ‘p’ is initialized to 1. The loop runs ‘b’ times and each time
‘a’ is multiplied by ‘p’. It may be stated here, that the syntax and the nitty-gritty of
programming has been introduced in the following chapters. However, the reader can
revisit this section after completing the next two units.

Program
a=int(input('Enter the first number\t'))
b=int(input('Enter the second number\t:'))
p=1
i=1
while (i<=b):
p=p*a
i+=1
print(a, ' to the power of ', b, 'is ',p)

Output:
Enter the first number 2
Enter the second number :10
2 to the power of 10 is 1024
The above task can also be accomplished using recursion. The following formula
can be used to find ‘a’ to the power of ‘b’.
ab = (ab/2)2, if b is even and
ab = (a(b-1)/2)2 × b, if b is odd
The logic has been implemented in the following program. The output follows.

Program
def pow (a, b):
if b==1:
return a
elif b%2==0:
return (pow(a, b/2)**2)
else:
return ((pow(a, int(b/2))**2)*a)
10 Problem Solving and Python Programming

pow(5,1)
pow(5,2)
pow(5,3)
pow(5,4)

Output:
5
25
125
625
3125

1.5 ASYMPTOTIC NOTATION


An algorithm can be analysed by considering its best and the worst case. For example,
in linear search, the best case would be finding the element at the very first position of
the list. The worst case of this algorithm would be the case where the element is found
at the last position, or is not found.
The asymptotic growth of a function can be defined in terms of its input size, for
a sufficiently large value of the input size, n. The asymptotic notations can be used
to compare the running time or the space requirement of algorithms. The best-case
running time of an algorithm can be depicted by its lower bound and the worst-
case running time can be depicted by its upper bound. The lower bound would
be henceforth represented by big omega, that is, Ω(). The upper bound would be
henceforth represented by big Oh, that is, O(). The formal definition of the two follows.

Big O: O()
The worst-case behaviour of an algorithm is depicted by the asymptotic upper bound
notation. For any two functions f(n) and g(n)
O(g(n)) = f(n), for all n > 0 and
f(n) ≤ c × g(n)

Omega: Ω()
The best-case behaviour of an algorithm is depicted by the asymptotic lower bound
notation. For any two functions f(n) and g(n)
Ω(g(n)) = f(n), for all n > 0 and
f(n) ≥ c × g(n)
Algorithmic Problem Solving 11

Theta: Θ()
The asymptotically tight bound for a function f(n) can be defined as follows. For any two
functions f(n) and g(n).
θ(g(n)) = f(n), for all n > 0 and
c1× g(n) ≤ f(n) ≤ c2× g(n)
It may also be noted that,
f(n) = O(g(n)) and
f(n) = Ω(g(n))
then,
f(n) = θ(g(n))

1.6 COMPLEXITY
The algorithm should be efficient both in terms of memory and time. That is, an
algorithm should take the least amount of space and time. In order to understand
the concept, let us consider five different algorithms to solve the same problem.
Assume that the number of elements given as input to the algorithm is n. The first
algorithm takes time proportional to n to accomplish the given task (O(n)), the second
algorithm takes time proportional to n2 to do the same task (O(n2)), the third takes
time proportional to n3(O(n3)), the fourth takes time proportional to log(n) (O(log n))
and the fifth takes time proportional to n log(n), that is O(n log n). This implies that,
if the number of elements double, the time taken to accomplish the given task by the
first algorithm would double, by the second algorithm would be four times, the third
algorithm eight times, the increase in time of the fourth would be less than the increase
in the first and the increase in the time by the fifth would be less than the increase in
the second. Therefore, the order of the time complexity would be as follows.
O(log n) < O(n) < O(n log n) < O(n2) < O(n3)
For example, linear search described in the following section takes O(n) time
whereas Binary Search takes O(log n) time. Therefore, Binary Search takes lesser time
as compared to Linear Search. Merge sort and bubble sort are the two most popular
algorithms for sorting. The Merge-Sort takes O(n log n) time and Bubble Sort takes
O(n2) time. Therefore, Merge-sort has lesser time complexity vis-à-vis Bubble sort and
is hence better.

1.7 ILLUSTRATIONS
Having seen the definition, characteristics and notations used for writing an algorithm,
let us now move to some basic examples. This section presents four problems and their
solutions.
12 Problem Solving and Python Programming

1.7.1 Minimum in a List

Illustration 1.1:
Given a List, L. Write an algorithm to find the minimum valued element in the list.

Solution:
Let the first element of the list be the minimum valued element (‘min’ = L[0]). The list is
scanned from left to right. At any point, if we are able to find an element having value
less than the value stored in ‘min’, the value of that element is stored in the variable
‘min’. The min1 function performs the requisite task.

Algorithm:
def min1(L):
{
min=L[0];
i=0;
while(i<len(L))
{
if(L[i]<min)
{
min=L[i];
}
i+=1;
}
return min;
}

Test:
min([51,12,71,91,13,19])

Output:
12
Get free access to e-books for 3 Months
Best Selling Textbooks in COMPUTER SCIENCE ENGINEERING
by
Renowned Authors from Prestigious Publishers

PHI
... & many more

Compliments from to all Faculty Members


already LIVE @ Download the App to access e-books:

Indian Institute of Technology Kanpur, Uttar Pradesh

Visvesvaraya Technological University (Consortium), Belgaum, Karnataka

National Institute of Technology, Kurukshetra, Haryana

LLEGE (DEEM
CO ED
NG T
RI

O
EE

BE
NJAB ENGIN

UNI
VERSITY)

Punjab Engineering College, Chandigarh


PU

Rajiv Gandhi Proudyogiki Vishwavidyalaya (Consortium), Bhopal, Madhya Pradesh

Bennett University (Times of India Group), Greater Noida, Uttar Pradesh

Jamia Millia Islamia - A Central University, New Delhi Username: COMP2021


.... & MANY OTHER REPUTED UNIVERSITIES Password: COMP2021

CONTACT US TO KEEP LEARNING!


Ms. Atika KhannaMr. Ranjan RoyMr. Manish Gupta
9999709935 93159052989315905295
PROBLEM SOLVING AND PYTHON PROGRAMMING
Fundamentals and Applications: NumPy, Pandas and Matplotlib

Click Here to Buy the Book Online


Committed to Educate the Nation

IN INDIA
NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS
LONDON • NEW DELHI

You might also like