DATA
STRUCTURES
AND
ALGORITHMS
Hafiz Ahsan Arshad
Course Introduction
■ Course Outline.
■ Evaluation Scheme.
Quizzes 10%
Assignments 20%
Project 15%
Mid Term 25%
Final Term 40%
■ Rules:
– Raise your hand before asking any question and then WAIT for
the permission.
– Never ever miss a class.
– Never ever “sleep” in the class.
– Never use mobile phones in the class.
What is Data?
■ Data refers to raw facts, figures, symbols, or values that represent
information.
■ Data by itself lacks meaning and context but becomes
meaningful when it is processed and interpreted.
■ Data can take various forms, including
– numbers, text, images, sounds, and more.
What is Data Structure?
■ A data structure is a way of organizing and storing data in
a computer or memory storage system in a structured and efficient
manner.
■ In a general sense, any representation that is used for storing
information is a data structure.
– For Example:
■ integer, float, char, array, classes etc.
Point to Ponder:
The choice of efficient data structure makes the
difference between a program running in a few
seconds or many days
What is Data Structure
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.
– Each data structure has costs and benefits.
■ It is very difficult to find a data structure that is better than others in all
situations.
■ A data structure requires:
– space for each data item it stores.
– time to perform each basic operation.
– programming effort.
Why are Data Structures
Important?
■ Efficient Data Handling:
– Data structures are designed to optimize data access and manipulation.
■ Memory Management:
– Data structures help manage memory effectively.
– They can allocate and deallocate memory dynamically, reducing memory wastage and
fragmentation.
– This is especially crucial in systems with limited memory resources.
■ Algorithm Efficiency:
– Many algorithms require specific data structures to operate efficiently.
– Choosing the right data structure can significantly impact the performance of an
algorithm.
■ Complex Problem Solving:
– Data structures allow for the representation and manipulation of complex data in a
structured way.
– They are essential for solving complex problems like databases, network routing, and
artificial intelligence.
Real-Life Examples:
■ Phone Contacts:
– May use Array, Link-list, BST or combination of Data Structures.
■ Social Media Applications:
– Combination of Data Structures like Queue, Graph etc.
■ Online Shopping Cart:
– Combination of Data Structures like Array, Link-list, Queue, Graph
etc.
■ GPS Navigation:
– May use Graph or combination of Data Structures.
Types of Data Structures
Primitive Data Structures
fundamental building blocks
■ Primitive data structures, are the
for representing simple, single values.
■ They are predefined by the programming language and typically occupy a
fixed amount of memory.
■ Examples: Common examples of primitive data types include:
– Integer Types: Represent whole numbers (e.g., int, long, short).
– Floating-Point Types: Represent real numbers with a decimal point
(e.g., float, double).
– Character Type: Represents individual characters (e.g., char).
– Boolean Type: Represents true or false values (e.g., bool).
■ Efficiency: Primitive data types are efficient in terms of memory usage
and performance because they are simple and have a fixed size.
Non-Primitive Data
Structures
■ Non-primitive data structures, are more complex data structures that can
store collections of values, objects, or references to other data.
■ They are often built using primitive data types as their components.
■ Examples: Common examples of non-primitive data structures include:
– Arrays: Ordered collections of elements of the same type.
– Lists: Dynamic collections that can hold elements of various types.
– Objects (Classes and Structures): Custom data types that can
encapsulate multiple attributes and behaviors.
■ Complexity: They are more complex than primitive data types because
they can hold multiple values or objects and may require dynamic memory
allocation
Ponder Yourself:
String is primitive data structure or non-primitive
data structure?
Linear Data Structures
■ In linear data
structures, data elements are organized in a
sequential manner, where each element has a unique
predecessor and successor, except for the first and last elements.
■ Accessing elements in linear data structures is straightforward, as you
can traverse from one element to the next in a linear fashion.
■ Examples:
– Arrays
– linked lists
– queues.
– Stacks
Non-Linear Data Structures
■ Non-linear data structures do not follow a strict linear sequence.
Instead, data elements can be interconnected in various ways,
forming complex relationships.
■ Accessing elements in non-linear data structures can involve
traversing multiple paths, making it more complex than linear
structures. Elements may have multiple predecessors and successors.
■ Examples:
– Trees
– Graphs