algorithm and data structure
TRANSCRIPT
1
Algorithm & Data structure
Sem-II 1395GHAZNI UNIVERSITY
COMPUTER SCIENCE DEPARTMENT
ASADULLAH [email protected]
05/03/23Algorithm & Data structure 2
Recommended books
Data Structures By Seymour Lipschutz [Schaum’s Outline]
An Introduction to Data structures with Applications by Tremblay and Sorenson
3 TAREEN 05/03/23Algorithm & Data structure
Data are values or a set of valuesData item refers to single unit of valuesData item
Group item : Data item that can be subdivided into sub item. Ex Name : First Name, Middle initial and Last Name
Elementary item: Data item that can not be sub divided into sub item Ex : PAN card number / Bank Pass Book Number is treated as single item
Data Type and Data Structure
TAREEN 05/03/23Algorithms and Data structure 4
Collection of data are frequently organized into a hierarchy of fields, records and files
Entity : Something that has certain attributes or properties which
may be assigned valuesValues may be numeric or non-numeric
Ex: The employee of an organization Attributes Name Age Sex Employee CodeValues John 33 M 13472
Data Type and Data Structure
Algorithm & Data structure 5 TAREEN 05/03/23
Cont…Entity with similar attributes ( e.g all employees of an
organization) form an entity set
Each attribute of an entity set has a range of values [ the set of possible values that could be assigned to the particular attribute]
Information: Data with given attribute or processed data
Algorithm & Data structure 6 TAREEN 05/03/23
Field is a single elementary unit of information representing an attribute of an entity
Record is the collection of field values of a given entity
File is the collection of records of the entities in a given entity set
TAREEN 05/03/23Algorithm & Data structure 7
Data Type
Algorithms and Data structure 8
A data type is a term which refers to the kind of data that may appear in computation.
Set of possible values for variablesOperations on those valuesEx : int, float, char ……….
TAREEN 05/03/23
Data Structure
Algorithms and Data structure 9
A data structure is an arrangement of data in a computer's memory or even disk storage.
The logical and mathematical model of a particular organization of data is called a data structure.
A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
TAREEN 05/03/23
The choice of a model depends on two factor1. It must be rich enough in structure to mirror the actual
relationships of the data in the real world 2. The structure should be simple enough that one can
effectively process the data when necessary Some example of common data structures are
Arrays Linked List Stack Queues, Trees and Graph
Con…
Algorithm & Data structure 10 TAREEN 05/03/23
Classification of Data Structures
There are two type of data structure
Algorithm & Data structure 11 TAREEN 05/03/23
Major OperationsThe data appearing in our data structure is processed
by means of certain operations. The following operations play a major role:
Traversing: Accessing each record exactly once so that certain items in the record may be processed. Searching: Finding the location of the one or all record with a given key value, or more conditions.Inserting: Adding new records to the structure.Deleting: Removing a record from the structure. Sorting: Arranging the records in some logical order Merging: Combining the records in two different sorted files into a single sorted files
Algorithm & Data structure 12 TAREEN 05/03/23
Literature
TAREEN 05/03/23Algorithm & Data structure13
Two ideas lie gleaming on the jeweler's velvet. The first is the calculus, the second, the algorithm.
The calculus and the rich body of mathematical analysis to which it gave rise made modern science possible; but it has been the algorithm that has made possible the modern world.
David Berlinski, The Advent of the Algorithm, 2000
Algorithm
An essential aspect to data structures is algorithms. Data structures are implemented using algorithms.
Even though there is no universally agreed-on wording to describe this notion, there is general agreement about what the concept means:
An algorithm is a well-defined set of rules for solving problems. OR
A clearly specified set of simple instructions to be followed to solve a problem.
TAREEN 05/03/2314
What is an Algorithm?
Algorithm & Datastructure15
An algorithm is a 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.
An Algorithm is a finite step – by – step list of well defined instructions for solving a particular problem. It is used to manipulate the data contained in the data structures as in searching and sorting. It states explicitly how the data will be manipulated.
TAREEN 05/03/23
What is an Algorithm?
Algorithm & Datastructure16
An algorithm is a well-defined set of rules for solving problems. OR
A clearly specified set of simple instructions to be followed to solve a problem.
A step-by-step procedure for taking any instance of a problem and producing a correct answer for that instance.
Transfer input to output.
TAREEN 05/03/23
Example
Algorithm & Data structure17
Computing the nth Fibonacci Term1, 1, 2, 3, 5, 8, 13, 21..fn = fn 1 + fn 2public class fibapp { public static void
main(String[] args) {
System.out.println(fib (10));
}
public static long fib( int n) {long a, b, c, i; if (n<2) return n;else { a=0; b=1; for(i=2; i<=n; i++) { c=a+b; a=b; b=c; }return b;} } }
TAREEN 05/03/23
An algorithm possesses the following properties:It must be correct.It must be composed of a series of concrete
steps.There can be no ambiguity as to which step will
be performed next.It must be composed of a finite number of steps.It must terminate.It takes zero or more inputsIt should be efficient and flexibleIt should use less memory space as much as
possibleIt results in one or more outputs
Algorithm Properties
Algorithm & Datastructure18 TAREEN 05/03/23
Describing algorithm
Algorithm & Datastructure19
Algorithms can be implemented in any programming language
Usually we use “pseudo-code” to describe algorithms
TAREEN 05/03/23
Implementation
Algorithm & Datastructure20
ComputerInput Output
Problem
Algorithms
TAREEN 05/03/23
Greatest Common DivisorThe first algorithm “invented” in history was
Euclid’s algorithm for finding the greatest common divisor (GCD) of two natural numbers
Definition: The GCD of two natural numbers x, y is the largest integer j that divides both (without remainder). I.e. j|x, j|y and j is the largest integer with this property.
The GCD Problem:Input: natural numbers x, y Output: GCD(x,y) – their GCD
Algorithm & Data structure21 TAREEN 05/03/23
Euclid’s GCD Algorithmpublic static int gcd(int x, int y) { while (y!=0) { int temp = x%y; x = y; y = temp; } return x;}
Algorithm & Data structure22 TAREEN 05/03/23
Euclid’s GCD Algorithm – sample run
Example: Computing GCD(48,120) temp x y After 0 rounds -- 72 120 After 1 round 72 120 72After 2 rounds 48 72 48After 3 rounds 24 48 24After 4 rounds 0 24 0
Output: 24
while (y!=0) { int temp = x%y; x = y; y = temp;}
Why do you need to study algorithms?
Algorithm & Data structure24
If you are going to be a computer professional, there are both practical and theoretical reasons to study algorithms.
I. From a practical standpoint, you have to know a standard set of important algorithms from different areas of computing; in addition, you should be able to design new algorithms and analyze their efficiency.
II.From the theoretical standpoint, the study of algorithms, sometimes called algorithmics, has come to be recognized as the cornerstone of computer science.
TAREEN 05/03/23
Key points
Algorithm & Data structure25
The non mbiguity requirement for each step of an algorithm cannot be compromised.
The range of inputs for which an algorithm works has to be specified carefully.
The same algorithm can be represented in several different ways.
Several algorithms for solving the same problem may exist.
Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds.
TAREEN 05/03/23
Fundamentals of Algorithmic Problem Solving
Algorithm & Data structure26
As we consider algorithms to be procedural solutions to problems.
We now list and briefly discuss a sequence of steps one typically goes through in designing and analyzing an algorithm.
1. Understanding the Problem2. Ascertaining or conforming the Capabilities of a Computational
Device3. Choosing between Exact and Approximate Problem Solving.4. Deciding on Appropriate Data Structures.5. Algorithm Design Techniques.6. Methods of Specifying an Algorithm.7. Proving an Algorithm's Correctness.8. Analyzing an Algorithm.9. Coding an Algorithm.
TAREEN 05/03/23
Important problem types
Algorithm & Data structure27
In the limitless sea of problems one encounters in computing, there are a few areas that have attracted particular attention from researchers. By and large, interest has been driven either by the problem's practical importance or by some specific characteristics making the problem an interesting research subject; fortunately, these two motivating forces reinforce each other in most cases.
Sorting Searching String processing Graph problems Combinatorial problems Geometric problems Numerical problems
TAREEN 05/03/23
05/03/23Algorithm and Data Structure 28
Thank You
05/03/23Academic Writing29