0% found this document useful (0 votes)
17 views

Lecture 1 - Introduction

This document provides an overview of the Data Structures course for Fall 2023 taught by Kainat Iqbal. It includes information about the course structure, evaluation criteria, topics to be covered, textbooks, and an example to motivate the use of different data structures like arrays and dictionaries to efficiently store and search user records.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 1 - Introduction

This document provides an overview of the Data Structures course for Fall 2023 taught by Kainat Iqbal. It includes information about the course structure, evaluation criteria, topics to be covered, textbooks, and an example to motivate the use of different data structures like arrays and dictionaries to efficiently store and search user records.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Structures

Fall 2023

Kainat Iqbal
[email protected]

National University of Computer and Emerging


Sciences

1-Introduction 1
Some Rules

• Be respectful to others.

• Raise your hand before asking any question


– Wait for the permission

• Never ever miss a class

• Never use mobile phone in the class

1-Introduction 2
Attendance Policy

• Attendance:
– At least 80% attendance is required.
– It will be marked in the first 10 minutes.

• Student arriving after the attendance will be marked “late”


– No excuse to students arriving immediately after the attendance

• Two late arrivals will be treated as absent

1-Introduction 3
Dishonesty and Plagiarism

• Any kind of cheating will be considered serious offense.

• Plagiarism in project or midterm/ final exam may result in F grade


in the course.

• Plagiarism in an assignment will result in zero marks in the whole


assignments category.
– All parties involve in cheating will get zero marks

• I take academic integrity extremely seriously


– I offer great trust but with little sympathy for violations
– Honest work is a vital feature of a university

1-Introduction 4
Some Details

• You are expected to do your own work


– Exceptions (group work), if any, will be clearly announced

• Sharing solutions, doing work for, or accepting work from others is


cheating

• Referring to solutions from this or other courses from previous


quarters is cheating
– But you can learn from each other: see the policy

1-Introduction 5
Assumed Background

• Prerequisite is Object Oriented Programming

• Topics you should have a basic understanding of:


– Variables, conditionals, loops, methods, fundamentals of defining
classes and inheritance, arrays, recursion, some sorting and searching
algorithms, basic algorithm analysis (e.g., O(n) vs O(n2) and similar
things)

• We can fill in gaps as needed, but if any topics are new, plan on
some extra studying

1-Introduction 6
Course & Reference Books
• Data Structures Using C++ (By D.S. Malik)

• Data Structures Using C and C++ (By Y. Langsam, M. J. Augenstein,


A. M. Tenenbaum)

• Data Structures and Algorithms (By A. V. Aho, J. E. Hopcroft, J. D.


Ullman)

• Schaum's Outline Series, Theory and problems of Data Structures (By


Seymour Lipschutz)

1-Introduction 7
Google Classroom

64igi2i

1-Introduction 8
Tentative Evaluation Breakdown

Evaluation Name Weightage


Quiz 10
Assignment 15
Sessional I 12.5
Sessional II 12.5
Project 10
Final 40

1-Introduction 9
Course Content
• Introduction to data structures and abstract data types
• Arrays
• Background: Templates and Algorithm Analysis
• Searching and sorting techniques
• Linked Lists
• Queues
• Stacks
• Trees: Definitions and terminology
• Trees: Binary Search Trees, AVL Trees
• Heap
• Graphs
• B+ Trees
• Hashing

1-Introduction 10
General Overview

How to organize information/data, usually in


Data Structures memory, so that it can be used efficiently?

Algorithm How to analyze the performance of algorithms to


Analysis evaluate their suitability for solving complex
problems?

1-Introduction 11
What is a Data Structure?

• In a general sense, any representation that is used for storing


information is a data structure
– Example: integers, structures, classes, arrays, etc.

• More typically, a data structure provides a way for organizing a


collection of data items
– Storing, accessing and modifying data items

• Most data structures have associated algorithms to perform


operations on data
– Search, insert, remove etc.

1-Introduction 12
Costs and Benefits

• Each data structure requires


– Space for storing data items
– Time to perform each basic operation
– Programming efforts

• Cost of a data structure


– Time and space resources it consumes

• Choice of data structure depends on many factors


– Type of data
– Frequency with which various data operations are applied

• Hard to define data structure that performs better in all situations


– Time and space tradeoff
1-Introduction 13
Goals of This Course

• Learn the commonly used data structures


– Form a programmer’s basic data structure “toolkit”

• Case studies of data structures

• Examine the costs and benefits of every data structure

1-Introduction 14
Motivational Example

• A cellular service company provides contract to its 10 million users

• Due to new security enforcements, the company wants to prevent


issuing of multiple contracts to users

• Method of Detecting Multiple Contracts


– Before issuing a new contract to user
– First search the id of user in existing contracts database
– In case of failure  issue a new contract
– In case of success  do not issue a new contract to user

1-Introduction 15
Example: Linear Array Data Structure (1)

NIC# Name Address


6584495-9 Muhammad Faheem House No 3 Gulshan Bahar Sec 16
1748425-5 Naeem Alam A-11 Shams Plaza Block-B N.Nazimabad
0889679-1 Arslan Akhtar H No 152 Bostang Colony
3419668-1 Zain Ahmed Sharfabad Street Gulshan Karachi
3445864-3 Sumair Farooq Post Office Tayyar, Multan
6395653-4 Ali Affan H.No. 425, Sector F-11/4, Islamabad
8224641-1 Syed Faraz Sharfabad Street Gulshan, Faisalabad

• Linear Array (with 10 million entries)


– 3 arrays (NIC, Name, Address)
– Structured array
– Class’s object array

1-Introduction 16
Example: Linear Array Data Structure (2)

New NIC# Name Address


Contract 6584495-9 Muhammad Faheem House No 3 Gulshan Bahar Sec 16
1748425-5 Naeem Alam A-11 Shams Plaza Block-B N.Nazimabad
0889679-1 Arslan Akhtar H No 152 Bostang Colony
3419668-1 Zain Ahmed Sharfabad Street Gulshan Karachi
3445864-3 Sumair Farooq Post Office Tayyar, Multan
6395653-4 Ali Affan H.No. 425, Sector F-11/4, Islamabad
8224641-1 Syed Faraz Sharfabad Street Gulshan, Faisalabad

Not Issue Issue

• Any disadvantage of linear array data structure?


• How to improve?

1-Introduction 17
Example: Improved Data Structure

• Create a dictionary data structure


– Group similar records together
– Similarity in terms of first digit of NIC number
– Add a dictionary entry for each distinct digit (0 – 9)

• Example: 3419668-1, 3445864-3, 1748425-5.


– 3 and 1 are dictionary entries

• Existing contracts are searched in two steps


1. Search the dictionary entry (i.e., group searching)
2. Search contract within the group (i.e., with the same NIC digit)

• How much improvement w.r.t. linear array?

1-Introduction 18
Example: Dictionary Data Structure (1)
NIC# Name Address
6584495-9 Muhammad Faheem House No 3 Gulshan Bahar Sec 16
1748425-5 Naeem Alam A-11 Shams Plaza Block-B N.Nazimabad
0889679-1 Arslan Akhtar H No 152 Bostang Colony
3419668-1 Zain Ahmed Sharfabad Street Gulshan Karachi
3445864-3 Sumair Farooq Post Office Tayyar, Multan
6395653-4 Ali Affan H.No. 425, Sector F-11/4, Islamabad
8224641-1 Syed Faraz Sharfabad Street Gulshan, Faisalabad

0 2

3 3 4

6 0 5

Dictionary Item Array Elements


1-Introduction 19
Example: Dictionary Data Structure (2)

• Another possibility
– Maintain pointers with structures (or records)
– Non NULL pointer indicates presence of next record

NIC# Name Address


0
6584495-9 Muhammad Faheem House No 3 Gulshan Bahar Sec 16
1748425-5 Naeem Alam A-11 Shams Plaza Block-B N.Nazimabad
3
0889679-1 Arslan Akhtar H No 152 Bostang Colony
3419668-1 Zain Ahmed Sharfabad Street Gulshan Karachi
6
3445864-3 Sumair Farooq Post Office Tayyar, Multan
6395653-4 Ali Affan H.No. 425, Sector F-11/4, Islamabad
8224641-1 Syed Faraz Sharfabad Street Gulshan, Faisalabad

1-Introduction 20
Practice Task

• Implement the example on the previous slides in C++ in two


different programs. In the first, implement the data structure as a
single array. In the second, implement the dictionary approach
where the data is indexed by the first digit of the CNIC. In both
programs, generate a random CNIC and search for it in your data
structure. Do this searching operation in a loop of 500, to simulate
500 searches.
• Now, one by one, run both programs and note the running time.
Use a Linux utility for checking the running time. Submit your
programs and your result of running time for both (in a separate
txt file).

1-Introduction 21
What is Data Structure Efficiency?

• Two major measures of efficiency of a data structure or algorithm


– Time and space

• Cost of data structure


– Time and space resources it consumes

• Efficiency of a data structure depends on its operations


– Makes a difference between program running in few seconds or days

• Difficult to define data structure that performs better in all


situations

1-Introduction 22
Abstract Data Types (1)

• A definition of data type solely in terms of


– Set of related data items (or values)
– Set of operations on the data

• Separation of logical properties from the implementation details


– Hide implementation details (Encapsulation!) User

• What not how is focus


Interface

Implementation

2-ADTs
Operations 23
Data Structures vs. ADT

• A data structure is physical implementation of an ADT


– Each operation associated with ADT is implemented by one or more
subroutines in the implementation

• Data structure usually refers to an organization of data in main


memory

• Example: Stack, or Queue are ADTs, but a C++ Queue


implemented in a particular class with particular functions is a
concrete data structure.

• The relationship between ADT and Data Structure is a bit like the
relationship between an interface (class.h file – tells WHAT is done)
and a class implementation (the class.cpp file – tells HOW it is
done).
2-ADTs 24
Array as an ADT

• An array is defined as
– Ordered collection of a fixed number of elements
– All elements are of the same data type

• Basic operations
– Direct access to each element in the array
– Values can be retrieved or stored in each element

3-Arrays ADT 25
C/C++ Implementation of an Array ADT

datatype arrayName[intExp];

As an ADT In C/C++
Ordered Index: 0,1,2, … SIZE-1
Fixed Size intExp is constant
Homogeneous dataType is the type of all elements
Direct Access Array subscripting operator [ ]

3-Arrays ADT 26
Summary

What is a data structure?


• In a general sense, any representation that is used for storing
information is a data structure
• Example: An integer, structures, classes, arrays, etc.
• More typically, a data structure provides a way for organizing a
collection of data items

Where is a data structure helpful?


• Data structure can make the difference between a program
running in a few seconds or many days
– Efficiency of data structure

• What is an ADT?
– A logical representation of a data structure.
0-Preliminaries 27
Any Question So Far?

1-Introduction 28

You might also like