0% found this document useful (0 votes)
9 views24 pages

1-Introduction To Data Stage and Algorithm-22!05!2024

Uploaded by

Gaming world
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)
9 views24 pages

1-Introduction To Data Stage and Algorithm-22!05!2024

Uploaded by

Gaming world
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
You are on page 1/ 24

Module 1

Overview of Data structures & Algorithms

Stages of Algorithms Development

Proof of correctness
Data structures
• Organization of data in memory

• Group the related items together

• A data structure is a way of organizing data that considers not only


the items stored but also their relationship to each other
Data structures
• Organized data and allowed operations
• Particular way of storing and organizing data in a computer so that it can
be used efficiently
• 2 types
Primitive DS all primitive data types
Integer, floating , double, char, pointers ( fundamental, defined by
programming languages)
Non primitive DS (user defined, derived from primitive)
Linear/sequential: Arrays, Linked lists, Stacks, Queues
Non-linear/non-sequential: Trees, Graphs

01-08-2022 3
On the basis of size the data structure can be classified as: Static
and Dynamic Data structures

• Static ( Memory size and locations are fixed )


The size of such data structures is fixed as the memory is allocated to them
during the compile time. However, the values of the elements stored are not
static and can be modified at any time.
Example - Array.
• Dynamic
The memory space allocated to such data structures can be modified (increased
or decreased) during run time , thus providing more flexibility to manipulate the
data stored in them.
Example - Linked List.

01-08-2022 4
Data structure types

01-08-2022 5
Standard operations

• Traversing
• Insertion
• Deletion
• Updation
• Sorting
• Searching
• Merging

01-08-2022 6
Need for Data structures
• As applications are getting complex and data rich, there are three
common problems that applications face now-adays.
• Data Search − Consider an inventory of 1 million(10^6) items of a store. If
the application is to search an item, it has to search an item in 1
million(10^6 ) items every time slowing down the search. As data grows,
search will become slower.
• Processor speed − Processor speed although being very high, fails if the
data grows to billion records.
• Multiple requests − As thousands of users can search data simultaneously
on a web server, even the fast server fails while searching the data.

• To solve the above-mentioned problems, data structures come to rescue.


Need for data structure
Helps to understand the relationship of one data element with the other and organize it within the
memory

8
Algorithm: Definition & Characteristics
• Algorithm is a finite sequence of unambiguous
instructions for solving a problem i.e., for obtaining
a required output for any legitimate input in a finite
amount of time.
Characteristics of an algorithm
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one meaning.

Input − An algorithm should have 0 or more well-defined inputs.

Output − An algorithm should have 1 or more well-defined outputs, and should match the desired
output.

Finiteness − Algorithms must terminate after a finite number of steps.

Feasibility − Should be feasible with the available resources.

Independent − An algorithm should have step-by-step directions, which should be independent of any
programming code.

Generality: The algorithm must be able to work for a set of inputs rather than a single input.
Stages of algorithm development
Step 1 : Describing the problem.
The problem should be clearly explained, so that it's easy for the developer to find
the solution for the problem.

Step 2: Analyze the problem


To figure out where to start and where to finish while solving the problem.
When determining the starting point, start with following questions:
What data are available?
Where is that data?
What formulas are related to the problem?
What rules are needed for the data?
What relationships exist among the data values?
Stages of algorithm development
When determining the ending point, the characteristics of a solution are described The
following questions help in determining the ending point.
• What new facts will arrive?
• What items will change?
• What changes will have been made to those items?
• What things will no longer exist?

Step 3. Develop a high-level Algorithm


It's typically a good idea to begin with: a high-level method that contains the majority of
a solution's components, but sometimes. more details can be added later
Stages of algorithm development
Problem Statement: I need to make a tea.
Analysis: I don't have milk.
High-level algorithm

 Go to a stores that sells milk.


 Purchase milk and come home.
 Prepare Tea.
Step 4. Add more details to the algorithm to improve it
• Though this algorithm seems to be satisfactory, it lacks many details such as the following.

• Which store I need to visit?


• Which milk product I need to buy?
• How I go to the stores: walk, drive, ride my two-wheeler, take the bus.

• These particulars are taken into account in the next step of our procedure.
Step - by - step refinement refers to the process of gradually
Stages of algorithm development
Step 5 : Review the Algorithm
Examine the algorithm step by step to see if it solves the original problem.

Does this algorithm solve a very specific problem or does it solve a more general
problem

Example

Area of circle having radius 5.2 meters (formula π*5.2 )2


-> Solves very specific

Area of any circle (formula π*R2) -> Solves a more general problem
Stages of algorithm development
Step 5 : Review the Algorithm

 Can this algorithm be simplified?

Example

One formula for computing the perimeter of a rectangle is:

length + width + length + width

simpler formula would be:

2.0 * (length + width)


Stages of algorithm development
Step 5 : Review the Algorithm

Is this solution similar to the solution to another problem? How are they alike? How they
are different?
Example
Consider the following two formulae for computing the area:
Rectangle area = length * width

Triangle area = 0.5 * base * height

Similarities: Each computes an area. Each multiplies two measurements.

Differences: Different measurements are used. The triangle formula contains 0.5.

Hypothesis: Perhaps every area formula involves multiplying two measurements.


Proof of correctness
• Once an algorithm is designed, we need to prove its correctness means
the algorithm yields the desired output for the specific input in a finite
amount of time.
• Depends on the algorithm, For some algorithms, the proof of correctness is easy and
for some algorithms, it is complex
• It depends on the type of instructions in the algorithm
• In order to show that an algorithm is incorrect, one instance of input is
sufficient for which the algorithm fails to give the desired output.
1. Proof by Induction

• Failure to find a counterexample to a given algorithm does not mean


“it is obvious” that the algorithm is correct.
• Mathematical induction is a very useful method for proving the
correctness of recursive algorithms.
• 1. Prove base case
• 2. Assume true for arbitrary value n
• 3. Prove true for case n + 1
Proving by Mathematical Induction
To prove that a statement is true for every natural number n
A recursive algorithm to compute Fibonacci numbers
and show its correctness by mathematical induction.
2. Proof by
Loop Variants
• How do you prove that the algorithm written is correct.
• How do you show that for any input your algorithm will provide right input.
• Loop invariant
• Formally prove with loop invariant, where you state that the desired
property is maintained in your loop.

Prove an invariant of the algorithm is true for all iterations


3-steps
1)Initialization : The invariant(s) is true prior to the first iteration of the loop.
2)Maintenance : If the invariant is true for iteration n, then it remains true for
iteration, n+1.
3)Termination : When the loop terminates, the invariant is true for entire input.

1.The formula is correct BEFORE loop execution


2.The formula is correct DURING loop execution,
including all the steps in between
3.The formula is correct AFTER loop execution
Proof by loop invariant : Example
Proof by loop invariant
Example: Linear search

You might also like