lecture 1

73
IN THE NAME OF ALLAH THE MOST MERCIFULL AND THE BENIFICIAL

Upload: vicky-butt

Post on 24-Nov-2014

433 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 1

IN THE NAME OF ALLAH THE MOST MERCIFULL AND THE BENIFICIAL

Page 2: Lecture 1

Data Structure and Algorithm

Zafar Mehmood khattak

Lecture 1 (October. 20, 2010)

[email protected]@uog.edu.pk

Page 3: Lecture 1

Course Information 3-Credit Hours Course Course Title:

Introduction to Data Structure and Algorithm Textbooks: Data Structure in C++ by CM Aslam, TA Qureshi

Data Structure using C++ by DS Malik Data Structure and Algorithm Analysis in C, Addison

Wesley, 1997. Data Structures and Algorithms (SAMS teach

yourself), Lafore, Sams Publishing, 1999.

Grading: Quizzes (10) Announced/Unannounced Assignment (5) project/presentation (10), Midterm test (25%) Final exam (50%)

Page 4: Lecture 1

Introduction to Data Structure and Algorithm

BS-3rd TermDepartment of CS&ITUniversity of Gujarat

Page 5: Lecture 1

What you will learn

This course will focus on solving problems efficiently

you will be introduced to a number of fundamental data structures and algorithms (or procedures) for manipulating them.

Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs.

Implement data structures in C++

Page 6: Lecture 1

What you will learnAfter following this course, students should:

Be able to understand the importance of complexity analysis of algorithms

Be aware of basic algorithm design techniques

Have the understanding of common data structures used in algorithms

Be reasonably able to select appropriate data structures and algorithms for a given situation

Page 7: Lecture 1

AL 7

Need for Data Structures

Data structures organize data more efficient programs.

More powerful computers more complex applications.

More complex applications demand more calculations.

Page 8: Lecture 1

AL 8

Organizing Data

Any organization for a collection of records that can be searched, processed in any order, or modified.

The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.

Page 9: Lecture 1

AL 9

Efficiency

A solution is said to be efficient if it solves the problem within its resource constraints. Space Time

The cost of a solution is the amount of resources that the solution consumes.

Page 10: Lecture 1

AL 10

Selecting a Data Structure

Select a data structure as follows:1. Analyze the problem to determine the

resource constraints a solution must meet.2. Determine the basic operations that must be

supported. Quantify the resource constraints for each operation.

3. Select the data structure that best meets these requirements.

Page 11: Lecture 1

AL 11

Some Questions to Ask

Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations?

Can data be deleted? Are all data processed in some well-defined

order, or is random access allowed?

Page 12: Lecture 1

AL 12

Data Structure Philosophy

Each data structure has costs and benefits. Rarely is one data structure better than

another in all situations. A data structure requires:

space for each data item it stores, time to perform each basic operation, programming effort.

Page 13: Lecture 1

AL 13

Goals of this Course

1. Reinforce the concept that costs and benefits exist for every data structure.

2. Learn the commonly used data structures. These form a programmer's basic data structure

“toolkit.”

3. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits

of new data structures that you or others might invent.

Page 14: Lecture 1

Outline Syllabus Introduction Analysis of Algorithms Basic data structures and operations

on them Arrays Stacks Queues Linked lists Trees Graphs Hash tables

Page 15: Lecture 1

Outline Syllabus Recursion Sorting Searching Complexity of Algorithms Polynomial and Intractable Algorithms Basic algorithm design techniques

Divide-and-conquer Greedy approach Dynamic Programming

Page 16: Lecture 1

Today Topics Introduction

Programming language review Data & Information What is Data Structures? Abstract Data types Categories of Data Structures Operations on Data Structures

Analysis of Algorithms What is Algorithms? Analyzing Algorithms Algorithmic Notation

Page 17: Lecture 1

Programming Language : Definition

A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks.

Page 18: Lecture 1

“The programming language problem has been solved; the name of the solution is C.”

- Dean of a top-3 research university, circa 1991.

“Well, Java”

- Same dean, several years later.

A Common Perception...

Page 19: Lecture 1

The Reality...

C

Java

C++ C#

Haskellperl

Python

Ruby

PHP

javascript

SQL

Visual Basic

Tcl/TK

regular expressions

F#XSLT

awk R

S

postscript

latex

make ML

Ocaml

ActionScript

bashXQuery

Page 20: Lecture 1

Evolution of Programming languagesFirst Generation : Machine languages

Strings of numbers giving machine specific instructions Example:

+1300042774+1400593419+1200274027

Second Generation : Assembly languages English-like abbreviations representing elementary

computer operations (translated via assemblers) Example:

LOAD BASEPAYADD OVERPAYSTORE GROSSPAY

Third Generation : High-level languages Codes similar to everyday English Use mathematical notations (translated via compilers) Example: grossPay = basePay + overTimePay

Page 21: Lecture 1

PL hierarchy

Page 22: Lecture 1

Different types of High-level PL

Weakly typed/strongly typed Structured And many other types

Page 23: Lecture 1

Typed Languages

Type information was added to programs to improve efficiency. For ex. An integer addition is performed

more efficiently than floating point addition.

Hence it is more advantageous to declare the value/variable as integer whenever it is possible.

Page 24: Lecture 1

Weakly Typed/Strongly Typed Language A strongly-typed programming language is

one in which each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.

Certain operations may be allowable only with certain data types. The language compiler enforces the data typing and use compliance.

Page 25: Lecture 1

Strongly Typed Language contd..

Different definitions are given for a language to be strongly typed and if that condition is not defined by a particular language it is said to weakly typed in that context.

Page 26: Lecture 1

For example..1. A language is strongly typed if it contains

compile -time checks for type constraint violations. If all checking is deferred to run time, it is weakly typed.

2. A language is strongly typed if it contains compile- or rum-time checks for type constraint violations. If no checking is done, it is weakly typed.

3. A language is strongly typed if conversions between different types are forbidden. If such conversions are allowed, it is weakly typed.

Page 27: Lecture 1

More Examples..

4. A language is strongly typed if conversions between different types must be indicated explicitly. If implicit conversions are performed, it is weakly typed.

Page 28: Lecture 1

Where does C fit in?

For example, under definitions 3,4 the C Language is weakly typed; — with definitions 1 and 2 it is open for further debate since C does perform type checks for compound types but not for scalar or array types.

Page 29: Lecture 1

C++/Java?

C++ and Java are stronger typed than C.

Page 30: Lecture 1

Overview of different programming languages

Imperative programming paradigm Structural programming languages Procedural programming languages

Declarative programming paradigm Functional programming languages

Modular programming paradigm Object oriented programming languages

http://www.answers.com/topic/programming-language

Page 31: Lecture 1

Structured Programming

Structured programming Disciplined approach to writing

programs Clear, easy to test and debug and easy

to modify

Structured programming is hard and takes time to master

Page 32: Lecture 1

Structured Programming……. Discipline for organizing and coding

programs. Simplifies control paths so that

programs can be easily understoodand modified.

Uses basic control structures and modules that have only one entry point and one exit point.

Page 33: Lecture 1

Control Structures

Basic Control Constructs Sequence Structure Selection Structure Iteration Structure

Advanced Control Construct Case Structure

Page 34: Lecture 1

Sequence Construct

Single steps or actions in the program logic

Statements executed in the order of appear-ance, with control passing unconditionally from one statement to the next. The program executes

Statement A followed by Statement B.

InitializeVariables

IncrementCounter

A

B

Page 35: Lecture 1

Selection Construct

A decision point in a procedure in which the outcome of a stated condition determines which of two actions will be taken.

Tests a condition and executes one of the two alternative instruction sets based on the results of the test.

Page 36: Lecture 1

Selection Constructcontinued

IF Hours is greater than 40

THEN Compute Overtime

PayELSE Compute Regular PayENDIF

Y NHours > 40

ComputeOvertime

ComputeRegular

Page 37: Lecture 1

Iteration Construct

The logic pattern in programming in which certain actions are repeated whenever a specified condition occurs.

The cycle repeats until such time as the specified condition no longer occurs

Page 38: Lecture 1

Iteration Construct ~ Test Before Looping (WHILE)

First, test the control condition IF condition is true

THEN perform the processELSE continue with the program

Process loops back to the condition

Condi-tion

F

PerformProcess

T

Page 39: Lecture 1

Iteration Construct ~ Perform Before Testing (FOR - NEXT)

A process (which may consist of one or more sequences) is executed.

The condition is tested IF the condition is

true

THEN repeat the processELSE continue the program

PerformProcess

Condi-tion

T

F

Page 40: Lecture 1

Structured programming

Only the following code structures are used to write programs:

1. Sequence of sequentially executed statements.

2. Conditional execution of statements (i.e., "if" statements).

3. Looping. 4. Structured SubRoutine calls (e.g.,

'gosub' but not 'goto').

Page 41: Lecture 1

Structure Programming contd..

In particular, the following language usage is forbidden:

"GoTo" statements. "break" or "continue" out of the middle

of loops. Multiple exit points to a

function/procedure/subroutine (i.e., multiple "return" statements).

Multiple entry points to a function/procedure/subroutine.

Page 42: Lecture 1

Data & Information Data

Collection of Raw Facts & Figures

Information

The processed data that gives useful meaning

Page 43: Lecture 1

Software Development

5 phases of software development problem analysis and specifications design coding testing maintenance

Page 44: Lecture 1

Analysis and Specification

Statement of specifications Customer requirements

Page 45: Lecture 1

Design

Objects Operations Algorithms Algorithms + Data Structures =

Programs

Page 46: Lecture 1

Coding

Language Style Integration Correctness / Readability /

Understandability Good Programming Practices

Page 47: Lecture 1

Testing

Validation are we building the right product (checking that

the documents, program modules, etc. are according to customer’s requirements.)

Verification Are we building the product right (checking

that products are correct, complete, consistent)

Black Box Testing (no structural testing) White Box Testing (examining internal

structure)

Page 48: Lecture 1

Maintenance

Bugs Modifications Enhancements

Page 49: Lecture 1

Data Types

Simple char, int, float, double

Structured arrays, structures, unions, classes

Advanced lists, queues, stacks, trees, graphs

Page 50: Lecture 1

ADTs

Abstraction separating data from implementation

ADT a collection of related data items

together with basic operations between them and operations to be performed on them

Page 51: Lecture 1

Abstract data types another What does ‘abstract’ mean? From Latin: to ‘pull out’—the essentials

To defer or hide the details Abstraction emphasizes essentials and defers the

details, making engineering artifacts easier to use

I don’t need a mechanic’s understanding of what’s under a car’s hood in order to drive it What’s the car’s interface? What’s the implementation?

Page 52: Lecture 1

52

Abstract Data Type (ADT) another def….

Def.

e.g. Phone book Basic operations: find number by name, add a new entry,

delete an entry, list all entries in orderWhy "abstract?" Data, operations, and relations are studied

independent of implementation.

What not how is the focus.

a collection of related data items together with an associated set of operations

Page 53: Lecture 1

53

Data Structures

Goal: to organize data Criteria: to facilitate efficient

storage of data retrieval of data manipulation of data

Design Issue: select and design appropriate data

types. (This is the real essence of OOP.)

Page 54: Lecture 1

Categories of Data Structures

Primitive or nonlinear data structure

The data structures whose elements are arranged in non-linear or non-sequence form are nonlinear data structures.

trees and graphs are nonlinear data structures

Page 55: Lecture 1

Categories of Data Structures

Non-Primitive or linear data structure

The Data structure whose elements are arranged in a sequence is called linear data structure.

Arrays, Linked Lists, Queues, Stacks etc are linear data structures

Page 56: Lecture 1

Operations on Data Structures Some commonly used operations performed on data

structures

Inserting: adding new data items into a data structure

Deleting: removing data items from a data structure Searching: finding specific data items in a data

structure Traversing: accessing each record or item in a data

structure exactly once for processing Sorting: arranging data items in a data structure

into a specific order Merging: combing two lists of data items into a

single data list

Page 57: Lecture 1

What is Algorithm?

An algorithm is a recipe or a well-defined procedure for performing a calculation, or in general, for transforming some input into a desired output.

Step-by-step procedure to solve a particular problem.

Story of the algorithm.

Page 58: Lecture 1

Analyzing Algorithm Why analyze algorithm?

To improve or to choose one among many

Criteria for analysis Correctness Amount of work done (efficiency) Amount of space used Simplicity, Clarity Optimality (whether it’s “the best possible”?)

Page 59: Lecture 1

Analyzing Algorithm

Amount of work done (efficiency)

Compare execution time of 2 algorithms? Perhaps OK, but …. Vary from computer to

computer and between different inputs

The number of instructions executed? Dependent on programmer, language,

compiler, machine architecture

Page 60: Lecture 1

Algorithmic Notation

Name of Algorithm Introductory Comment Steps Comments Variable name Operators Assignment Statement

Page 61: Lecture 1

Algorithmic Notation INPUT & OUTPUT STATEMENTS

SELECTION STATEMENTS

LOOPING STATEMENTS

SUB ALGORITHMS Type of Sub-Algorithm

Function sub-algorithm Procedure sub-algorithm

Page 62: Lecture 1

Another definition

Informally, an algorithm is any well defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.

Thus an algorithm is a sequence of computational steps that transform the input into the output.

Algorithm is a tool to solve computational problem.

Page 63: Lecture 1

Example of an algorithm

Problem: find the maximum (largest) value in a finite sequence of integers.

We want to develop an algorithm that can be used whenever the problem of finding the largest element in a finite sequence of integers.

Page 64: Lecture 1

For example : we have 15, 8, 20 , 7, 52, 30,23 as the sequence of integers.

We want an algorithm that would give 52 as an answer (since 52 is the largest in this sequence of integers) when applied to this list.

Page 65: Lecture 1

Solution of finding the maximum integer:

We perform the following steps.

1. Set the temporary maximum equal to the first integer in the sequence. (The temporary ,maximum will be the largest integer examined at any stage of the procedure.)

2. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the

Page 66: Lecture 1

Continued…

temporary maximum equal to this integer.

3. Repeat the previous step if there are more integers in the sequence.

4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.

Page 67: Lecture 1

Pseudocode

We can specify an algorithm in many ways, like we can use English or a programming language.

We employ whatever method is clear and concise to specify an algorithm.

We will use a pseudocode to specify algorithms.

Pseudocode provides an intermediate step between an algorithm written in English or in a programming language.

Page 68: Lecture 1

Why use pseudocode?

When an algorithm is written in English, it is difficult to conceive how that algorithm can be performed on a computer.

Use of a programming language to specify an algorithm often leads to description that is complicated and difficult to understand. Furthermore, since many programming languages are in common use so it would be undesirable to choose one particular language.

Page 69: Lecture 1

Pseudocode for finding maximum in a list

Procedure max(a1,a2,…,an: integers)

max:= a1

for i := 2 to n

if max < ai then max := ai

// max is the largest element //

Page 70: Lecture 1

Some concepts regarding algorithms

The input sequence 15, 8, 20 , 7, 52, 30,23 which we used as our example of “finding the maximum integer in a sequence” is called instance of the problem.

An algorithm is said to be correct if, for every input instance, it halts with the correct output. We say that a correct algorithm solves the given computational problem.

Page 71: Lecture 1

Properties of Algorithms

There are several properties that algorithms generally share. They are:

Input: An algorithm has input values from a specified.

Output: From each set of values an algorithm produces output values from a specified set. The output values are the solution of the problem.

Page 72: Lecture 1

Definiteness: The steps of an algorithm must be defined precisely.

Correctness: An algorithm should produce the correct output values for each set of input values.

Finiteness: An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set.

Page 73: Lecture 1

Next Lecture Arrays Data Structures

One Dimensional Array Two Dimensional Array N Dimensional Array

Operations on Arrays Insert Delete Traversing