IN THE NAME OF ALLAH THE MOST MERCIFULL AND THE BENIFICIAL
IN THE NAME OF ALLAH THE MOST MERCIFULL AND THE BENIFICIAL
Data Structure and Algorithm
Zafar Mehmood khattak
Lecture 1 (October. 20, 2010)
[email protected]@uog.edu.pk
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%)
Introduction to Data Structure and Algorithm
BS-3rd TermDepartment of CS&ITUniversity of Gujarat
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++
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
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.
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.
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.
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.
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?
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.
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.
Outline Syllabus Introduction Analysis of Algorithms Basic data structures and operations
on them Arrays Stacks Queues Linked lists Trees Graphs Hash tables
Outline Syllabus Recursion Sorting Searching Complexity of Algorithms Polynomial and Intractable Algorithms Basic algorithm design techniques
Divide-and-conquer Greedy approach Dynamic Programming
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
Programming Language : Definition
A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks.
“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...
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
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
PL hierarchy
Different types of High-level PL
Weakly typed/strongly typed Structured And many other types
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.
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.
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.
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.
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.
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.
C++/Java?
C++ and Java are stronger typed than C.
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
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
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.
Control Structures
Basic Control Constructs Sequence Structure Selection Structure Iteration Structure
Advanced Control Construct Case Structure
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
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.
Selection Constructcontinued
IF Hours is greater than 40
THEN Compute Overtime
PayELSE Compute Regular PayENDIF
Y NHours > 40
ComputeOvertime
ComputeRegular
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
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
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
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').
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.
Data & Information Data
Collection of Raw Facts & Figures
Information
The processed data that gives useful meaning
Software Development
5 phases of software development problem analysis and specifications design coding testing maintenance
Analysis and Specification
Statement of specifications Customer requirements
Design
Objects Operations Algorithms Algorithms + Data Structures =
Programs
Coding
Language Style Integration Correctness / Readability /
Understandability Good Programming Practices
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)
Maintenance
Bugs Modifications Enhancements
Data Types
Simple char, int, float, double
Structured arrays, structures, unions, classes
Advanced lists, queues, stacks, trees, graphs
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
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?
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
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.)
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
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
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
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.
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”?)
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
Algorithmic Notation
Name of Algorithm Introductory Comment Steps Comments Variable name Operators Assignment Statement
Algorithmic Notation INPUT & OUTPUT STATEMENTS
SELECTION STATEMENTS
LOOPING STATEMENTS
SUB ALGORITHMS Type of Sub-Algorithm
Function sub-algorithm Procedure sub-algorithm
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.
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.
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.
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
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.
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.
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.
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 //
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.
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.
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.
Next Lecture Arrays Data Structures
One Dimensional Array Two Dimensional Array N Dimensional Array
Operations on Arrays Insert Delete Traversing