CS-250 Data Structure and Algorithms
Dr. Muhammad Kamran Khan
Assistant Professor (Comp. Sc)
[email protected] Department of Electrical Engineering
Military College of Signals
National University of Sciences and Technology (NUST
1
Computer Science Department
Contact Details
• Email:
[email protected]• Office: Room 105, Ground floor (EED)
• Contact Hours
• Any time (subject to availability)
• Monday: 13:10 – 14:00
• Tuesday: 14:20 – 15:10
Computer Science Department
Some Rules
• Raise your hand before asking any question!!
• Don’t miss a class/quiz
• Don’t “sleep” in the class
• Avoid using mobile phones in the class
• Above all, whatever you do, please do not disturb
others
Computer Science Department
How to be successful in this
course??
• Participate in the lecture, lab and learn
• Practice coding (practice makes a man perfect)
• Ask questions asap and get help when needed
• Talk to your peers and group members
• Share ideas but not code
Computer Science Department
Attendance Policy
• Attendance will be taken at the beginning of the
lecture
• 75% attendance is mandatory
• Students coming late will be marked as late
• 2 late arrivals are equal to an absent
• Short Attendance in Lab is not acceptable
Computer Science Department
Dishonesty, Plagiarism
• All parties involved in any kind of cheating in (Quizzes,
Assignments & Projects) will get ZERO in that exam
• Plagiarism in midterm/ final exam may result in F grade
in the course
• Habitual cases will be awarded F
Computer Science Department
Introduction to Data Structure
and
Abstract Data Types (ADT)
(Week 01)
7
Computer Science Department
What is Data?
Data
• “facts and statistics collected together for reference or
analysis.” – Google
Types of data
• Textual, numeric, audio, video
Importance of data?
• primary purpose of computer programs is to store
and retrieve information, usually as fast as possible
8
Computer Science Department
Computer Program
• To exactly know, what is data structure? We must know:
-What is a computer program?
Computer Program
Solution
Problem
Process
Input (Algorithm) Output
(DS) (DS)
(Algorithm)
Data Structures + Algorithms = Programs
9
Computer Science Department
What is a Data Structure?
- A particular way of organizing data in a
computer so that it can be used effectively
E.g. we can store a list of items having the
same data-type using the array data
structure
- Method of representing logical relationships
between individual data elements related to
the solution of a given problem
Resource constraints space, time
10
Computer Science Department
Basic Data Structures
Basic
Data Structures
Linear Non-Linear
Data Structures Data Structures
Linked Hash
Arrays Stacks Queues Trees Graphs
Lists Tables
11
Computer Science Department
array
Linked list
queue
tree stack
12
Computer Science Department
Data Structures Operations
Traversing: Accessing each record exactly once so that each data
items in the record is traversed (or visited)
Searching: Finding the location of the record with the given key
value or finding the location of all records which satisfy one or more
conditions
Insertion: Adding a new record to the structure
Deletion: Removing a record from the structure
Sorting: Arrange the records in a logical order
Merging: Combining records from two or more files or data
structures into one
13
Computer Science Department
Types of Data Structure
• Linear: Values are arranged in linear fashion.
– Array: Fixed-size
– Linked-list: Variable-size
– Stack: Add to top and remove from top
– Queue: Add to back and remove from front
– Priority queue: Add anywhere, remove the highest
priority
14
Computer Science Department
Types of Data Structure
• Non-Linear: The data values in this structure are not
arranged in order.
– Hash tables: Unordered lists
which use a ‘hash function’ to insert and search
– Tree: Data is organized in branches.
– Graph: A more general branching structure, with less strict
connection conditions than for a tree
15
Computer Science Department
Type of Data Structures
• Homogenous: In this type of data structures, values of
the same types of data are stored.
– Array
• Non-Homogenous: In this type of data structures, data
values of different types are grouped and stored.
– Structures
– Classes
16
Computer Science Department
Selection of Data Structure
- Each data structure has costs and benefits
(Rarely is one data structure better than another in
all situations)
- A data structure requires:
1: space for each data item it stores
2: time to perform each basic operation
3: programming effort
- Each problem has constraints on available time and
space (only after a careful analysis of problem
characteristics can we know the best data structure for
the task) 17
Computer Science Department
Selection of 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
18
Computer Science Department
Characteristics of Data Structure
19
Computer Science Department
Characteristics of Data Structure
20
Computer Science Department
Characteristics of Data Structure
“I will, in fact, claim that the difference between a bad programmer and a
good one is whether he considers his code or his data structures more
important. Bad programmers worry about the code. Good programmers worry
about data structures and their relationships.”
– Linus Trovalds, 2006
21
Computer Science Department
Abstract Data Type (ADT) and
Data Structures
Abstract Data Type Data Structures
• ADT stores data and allow various • Physical implementation of an
operations on the data to access and ADT
change it • Data structures used in
• A mathematical model, together with implementations are provided in
various operations defined on the a language (primitive or built-in)
model or are built from the language
• An ADT is a collection of data and constructs (user-defined)
associated operations for • Each operation associated with
manipulating that data the ADT is implemented by one or
more subroutines in the
implementation
22
Computer Science Department
Abstract Data Type
• ADT is a way of looking at a data structure focusing on
what it does and ignoring how it does its job
• E.g. A stack is an ADT supporting push, pop and
isEmpty operations
• ADTs support abstraction, encapsulation, and
information hiding.
• Abstraction is the structuring of a problem into well-
defined entities by defining their data and operations.
• The principle of hiding the used data structure and to
only provide a well-defined interface is known as
encapsulation.
23
Computer Science Department
The Core Operations of ADT
• Every Collection ADT should provide a way to:
– add an item
– remove an item
– find, retrieve, or access an item
• Many, many more possibilities
– is the collection empty
– make the collection empty
– give me a sub set of the collection
24
Computer Science Department
Some frequently used DS.
25
Computer Science Department
Stacks
• Collection with access only to the last
element inserted
• Last in first out Data4 Top
• insert/push Data3
• remove/pop Data2
• top Data1
• make empty
26
Computer Science Department
Queues
• Collection with access only to the item that
has been present the longest
• Last in last out or first in first out
• enqueue, dequeue, front
• priority queues and dequeue
Front Back
Data1 Data2 Data3 Data4
27
Computer Science Department
List
• A Flexible structure, because can grow and
shrink on demand.
Elements can be:
Inserted
Accessed
Deleted
At any position
last
first
28
Computer Science Department
Tree
• A Tree is a collection of elements called nodes.
• One of the node is distinguished as a root, along with a
relation (“parenthood”) that places a hierarchical
structure on the nodes.
Root
29
Computer Science Department