Week 1:: Data Structure and Algorithm

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

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 meeting times


• Lectures: 2 sessions/week (2+1)
• Labs: 1 session/week (1hr)

• 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

To emphasize the importance of data structures and


efficient algorithmic design for better management
of computing resources while programming.

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

At the completion of this course students are expected to be able to:

• Identify and apply appropriate data structures for a particular


scenario.
• Design and implement Abstract Data Types (ADT) on demand.
• Analyze the complexity of algorithms and understand time-space
tradeoff.
• Provide efficient data management in terms of insertion and retrieval.

7
Course Outline
(Before Mids)

• Introduction to Data Structures


• Linear Data Structures
• Arrays (static vs. dynamic)
• Linked List (singly vs. doubly)
• Stack (static vs. dynamic)
• Queues (static vs, dynamic)
• ADT, Implementation in C++, Variants and Applications
• Recursive Algorithms
8
Course Outline
(After Mids)

• Non-Linear Data Structures


• Binary Trees and Applications
• Binary Search, Expression, Huffman, Heaps
• Hash Tables with Collision Resolution Techniques
• Open Addressing, Separate Chaining, Double Hashing
• Graphs and Applications
• Djikstra, Minimum Spanning Trees
• Algorithm Analysis
• Searching and Sorting Algorithms
9
Assessments
• Tentative
• Assignments (4)
• Assessments (4)
• Optional Assignment/Assessment (1)
• MidTerm (1)
• Final (1)
• Project
• Submissions will only be accepted on LMS, No other
means are allowed 10
Reference Material
• Data Structures and Algorithm Analysis in C++, by Mark Allen Weiss, 4 th
Edition, Published by Addison-Wesley, 2013.
• Data Structures and Algorithms using C & C++, Augenstein &
Tenenbaum, 2015.
• C++ Plus Data Structures, 5th Edition, Nell Dale, Jones and Bartlett
Learning, 2015.
• Data Structures using C++, Varsha H. Patil, 6th Edition, Oxford University
Press, 2016.
• Data Structures and Algorithms, Aho, Hopcroft & Ullman.
• A Practical Introduction to Data Structures and Algorithm Analysis, C.A.
Shaffer
• Introduction to Data Structures in C, Ashok N. Kamthane

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 Green Record

Judy Field

01001010 Byte (ASCII character J)

1 Bit

15
Data structures …..What?

In computer science, a data structure is


a data organization, management, and storage format that
enables efficient access and modification.

16
17
Data structures…..Why?

• Market value: Big tech companies like Microsoft etc.


focus mainly on data structures.
• Computer & Telecom industry
• Search engines (Google, Amazon etc.)

18
19
Time it took you to find a shirt …

20
Find your friend in here……

21
Where do Algorithms fit …???

An algorithm is a set of sequential instructions for solving


a problem. Algorithms process (manipulate) data.
Example: How to make a cup of tea?

• Can be written in simple English


• Can be a flow chart
• Can be a pseudocode
22
Algorithms

• Algorithm: Step by step procedure


• Computer Program: Implementation of the algorithm
• Provides the required
results
Computer Program
• Easy to understand,
Algorithm
code and debug
+
Input Implementation Output

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

• No single data structure works well for all


purposes, and so it is important to know the
strengths and limitations of several of them
• Choose the most appropriate data structure for a
given data/problem

27
So As We Discussed Earlier….

A data structure is a way to store and organize data in


order to facilitate access and modifications

• 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)

An Abstract Data Type (ADT) specifies how a collection of


data will be stored and what operations can be performed on
the data to access and change it.

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…….

Extreme hard work as not an


easy subject

44
Take home message….

• Sign up at Piazza to access the course material


• Classes will be conducted in MS Teams
• All submissions must be done in LMS to be marked
• Revise previous concepts of data abstraction and
abstract data types
• Must be able to describe what a data structure is
• Labs are a reflection of the course
• Plagiarism will reward you NOTHING
45
DATA STRUCTURE AND ALGORITHM

Week 1:
Linear Data Structures
Momina Moetesum
Dept of CS
Bahria University, Islamabad
[email protected]
[email protected]

46
Linear Data Structures

• A type of data structure that arranges the data items in an


orderly manner where the elements are either physically
or logically attached adjacently
• Items are traversed sequentially
• Single-level
• Easier to implement

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

• An Array is a finite sequence of storage cells, for which the


following operations are defined:
• Create an Array A with a storage for N items
• A[i]=item; stores an item in the ith position of the Array A
• A[i] returns the value of the item stored in the ith position of the
Array A

50
Array as an ADT

• Data: A fixed-size sequence of elements, all of


the same type
• Operations:
Direct access to each element in the array by
specifying its position so that values can be
retrieved from or stored in that position.

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

int *a; int *a;


int size=5; int size;
a = new int[size]; cin>>size;
for(int i=0;i<size;i++) a = new int[size];
{ for(int i=0;i<size;i++)
cin>>a[i]; {
} cin>>a[i];
}

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

You might also like