Instructor : Kiran Ijaz Lecture : 01 Date : 5 th August, 2008
Instructor : Kiran Ijaz Lecture : 01Date : 5th August, 2008
Session Outline and Learning OutcomesThe learning outcomes of the session
are to:Understand the contents and requirements
of the courseUnderstand the fundamental concepts
behind computer programsDefine Data Structures and their purpose
along with their associated operationsUnderstand and practically apply the
Performance Measures of Algorithms through Time Complexity Analysis
Life Lines for Course Execution Instructor: Kiran Ijaz
Office: N -111B email: [email protected]
Online course content & coordination: Course Folder:
\margala\DS_C&D_Fall08 Group Home Page:
Will be announced in next calss Course Yahoo Group Email:
Will be announced in next calssTeaching Assistant: Tayyab Bin Tariq & Ehtasham-ul-HaqOffice Hours:
Monday : 12:00 to 2:30 Wednesday: 1:00 to 2:00 Friday: 10:30 to 12.30
Course ContentsIntroductionAlgorithm Complexity AnalysisSimple Data Types and Abstract Data TypesArrays and ListsElementary Data StructuresStack and QueuesRecursion and Time Complexity of Recursive
AlgorithmsTrees and GraphsSet structureSearching techniquesHashingSorting techniques
Textbooks and Supplementary Reading ListIntroduction to Algorithms by Thomas
H. Cormen et alData Structures and Algorithms by A. V.
Aho, J. E. Hopcroft, J. D. UllmanData Structures using C and C++ 2nd
Edition by Yedidyah Langsam, Moshen J. Augenstein and Aaron M. Tenenbaum
Theory and problem of Data structures By Seymour Lipschutz
AssessmentAssignments 10%Quizzes (Not Announced) 10%Project 15%Mids 25% (12.5+12.5)Final 40%
No retake of quizzes, mids or finalNo extension in deadline of assignments and projectSubmission guidelines must be followedSubmit compressed files (Winzip Only) Name your submission folder in the format
RollNo_Name_Assessment# e.g. 302_kiran_Assessment#3
Labs and Tutorials3 Credit Course with no regular labTutorials can be arranged if needed
Attendance is Compulsory
Make Groups of 3s for Lab and Class assignments
Importance of FeedbackRegular Feedback and Constant Interaction is
ExpectedShow full involvement and Participation in
the CourseHave Trust and Faith!
Introduction
Computer Program
What is a Computer Program?
Input(DS)
Process(Algorithm)
Output(DS)
Data Structures+Algorithms=Programs
ProblemSolution
Data structures let the input and output be represented in a way that can be handled efficiently and effectively.
Array
Linked List
TreeQueue
Stack
Data Structures: Formal DefinitionData may be organized in different ways
Data structure is the logical or mathematical model of a particular organization of data
Must be rich enough to mirror the actual relationships of the data in the real world
Simple
The Need for Data StructuresGoal: to organize data
Criteria: to facilitate efficient storage of dataretrieval of data manipulation of data
Design Issue: select and design appropriate data types.
(This is the real essence of OOP.)
Data Structure OperationsTraversing
Accessing each record exactly once so that certain items in the record may be processed
Searching Finding the location of the record with the given key value
or finding the location of all records which satisfy one or more conditions
Insertion Adding a new record to the structure
Deletion Removing a record from the structure
Sorting Arrange the records in a logical order
Merging Combining records from two or more files or data
structures into one
Program Efficiency & Complexity Analysis of Algorithms
Steps Involved In Writing A Computer ProgramProblem formulation & specification
Design of solution
Implementation
Testing
Documentation
Evaluation of the solution
Problem Formulation & SpecificationHalf the battle is knowing what
problem to solveMost problems have no simple precise
specification.Some problems are impossible to
formulate.Identify the problem parametersExpressing the problem by a formal
model.Looking for a solution for the modelIn the absence of a solution, discover
about the model
What is an Algorithm?An algorithm is a definite procedure for
solving a problem in finite number of steps
Algorithm is a well defined computational procedure that takes some value(s) as input, and produces some value(s) as output
Algorithm is finite number of computational statements that transform input into the output
Algorithm Definition : A finite set of statements that guarantees an optimal solution in finite interval of time
What is an Algorithm? Finite sequence of instructions
An input should not take the program in an infinite loop
Each instruction having a clear meaning Very subjective. What is clear to me, may not be clear to you.
Each instruction requiring finite amount of effortVery subjective. Finite on a super computer or a P4?
Each instruction requiring finite time to completeVery subjective. 1 min, 1 hr, 1 year or a lifetime?
Favorite AlgorithmsTakes less memory (Space Efficient)Smaller execution timeSmaller programming timeTime complexity (most important)
Efficient AlgorithmsConsumes lesser amount of resources while
solving a problem of size nMemoryTime
So do we just measure the processor time?
Measuring EfficiencyThe efficiency of an algorithm is a
measure of the amount of resources consumed in solving a problem of size n.
The resource we are most interested in is time We can use the same techniques to analyze the
consumption of other resources, such as memory space.
It would seem that the most obvious way to measure the efficiency of an algorithm is to run it and measure how much processor time is needed
Is it correct??
Real Time Execution Vs. Algorithm ComplexitySame algorithms running on different
processors, don’t take the same time, why?Processor speedProcessor typeProgramming languageQuality of compilerSize and nature of inputOperating system
Running Time of an AlgorithmFactors affecting running time:
Nature of inputNumber of inputNumber of steps/primitive operations
Running time is measured in terms of number of steps/primitive operations.
Generally time grows with size of input, so running time of an algorithm is usually measured as function of input size.
Independent from machine, OSWould not vary from processor to
processor as algorithm steps would remain the same
Analyzing an AlgorithmFinding running time of an
AlgorithmRunning time is measured by number of
steps/primitive operations performedSteps means elementary operation like
,+, *,<, =, A[i] etc We will measure number of steps taken in
term of size of input
Analysis of AlgorithmsAssume input size to be N/nPrimitive steps: +,-,*,/,= etc.What about loops and control structures?
Simple Example (1)// Input: int A[N], array of N integers// Output: Sum of all numbers in array A
int Sum(int A[], int N) { int s=0; for (int i=0; i< N; i++) s = s + A[i]; return s;}
How should we analyse this?
Simple Example (2)// Input: int A[N], array of N integers// Output: Sum of all numbers in array A
int Sum(int A[], int N){ int s=0;
for (int i=0; i< N; i++)
s = s + A[i];
return s;}
1
2 3 4
5 6 7
8
1,2,8: Once3,4,5,6,7: Once per each iteration of for loop, N iterationTotal: 5N + 3The complexity function of the algorithm is : f(N) = 5N +3
Simple Example (3) Growth of 5n+3
Estimated running time for different values of N:
N = 10 => 53 stepsN = 100 => 503 stepsN = 1,000 => 5003 stepsN = 1,000,000 => 5,000,003 steps
As N grows, the number of steps grow in linear proportion to N for this function “Sum”
What Dominates in Previous Example?What about the +3 and 5 in 5N+3?
As N gets large, the +3 becomes insignificant5 is inaccurate, as different operations require
varying amounts of time and also does not have any significant importance
What is fundamental is that the time is linear in N.Asymptotic Complexity: As N gets large, concentrate on the highest order term:
Drop lower order terms such as +3 Drop the constant coefficient of the highest
order term i.e. 5
Asymptotic ComplexityThe 5N+3 time bound is said to "grow
asymptotically" like N This gives us an approximation of the
complexity of the algorithm Ignores lots of (machine dependent) details,
concentrate on the bigger picture