algorithm and data structure

29
1 Algorithm & Data structure Sem-II 1395 GHAZNI UNIVERSITY COMPUTER SCIENCE DEPARTMENT ASADULLAH TAREEN [email protected]

Upload: asadullah-tareen

Post on 12-Apr-2017

46 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Algorithm and Data structure

1

Algorithm & Data structure

Sem-II 1395GHAZNI UNIVERSITY

COMPUTER SCIENCE DEPARTMENT

ASADULLAH [email protected]

Page 2: Algorithm and Data structure

05/03/23Algorithm & Data structure 2

Page 3: Algorithm and Data structure

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

Page 4: Algorithm and 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

Page 5: Algorithm and Data structure

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

Page 6: Algorithm and Data structure

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

Page 7: Algorithm and Data structure

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

Page 8: Algorithm and Data structure

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

Page 9: Algorithm and Data structure

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

Page 10: Algorithm and Data structure

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

Page 11: Algorithm and Data structure

Classification of Data Structures

There are two type of data structure

Algorithm & Data structure 11 TAREEN 05/03/23

Page 12: Algorithm and Data structure

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

Page 13: Algorithm and Data structure

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

Page 14: Algorithm and Data structure

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

Page 15: Algorithm and Data structure

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

Page 16: Algorithm and Data structure

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

Page 17: Algorithm and Data structure

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

Page 18: Algorithm and Data structure

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

Page 19: Algorithm and Data structure

Describing algorithm

Algorithm & Datastructure19

Algorithms can be implemented in any programming language

Usually we use “pseudo-code” to describe algorithms

TAREEN 05/03/23

Page 20: Algorithm and Data structure

Implementation

Algorithm & Datastructure20

ComputerInput Output

Problem

Algorithms

TAREEN 05/03/23

Page 21: Algorithm and Data structure

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

Page 22: Algorithm and Data structure

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

Page 23: Algorithm and Data structure

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;}

Page 24: Algorithm and Data structure

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

Page 25: Algorithm and Data structure

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

Page 26: Algorithm and Data structure

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

Page 27: Algorithm and Data structure

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

Page 28: Algorithm and Data structure

05/03/23Algorithm and Data Structure 28

Thank You

Page 29: Algorithm and Data structure

05/03/23Academic Writing29