Introduction To Data Structure and Algorithms
Introduction To Data Structure and Algorithms
(CS202)
1
Goals of this Course
1. Reinforce the concept that costs and benefits exist for
every data structure.
• To comprehend Array
CS Department, University of 5
Sahiwal
What is a Computer Program?
To exactly know, what is data structure? We must know:
What is a computer program?
Some mysterious
processing Output
Input
CS Department, University of 6
Sahiwal
Definition
An organization of information, usually in memory, for better
algorithm efficiency
CS Department, University of 7
Sahiwal
Data Structures
Prepares the students for (and is a prerequisite for) the
more advanced material students will encounter in later
courses.
CS Department, University of 8
Sahiwal
Need for Data Structures
Data structures organize data more efficient programs.
CS Department, University of 9
Sahiwal
3 steps in the study of data
structures
Logical or mathematical description of the structure
CS Department, University of 10
Sahiwal
Organizing Data
Any organization for a collection of records that can be
searched, processed in any order, or modified.
CS Department, University of 11
Sahiwal
What data structure to use?
Data structures let the input and output be represented in a way that
can be handled efficiently and effectively.
array
Linked list
queue
tree stack
CS Department, University of 12
Sahiwal
Data Structures
Data structure is a representation of data and the operations
allowed on that data.
CS Department, University of 13
Sahiwal
Algorithm Analysis
Problem Solving
Space Complexity
Time Complexity
CS Department, University of 14
Sahiwal
1. Problem Definition
What is the task to be accomplished?
Calculate the average of the grades for a given student
Find the largest number in a list
CS Department, University of 15
Sahiwal
2. Algorithm Design/Specifications
Algorithm: Finite set of instructions that, if followed,
accomplishes a particular task.
Describe: in natural language / pseudo-code / diagrams /
etc.
Criteria to follow:
Input: Zero or more quantities (externally produced)
Output: One or more quantities
Definiteness: Clarity, precision of each instruction
Effectiveness: Each instruction has to be basic enough
and feasible
Finiteness: The algorithm has to stop after a finite (may
be very large) number of steps
CS Department, University of 16
Sahiwal
4,5,6: Implementation, Testing and
Maintenance
Implementation
Decide on the programming language to use
C, C++, Python, Java, Perl, etc.
Write clean, well documented code
CS Department, University of 17
Sahiwal
3. Algorithm Analysis
Space complexity
How much space is required
Time complexity
How much time does it take to run the algorithm
CS Department, University of 19
Sahiwal
Space Complexity (cont…)
CS Department, University of 20
Sahiwal
Time Complexity
Often more important than space complexity
space available tends to be larger and larger
time is still a problem for all of us
CS Department, University of 21
Sahiwal
Efficiency
A solution is said to be efficient if it solves the problem
within its resource constraints.
Space
Time
CS Department, University of 23
Sahiwal
Some Questions to Ask
Are all data inserted into the data structure at the
beginning, or are insertions interspersed with other
operations?
CS Department, University of 25
Sahiwal
Lists (Array /Linked List)
Items have a position in this Collection
Random access or not?
Array Lists
internal storage container is native array
Linked Lists
public class Node
{ private Object data;
private Node next;
}
last
first
CS Department, University of 26
Sahiwal
Stacks
Collection with access only to the last element inserted
Last in first out
Data4 Top
insert/push
remove/pop Data3
top Data2
make empty
Data1
CS Department, University of 27
Sahiwal
Queues
Collection with access only to the item that has been
present the longest
Last in last out or first in first out
enqueue, dequeue, front, rear
priority queues and deques
Front Rear
Deletion
Insertion
CS Department, University of 28
Sahiwal
Trees
Similar to a linked list
public class TreeNode
{ private Object data;
private TreeNode left;
private TreeNode right;
} Root
CS Department, University of 29
Sahiwal
Hash Tables
Take a key, apply function
f(key) = hash value
store data or object based on hash value
Sorting O(N), access O(1) if a perfect hash function and
enough memory for table
how deal with collisions?
CS Department, University of 30
Sahiwal
Other ADTs
Graphs
Nodes with unlimited connections between other nodes
CS Department, University of 31
Sahiwal
Other ADTs (cont…)
Data may be organized in many ways
E.g., arrays, linked lists, trees etc.
The choice of particular data model depends on two
considerations:
It must be rich enough in structure to mirror the actual
relationships of data in the real world
The structure should be simple enough that one can
effectively process the data when necessary
CS Department, University of 32
Sahiwal
Example
Data structure for storing data of students:-
Arrays
Linked Lists
Issues
Space needed
Operations efficiency (Time required to complete operations)
Retrieval
Insertion
Deletion
CS Department, University of 33
Sahiwal
Abstract Data Types
In Object Oriented Programming data and the operations that
manipulate that data are grouped together in classes
CS Department, University of 34
Sahiwal
Why Abstract?
Specify the operations of the data structure and leave
implementation details to later
in Java use an interface to specify operations
CS Department, University of 35
Sahiwal
The Core Operations
Every Collection ADT should provide a way to:
add an item
remove an item
find, retrieve, or access an item
Many, many more possibilities
is the collection empty
make the collection empty
give me a sub set of the collection
and on and on and on…
Many different ways to implement these items each with
associated costs and benefits
CS Department, University of 36
Sahiwal
Implementing ADTs
when implementing an ADT the operations and behaviors are
already specified
CS Department, University of 37
Sahiwal
Pseudo Code and Flow Charts
Pseudo Code
Basic elements of Pseudo code
Basic operations of Pseudo code
Flow Chart
Symbols used in flow charts
Examples
CS Department, University of 38
Sahiwal
Pseudo Code and Flow Charts
There are two commonly used tools to help to
document program logic (the algorithm).
These are
Flowcharts
Pseudocode.
Generally, flowcharts work well for small problems
but Pseudocode is used for larger problems.
CS Department, University of 39
Sahiwal
Pseudo-Code
Pseudo-Code is simply a numbered list of instructions to
perform some task.
CS Department, University of 40
Sahiwal
Writing Pseudo Code
Number each instruction
This is to enforce the notion of an ordered sequence of operations
Furthermore we introduce a dot notation (e.g. 3.1 come after 3 but
before 4) to number subordinate operations for conditional and
iterative operations
CS Department, University of 41
Sahiwal
Pseudo-code
Statements are written in simple English without regard to the
final programming language.
CS Department, University of 43
Sahiwal
Basic Elements of Pseudo-code
Assignment Operation
This operation associates a value to a variable.
While writing Pseudo-code you may follow your own
syntax.
Some of the possible syntaxes are:
Assign 3 to x
Set x equal to 3
x=3
CS Department, University of 44
Sahiwal
Basic Operations of Pseudo-code
Read Operation
In this operation we intend to retrieve the value previously
assigned to that variable. For example Set Value of x equal
to y
Read the input from user
This operation causes the algorithm to get the value of a
variable from the user.
Get x Get a, b, c
CS Department, University of 45
Sahiwal
Flow Chart
Some of the common
symbols used in flowcharts
are shown.
CS Department, University of 46
Sahiwal
Flow Chart (cont…)
With flowcharting, essential steps of an algorithm are
shown using the shapes above.
The flow of data between steps is indicated by arrows,
or flowlines. For example, a flowchart (and equivalent
Pseudocode) to compute the interest on a loan is
shown below:
CS Department, University of 47
Sahiwal
CS Department, University of 48
Sahiwal
Recommended Book
• Schaum's Outline Series, Theory and problems of Data Structures
by Seymour Lipschutz
• Data Structures using C and C++,2nd edition by A.Tenenbaum,
Augenstein, and Langsam
• Principles Of Data Structures Using C And C++ by Vinu V Das
• Sams Teach Yourself Data Structures and Algorithms in 24 Hours,
Lafore Robert
• Data structures and algorithms, Alfred V. Aho, John E. Hopcroft.
• Standish, Thomas A., Data Structures, Algorithms and Software
Principles in C, Addison-Wesley 1995, ISBN: 0-201-59118-9
• Data Structures & Algorithm Analysis in C++, Weiss Mark Allen
CS Department, University of 49
Sahiwal
Questions
Any Question Please?