Week 1:: Data Structure and Algorithm
Week 1:: Data Structure and Algorithm
Week 1:: Data Structure and Algorithm
Week 1:
Introduction
Momina Moetesum
Dept of CS
Bahria University, Islamabad
[email protected]
[email protected]
1
Instructor
Ms. Momina Moetesum
Office: Cabin # D-5, XC Basement
Contact: [email protected]
[email protected]
On-Campus Availability
Monday & Thursday
(Only on schedule)
2
Course Information
• Pre-requisite
• Computer Programming/Object Oriented Programming
• Course Resources
• Lectures slides, assignments, solutions to problems, projects, and
announcements will be uploaded on course web page (Piazza & LMS).
3
Course Pages
1) Piazza:
https://piazza.com/bahria_university_Islama
bad/fall2020/csc221/home
2) BU-LMS
4
Course Aim
5
Course objectives
• Equip students with skills to select and design data structures and
algorithms that are appropriate for real world problem solving
• Developing basic algorithms for manipulating stacks, queues,
linked lists, trees, graphs.
• Study and design recursive algorithms for applications in trees and
graphs
• Study and implement searching and sorting algorithms
• Study the computational complexities of different algorithms
6
Expected Student Outcomes
7
Course Outline
(Before Mids)
11
Grading policy
10%
10%
Quizzes
Assignment
50% 10% Term Project
Mid Semester Exam
Final Exam
20%
Credits : 3
12
13
Data Hierarchy
• From smallest to largest
• Bit: (1s or 0s)
• Byte: 8 bits (char)
• Field: group of characters with
some meaning
• Record: group of related fields
• Database: group of related
files
14
Data Hierarchy
Sally Black
Tom Blue
Judy Green File
Iris Orange
Randy Red
Judy Field
1 Bit
15
Data structures …..What?
16
17
Data structures…..Why?
18
19
Time it took you to find a shirt …
20
Find your friend in here……
21
Where do Algorithms fit …???
23
Data Structure and Algorithms -
Relation
• Algorithm: Step by step procedure
• Computer Program: Implementation of the algorithm
• Data structure: Organization of data that is manipulated by the algorithm or
the computer program
• Provides the required
results
• Easy to understand, code
Good Computer Program
and debug
• Makes efficient use of
Efficient Algorithm resources
Appropriat +
Input e Data Implementation Output
Structure
24
Efficient Algorithm…???
• Efficiency of an algorithm depends on the amount of resources it uses.
• Space (# of variables used)
• Time (# of instructions used)
• Efficiency as function of input size (n)
• Number of data items
• Time-Space Trade-off
• Ideally, an efficient algorithm is one which uses less memory and requires less time to
complete the task at hand.
• In reality, this is not always possible!!!
• Time-Space trade-off is a situation where memory use can be reduced at the cost of slower
program execution (and conversely, the computation time can be reduced at the cost of
increased memory use)
25
26
Appropriate Data Structure
27
So As We Discussed Earlier….
• DS - What?
• A method/technique of organizing data.
• DS - Why?
• To manage huge amount of data efficiently.
28
How We Will Approach Data
Structures….
We will study data structures from three perspectives:
1. Specification
• ADT (Description of data structures)
• Data (Attributes)
• Operations (Behaviours)
2. Implementation in C++
3. Application in problem solving
29
Abstract Data Type (ADT)
30
Example: Integer as an ADT
• Data:
{-1, -2,…,-} U {0, 1, …, }
• Operations:
Addition, subtraction,
multiplication, division etc.
31
ADT vs. Data Structure
• ADT
• An ADT is a collection of data and associated operations for
manipulating that data
• Data Structures
• Physical implementation of an ADT
• Data structures used in implementations are provided in a
language (primitive or built-in) or are built from the language
constructs (user-defined)
32
ADT vs. Data Structure
33
C++ Implementation of Integer
ADT
10/26/2020 34
C++ Implementation of Integer
ADT
2
1
v
10/26/2020 35
1
C++ Implementation of Integer
2 3
ADT
10/26/2020 36
C++ Implementation of Integer
ADT 1
10/26/2020 37
1
C++ Implementation of Integer
ADT
10/26/2020 38
C++ Implementation of Integer
1 ADT
10/26/2020 39
C++ Implementation of Integer
1 ADT
10/26/2020 40
C++ Implementation of Integer
1
ADT
2 3
10/26/2020 41
C++ Implementation of Integer
1
ADT
2
3
10/26/2020 42
C++ Implementation of Integer
1 2 ADT
10/26/2020 43
What will it be like…….
44
Take home message….
Week 1:
Linear Data Structures
Momina Moetesum
Dept of CS
Bahria University, Islamabad
[email protected]
[email protected]
46
Linear Data Structures
47
Examples
48
Physically vs. Logically Linear
• The linear data structure whose successive components occupy
consecutive memory locations are called physically linear data
structures. Memory utilization is inefficient.
• Example: Arrays
• The linear data structure whose components are accessed in some
sequence but are not necessarily placed in consecutive memory
locations are called logically linear data structures.
• Example: Linked Lists
• Queues and Stacks can be either physical or logical
depending upon the data structure being used for
implementation. 49
Arrays
50
Array as an ADT
51
Static vs. Dynamic Arrays
10/26/2020 52
Static Arrays
int arr[]={1,2,3,4,5}; int size = 5;
int arr[size];
const int size=5;
int arr[size];
for(int i=0;i<size;i++) int size;
{ cin>>size;
cin>>arr[i]; int arr[size];
}
int arr[5];
for(int i=0;i<5;i++)
{
cin>>arr[i];
} 53
Dynamic Arrays
delete []a;
54
Accessing Array Elements
int main()
{
int a[] = {1, 2, 3, 4, 5};
int *p;
p=a;
for(int i=0; i<5; i++)
{
//Subscript notation with array name
cout<<a[i];
//Subscript notation with pointer name
cout<<p[i];
//Offset notation using array name
cout<<*(a+i);
//Offset notation using pointer name
cout<<*(p+i);
return(0);
10/26/2020 55
Arrays as Data Structures
(Applications)
• Sorting items
• Ascending order
• Descending order
• Lexicographical order
• Searching items
• Linear Search (Unsorted Arrays)
• Binary Search (Sorted Arrays)
56
Bubble Sort
57
Bubble Sort
58
Linear Search
59
Linear Search
60
Binary Search
61
Binary Search
62
Binary Search
63
Binary Search
64
Binary Search
• Binary Search:
65
Binary Search vs. Linear Search
10/26/2020 66