Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

DATA STRUCTURES AND

ALGORITHMS

Lecture # 1 & 2
Introduction
INSTRUCTOR: Saeed
Ahmed
About My Self
■ BS (CS.) Iqra University
■ MS CS Iqra University
■ PhD (A.I Progress ) Sindh Madressatul Islam University (SMIU)
------------------------------------------------------------
Previously:
■ Asst. Professor Iqra University, Karachi
■ Trainer Different Company and Banking Sector
■ Science Teacher Collegiate Center (Intermediate)
■ Science Teacher Higher Secondary School, Karachi

2
Recommended Material
■ Lecture Slides

■ Data Structures and Algorithms in C++


by Adam Drozdek

■ Data Structures and Algorithm Analysis in C++


by Mark Allen Weiss

3
Tentative Grading Scheme
■ Assignments (15%)

■ Quizzes (15%)

■ 1 Mid Term (30%)

■ 1 Final Term (40%)

4
Course Access
■ LMS

■ YouTube Channel
https://www.youtube.com/channel/UCJYGG7l-M_w9eJ8aXY_z8j

■ Email
saeedahmedawan@gmail.com
Where it Stands?

6
Objectives
■ Implement various data structures and their algorithms, and
apply them in implementing simple applications.

■ Analyze simple algorithms and determine their Complexities.

■ Apply the knowledge of data structures to other application


domains.

■ Design new data structures and algorithms to solve problems

7
Course Contents
Understanding the Title!!!

■ DATA

■ STRUCTURES

■ ALGORITHMS

9
Data
■ Data is defined as facts or figures, or information that's stored in or
used by a computer.

■ Simply, anything that is stored or processed by a computer is called


data.

■ Computer works on binary data.

010101010
Structure
■ Structure refers to the arrangement of items in a specific format and
pattern.

■ Simply, a structure is something of many parts that is put together.

■ Example: Arrangement of DNA Elements.


How Computer Uses Memory?
■ Many devices are used to store data.

RAM - Random Access Memory


Data Structures
■ A data structure is a specialized format for
organizing, processing, retrieving and
storing data.

■ Simply saying, it refers to different ways of


storing data in the computer.
Why Data Structures?
■ They help to manage and organize data.

■ They are essential ingredients in creating fast and powerful


algorithms.

■ They make code cleaner and easier to understand.


Example
■ Consider the following map:
Example

■ You can store this information in your computer in a number of


ways. Home (49.2,123.4)
Shop (49.3,123.4)
Market (49.3,123.5)
School (49.3,123.6)
Mosque (49.2,123.6)

■ Now, you can tell where each location is.


■ But you cannot tell how these locations are connected.
Example

■ Another way to store such information is the list.


(Home,Shop)
(Home,Market)
■ Now, connections are obvious. (Home,Mosque)
(Shop,Home)
(Shop,Market)
(Market,School)
(School,Market)
(School,Mosque)
(Mosque,School)
Example

■ Here is another alternate:


Home (Shop,Market,Mosque)
Shop (Home,Market)
Market (School)
School (Market,Mosque)
Mosque (School)

■ There are many others.


Example

■ These were a few methods to store the data.

■ Different data structures.

■ Each has its own characteristics.

■ First two were static, last one was dynamic.


Primitive/Built-in Data Structures
■ Primitive data structures Data
(Data Types) are those which Structures
are predefined way of storing
data by the system.
■ The set of operations that can Non-
Primitive
be performed on these data primitive
are also predefined.

Integer Float Char Boolean Pointer String


Non-Primitive Data Structures
Non-
primitive

These data types are not actually defined by


Static the programming language but are created by Dynamic
the programmer.

Non-
Linear Others
Linear

Array Linked
Stack Queue Trees Graphs
List
More Data Structures
Others

Hash
Files Trees Tuple Sets Dictionary
Tables

■ Some languages support even more Data Structures.

■ In some languages, many of the above non-primitive data structures are


built-in i.e., primitive.
Data Types
vs.
Abstract Data Types
Data Types
■ Two important things about data type.

1. It defines a certain domain of values.

2. It defines operations allowed on those values.

int type
• Takes only integer values from __ to __.
• Operations: Addition, Multiplication, Subtraction, Modulus, Division,
bitwise operations, etc.
Data Types
■ Two important things about data type.

1. It defines a certain domain of values.

2. It defines operations allowed on those values.

float type
• Takes only floating point values from __ to __.
• Operations: Addition, Multiplication, Subtraction, Division, etc.
(bitwise and modulus operator not allowed.)
User-Defied Data Types
■ The operations and values of user defined data types are not
specified in the language itself, but is specified by the user.

struct point {
■ Like:
int x;
– Structure,
int y;
– Union,
– Enumeration, etc. };

■ For example, by using structures, we are defining our own type by


combining other data types.
Abstract Data Types (ADTs)
■ ADTs are like user defined data types which defines:
1. operations on values using FUNCTIONS
All these operations are functions
2. without specifying: but the user does not know about
1. what is there inside the function and the implementation details. That is
why these are abstract for user.
2. how the operations are performed.

Stack ADT Operations:


• A stack consists of initialize(); initializing it to be empty
elements of the same push(); insert an element in stack
type arranged in a pop(); remove an element from
sequential order. stack
isEmpty(); check if stack is empty
isFull(); check if stack is full
Abstract Data Types (ADTs)
■ ADTs are just skeletons.

■ There are multiple ways to implement an ADT.

■ A Stack ADT can be implemented using:


– Static Arrays
– Dynamic Arrays
– Linked Lists
Why ADTs?
■ The program which implements the data structure is known as
implementation.
Implementation
■ The program which uses data structure is called a client Program
program.
■ It has access to ADT i.e. the interface.
ADVANTAGE:
■ If someone wants to use stack ADT in the program, he
can use push and pop operations without knowing its
implementation. Just has access to interface.
■ In future, if implementation of stack ADT is changed
from arrays to linked list, then client program will not
suffer. Client
Program
Algorithm
■ An algorithm is a sequence of instructions that one must perform in
order to solve a well-formulated problem.

■ A well-formulated problem is unambiguous and precise, leaving no


room for misinterpretation. The number of steps are always finite.

■ In other words, algorithm is the method of translating the inputs into


the outputs.
6 Characteristics of an Algorithm
Representation of Algorithm
■ Algorithms can be represented in a number of ways:

– Flowcharts

– Pseudocode

– Natural Language

– Programming Language
Pseudocode
■ Pseudocode is a language
computer scientists often use to
describe algorithms.

■ It ignores many of the details


that are required in a
programming language.

■ It has many versions. No strict


rules.
Another Definition
■ Algorithms refer to operations on different data structures and the set
of instructions for executing them.

■ For explanation, we will consider the same previous example.


Example
■ We want to find the shortest path
from home to school.

■ We have three potential paths:


– Home -> shop -> market -> school
– Home -> market -> school
It seems very trivial problem
for us but computer is just an
– Home -> mosque -> school
efficient slave. It can not think
on its own.
Example
■ For this purpose, we write a set of
instructions that can be coded
later.
1. Find all the places you can go from home.
2. From each of those places, find all paths.
3. Keep track of the distance you have travelled.
4. Repeat this process until you get to school.
5. Compare the distance you have travelled.
6. Determine the shortest path.
Example
■ Suppose the distance is stored in a
table like data structure.
H
0
(Home,Shop) 4
(Home,Market) 7
(Home,Mosque) 12 S Mrt M
(Shop,Home) 4 4 8 12
(Shop,Market) 4
(Market,School) 4
(School,Market) 4
(School,Mosque) 6
(Mosque,School) 6
Example
■ Suppose the distance is stored in a
table like data structure.
H
0
(Home,Shop) 4
(Home,Market) 7
(Home,Mosque) 12 S Mrt M
(Shop,Home) 4 4 7 12
(Shop,Market) 4
(Market,School) 4 Mrt sch sch
(School,Market) 4 8 11 18
(School,Mosque) 6
(Mosque,School) 6
Example
■ Suppose the distance is stored in a
table like data structure.
H
0
(Home,Shop) 4
(Home,Market) 7
(Home,Mosque) 12 S Mrt M
(Shop,Home) 4 4 7 12
(Shop,Market) 4
(Market,School) 4 Mrt sch sch
(School,Market) 4 8 11 18
(School,Mosque) 6
(Mosque,School) 6
sch
12
Example
■ Suppose the distance is stored in a
table like data structure.
H
0
(Home,Shop) 4
(Home,Market) 7
(Home,Mosque) 12 S Mrt M
(Shop,Home) 4 4 7 12
(Shop,Market) 4
(Market,School) 4 Mrt sch sch
(School,Market) 4 8 11 18
(School,Mosque) 6
(Mosque,School) 6
sch
12
Example
■ Suppose the distance is stored in a
table like data structure.
H
0
(Home,Shop) 4
(Home,Market) 7
(Home,Mosque) 12 S Mrt M
(Shop,Home) 4 4 7 12
(Shop,Market) 4
(Market,School) 4 Mrt sch sch
(School,Market) 4 8 11 18 Note: We have used a
(School,Mosque) 6 TREE data structure to
sch
(Mosque,School) 6
12 get our result.
Correct vs. Incorrect Algorithms
■ We say that an algorithm is correct when it
translates every input instance into the
correct output.

■ An algorithm is incorrect when there is at


least one input instance for which the
algorithm gives an incorrect output.
Remarks
■ There may be infinite ways to solve a problem.

■ It means there may be infinite algorithms.

■ Which algorithm is the best?


– We mainly focus on two perimeters
■ Time Complexity
■ Space Complexity
Optimal Algorithm
■ Among all possible solutions to a problem, the
best and optimal solution is the one which:

1. Takes minimum computational steps to determine the


output.

2. Consumes minimum memory space to solve the


problem.
Basic Operations on Data Structures
■ You might have noticed that we have done many
operations on the data structures shown on the
right.

– Read/traverse values
– Search values
– Insert values
Basic Operations on Data Structures
■ There are many other operations that can be
performed on data structures.

– Delete values
– Sort values
– Swap two values
– Update values
– Etc.
Basic Operations on Data Structures
■ Each of these operation has separate algorithms
for each data structure.

– Read/traverse values
– Search values
– Insert values
– Delete values
– Sort values
– Swap two values
– Update values
– Etc.
End of Lecture

THANK YOU

You might also like