2. Contact Information
• Instructor:
Fayyaz Awan (Lecturer)
Department of Computer Sciences
Comsats Insititute of Information Technology, Wah Cantt
• Office Hours:
Monday, 11:00 am – 4:30 pm
3. Need for Data Structures
Data structures organize data more efficient programs.
More powerful computers more complex applications.
More complex applications demand more calculations.
4. 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.
5. 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.
6. 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.
7. 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?
8. 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.
9. Algorithms
• An algorithm is any well defined computational procedure
that takes some values or set of values as input and produces
some value or set of values as output.
• An algorithm is thus a sequence of computational steps that
transforms the input into output.
10. How do you learn algorithms to
cook?
• To become good chef, you don’t learn lots of recipes by heart.
• You study existing recipes.
• You practice making them.
• You experiment with your own variations.
• Until you can invent your own recipes.
11. How do you learn about
algorithms?
• A good computer scientist doesn’t need to memorize a big
book of algorithms.
• We will begin studying famous algorithms.
• You can practice executing them by hand.
• Once you recognize more general patterns, you can
experiment with your own variations.
• At the end of the course, I expect you to invent algorithms
yourself.
12. Analysis of Algorithms
The theoretical study of computer-program performance and
resource usage. What’s more important than performance?
• modularity
• correctness
• maintainability
• functionality
• robustness (strengths)
• user-friendliness
• programmer time
• simplicity
• extensibility
• reliability
13. Origin of word: Algorithm
• The word Algorithm comes from the name of the muslim
author Abu Ja’far Mohammad ibn Musaal-Khowarizmi. He
was born in the eighth century at Khwarizm (Kheva), a town
south of river Oxus in present Uzbekistan. Uzbekistan, a
Muslim country for over a thousand years, was taken over by
the Russians in 1873.
• Much of al-Khwarizmi’s work was written in a book titled al
Kitab al-mukhatasar fi hisab al-jabrwa’l-muqabalah (The
Compendious Book on Calculation by Completion and
Balancing). It is from the titles of these writings and his name
that the words algebra and algorithm are derived. As a result
of his work, al-Khwarizmi is regarded as the most outstanding
mathematician of his time
14. Algorithm: Informal
Definition
• An algorithm is any well-defined
computational procedure that takes some
values, or set of values, as input and produces
some value, or set of values, as output.
• An algorithm is thus a sequence of
computational steps that transform the input
into output.
15. Algorithms, Programming
• A good understanding of algorithms is essential
for a good understanding of the most basic
element of computer science: programming.
Unlike a program, an algorithm is a
mathematical entity, which is independent of a
specific programming language, machine, or
compiler. Thus, in some sense, algorithm
design is all about the mathematical theory
behind the design of good programs.
16. Why study algorithms and
performance?
• Algorithms help us to understand
scalability.
• Performance often draws the line between
what is feasible and what is impossible.
• Algorithmic mathematics provides a
language for talking about program
behavior.
• Performance is the currency of computing.
17. Why Study Algorithms?
• Necessary in any computer programming problem
• Improve algorithm efficiency: run faster, process more data, do something that
would otherwise be impossible
• Solve problems of significantly large sizes
• Compare algorithms
• Algorithms as a field of study
• Learn about a standard set of algorithms
• New discoveries arise
• Numerous application areas
• Learn techniques of algorithm design and analysis
• how does one coordinate the efforts of many programmers working on a
single piece of software
18. Applications
• Multimedia
• CD player, DVD, MP3, JPG, DivX
• Internet
• Packet routing, data retrieval (Google)
• Communication
• Cell-phones, e-commerce
• Computers
• Circuit layout, file systems
• Science
• Human genome (is the complete set of genetic information for
humans)
• Transportation
• Airline crew scheduling, UPS deliveries
19. Analyzing Algorithms
• Predict the amount of resources required:
• memory: how much space is needed?
• computational time: how fast the algorithm runs?
• FACT: running time grows with the size of the input
• Input size (number of elements in the input)
• Size of an array, polynomial degree, # of elements in a matrix, # of bits in the
binary representation of the input, vertices and edges in a graph
Def: Running time = the number of primitive operations (steps) executed
before termination
• Arithmetic operations (+, -, *), data movement, control, decision making (if,
while), comparison
20. Definition: Algorithm
• A computer algorithm is a detailed step by step
method for solving the problem
• An algorithm is a sequence of Clear-cut instructions
for solving the problem in a finite amount of time.
• An algorithm is a set of well defined computational
procedure that takes some values, or set of values,
as input and produces some value or set of values
as output.
• More Generally an algorithm is any well defined
computational procedure that takes collection of
elements as input and procedures collection of
elements as output.
21. Definition: Data Structures
• A data structure is systematic way of organizing
and accessing data with a specific relationship
between the elements.
• Why data structures?
Ultimate goal -- to write efficient programs. In order
to do that, one needs to organize the data in such a
way that it can be accessed and manipulated
efficiently.
22. Definition: Data Structures (Cont…)
• A data structure is a way of arranging data in a
computer's memory or other disk storage.
• A data structure is a collection of data, organized so that
items can be stored and retrieved by some fixed
techniques.
• Example: Suppose you are hired to create a database of
names with all company's management and employees.
24. Data Structures Types
• There are several common data structures: arrays,
linked lists, queues, stacks, binary trees, hash
tables, etc.
• These data structures can be classified as
either linear or nonlinear data structures, based on
how the data is conceptually organized or
aggregated.
25. LinearVs Non-LinearData Structures
Linear Structures Non-Linear Structures
The array, list, queue, and stack
belong to this category.
Each of them is a collection that
stores its entries in a linear sequence,
and in which entries may be added or
removed at will.
They differ in the restrictions they
place on how these entries may be
added, removed, or accessed. The
common restrictions include FIFO and
LIFO.
Trees and graphs are classical non-
linear structures.
Data entries are not arranged in a
sequence, but with different rules.
27. Data Structure Operations
• The data appearing in our data structures are processed
by means of certain operations. The following four
operations play a major role :
1. Traversing: Accessing each record exactly once so that certain
items in the record may be processed.
2. 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.
3. Inserting: Adding a new record to the structure.
4. Deleting : Removing a record from the structure.
28. Data Structure Operations (Cont…)
• Sometimes two or more of the operations may be
used in given situations; e.g. we may want to delete
the record with a given key, which may mean we
first need to search for the location of the record.
The following two operations , which are used in
special situations, will be considered:
1. Sorting: Arranging record in some logical order
(ascending/ descending)
2. Merging: Combining the records in two different
sorted files into a single sorted file.