You are on page 1of 33

Data

s Structures and Algorithms

Lecture 01
BSIT/BSSE (3+1 Credit Hours)

BSCS 3rd
MOBILE ALERT
Kindly Switch Off your Mobile/Cell Phone

OR

Switch it to Silent Mode


Use of Mobile Phones is not allowed in the class, If mobile phone rings (due to: call, sms, allarm,
reminder or any other), you will be fined Rs. 50/- immediately.

2 BSCS 3rd
ABOUT THIS COURSE
Course Code
Course Title Data Structures & Algorithms
Credit Hours 3+1
Abbreviation DS
Pre-requisite Computer Programming
Type of Course Core
Course Description
Data structures are essential building blocks for designing efficient algorithms. Thus, they play a
central role in computer science and are important in many areas of electrical engineering,
computational biology, computational finance, etc. They are used in a variety of applications
today including search engines (e.g., Google, Bing), social networking applications (e.g.,
Facebook, Twitter), embedded systems (e.g., cell phones, robots), and DNA analysis. This course
will introduce the fundamentals of data structures and will provide a thorough understanding of
how to systematically organize data in a computer system. In addition, this course will introduce
students to analytical tools for comparing data structures in terms of their time and space
complexities. Moreover, students will appreciate the importance of programming structures,
abstractions, and algorithms for improving the efficiency of computer programs.

3 BSCS 3rd
COURSE MATERIAL

TEXT BOOK
Data structures and algorithms in c++
by adam drozdek 4th edition

Data structures and algorithms in c++


By Sartaj sahni

4 BSCS 3rd
COURSE MATERIAL

Reference Books
Data Structures and Algorithm Analysis in C++
by Mark Allen Weiss

5 BSCS 3rd
LAB ASSESSMENT

Final Exam 75% (25+50)


Practical Assignments 25%

Attendance/Workbook
Total 100%

6 BSCS 3rd
WEEKLEY COURSE SCHEDULE
Weeks Topics
Week01 Overview: Data Structures, Abstract Data Types, and Applications,
Overview Algorithms
Week02 Arrays

Week03 Arrays, Lists (Singly Linked List, Doubly Linked List), Stacks

Week04 Stacks, Queues

Week05 Trees: Foundations, Tree Traversals, Binary Trees

Week06 Binary Trees: Analysis, Applications

Week07 Binary Search Trees (BST): Basics, BST Analysis

Week08 Hash Tables: Hash Functions, Heaps: Binary Heaps, HeapSort

Week09 MIDTERM EXAMINATION

7 BSCS 3rd
WEEKLEY COURSE SCHEDULE

Weeks Topics
Week10 Sorting: Insertion Sort, Selection Sort, Mergesort

Week11 Sorting: Quicksort, Bucket-Sort, Radix-Sort (optional)

Week12 Data Compression: Applications, Huffman Coding

Week13 Graphs: Basics, Data Structures for Graphs

Week14 Graph Traversals: Depth First Search, Breadth First Search

Week15 Weighted Graphs: Minimum Spanning Trees, Topological Sort

Week16 Shortest-Path Algorithms: Dijkstra’s Algorithm

Week17 Review

Week18 FINAL EXAMINATION

8 BSCS 3rd
Topics to be covered
 Data Management concepts
 Data types
• Primitive
• Non-primitive
 Types of Data Structures
• Linear Data Structures
• Non Linear Data Structures
 Performance Analysis and Measurement
• Time analysis of algorithms
• Space analysis of algorithms

Unit – 1: Introduction to Data Structure 2 Darshan


BSCS 3rdInstitute of Engineering & Technology
What is Data?
 Data is the basic fact or entity that is utilized in calculation or
manipulation
 There are two different types of data Numeric data and
alphanumeric data
 When a programmer collects such type of data for processing, he
would require to store them in computer’s main memory.
 The process of storing data items in computer’s main memory is
called representation
 Data to be processed must be organized in a particular fashion,
these organization leads to structuring of data, and hence the
mission to study the Data Structures

Unit – 1: Introduction to Data Structure 3 Darshan


BSCS 3rdInstitute of Engineering & Technology
What is Data Structure?
 Data Structure is a representation of the logical relationship
existing between individual elements of data.
 In other words, a data structure is a way of organizing all data
items that considers not only the elements stored but also their
relationship to each other
 We can also define data structure as a mathematical or logical
model of a particular organization of data items
 Data Structure mainly specifies the following four things
• Organization of Data
• Accessing Methods
• Degree of Associativity
• Processing alternatives for information

Unit – 1: Introduction to Data Structure 4 Darshan


BSCS 3rdInstitute of Engineering & Technology
What is Data Structure? Cont..
 The representation of a particular data structure in the memory
of a computer is called Storage Structure
 The storage structure representation in auxiliary memory is called
as File Structure

Algorithm Data Structure Program

Unit – 1: Introduction to Data Structure 5 Darshan


BSCS 3rdInstitute of Engineering & Technology
Classification of Data Structure
Data Structure

Primitive Data Structure Non-Primitive Data Structure

Integer Characters Arrays Lists Files

Floating Pointers
Point Linear Non-linear
List List

Stack Queue Graphs Trees

Unit – 1: Introduction to Data Structure 6 Darshan


BSCS 3rdInstitute of Engineering & Technology
Primitive/Non-primitive data structures
 Primitive data structures
• Primitive data structures are basic structures and are directly
operated upon by machine instructions
• Integers, floats, character and pointers are examples of primitive
data structures
 Non primitive data structure
• These are derived from primitive data structures
• The non-primitive data structures emphasize on structuring of a
group of homogeneous or heterogeneous data items
• Examples of Non-primitive data type are Array, List, and File

Unit – 1: Introduction to Data Structure 7 Darshan


BSCS 3rdInstitute of Engineering & Technology
Non primitive Data Structure
 Array: An array is a fixed-size sequenced collection of elements of
the same data type.
 List: An ordered set containing variable number of elements is
called as Lists.
 File: A file is a collection of logically related information. It can be
viewed as a large list of records consisting of various fields.

Array

File
List

Unit – 1: Introduction to Data Structure 8 Darshan


BSCS 3rdInstitute of Engineering & Technology
Linear / Non-Linear data structure
 Linear data structures
• A data structure is said to be Linear, in which data items are
arranged in sequence memory location.
• Examples of Linear Data Structure are Stack and Queue.
 Nonlinear data structures
• Nonlinear data structures are those data structure in which data
items are not arranged in a sequence
• Examples of Non-linear Data Structure are Tree and Graph

Stack Queue Tree Graph


Unit – 1: Introduction to Data Structure 9 Darshan
BSCS 3rdInstitute of Engineering & Technology
Operations of Data Structure
 Create: it results in reserving memory for program elements
 Destroy: it destroys memory space allocated for specified data
structure
 Selection: it deals with accessing a particular data within a data
structure
 Updation: it updates or modifies the data in the data structure
 Searching: it finds the presence of desired data item in the list of
data items

Unit – 1: Introduction to Data Structure 10 Darshan


BSCS 3rdInstitute of Engineering & Technology
Operations of Data Structure Cont…
 Sorting: it is a process of arranging all data items in a data
structure in a particular order
 Merging: it is a process of combining the data items of two
different sorted list into a single sorted list
 Splitting: it is a process of partitioning single list to multiple list
 Traversal: it is a process of visiting each and every node of a list in
systematic manner

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
Why study algorithms?

Their impact is broad and far-reaching.


 Internet. Web search, packet routing, distributed file sharing, ...
 Biology. Human genome project, protein folding, ...
 Computers. Circuit layout, file system, compilers, ...
 Computer graphics. Movies, video games, virtual reality, ...
 Security. Cell phones, e-commerce, voting machines, ...
 Multimedia. MP3, JPG, DivX, HDTV, face recognition, ...
 Social networks. Recommendations, news feeds, advertisements, ...
 Physics. N-body simulation, particle collision simulation, ...

19 BSCS 3rd
Why study algorithms?

To solve problems that could not otherwise be addressed.


Ex. Network connectivity. [stay tuned]

20 BSCS 3rd
Why study algorithms?

To become a proficient programmer.

21 BSCS 3rd
Why study algorithms?

They may unlock the secrets of life and of the universe.


Computational models are replacing math models in scientific inquiry.

20th century science 21st century science

(formula based) (algorithm based)

22 BSCS 3rd
Why study algorithms?

23 BSCS 3rd
What is algorithm?
 A finite set of instructions which accomplish a particular task
 A method or process to solve a problem
 Transforms input of a problem to output

Algorithm = Input + Process + Output

Algorithm development is an art – it needs practice, practice and only practice!

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
What is a good algorithm?
 It must be correct
 It must be finite (in terms of time and size)
 It must terminate
 It must be unambiguous
 Which step is next?
 It must be space and time efficient

A program is an instance of an algorithm, written in some specific programming


language

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
A simple algorithm
 Problem: Find maximum of a, b, c
 Algorithm
 Input = a, b, c
 Output = max
 Process
o Let max = a
o If b > max then
max = b
o If c > max then
max = c
o Display max

Order is very important!!!

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
Algorithm development: Basics
 Clearly identify:

 what output is required?


 what is the input?
 What steps are required to transform input into output
o The most essential bit
o Needs problem solving skills
o A problem can be solved in many different ways
o Which solution, amongst the different possible solutions is optimal?

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
How to express an algorithm?
 A sequence of steps to solve a problem
 We need a way to express this sequence of steps
 Natural language (NL) is an obvious choice, but not a good choice. Why?
o NLs are notoriously ambiguous (unclear)
 Programming language (PL) is another choice, but again not a good choice.
Why?
o Algorithm should be PL independent
 We need some balance
o We need PL independence
o We need clarity
o Pseudo-code provides the right balance

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
What is pseudo-code?
 Pseudo-code is a short hand way of describing a computer program
 Rather than using the specific syntax of a computer language, more
general wording is used
 It is a mixture of NL and PL expressions, in a systematic way
 Using pseudo-code, it is easier for a non-programmer to understand the
general workings of the program

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
Pseudo-code: general guidelines
 Use PLs construct that are consistent with modern high level languages,
e.g. C++, Java, ...
 Use appropriate comments for clarity
 Be simple and precise

Unit – 1: Introduction to Data Structure 11 Darshan


BSCS 3rdInstitute of Engineering & Technology
Time and space analysis of algorithms
 Sometimes, there are more than one way to solve a problem.
 We need to learn how to compare the performance different
algorithms and choose the best one to solve a particular problem.
 While analyzing an algorithm, we mostly consider time complexity
and space complexity
 Time complexity of an algorithm quantifies the amount of time
taken by an algorithm to run as a function of the length of the
input

Unit – 1: Introduction to Data Structure 12 Darshan


BSCS 3rdInstitute of Engineering & Technology
Time and space analysis of algorithms
 Space complexity of an algorithm quantifies the amount of space
or memory taken by an algorithm to run as a function of the
length of the input
 Time & space complexity depends on lots of things like hardware,
operating system, processors, etc.
 However, we don't consider any of these factors while analyzing
the algorithm. We will only consider the execution time of an
algorithm

Unit – 1: Introduction to Data Structure 13 Darshan


BSCS 3rdInstitute of Engineering & Technology
Calculate Time Complexity of Algorithm
 Time Complexity is most commonly estimated by counting the
number of elementary functions performed by the algorithm
 Since the algorithm's performance may vary with different types
of input data,
• hence for an algorithm we usually use the worst-case Time
complexity of an algorithm because that is the maximum time
taken for any input size

Unit – 1: Introduction to Data Structure 14 Darshan


BSCS 3rdInstitute of Engineering & Technology

You might also like