0% found this document useful (0 votes)
25 views62 pages

Introduction to Data Structures and Algorithms

Uploaded by

Riya Kaku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views62 pages

Introduction to Data Structures and Algorithms

Uploaded by

Riya Kaku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Introduction To

Data Structure
Prof. Hemal Rajyaguru
Data Management Concepts
• A program should give correct results but along with that it
should run efficiently.
• Efficient program executes:
• Minimum time
• Minimum memory space

• To write efficient program we need to apply Data management


concepts.
Data Management Concepts
• Data Management concept includes,
• Data collection
• Organization of data in proper structure

“A data structure is a particular way of storing and


organizing data in a computer so that it can be
used efficiently.”
Data Structure
• When selecting DS, must perform below steps:
1. Analysis of the problem to determine basic operations like
insert/delete/search a data in DS
2. Quantify the resource constraints for each operation
3. Select DS that best meets these requirements

• First- data and operations that are to be performed on


them
• Second – representation of data
• Third – implementation of that representation
Data Types
• A data type is a classification of data, which can store a
specific type of information.

Data Type = Basic Data Type = Primitive Data Type


• Primitive data types are predefined, supported by C language.
• int, char, float, double
Data Types
• Non-Primitive data types are not defined by C language, but
are created by the programmer.
• They are created using the basic data types.
• Example:
1. Linked List
2. Stacks
3. Queue
4. Graph
Types of Data Structure
1. Arrays
An array is a data structure consisting of a collection of similar
data elements, each identified by one array index.

• The elements in array are stored in consecutive memory


locations.
Arrays
• Array Syntex:
int Age[10];

• Limitations of Arrays:

• Fixed size

• Data elements are stored in continuous memory locations which may


not be available always

• Adding and removing of elements is tough because of shifting the


elements from their positions
Types of Data Structure
2. Linked List
• Consisting of a group of nodes which together represent a
sequence.
• Under the simplest form, each node is composed of a
datum and a reference (in other words, a link) to the next
node in the sequence.
• This structure allows for efficient insertion or removal of
elements from any position in the sequence.
Linked List

A linked list whose nodes contain two fields: an integer value


and a link to the next node. The last node is linked to a
terminator (NULL pointer) used to signify the end of the list.
Linked List
• Advantage: Provides quick insert and delete operations

• Disadvantage: Slow search operations and requires more


memory space.(Why?)
• Need to traverse one by one element as they are not having
Index.

• Example : Chain(Hand to hand)


• Find other example
Types of Data Structure
3. Stacks
• Stack can be represented as a linear array.
• Every stack has a variable TOP associated with it, to store
the address of the topmost element of the stack.
Stacks
• A stack is a last in, first out (LIFO) data structure.
If TOP = NULL, then it indicates stack is empty
If TOP = MAX, then it indicates stack is full.
Stacks
• Elements are removed from the stack in the reverse order to
the order of their addition: therefore, the lower elements are
those that have been on the stack the longest.

• Advantage: last-in first-out (LIFO) access.


• Disadvantage: Slow access to other elements.

• Activity: Find 10 more Example


Types of Data Structure
4. Queue

Queue is a data structure in which data can be added to


one end and retrieved from the other.

• You can think of it as a line in a grocery store. The first one in


the line is the first one to be served. Just like a queue.

• A queue is also called a FIFO (First In First Out) to demonstrate


the way it accesses data.
Queue
• Advantage: Provides first-in, first-out data access.
• Disadvantage: Slow access to other items.
Types of Data Structure
5. Trees
• A tree is a widely used data structure that simulates a
hierarchical tree structure with a set of linked nodes.

• Every node contains a left pointer, a right pointer and a data


element.

• Every tree has a root element pointed by a root pointer.

• If root = NULL, tree is empty.


Trees
• A simple unordered tree; in this diagram, the node labelled 7 has
two children, labelled 2 and 6, and one parent, labelled 2. The root
node, at the top, has no parent.
Trees
• Advantage: Provides quick search, insert, delete operations.
• Disadvantage: Complicated deletion algorithm.

• Example: Family Tree


Types of Data Structure
6. Graph
• A graph is a data structure that is meant to implement
the graph concepts from mathematics.

• It is basically a collection of vertices(nodes) and edges that


connect these vertices.

• A graph is often viewed as a generalization of the tree


structure, where complex relationship can be represented.
Graph
• Advantage: Best models real-world situations.
• Disadvantages: Some algorithms are slow and very complex.
Types of Data Structure
1. Linear Data Structure
• Elements are stored sequentially
• We can traverse either forward or backward
• Examples: Arrays, Stacks, Queues, Linked list
2. Nonlinear Data Structure
• Not stored in sequential order
• Branches to more than one node
• Can’t be traversed in a single run
• Examples: Tree, Graphs
Abstract Data Type
• ADT is the way we look at a Data Structure, focusing on what it
does and ignoring how it does its job.

• Examples: Stack, Queue

• When ever end user uses Queue, he is concerned about only


the type of data and operations that can be performed on it.
Abstract Data Type
• Fundamentals of how the data is stored is invisible to users.

• They will have push() and pop() functions only.


Algorithm
“A formally defined procedure for performing some calculation”
• An algorithm provides a blueprint to write a program to solve
a particular problem.

• Algorithm is a set of instructions that solve a problem.

• It is possible to have multiple algorithms to tackle the same


problem, but choice depends on time and space complexity.
Key features of an Algorithm
• Any algorithm will be having finite steps.
• Algorithm exhibits three key features:
1. Sequence
2. Decision
3. Repetition
Sequence
• Each step of an algorithm is executed in a specific order.

• Example:
Step1: Input first number as A
Step2: Input second number as B
Step3: SET SUM = A+B
Step4: PRINT SUM
Step5:END
Decision
• Decision statements are used when outcome of a process
depends on some condition.
• Example: if x = y, then print EQUAL. So general form of IF
construct can be given as below:
IF condition then process

IF condition
THEN process1
ELSE process2
Decision
• Example:
Step1: Input first number as A
Step2: Input second number as B
Step3: IF A = B
Then PRINT “EQUAL”
ELSE
PRINT “NOT EQUAL”
Step4: END
Repetation
• Repetition involves executing one or more steps for a number
of times.
• Which can be implemented using while, do-while and for
loops.
• These loop will execute one or more steps until some
condition is true.
Repetition
• Example:
Step1: [INITIALIZE] SET I=0, N=10
Step2: Repeat Step-2 & 3 while I<=N
Step3:PRINT I
Step4: END
Example#1
AIM: Write an algorithm to find sum of N numbers.
Step1: Input N
Step2: SET I = 0, SUM = 0
Step3: Repeat Step 3 and 4 While I<=N
Step4: SET SUM = SUM + I
SET I = I + 1
Step5: PRINT SUM
Step6:END
Time and Space Complexity
• The analysis of algorithms is the determination of the number
of resources (such as time and storage) necessary to execute
them.

• Time Complexity of an algorithm is basically the running time


of a program, as a function of the input size.
Time and Space Complexity
• Space Complexity of an algorithm is the amount of computer
memory that is required during the program execution, as a
function of input size.
• The space needed by a program depends on:
1) Fixed Part: that varies from problem to problem. It includes
instruction, constants, variables etc.
2) Variable Part: that varies from problem to problem. It
includes space needed for recursion, dynamic value
allocation to variables.
Best, worst and average case
• Best, Worst and Average cases of a given algorithm express
what the resource usage is at least, at most and on average,
respectively.

• In real-time computing, the worst-case execution time is often


of particular concern since it is important to know how much
time might be needed in the worst case to guarantee that the
algorithm will always finish on time.
Best-case performance for algorithm
• The term best-case performance is used in computer science
to describe the way of an algorithm behaves under optimal
conditions.

• For example, the best case for a simple linear search on a list
occurs when the desired element is the first element of the
list.
Worst-case performance for algorithm
• This denotes the behavior of the algorithm with respect to the
worst-possible case of the input instances.

• Worst-case running time of an algorithm is an upper bound on


the running time for any input.

• This provides an assurance that this algorithm will never go


beyond this time limit.
Average-case performance for algorithm
• Running time of an algorithm is an estimate of the running
time for an ‘average’ input.

• It specifies the expected behavior of the algorithm when the


input is randomly drawn from a given distribution.
Time-Space Trade-off
• There can be more than one algorithm to solve a particular
problem.

• One may require less memory space and one may require less
CPU time to execute.

• Hence, there exists a time-space trade-off among algorithms.


Time-Space Trade-off
• So, if space is a big constraint, then one might choose a
program that takes less space at the cost of more CPU time.

• On the contrary, if time is a major constraint then one might


choose a program that minimum time to execute at the cost of
more space.
Expressing Time & Space Complexity
• Time & Space Complexity can be expressed using function f(n),
where n is the input size

• Required when:
1. We want to predict the rate of growth of
complexity as size of the problem increases
2. Multiple algorithms available, but we need
to find most efficient
Expressing Time & Space Complexity
• Most widely used notation to express this function f(n) is Big-
Oh notation.

• It provides upper bound for the complexity.


Algorithm Efficiency
• If a function is linear (without any loop or recursion), the
running time of that algorithm can be given as the number of
instructions it contains.

• Running time may vary because of loops or recursive


functions.
Algorithm Efficiency
• The efficiency is expressed in terms of the number of elements
that has to be processed.

• So, if n is the number of elements, then the efficiency can be


stated as:
f(n) = efficiency
Linear Loops
• To calculate the efficiency with single loop, we first need to
determine number of times the loop will be executed.
for(i=0; i<100;i++)
statement block;
• Here loop factor is 100. Efficiency is directly proportional to
the number of iterations. The general formula:
• f(n)=n
Linear Loops
for(i=0; i<100;i+=2)
statement block;

• Here, the number of iterations are just half the number of


loop factor.

f(n) = n/2
Logarithmic Loops
• In linear loops, the loop update either adds or subtracts.

• In logarithmic loops, the loop-controlling variable is either


multiplied or divided during each iteration of the loop.
Logarithmic Loops
for(i=1; i<100;i*=2)
statement block;
• As loop –controlling variable is multiplied each time, loop will
be executed only 7 times, not 100 times.

• In general terms:
f(n) = log n
Nested Loop
• Loops that contain inner loop.
• In order to analyze nested loops, we need to determine the
number of iterations each loop completes.

No. of No. of
iterations iterations Total no. of
in inner in outer iterations
loop loop
Linear logarithmic loop
• If the loop-controlling variable in inner loop is multiplied after each
iteration.
• for(i=0; i<10;i++)
for(j=1; j<10;j*=2)
statement block;
• Number of iterations in inner loop: log 10
• Number of iterations in outer loop: 10
• Number of iterations in code: 10 log 10
• In general terms: f(n) = n log n
Quadratic Loop
• Number of iterations in inner loop is equal to number of
iteration in outer loop.
• for(i=0; i<10;i++)
for(j=0; j<10;j++)
statement block;

• Generalized formula: f(n) = n2.


Dependent Quadratic Loop
• Number of iterations in inner loop is dependent on outer loop.
• for(i=1; i<=10;i++)
for(j=1; j<i;j++)
statement block;
• The inner loop will execute as:
1 + 2 + . . . + 10 = 55 (As n(n+1)/2=10*(11)/2)
Big O Notation
• Big O notation is used to describe the performance or
complexity of an algorithm.

• Big O specifically describes the worst-case scenario, and can


be used to describe the execution time required or the space
used (e.g. in memory or on disk) by an algorithm.
Thank You

You might also like