0% found this document useful (0 votes)
6 views32 pages

Lecture 1 - Introduction To Data Structures

The document provides an overview of data structures and algorithms, emphasizing their importance in organizing data for efficient processing and retrieval. It discusses various types of data structures such as arrays, linked lists, stacks, queues, trees, and graphs, along with their operations and real-world applications. Additionally, it highlights the need for selecting appropriate data structures based on problem constraints and efficiency considerations.

Uploaded by

daniyalhassan436
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views32 pages

Lecture 1 - Introduction To Data Structures

The document provides an overview of data structures and algorithms, emphasizing their importance in organizing data for efficient processing and retrieval. It discusses various types of data structures such as arrays, linked lists, stacks, queues, trees, and graphs, along with their operations and real-world applications. Additionally, it highlights the need for selecting appropriate data structures based on problem constraints and efficiency considerations.

Uploaded by

daniyalhassan436
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lecture No.

01

Data Structures and Algorithms

Dr. Anum Tariq


Data Structures
Organizing Data
Any organization/arrangement for a collection of
records that can be searched, processed in any order, or
modified.
It is a logical way of storing data and it also define
mechanism to retrieve data.
The choice of data structure and algorithm can make
the difference between a program running in a few
seconds or many days.
Real-World Applications
• Search Engines: When you type a query into Google, it quickly
retrieves relevant web pages using a structure that organizes all the
pages it has indexed. This allows it to show results almost instantly.
• Social Media Feeds: When you scroll through Facebook, the
platform uses algorithms to decide which posts you see first. It looks
at your interactions (likes, comments) to prioritize content you’re
more likely to engage with.
• Recommendation Systems: When you finish watching a movie on
Netflix, it suggests other movies based on what you've watched
before. It analyzes your viewing habits and compares them to those of
other users to make personalized recommendations.
AL 3
Everyday Algorithms
• Google Search: When you search for "best pizza near me,"
Google sorts through millions of web pages to find the most
relevant ones based on factors like location and user ratings.
• Sorting: In a shopping app, if you sort products by price,
the app uses an algorithm to rearrange the list from the
lowest to highest price.
• Navigation: When you use Google Maps to find directions,
it calculates the fastest route by considering traffic data,
road conditions, and distance.
AL 4
Problem-Solving Skills
Example Problem: "How can we find the highest score in a list of game scores?"
Approaches:
•Linear Search: Look at each score one by one and keep track of the highest
score you find. This is like going through each score until you find the biggest
one.
•Sorting: Sort the scores from highest to lowest, then take the first score in the
sorted list. While this works, it's more work than needed just to find the highest
score.
•Divide and Conquer: Split the list into two smaller lists, find the highest score in
each, and then compare the two. This method helps you break the problem into
smaller, easier parts.

AL 5
Need for Data Structures

 Data structures organize data  more efficient programs.

 More powerful computers  more complex applications.

 More complex applications demand more calculations.


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.
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.
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?
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.
Data Structure Types

11
Data Structures and Algorithms
Linear Data Structures:

• A data structure is said to be linear if the elements


form a sequence.
• Two ways of representation:
– By means of Sequential memory Locations
– By means of Links
1.Array
2.Stack
3.Queue
4.Linked List

12
Data Structures and Algorithms
Non Linear Data Structures:

• Don’t form a direct sequence among elements.


1.Tree
2.Graph

13
Data Structures and Algorithms
Operations on Data Structures
• Traversing: Accessing each record exactly once so that certain item in
the record may be processed.
• Searching: finding the location of the record with a given key value .

• Insertion : add a new record to the structure


• Deletion : removing a record from the structure

14
Data Structures and Algorithms
1: Arrays

• An array is a collection of homogeneous type of data


elements.
• An array is consisting of a collection of elements

15
Data Structures and Algorithms
Operations performed on arrays

1.Traversing
2.Search
3.Insertion
4.Deletion
5.Sorting
6.Merging

16
Data Structures and Algorithms
Limitations of Arrays

• Size of array is fixed.


• Inserting/Deleting an element is expensive

17
Data Structures and Algorithms
Arrays
 Array declaration: int x[6];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive integers.
 To access a cell, use the array name and an index:
x[0], x[1], x[2], x[3], x[4], x[5]
Array Layout
x[0]
Array cells are
x[1]
contiguous in
computer memory x[2]

The memory can be x[3]


thought of as an
x[4]
array
x[5]
What is Array Name?
 ‘x’ is an array name but there is no variable x. ‘x’ is not an
lvalue. (lvalue simply means an object that has an
identifiable location in memory (i.e. having an address))
 For example, if we have the code
int a, b;
then we can write
b = 2;
a = b;
a = 5;
But we cannot write
2 = a;
Array Name
 ‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
2. Linked List

• A Linked list is a linear collection of data elements .It has two


part one is info and other is link part.info part gives
information and link part is address of next node

22
Data Structures and Algorithms
2. Linked List

• A Linked list is a linear collection of data elements .It has two


part one is info and other is link part.info part gives
information and link part is address of next node

23
Data Structures and Algorithms
Advantage of Linked List over Arrays

• Dynamic Size
• Ease of Insertion and Deletion

Drawbacks:
• Random access is not allowed

24
Data Structures and Algorithms
3. Stacks(LIFO)
• A Stack is a list of elements in which an element may
be inserted or deleted at one end which is known as
TOP of the stack.
• Operation on LIFO:
– Push: add an element in stack
– Pop: remove an element in stack
• Example: Operating System stacks for addresses of programs.

25
Data Structures and Algorithms
Stack Representation
• A stack is an ordered collection of items, generally
implemented with only two principle operations, called Push
and Pop.

26
Data Structures and Algorithms
Stack Applications

• Solving Recursion - recursive calls are placed onto a stack and


removed from there once they are processed.
• Evaluating post-fix expressions
• Solving Towers of Hanoi
• Backtracking
• Depth-first search
• Converting a decimal number into a binary number

27
Data Structures and Algorithms
4. Queues(FIFO)
• A queue is defined as a special type of data structure where the
elements are inserted from one end and elements are deleted from
the other end.
– The end from where the elements are inserted is called REAR end.
– The end from where the elements are deleted is called FRONT end.

• Queue is organized as First In First Out (FIFO)Data Structure.


– Insertion : add a new element in queue (Enque)
– Deletion: Removing an element in queue (Deque)

• Examples: Queues in Operating Systems

28
Data Structures and Algorithms
4. Queues(FIFO) Applications

• Access to shared resources (e.g., printer)


• Multiprogramming
• Message queue

29
Data Structures and Algorithms
5. Trees
• Tree is a hierarchical data structure.
• The very top element of a tree is called the root of the tree.
• Except the root element every element in a tree has a parent
element, and zero or more child elements.
• All elements in the left sub-tree come before the root in sorting
order, and all those in the right sub-tree come after the root.
• Tree is the most useful data structure when you have hierarchical
information to store.
• For example, directory structure of a file system; there are many
variants of tree you will come across. Some of them are Red-black
tree, threaded binary tree, AVL tree, etc.
30
Data Structures and Algorithms
5. Tree’s Applications
• Genealogical tree
– pictures a person's descendants and ancestors
• Game trees
– shows configurations possible in a game such as the Towers of Hanoi
problem
• Parse trees
– used by compiler to check syntax and meaning of expressions such as
2*(3+4)

31
Data Structures and Algorithms
6. Graphs
• Graph is a networked data structure that connects a collection
of nodes called vertices, by connections, called edges.
• An edge can be seen as a path or communication link between
two nodes.
• These edges can be either directed or undirected.
• If a path is directed then you can move in one direction only,
while in an undirected path the movement is possible in both
directions.

32
Data Structures and Algorithms

You might also like