Data Structure and Algorithm Analysis
13 CS- I & II
Engr. Maria Shaikh
8/16/2014 2
Course Structure and Contact Info
Schedule 3+1 (Theory + Practical)
Exams 3 Sessional Tests
Home Assignments / Presentation
Mid Term + Final Exams
Engr. Maria Shaikh
Relevant Literature + Lecture Foils
1.A. M. Tanenbaum; Data Structure using C, (latest edition)Prentice hall publishers
2.A. A. Puntambekar, Data Structure using, Technical Publications Pune
3.Clifford A. Shaffer; Data Structure and Algorithm Analysis, Edition 3.2 by Dover Publications
8/16/2014 3Engr. Maria Shaikh
Curiosity killed the cat
How many of you have your own computers/laptops?
Familiar with Programming?
Ever used any Computer Software?
Why we do programming?
8/16/2014 4Engr. Maria Shaikh
5Read Ahead
You are expected to read the lecture notes before the lecture.
This will facilitate more productive discussion during class.
Like in an English class
Also please proof readassignments & tests.
Introduction to Basic Concepts and Data Structures
Basic Terminologies
1. Data: Data are simply raw Facts and Figures. Or Data are simply values or set of values.
2. Data Items: A data items refers to single unit of values. Or
A unit of data stored in a field. For example, "M000675are data item in the "Account Number
3. Group Items: Data items that are breakdown into
subitems are called Group Items.
8/16/2014 Engr. Maria Shaikh 6
Introduction to Basic Concepts and Data Structures
Example: An Employees name may be divided into three subitems- First Name, Middle initial and Last Name.
4. Elementary Items: Data items which can not be further
divided into subitems are called Elementary Items.
Example: The Social Security number would normally be treated as Single Item.
5. Entity: An entity is something that has certain attributes or properties which may be assigned values. The values may be either numeric or non numeric.
8/16/2014 Engr. Maria Shaikh 7
Introduction to Basic Concepts and Data Structures
An entity is an object that exists and is distinguishablefrom other objects. For instance, John Harris withS.I.N. 890-12-3456 is an entity, as he can be uniquelyidentified as one particular person in the universe.
An entity may be concrete (a person or a book, forexample) or abstract (like a holiday or a concept).
Example: The following are possible attributes and their corresponding values for an entity, an Employee of a given organization:
Attributes: Name Age Sex Social Security No
Values: Marium 21 Female 1325-76899-09
8/16/2014 Engr. Maria Shaikh 8
Pictorial View of Entity and its Attributes
8/16/2014 Engr. Maria Shaikh 9
EMPLOYEE
Name
SexAge
SSNO
Introduction to Basic Concepts and Data Structures
6. Entity Set: Entities with similar attributes (e.g. all the employees in an organization forms an entity set.
7. Information: It is meaningful or processed data.
8. Hierarchy: The way the data are organized into hierarchy of fields, records and files reflects the relationship between attributes, entities and entity set.
i. Field: A field is a single elementary unit of information representing an attribute of an entity. OR
Fields are vertical columns in a table.
ii. Record: A record is a collection of field values of a given entity. OR
8/16/2014 Engr. Maria Shaikh 10
Introduction to Basic Concepts and Data Structures
A record is the collection of fields that relate to a singleentity. For example, we could have a student record thatincludes fields for the students name, address,homeroom, date of birth, etc. OR
Records are horizontal rows in a table.
iii. File: A file is a collection of related records. Forexample, a student file might include all of the records ofstudents enrolled at a school. A police department mightkeep a file of criminal records, which includes detailsabout all known criminals.
8/16/2014 Engr. Maria Shaikh 11
Introduction to Basic Concepts and Data Structures
Within a file, all records usually have the samestructure. That is, every record in the file containsthe same fields. Only the data stored in the fields ofdifferent record will be different
8/16/2014 Engr. Maria Shaikh 12
13
Programs and Programming
What is a program? A set of instructions working with data
designed to accomplish a specific task
The recipe analogy Ingredients are the Data
Directions are the Program Statements
What is programming The art and craft of writing programs
The art to control these idiot servants and nave children
Engr. Maria Shaikh
14
Introduction to Programming
Programming is to solve problems using computers How to do it at all ? How to do it robustly ? How to do it effectively ?
Programming consists of two steps: Algorithmic design (the architects) Coding (the construction workers)
Programming requires: A programming language (C/C++/C#) to express your
ideas A set of tools to design, edit, and debug your code A compiler to translate your programs into machine code A machine to run the executable code
Engr. Maria Shaikh
15
Crafting Programs Effectively
Program design design process stepwise refinement & top-down design bottom-up design modularization, interfaces use of abstractions
Programming style structured programming readable code effective use of language constructs formatting
software organization Documentation and comments
Engr. Maria Shaikh
16
Good Programs
There are a number of facets to good programs: they must
run correctly
run efficiently
be easy to read and understand
be easy to debug and
be easy to modify
better running times will generally be obtained from use of the most appropriate data structures and algorithms
Engr. Maria Shaikh
Preliminary Info Regarding Data Structures
Before introducing data structures we shouldunderstand that computers do store, retrieve, andprocess a large amount of data.
If the data is stored in well organized way onstorage media and in computer's memory then it canbe accessed quickly for processing that furtherreduces the latency and the user is provided fastresponse.
8/16/2014 Engr. Maria Shaikh 17
Data Structures
Data structure introduction refers to a scheme fororganizing data, or in other words a data structure is anarrangement of data in computer's memory in such a waythat it could make the data quickly available to theprocessor for required calculations.
A data structure should be seen as a logical conceptthat must address two fundamental concerns. First, howthe data will be stored, and second, what operations willbe performed on it?
8/16/2014 Engr. Maria Shaikh 18
19
Data Structure
Definition A data structure is a collection of data, generally
organized so that items can be stored and retrieved by some fixed techniques.
A particular format of representing information.
Example An array
Stored and retrieved based on an index assigned to each item
Engr. Maria Shaikh
Types of Data Structure
DATA STRUCTURE
NON LINEAR
LINEAR
Engr. Maria Shaikh
Algorithm
Along with data structures introduction, in real life, problem solving is done with help of data structures and algorithms.
An algorithm is a step by step process to solve a problem in a finite amount of time.
In programming, algorithms are implemented in form of methods or functions or routines.
To get a problem solved we not only want algorithm but also an efficient algorithm.
One criteria of efficiency is time taken by the algorithm, another could be the memory it takes at run time.
8/16/2014 Engr. Maria Shaikh 21
Algorithm
The best algorithm is the one which has a finebalance between time taken and memory consumption.
But, as we know the best exists rarely, and wegenerally give more priority to the time taken by thealgorithm rather than the memory it consumes.
8/16/2014 Engr. Maria Shaikh 22
Analysis
Generalized information gathering
i. How much time an operation will take?
ii. How much memory will be utilized?
iii. Which data Structure will be more effective?
8/16/2014 Engr. Maria Shaikh 23
24
Why Data Structure and Algorithms
Computer is becoming ubiquitous programming gets you more out of computer learn how to solve problems dealing with abstractions be more precise
Software is designed to help people solve problems in reality
To solve the problems, there are some THINGS, or INFOs in reality to be processed
Those THINGS or INFOs are called DATA
DATA and their RELATIONS can be complicated
Engr. Maria Shaikh
Why Data Structure and Algorithms
Reasonable organization of DATA helps improving software efficiency, decreasing software design difficulty.
DATA STRUCTURE is a smart way to organize DATA, depends on the features of DATA, and how the DATA are processed.
Unfortunately, most people know little about Computer Science know little about Programming write bad or buggy programs become lost when writing large programs
8/16/2014 Engr. Maria Shaikh 25
Data Structure & Data Structure Operations
Any logical or mathematical organization of data is referred to as a Data Structures.
There are two basic criteria to this choice.
i. It must be rich enough in structure to mirror the actual relationships of the data in the real world.
ii. The structure should be simple enough that one can effectively process the data when necessary.
8/16/2014 Engr. Maria Shaikh 26
Elementary Data Structure Organization
8/16/2014 Engr. Maria Shaikh 27
Primitive Data Structure
The primitive data types are the basic data types that are available in most of the programming languages. The primitive data types are used to represent single values.
Primitive data are only single values; they have no special capabilities.
There are 8 primitive data types: byte, short, int, long,
float, double, char, Boolean. Primitive data are only single values; they have no special
capabilities.
8/16/2014 Engr. Maria Shaikh 28
Non Primitive Data Structures
The data types that are derived from primary data types are known as non-Primitive data types. These data types are used to store group of values.
The non-primitive data structures are
Linear Data structures
Non Linear Data structures
8/16/2014 Engr. Maria Shaikh 29
Linear Data structures
This Data Structures involve arranging the elements inLinear fashion.
Eg.
Stacks
Queue
Linked Lists
Arrays
8/16/2014 Engr. Maria Shaikh 30
Linear Data structures
Arrays: It is linear collection of homogeneous data items.
Stacks: A data structure where insertion can only be done in the end part and deletion can only be done in the end part as well
Last-in first-out data structure (LIFO)
Supports the following operations
push inserts an item at the end
pop deletes the end item
peek returns the last element
8/16/2014 Engr. Maria Shaikh 31
Linear Data structures
Stacks: The Queue is like the List but with limited insertion
and deletion.
Insertion can be done only at the end or rear
Deletion can only be done in the front
FIFO first-in-first-out data structure
Operations
enqueue
dequeue
8/16/2014 Engr. Maria Shaikh 32
Linear Data structures
Linked Lists:
Each element (node) inside a linked list is linked to the previous node and successor (next) node.
This allows for more efficient insertion and deletion of nodes.
8/16/2014 Engr. Maria Shaikh 33
Non Linear Data structures
This Data structures involve representing theelements in Hierarchical order.
Eg:
Trees
Graphs
8/16/2014 Engr. Maria Shaikh 34
Non Linear Data structures
Trees: A tree has a set of nodes and directed
edges that connect them a directed edge connects a parent to its
children
Tree properties one node is distinguished as the root
every node (except the root) is connected by an edge from exactly one other node
A unique path traverses from the root to each node
8/16/2014 Engr. Maria Shaikh 35
Non Linear Data structures
Graphs: Graph is a mathematical structure used to model pair
wise relations between objects from a certain collection.
8/16/2014 Engr. Maria Shaikh 36
Vertices
Edges
Data Structures Operations
a) Traversing: Accessing each record exactly once so that certain items in the record may be processed.b) Searching: Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions.c) Inserting: Adding a new record to the structure.d) Deleting: Removing a record from the structure.
Following two are special operations:a) Sorting: Arranging the records in some logical order.b) Merging: Combining the records in two different sorted files into a single sorted file.
8/16/2014 Engr. Maria Shaikh 37
ALGORITHM
"An algorithm is a well defined list of steps to solve a particular problem". "It is an abstract form of a program."
A set of algorithms are always used to perform operations on data structure. Data may be organized in many ways and many levels so algorithm has to be designed in such a manner so that it can perform the desired operation on the stored data.
An algorithm is a well defined list or sequence of steps to solve a particular problem. There may be more than one algorithm to solve a particular problem. The choice of a particular algorithm depends on the following considerations:8/16/2014 Engr. Maria Shaikh 38
ALGORITHM
Performance requirements i.e. time complexity
Memory requirements i.e. space complexity
Each algorithm is a list of well-defined instructions for completing a task. Starting from an initial state, the instructions describe a computation that proceeds through a well-defined series of successive states, eventually terminating in a final ending state.
8/16/2014 Engr. Maria Shaikh 39
ALGORITHM COMPLEXITY
"The complexity of an algorithm is the amount of time and the amount of space that is consumed by algorithm to run to completion". The complexity is of the following typesSpace Complexity: - It is also known as memory requirement.
Time Complexity: - It is also known as performance requirement.
8/16/2014 Engr. Maria Shaikh 40
SPACE COMPLEXITY
"The space complexity of an algorithm is the amount of memory it needs to run to completion". The space needed by a program consists of following components-Instructions space- to store executable version of program.
Data space- to store all constants, variables etc.
Environment stack space- it is used in case of recursive program .
8/16/2014 Engr. Maria Shaikh 41
SPACE COMPLEXITY
The total space needed by a program can bedivided in two parts- A fixed part that isindependent of particular problem, andincludes instructions space, space forconstants, variables and fixed size structurevariables A variable part that includesstructure variables whose size depends on theparticular problem being solved dynamicallyallocated space and the recursion stack space.
8/16/2014 Engr. Maria Shaikh 42
TIME COMPLEXITY
"The time complexity of an algorithm is the amount of time it needs to run to completion". Some of the reasons for studying time complexity areWe may be interested to know in advance that whether the program will provide a satisfactory real time response.
There may be several possible solutions with different time requirements or with different time complexity.
8/16/2014 Engr. Maria Shaikh 43
ALGORITHM: Time-Space Trade Off
The best algorithm to solve a given problem is one that requires less space in memory and takes less time to complete its execution.
But in practice it is not always possible to achieve both these objectives.
As we know there may be more then one approach to solve a particular problem. One approach may take more space but takes less time to complete its execution while the other approach may take less space but takes more time to complete its execution.
8/16/2014 Engr. Maria Shaikh 44
ALGORITHM: Time-Space Trade Off
We may have to sacrifice one at the cost of the other. If space is our constraint, then we have to choose a program that requires less space at the cost of more execution time. On the other hand if time is our constraint then we have to choose a program that takes less time to complete its execution at the cost of more space
8/16/2014 Engr. Maria Shaikh 45
Variables A variable are place holders to store data. Variables in a computer program are analogous to
"Buckets" or "Envelopes" where information can be maintained and referenced. On the outside of the bucket is a name. When referring to the bucket, we use the name of the bucket, not the data stored in the bucket.
Variables can be characterized as a 6-tuple of attributes:Name: identifierAddress: memory location(s)Value: particular value at a momentType: range of possible valuesLifetime: when the variable accessibleScope: where in the program it can be accessed
Engr. Maria Shaikh
Data Types
A data type in a programming language is a set of data with values having predefined characteristics.
Examples of data types are: integer, floating point unit number, character, string, and pointer.
Usually, a limited number of such data types come built into a language.
The language usually specifies the range of values for a given data type, how the values are processed by the computer, and how they are stored
8/16/2014 Engr. Maria Shaikh 47
Type Checking
Generalize the concept of operands and operators to include subprograms and assignments
Type checking is the activity of ensuring that the operands of an operator are of compatible types
A Compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type.
This automatic conversion is called a coercion.
A type error is the application of an operator to an operand of an inappropriate type
8/16/2014 Engr. Maria Shaikh 48
Compatibility of a Variable
Type compatibility by name: means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name.
Type compatibility by structure: means that two variables have compatible types if their types have identical structures
More flexible, but harder to implement
8/16/2014 Engr. Maria Shaikh 49
Scope and life time of a variable
Variables come into existence when the program starts (but are uninitialized) and go away when the program ends
Programs so far are a collection of variables that can be accessed by any instruction in any part of the program.
The programmer has to make and enforce all decisions over how variables are used and accessed
We want to give variables
A scope - defining which parts of a program can access a variable.
A lifetime - defining when a variable is created and destroyed.
8/16/2014 Engr. Maria Shaikh 50
Scope
Files, function bodies and compound statements define a scope
A scope is a region of a program text in which declarations are valid
The concept of locality states that declarations are local to their scope
A name cannot be used outside of its scope
A name can only be defines ONCE in any scope
8/16/2014 Engr. Maria Shaikh 51
File Scope
A C++ source file defines a file scope
Splitting a program into different files restricts variables defined in the file to the file scope
Programs have a global scope
Variables in the global scope may be accessed from any part of the program
By default any name declared in file scope is GLOBAL
The scope of a global name extends from its point of declaration to the end of the file it is declared in
Try to avoid the use of global variable
8/16/2014 Engr. Maria Shaikh 52
Local Scope
Names declared in a function body or compound statement are local to that scope
{
int local_x ; // local to this scope
// some statements
}
The name local _ x is valid from its point of declaration to the end of the scope - the closing bracket.
Outside of the compound statement local _ x is not accessible.
8/16/2014 Engr. Maria Shaikh 53
Nesting Scopes Scopes can be nested
{ int x;
int y;
// some statements
{
int x; // hide outer x
x = 1; // use the local x
y = 2; // y still visible
}
// more statements
}
Note - Names in an outer scope are accessible unless they are hidden by a local declaration of the same name
8/16/2014 Engr. Maria Shaikh 54
Lifetimes of Variables
A variable is created when its scope is entered during the execution of a program.
A variable is destroyed when its scope is left during the execution of a program.
{ // enter scope here
int local_x;
// some statements using local_x
} // leave scope here
8/16/2014 Engr. Maria Shaikh 55
Lifetimes of Variables
When the program enters the scope local_x is created.
When the program leaves the scope local_x is destroyed.
When the program enters the scope again a NEW variable called local_x is created.
8/16/2014 Engr. Maria Shaikh 56
Control Structures
Control structures
SequentialIterative / Repetitive
Conditional
8/16/2014 Engr. Maria Shaikh 57
For Loop
While Loop
Do Loop
If-Statement
If-else
Switch Case
END OF SLIDE SET 1
8/16/2014 Engr. Maria Shaikh 58