Ch1 PDF
Ch1 PDF
CSC – 1204
Data Structure and Algorithms
Data Structure and Algorithms
• Data Structure
How to organize data ? (processing or storing in computer)
e.g., Arrays
• Algorithm
Design Issue:
select and design appropriate data types
(This is the main motivation to learn and understand data
structures)
Why Study Data Structures?
Linked List
Queue Stack
Tree
• Searching
Finding the location of the data element (key) in the structure
• Insertion
Adding a new data element to the structure
• Deletion
Removing a data element from the structure
• Sorting
Arrange the data elements in a logical order (ascending/descending)
• Merging
Combining data elements from two or more data structures into one
Problem Solving
(Baqala, Home)
School
Masjid (Home , Masjid)
Baqala
(Masjid, Home)
(Home , Intersection)
(Baqala , Masjid)
(Masjid, School)
Hash Table
Or
Hash Map
Algorithm to Solve Problem
School
Masjid
Baqala Problem ?
Finding Shortest Path from (Home to School)
Home Intersection
problem
algorithm
Clearly identify:
What output is required?
What is the input?
What steps are required to transform input into output
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?
A Simple Algorithm
Problem: Find maximum of a, b, c
Algorithm
Input = a, b, c
Output = max (among three)
Process
o Let max = a
o If b > max then
max = b
o If c > max then
max = c
o Display max
Input range
for num←0; num<=range; num←num+1 do
if num % 2 = 0 then
print num is even
else
print num is odd
endif
endfor
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
• ADTs are entities that are definitions of data and operations, but
don’t have implementation details
•Basic definitions
Type: a set of objects.
ADT:
Data Items:
Type
Logical Form
Operations
36
Data Structure Philosophy
• programming effort.
Data Structure Philosophy (cont.)
• Bank example:
Collection (Cont.)
Collections have a defined set of properties that describe
them and operations that can be performed on them.
• Collection property such as:
• Count: number of items in the collection.
• Collection operations such as:
Add() adds a new item to the collection.
Queue(Sequential Access)
Hierarchical Collections
47
Group Collections
• Examples:
Sets.
Graphs.
Networks. Graph Collection
Sets Collection
Network Collection
Some Questions to Ask