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

Algorithms and Data Structures

The document discusses data structures and algorithms. It defines data structures as organized ways of storing data in a computer for efficient use. Algorithms are defined as sets of instructions for transforming input data into output. The document discusses common data structure operations like traversing, searching, inserting, and deleting. It also covers different types of data structures and algorithms as well as analyzing their performance in terms of time and space complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Algorithms and Data Structures

The document discusses data structures and algorithms. It defines data structures as organized ways of storing data in a computer for efficient use. Algorithms are defined as sets of instructions for transforming input data into output. The document discusses common data structure operations like traversing, searching, inserting, and deleting. It also covers different types of data structures and algorithms as well as analyzing their performance in terms of time and space complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structures and Algorithms

Data Structure and Algorithm Design Goals

 Correctness
 Efficiency

Implementation Goals

 Robustness
 Adaptability
 Reusability

Data Representation

 The behavior of the algorithm when input is in already in order.

 For example in sorting, if elements are already sorted for a specific algorithm.

 The best case running time rarely occurs in practice comparatively with the first and second
case.

Data

 Data Encapsulation or Information Hiding – the concealing of the implementation details of


data object from the outside world

 Data Abstraction – the separation between specification of a data object and its
implementation

 Data Type – a collection of objects and set of operations that act on those objects

 Primitive

 Composite

What is Data Structure?

 In computer science, a data structure is a particular way of storing and organizing data in a
computer so that it can be used efficiently.

 Data may be organized in many different ways, the logical or mathematical model of a particular
organization of data in memory or on disk is called Data Structure.

 Algorithms are used for manipulation of data.

Data Structure Operation

 The data appearing in our data structure is processed by means of certain operations. In fact,
the particular data structure that one chooses for a given situation depends largely on the
frequency with which specific operations are performed. The following four operations play a
major role:
 Traversing

 Accessing each record exactly once so that certain items in the record may be
processed.(This accessing or processing is sometimes called 'visiting" the records.)

 Searching

 Finding the location of the record with a given key value, or finding the locations of all
records, which satisfy one or more conditions.

 Inserting

 Adding new records to the structure.

 Deleting

 Removing a record from the structure.

Usage of Data Structures / Data Structures +Algorithms

 Used by OS, compilers, DBMS, data communications

 Image processing, digital signal processing, simulations, numerical computations, cryptography,


data compressions and genetic studies

Types of Data Structure


Characteristics of data structure

What is an Algorithm?

 An algorithm is a finite set of instructions that takes some raw data as input and transforms it in
to refined data.

 An algorithm is a well-defined list of steps for solving computational problem.

1. Input: Zero or more quantities are externally supplied

2. Output: At least one quantity is produced

3. Definiteness: Each instruction is clear and unambiguous

4. Finiteness: If we trace out the instructions of


an algorithm, then, for all cases, the algorithm terminates after a finite number of steps

5. Effectiveness: every instruction must be basic enough to be


carried out, in principle, by a person using only pencil and paper. Its not enough that each opera
tion be definite as in (3): it also must be feasible.
How to express algorithm?

Many solutions

 Natural language: must be well defined and unambiguous

 Graphic representations: flowcharts

 Programming languages: low level implementation

Recursive Algorithms

 A recursive algorithm calls itself which usually passes the return value as a parameter to the
algorithm again. This parameter is the input while the return value is the output.

 Limitation of recursion (only factorials, ackermann, fibonacci, ...)?

 A tool for theorician?

 Example: Fibonacci – f(0)=0,1,1,2,3,5,8,13,21,34,55

Complexity of Algorithm

 Efficiency or complexity of an algorithm is stated as a function relating the length to the number
of steps (time complexity) or storage location (space complexity). f (n)

 In simple words complexity of an algorithm is the time and space it uses.

Performance Analysis

How to judge a program?

 Does it do what we want it to do?

 Does it work correctly according to original specifications of the task?

 Is there documentation that describes how to use it and how it works?

 Are the functions created in such way that they perform logical subfunctions?

 Is the code readable?

Performance Analysis

 From a performance point of view, we define two criteria:

 Space complexity: the amount of memory needed by a program to run to completion

 Time complexity: the amount of computer time needed by a program to run to comple
tion

 Two phases in performance evaluation

 performance analysis: a priori estimates;

 performance measurement: a posteriori testing.


Space Complexity

 The space needed by a program is seen to be the sum of two components

 fixed part: independent of the characteristics (e.g. number, size) of the inputs and outp
uts

 instruction space (space of the code itself)

 space for constants, ... –

 variable part: dependent on the particular problem instance being solved, hence on the
inputs and outputs characteristics

 variables whose size depends on inputs/outputs,

 recursion stacks (when it depends on inputs/outputs).

Time Complexity

 Try to guess the time complexity experimentaly

 program is typed, compiled and run on a specific machine. Execution time is physically c
locked,

 TP(n) is measured...


But, the value measured is inaccurate (multiuser systems, system load, number of runni
ng programs, ...)

Asymptotic Notation

 Helps to compare algorithms.

 Suppose we are considering two algorithms, A and B, for solving a given problem. Furthermore,
let us say that we have done a careful analysis of the running times of each of the algorithms
and determined them to be Ta(n) and Tb(n),respectively, where n is a measure of the problem
size. Then it should be a fairly simple matter to compare the two functions and to determine
which algorithm is the best!

Types of Analysis

 Worst case running time

 Average case running time

 Best case running time


Worst case Running Time

 The behavior of the algorithm with respect to the worst possible case of the input instance.

 The worst-case running time of an algorithm is an upper bound on the running time for any
input. Knowing it gives us a guarantee that the item does not occur in data.

 There is no need to make an educated guess about the running time.

Average case Running Time

 The expected behavior when the input is randomly drawn from a given distribution.

 The average-case running time of an algorithm is an estimate of the running time for an
"average" input.

 Computation of average-case running time entails "knowing all possible input sequences, the
probability distribution of occurrence of these sequences, and the running times for the
individual sequences”.

 Often it is assumed that all inputs of a given size are equally likely.

Best case Running time

 The behavior of the algorithm when input is in already in order.

 For example in sorting, if elements are already sorted for a specific algorithm.

 The best case running time rarely occurs in practice comparatively with the first and second
case.

Time-Space Tradeoff

 In computer science, a space-time or time-memory tradeoff is a way of solving a problem or


calculation in less time by using more storage space (or memory), or by solving a problem in
very little space by spending a longtime.

 So if your problem is taking a long time but not much memory, a space-time tradeoff would let
you use more memory and solve the problem more quickly.

 Or, if it could be solved very quickly but requires more memory than, you can try to spend more
time solving the problem in the limited memory.
Arrays

 is a collection of Homogenous, Ordered and Finite set of elements.


o -Homogenous implies all elements must be of the same type and have the same
structure
o -Ordered means that elements are organized in sequence.
o -Finite means that each array contains a fixed number of elements.
 -is a Linear data Structure
 -Its data type can be Primitive, Composite or Other Data Types.

Accessing Array

In an array the position of an element is identified by a variable called Index.

 -The range of values for the index is referred to as Index Set.


 -Smallest value in of index is called Lower Bound and largest Upper Bound.
 -Individual elements of an array are identified by Array Name and Array Index.
 -Programming Languages normally use Bracket Notation.

Ex. x[0],x[1], x[2],x[3], x[4]……x[9]

Declaring Array

Before an array is used in a program, it must be declared.

-Compiler reserves necessary storage space for the array on the basis of declaration

-Declaration specifies array name, data type, and optionally size

-Ex intArray = new int[20]

Array Storage Structure

 Storage Structure represents arrangement of data elements in the memory.


 -Array is stored in contiguous memory locations.
 -Number of storage cells allocated depends on the data type.
 -Address of the first element is called Base Address.
Array Operations

Four Basic Array Operations

-Retrieving

-Adding

-Deleting

-Inserting

Retrieving

o -It is also called Accessing


o -To retrieve an element we must know the Index value
o -As computer can directly retrieve an element, accessing is very fast.
o -Ex. X[3] would access the 3rd index.

Adding

 -Adding operation inserts new element in a position specified by the index


 -Adding operation is also sometimes referred to as Assigning.
 -If the array already contains some data, the exiting element is overwritten.
 -Ex. x[2]= 10
Deleting

 -Deleting operation removes an existing element with the given index.


 -To delete x[index], all elements following x[index] are moved one position to the left.
 -In this way the element to be deleted is overwritten by the element to its right.
 -Thus, x[index],x[index+1],x[index+2]….. X[n-1] are shifted to the left.
 -In this way the element to be deleted is overwritten by the element to its right.
 -Thus, x[index],x[index+1],x[index+2]….. X[n-1] are shifted to the left.

Inserting

o -It adds new element in a specified position, without replacing any existing element
o -The position is specified in terms of index value or position relative to an existing
element.
o -Ex. We want to insert element z before x[index] The elements
x[index],x[index+1],x[index+2]….. x[n-1] are moved to the right position
Array Traversing

o -Traversing involves accessing and processing an array element exactly once


o -It is also called Visiting.
o -To traverse, we set up a loop varying from lower to upper bound.
o -Ex. Finding the largest element
 Finding smallest element
 Computing average of all elements
 Printing contents on an array

Advantages of an Array

 An array uses a single variable to represent a large set of homogenous data collection.

 An array provides direct access to a storage address for an element. Therefore, retrieval of an
element is extremely fast.

 The elements on an array can be manipulated easily using an index. Thus, processing of array is
flexible.

 It is easy to create and initialize an array.

 Arrays are good at implementing iterative algorithms.

 Multi-dimensional arrays facilitate grouping of data collection into hierarchical structure

Disadvantages of an Array

 Array size cannot be increased or decreased during runtime.

 Insertion and deletion operations on arrays are not efficient.

 For very large data sets, the program may run out of the storage space

You might also like