DATA STRUCTURES AND ALGORITHM
Contact Information
2
Instructor: Sarwar Shah Khan
Email:
[email protected] Course Objectives
3
Impart the basic concepts of data structures and
algorithms.
Understand concepts about searching and sorting
techniques
Understand basic concepts about stacks, queues, lists,
trees and graphs
Understanding about writing algorithms and step by
step approach in solving problems with the help of
fundamental data structures
Practice a variety of data structure and implement
with real time scenario
4 Introduction to Data Structure
A data structure is a particular way of storing and
organizing data in a computer so that it can be
used efficiently
Need for Data Structures
Data structures organize data more efficient
programs.
More powerful computers more complex
applications.
More complex applications demand more
calculations.
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.
What is Data Structure?
7
Data structure is a representation of data and the
operations allowed on that data.
A data structure is a way to store and organize data
in order to facilitate the access and modifications.
Data Structure is the method of representing of
logical relationships between individual data
elements related to the solution of a given problem.
Data Structures
8
A data structure is a particular way of organizing data
in computer memory so that it can be used efficiently.
Classification of Data Structure
Data structure can be classified into two main categories:
Primitive & Non-Primitive
1. Primitive Data Structure
Basic data types are known as primitive data structures.
Also called simple Data Structure e.g. integer, real,
Boolean, character etc.
Data Structures
9
2. Non-Primitive Data Structure
The data types which are derived from primary data types are known as
non-primitive data structure. These are used to store group of values e.g.
array, structures, link list, stacks, queues etc.
Non-Primitive data structure are further divided into two categories:
Linear & Non-Linear
Linear Data Structure
A data structure is said to be linear if its elements form a sequence i.e.
elements are attached with one another e.g. array, link list, stacks, queues
etc.
Non-Linear Data Structure
In non-linear data structure elements are not organized in a sequential
fashion. A data item can be attached to several other data elements e.g.
tree, graph etc.
Terms in Data Structures
10
Interface
Each data structure has an interface. Interface
represents the set of operations that a data structure
supports. An interface only provides the list of
supported operations, type of parameters they can
accept and return type of these operations.
Implementation
Implementation provides the internal representation
of a data structure. It also provides the definition of
algorithms used in the operation of data structure.
thanks to www.tutorialspoint.com
Characteristics of Data Structure
11
Correctness
Data structure implementation should implement
its interface correctly.
Time Complexity
Running time or the execution time of operations
of data structure must be as small as possible.
Space Complexity
Memory usage of a data structure operation
should be as little as possible.
thanks to www.tutorialspoint.com
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.
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.
14 Introduction to Algorithms
A precise rule (or set of rules) specifying
how to solve some problem.
Algorithms
15
An algorithm is a clearly defined set of instructions to be
followed to solve a problem. Algorithms are generally
created independent of underlying programming languages.
From the data structure point of view, following are some
important categories of algorithms
Search − Algorithm to search an item in a data structure
Sort − Algorithm to sort items in a certain order
Insert − Algorithm to insert item in a data structure
Update − Algorithm to update an existing item in a data structure
Delete − Algorithm to delete an existing item from a data
structure
thanks to www.tutorialspoint.com
Characteristics of an Algorithm
16
Not all procedures can be called an algorithm. An algorithm
should have the following characteristics:
Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should be
clear and must lead to only one meaning.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
Finiteness − Algorithms must terminate after a finite number of
steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions,
which should be independent of any programming code.
thanks to www.tutorialspoint.com
Example
17
Problem − Design an algorithm to add two
numbers and display the result.
Step 1 – START
Step 2 – declare three integers a, b & c
Step 3 – define values of a & b
Step 4 – add values of a & b
Step 5 – store output of Step 4 to c
Step 6 – print c
Step 7 – STOP
thanks to www.tutorialspoint.com
Example
18
Problem − Design an algorithm to add two numbers and
display the result.
Step 1 – START ADD
Step 2 – get values of a & b
Step 3 – c a + b
Step 4 – display c
Step 5 – STOP
Second method is preferable in Data Structure and
Algorithms
thanks to www.tutorialspoint.com
Why Study Algorithms?
19
Algorithms solve problems
Good choice: more efficient programs
Bad choice: poor programs performance
Example:
Problem: Find the largest element ‘k’ out of ‘N’ integers
Easy algorithms: sort all integers, then list the first or last
element
Better algorithm: take first element then read through the list
Different algorithms perform better on different inputs
Input size also affect the performance.
Notion of Algorithm and Problem
20
Problem
Algorithm
Input “Computer” Output
Representation of an Algorithms
21
An algorithm may be represented in different
forms:
A description using English/other languages
A real computer program, e.g. C++ or java
A pseudo-code, C-like program, program-language-
like program.
Program = algorithms + data structures
Basic Issues Related to Algorithms
22
How to design algorithms
How to express algorithms
Proving correctness
Efficiency (or complexity) analysis
Theoretical analysis
Empirical analysis
Optimality
Algorithm Efficiency
23
There are often many algorithms for a given
problem. How do we choose the best?
Goals of program design:
Algorithm is to be easy to understand, code, debug
Algorithm makes efficient use of computer’s resources
How to measure the efficiency?
Empirical comparison (run the program)
Asymptotic algorithm analysis (without running the
program)
Factors affecting running time (size of the input)
Best, Worst and Average Cases
24
Not all inputs of a given size take the same time.
Each algorithm has three cases:
Best case:
Worst Case:
Average Case:
Example: Best, Worst and Average Cases
25
Sequential search for ‘k’ in an array of ‘n’ integers:
Best case: ‘k’ is the first element of the array.
Worst case: the search must visit every element once.
This happens when the value being searched for is
either the last element in the list, or is not in the list
Average case: on average, assuming the value searched
for is in the list and each list element is equally likely
to be the value searched for, the search visits only n/2
elements.
Abstract Data Type (ADT)
26
An abstract data type (ADT) is a set of objects together with a set of
operations. Abstract data types are mathematical abstractions; nowhere
in an ADT’s definition is there any mention of how the set of
operations is implemented.
An abstract data type (or ADT) is a class that has a defined set of
operations and values. In other words, you can create the starter motor
as an entire abstract data type, protecting all of the inner code from the
user. When the user wants to start the car, they can just execute the
start() function.
Objects such as lists, stacks and queues along with their operations, can
be viewed as ADTs, just as Integers, Reals and Booleans are data types.
Integers, Reals and Booleans have operations associated with them, and
so they are also called ADTs.
thanks to www.tutorialspoint.com
Fundamental Data Structures
27
Basic Data Structures
Linear Data Non-Linear Data Structures
Structures
Arrays Linked Stack Queues Graphs Trees Hash Tables
Lists s
array
Linked list
queue
tree stack
Linear Data Structures
29
A data structure is said to be linear if its elements
form a sequence or a linear list.
Examples:
Arrays
Linked Lists
Stacks
Queues
Non-Linear Data Structures
30
A data structure is said to be non-linear if its
elements does not form a sequence or a linear list.
Examples:
Trees
Graphs
Hash Tables
Each element may be connected with two or more
other nodes or items in a non-linear arrangement.
Operations on Data Structures
31
Traversal: Travel through the data structure
Search: Traversal through the data structure for a given
element
Insertion: Adding new elements to the data structure
Deletion: Removing an element from the data structure
Sorting: Arranging the elements in some type of order
Merging: Combining two similar data structures into
one
32 Linear Data Structures
Arrays
Linked List
Stacks
Queues
Arrays
33
A sequence of n items of the same data type that are
stored contiguously in computer memory and made
accessible by specifying a value of the array’s index.
Properties:
fixed length (need preliminary reservation of memory)
contiguous memory locations
direct access
Insert/delete
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
1 2 3 4 5 6 7 8 9 10
Array a with 10 integer elements
Linked List
34
A sequence of zero or more nodes each containing two kinds of
information: some data and one or more links called pointers to
other nodes of the linked list.
Properties
dynamic length
arbitrary memory locations
access by following links
Insert/delete
Types of Linked List
Singly linked list (next pointer)
Doubly linked list (next + previous pointers)
Stacks
35
A stack is a data structure that uses last-in, first-out
(LIFO) ordering and allows reading and writing on
the top element only.
Properties
insertion/deletion can be done only at the top
LIFO
Two operations
Push (insertion)
Pop (removal)
Queues
36
Collection with access only to the item that has been
present the longest
Properties
Insertion/enqueue from the rear (back) and deletion/
dequeue from the front.
FIFO
Two operations
Enqueue
Dequeue
Front 20 30 10 60 57 29 Back
37 Non-Linear Data Structures
Graphs
Trees
Hash Tables
Graphs
38
Formal definition: A graph G = <V, E> is defined by a
pair of two sets: a finite set V of items called vertices and a set
E of vertex pairs called edges.
Undirected and directed graphs (digraphs).
Complete, dense, and sparse graphs
Undirected Graph Directed Graph
Trees
39
A Tree is a way of representing the
hierarchical nature of a structure in a
graphical form.
Properties of trees
Root Node
Child Node
Parent Node Unordered Tree
Leaf Node
Types
Unordered Tree
Binary Tree is an ordered tree data
structure in which each node has at most
two children.
Binary Tree
Hash Tables
40
A hash table is a data structure that uses a hash
function to map identifying values, known as keys
(e.g., a person's name), to their associated values
(e.g., their telephone number).
Summary
41
A data structure is a particular way of storing and organizing
data in a computer so that it can be used efficiently.
Linear Data Structures
Arrays
Linked List
Stacks
Queues
Non Linear Data Structures
Graphs
Trees
Hash Tables