HG Notes2
HG Notes2
ppypython
Queue
A queue is a linear structure that allows insertion of elements
from one end and deletion from the other. Thus it follows, First In
First Out(FIFO) methodology. The end which allows deletion is
known as the front of the queue and the other end is known as the
rear end of the queue.
Tree
IV SEM CSE 1
Data structure with python 20CS41P
ppypython
A tree is a non-linear but hierarchical data structure. The topmost element is known as the
root of the tree since the tree is believed to start from the root. The elements at the end of
the tree are known as its leaves. Trees are appropriate for storing data that aren’t linearly
connected to each other but form a hierarchy.
Graph
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect any two
nodes in the graph. A Graph consists of a finite set of vertices(or nodes) and set of Edges
which connect a pair of nodes.
Hashmap
Hash maps are indexed data structures. A hash map makes use of a hash
function to compute an index with a key into an array of buckets or slots.
Its value is mapped to the bucket with the corresponding index. The key is
unique and immutable. In Python, dictionaries are examples of hash
maps.
call the language Python.
IV SEM CSE 2
Data structure with python 20CS41P
ppypython
UNIT 2
ALGORITHM ANALYSIS
Algorithm analysis is the process of evaluating the performance of an
algorithm, usually in terms of its time and space complexity. There are
several ways to analyze the performance of an algorithm, including
asymptotic analysis, which analyzes the behavior of an algorithm as the
size of the input grows indefinitely.
Space complexity refers to the total amount of memory space used by an
algorithm/program, including the space of input values for execution.
Calculate the space occupied by variables in an algorithm/program to
determine space complexity.
IV SEM CSE 3
Data structure with python 20CS41P
ppypython
IV SEM CSE 4
Data structure with python 20CS41P
ppypython
If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive constant
C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
It returns the highest possible output value (big-O)for a given input.
The execution time serves as an upper bound on the algorithm’s time complexity.
IV SEM CSE 5
Data structure with python 20CS41P
ppypython
IV SEM CSE 6
Data structure with python 20CS41P
ppypython
UNIT 3
Algorithm Design Strategies
Types of Brute Force Algorithm in Data Structures
1. Optimizing: In this case, the best solution is found. To find the best solution, it may
either find all the possible solutions to find the best solution or if the value of the
best solution is known, it stops finding when the best solution is found. For
example: Finding the best path for the traveling salesman problem. Here the best
path means that traveling all the cities and the cost of travelling should be minimal.
2. Satisficing: It stops finding the solution as soon as the satisfactory solution is found.
For example, finding the traveling salesman path within 10% of optimal.
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in the wrong order. This algorithm is not suitable for
large data sets as its average and worst-case time complexity are quite high.
We sort the array using multiple passes. After the first pass, the maximum element
goes to end (its correct position). Same way, after second pass, the second largest
element goes to second last position and so on.
In every pass, we process only those elements that have already not moved to
correct position. After k passes, the largest k elements must have been moved to the
last k positions.
In a pass, we consider remaining elements and compare all adjacent and swap if
larger element is before a smaller element. If we keep doing this, we get the largest
(among the remaining elements) at its correct position.
IV SEM CSE 7
Data structure with python 20CS41P
ppypython
IV SEM CSE 8
Data structure with python 20CS41P
ppypython
IV SEM CSE 9
Data structure with python 20CS41P
ppypython
Time and Space Complexity of Linear Search Algorithm:
Time Complexity:
Best Case: In the best case, the key might be present at the first index. So the best case
complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index i.e., opposite to
the end from which the search has started in the list. So the worst-case complexity is O(N)
where N is the size of the list.
Average Case: O(N)
Auxiliary Space: O(1) as except for the variable to iterate through the list, no other variable
is used.
Applications of Linear Search Algorithm:
Unsorted Lists: When we have an unsorted array or list, linear search is most commonly
used to find any element in the collection.
Small Data Sets: Linear Search is preferred over binary search when we have small data
sets with
Searching Linked Lists: In linked list implementations, linear search is commonly used
to find elements within the list. Each node is checked sequentially until the desired
element is found.
Simple Implementation: Linear Search is much easier to understand and implement as
compared to Binary Search or Ternary Search.
Advantages of Linear Search Algorithm:
Linear search can be used irrespective of whether the array is sorted or not. It can be used
on arrays of any data type.
Does not require any additional memory.
It is a well-suited algorithm for small datasets.
Disadvantages of Linear Search Algorithm:
Linear search has a time complexity of O(N), which in turn makes it slow for large
datasets.
Not suitable for large arrays.
IV SEM CSE 10
Data structure with python 20CS41P
ppypython
UNIT 4
Divide and Conquer
Divide and Conquer Algorithm is a problem-solving technique used to solve problems
by dividing the main problem into subproblems, solving them individually and then
merging them to find solution to the original problem. In this article, we are going to
discuss how Divide and Conquer Algorithm is helpful and how we can use it to solve
problems.
2. Conquer:
Solve each of the smaller subproblems individually.
IV SEM CSE 11
Data structure with python 20CS41P
ppypython
3. Merge:
Combine the sub-problems to get the final solution of the whole
problem.
Once the smaller subproblems are solved, we recursively combine their
solutions to get the solution of larger problem.
The goal is to formulate a solution for the original problem by merging
the results from the subproblems.
IV SEM CSE 12
Data structure with python 20CS41P
ppypython
halves back together. This process is repeated until the entire array is
sorted.
IV SEM CSE 13
Data structure with python 20CS41P
ppypython
Choice of Pivot
There are many different choices for picking pivots.
Always pick the first (or last) element as a pivot . The below
implementation picks the last element as pivot. The problem with this
approach is it ends up in the worst case when array is already sorted.
Pick a random element as a pivot . This is a preferred approach because
it does not have a pattern for which the worst case happens.
IV SEM CSE 14
Data structure with python 20CS41P
ppypython
hat offer a
Binary Search Algorithm – Iterative and Recursive Implementation
IV SEM CSE 15
Data structure with python 20CS41P
ppypython
wide
Dynamic Programming (DP) Introduction
range of
IV SEM CSE 16
Data structure with python 20CS41P
ppypython
Ever wondered about the cool math behind the Fibonacci series? This simple pattern
has a remarkable presence in nature, from the arrangement of leaves on plants to the
spirals of seashells. We’re diving into this Fibonacci Series sequence. It’s not just
math, it’s in art, nature, and more! Let’s discover the secrets of the Fibonacci
series together.
What is the Fibonacci Series?
The Fibonacci series is the sequence where each number is the sum of the previous
two numbers of the sequence. The first two numbers of the Fibonacci series are 0 and
1 and are used to generate the Fibonacci series.
unctionaities. These
where, F0 = 0 and F1 = 1.
For example, Fibonacci series 10 terms are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Please refer Different Ways to Find Nth Fibonacci Number for details.
modules and packages help simplify complex tasks by providing pre-built functionalities,
saving developers time and effort.
Program to print first N terms of Fibonacci Series:
Given a number n, our task is to print first n terms of the Fibonacci Series.
Input : n = 5
Output : 0 1 1 2 3
Input n = 1Output : 0
Python’s standard library includes modules and packages for regular expressions, GUI
IV SEM CSE 17
Data structure with python 20CS41P
ppypython
programming, networking, data processing, and much more. This vast ecosystem ensures that
developers have access to a wide array of tools without the need for external dependencies. It
streamlines software development and ensures compatibility across different versions of
Python.
By leveraging the functionalities provided by the standard library, developers can
significantly boost their productivity and reduce development time.
2.Cross-Platform Compatibility
Python’s cross-platform compatibility allows it to run seamlessly on different operating
systems such as Windows, macOS, and Linux. Developers can write their code once and
deploy it on multiple platforms, saving time and effort in application development.
Cross-platform compatibility is crucial in modern software development, as it enables
applications to reach a broader audience across various devices and operating systems.
Python’s ability to run on multiple platforms makes it a versatile choice for developers who
want to build applications that work everywhere.
IV SEM CSE 18
Data structure with python 20CS41P
ppypython
7. Versatility
Python’s versatility is one of its key strengths. It can be used for a wide range of applications,
including web development, data analysis, machine learning, scripting, and more. This
versatility allows developers to use a single language for diverse projects, simplifying their
workflow and reducing the learning curve associated with switching between different
languages.
Furthermore, using a single language like Python for different projects offers several benefits.
It facilitates code reuse, eases maintenance, and simplifies debugging across various
applications. Python’s versatility promotes efficiency and consistency in software
development.
IV SEM CSE 19
Data structure with python 20CS41P
ppypython
The Python Package Index lists thousands of third party modules for Python.
IV SEM CSE 20
Data structure with python 20CS41P
ppypython
Education
Python is a superb language for teaching programming, both at the introductory level and in
more advanced courses.
Desktop GUIs
The Tk GUI library is included with most binary distributions of Python.
Some toolkits that are usable on several platforms are available separately:
wxWidgets
Kivy, for writing multitouch applications.
Qt via pyqt or pyside
Platform-specific toolkits are also available:
GTK+
Microsoft Foundation Classes through the win32 extensions
Software Development
Python is often used as a support language for software developers, for build control and
management, testing, and in many other ways.
Business Applications
Python is also used to build ERP and e-commerce systems:
Python IDE:
IDLE
IV SEM CSE 21
Data structure with python 20CS41P
ppypython
PyCharm
Proprietary software
Spyder
MIT License
Thonny
MIT License
Atom
MIT License
PyDev
Eclipse Public License
Visual Studio
Proprietary software
PyScripter
MIT License
Wing IDE
Proprietary software
IV SEM CSE 22
Data structure with python 20CS41P
ppypython
12 more
Creating a Comment
Comments starts with a #, and Python will ignore them:
Types of comments:
IV SEM CSE 23
Data structure with python 20CS41P
ppypython
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators
1. Keywords
IV SEM CSE 24
Data structure with python 20CS41P
ppypython
2. Identifiers
The initial letter of the identifier should be any letter or underscore (_).
Except for the initial letter, any digit from 0 to 9 can be part of the identification.
Except for the underscore (_), an identifier cannot contain any special characters.
Case matters when it comes to identifier names. Myself and myself, for
example, are not the same thing.
3. Operators
Operators are tokens that, when applied to variables and other objects in
an expression, cause a computation or action to occur. Operands are the
variables and objects to which the computation is applied. There are 7
different operators.
i) Arithmetic Operators
IV SEM CSE 25
Data structure with python 20CS41P
ppypython
( + ) Operands on either right and left sides of the operator are added.
IV SEM CSE 26
Data structure with python 20CS41P
ppypython
( – ) Subtract the right-hand operand from the left-hand operand with the
subtraction operator.
(➗) the left-hand operand by the right-hand operand with the division operator.
(%) a percentage divides the left-hand operand by the right-hand operand and
returns the remainder with the modulus operator.
(>) Check if two operands’ values are not identical (same as the!= operator).
(+=)It adds the right side input to the left side input and then assigns the
result to the left side input.
(-= )Augmented assignment operator- It takes the right side operand and
subtracts it from the left side operand, then assigns the result to the left side
operand.
The logical AND operator makes a condition true if both operands are true or non-zero.
The logical OR operator returns true if one of the two operands is true or non-zero.
v) Bitwise Operators
IV SEM CSE 27
Data structure with python 20CS41P
ppypython
set in one input value but not both, it copies the matching binary 1 to the
result.
IV SEM CSE 28
Data structure with python 20CS41P
ppypython
4. Literals
Literals, tokens in Python, are data elements with a fixed value. Literals
return a value for an object of the specified type. Python supports a
variety of literals:
String Literals
Numeric Literals. These are further of three types, integer, float, and complex literals.
Boolean Literals
Literal Collection
Lists, tuples, dictionaries, and sets are all examples of literal collections in Python.
5. Punctuators
IV SEM CSE 30
Data structure with python 20CS41P
ppypython
UNIT 2
BASIC I/O PERATIONS ,DATA TYPES,OPERATORS
INPUT():
Developers often have a need to interact with users, either to
get data or to provide some sort of result. Most programs
today use a dialog box as a way of asking the user to provide
some type of input. While Python provides us with two inbuilt
functions to read the input from the keyboard.
input ( prompt )
raw_input ( prompt )
input (): This function first takes the input from the user and
converts it into a string. The type of the returned object always
will be <class ‘str’>. It does not evaluate the expression it just
returns the complete statement as String. For example, Python
provides a built-in function called input which takes the input
from the user. When the input function is called it stops the
program and waits for the user’s input. When the user presses
enter, the program resumes and returns what the user typed.
Syntax:
inp =
input('STATEMENT')
Example:
1. >>> name = input('What is your name?\n') # \n --->
newline
---> It causes a line
break
>>> What is your
name? Ram
>>>
print(name)
Ram
IV SEM CSE 31
Data structure with python 20CS41P
ppypython
IV SEM CSE 32
PYTHON PROGRAMMING 20CS31P
String literals: