0% found this document useful (0 votes)
15 views

Introduction To Data Structure and Algorithms

Uploaded by

Sufi Tech
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Introduction To Data Structure and Algorithms

Uploaded by

Sufi Tech
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Data Structures and Algorithm

(CS202)

Ms. Quratulain Shoaib


Lecturer
Department of Computer Science

1
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.
Objective of this Course
 Algorithms and Data Structures
 Static Data Structures
 Searching Algorithms
 Sorting Algorithms
 List implementation through Array
 ADT: Stack
 ADT: Queue
 Dynamic Data Structures (Linear)
 Linked List (Linear Data Structure)
 Dynamic Data Structures (Non-Linear)
 Trees, Graphs, Hashing
Acknowledgment

This is to acknowledge that Lecture Handout and


Video Lectures of the course ‘ Data Structures and
Algorithm’ by Dr. Sohail Aslam, Virtual University of
Pakistan will be used as a learning material for this
course.

CS Department, University of Sahiwal 4


Objective of Lecture 1
• To know about Introduction to Data Structures

• To understand Selecting a Data Structure

• To understand Data Structure Philosophy

• To comprehend Array

• To comprehend List data structure

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

 such as queue, stack, linked list and tree.

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.

 Cover well-known data structures such as dynamic arrays,


linked lists, stacks, queues, tree and graphs.

 Implement data structures in C++

CS Department, University of 8
Sahiwal
Need for Data Structures
 Data structures organize data  more efficient programs.

 More powerful computers  more complex applications.

 More complex applications demand more calculations.

CS Department, University of 9
Sahiwal
3 steps in the study of data
structures
Logical or mathematical description of the structure

Implementation of the structure on the computer

Quantitative analysis of the structure, which includes


determining the amount of memory needed to store the
structure and the time required to process 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.

 The choice of data structure and algorithm can make the


difference between a program running in a few seconds or
many days.

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

 Classifying Functions by Their Asymptotic Growth

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

 What are the time /space performance requirements ?

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

 Test, test, test

 Integrate feedback from users, fix bugs, ensure


compatibility across different versions  Maintenance

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 Sahiwal 18


Space Complexity
 Space complexity = The amount of memory required by an
algorithm to run to completion
the most often encountered cause is “memory leaks” –
the amount of memory required larger than the memory
available on a given system

 Some algorithms may be more efficient if data completely


loaded into memory
Need to look also at system limitations
e.g. Classify 2GB of text in various categories – can I
afford to load the entire collection?

CS Department, University of 19
Sahiwal
Space Complexity (cont…)

1. Fixed part: The size required to store certain


data/variables, that is independent of the size of the
problem:
- e.g. name of the data collection

2. Variable part: Space needed by variables, whose size is


dependent on the size of the problem:
- e.g. actual text
- load 2GB of text VS. load 1MB of text

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

 3-4GHz processors on the market


still …
researchers estimate that the computation of various
transformations for 1 single DNA chain for one single
protein on 1 TerraHZ computer would take about 1 year
to run to completion

 Algorithms running time is an important issue

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

 The cost of a solution is the amount of resources that the


solution consumes.

CS Department, University of Sahiwal 22


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.

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?

Can data be deleted?

Are all data processed in some well-defined order, or is


random access allowed?

CS Department, University of Sahiwal 24


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.

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

Data1 Data2 Data3 Data4

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

Abstract Data Types (ADTs) or data structures are collections


store data and allow various operations on the data to access
and change it

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

many, many different ADTs


picking the right one for the job is an important step in design
"Get your data structures correct first, and the rest of the
program will write itself."              
-Davids Johnson

High level languages often provide built in ADTs,


 the C++ Standard Template Library, the Java Standard Library

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

Implementer’s first choice is what to use as the internal


storage container for the concrete data type
the internal storage container is used to hold the items in the
collection
often an implementation of an ADT

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

Each instruction should be unambiguous and effective.

Completeness. Nothing is left out.

CS Department, University of 41
Sahiwal
Pseudo-code
Statements are written in simple English without regard to the
final programming language.

Each instruction is written on a separate line.

The pseudo-code is the program-like statements written for


human readers, not for computers. Thus, the pseudo-code
should be readable by anyone who has done a little
programming.

Implementation is to translate the pseudo-code into


programs/software, such as “C++” language programs.
CS Department, University of 42
Sahiwal
Basic Elements of Pseudo-code
A Variable
 Having name and value
 There are two operations performed on a variable
 Assignment Operation is the one in which we associate a value to a
variable.
 The other operation is the one in which at any given time we intend
to retrieve the value previously assigned to that variable (Read
Operation)

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?

You can contact me at: quratulain@uosahiwal.edu.pk

Your Query will be answered within one working day.

CS Department, University of Sahiwal 50


Thanks

CS Department, University of Sahiwal 51

You might also like