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

Lec 1 - Basics (DS and Algo)

Uploaded by

headcodfree
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lec 1 - Basics (DS and Algo)

Uploaded by

headcodfree
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 53

Data Structure and Algorithm

CSC – 1204

Section: 4123 & 4137

Dr. Tariq Ali


Welcome to CSC-1204

• Instructor: Dr. Tariq Ali

Email: [email protected]

2
Grading Policy

Midterm Exam-I 20%


Midterm Exam-2 20%
Quizze-Class Participation 10%
Home Work & Reports 10%
Final Exam 40%

3
Data Structure and Algorithms

Why to study ?
Data Structure and Algorithms

•Data Structure
How to organize data ? (processing or storing in computer)
e.g., Arrays

•Algorithm
A step-by-step procedure for solving a problem
e.g., how to travel from one point to other ?
Data Structure and Algorithms

•It helps to understand the nature of the problem


•Possible solutions (one or multiple)
•Gives better idea that how efficient it is to use each
of them
– How many solutions are available or suitable ?
– Which one is more efficient, if multiple solutions possible
– Ultimately, helps to find optimized solution
Course Outline
 Basics of Data Structures and Algorithms
 Static and Dynamic Data Structures
 Basic Searching and Sorting Algorithms
 Abstract data types
 Arrays
 Linked Lists
 Stacks and Queues
 Graphs and Trees
What is data?
 Data
 A collection of facts from which conclusion may be drawn
 Example: Data: Temperature 35°C; Conclusion: It is hot.
 Types of data
 Textual: For example, your name (Mohammad)
 Numeric: For example, your ID (090254)
 Audio: For example, your voice
 Video: For example, your voice and picture
 Image …..
Data ?
Data Sources and Uses
What is Data Structure?
 A particular way of storing and organizing
data in a computer so that it can be used
efficiently and effectively.
 Data structure is the logical or mathematical
model of a particular organization of data.
 A group of data elements grouped together
under one name.
 For example, an array of integers
Need for Data Structures
 Goal: to organize data

 Criteria: to facilitate efficient


 storage of data
 retrieval of data
 manipulation of data

 Design Issue:
 select and design appropriate data types
(This is the main motivation to learn and understand data
structures)
Why Study Data

Why Study Data Structures?


Structures?

• We study data structures to organize data.


 Good choice: more efficient program.
 Bad choice: poor program performance.

• Justification: over time, there are:


 More powerful computers.
 More complex applications.
 More complex tasks.

• Characteristics of problem’s solution:


 Efficient: if it solves problem within resource
constraints:
 Time and Space.

 Cost: the amount of resources a solution will


consume.
Types of Data Structures
Array

Linked List

Queue Stack
Tree

There are many, but we named a few. We’ll learn these


data structures in detail!
Types of Data Structures

 Primitive Data Structures


 Basic (simple) data structures that directly operate upon the machine instructions
 They have different representations on different computers.
 Integers, Floating point numbers, Character constants, String constants
 Non-primitive Data Structures
 More complicated data structures
 They are derived from primitive data structures
 Arrays, Lists and Files
Data Structure Operations
• Traversing
 Accessing each data element exactly once so that certain items in the
data may be processed

• 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

Problem solving principles:


1.Completely understand the problem
2.Devise a plan to solve it
3.Carry out the plan
4.Review the results
What is algorithm?
 Operations, we can perform on Data Structures
 A finite set of instructions to executing them
 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!
Array
Or
(Home , Baqala) Lists

(Baqala, Home)
School
Masjid (Home , Masjid)
Baqala
(Masjid, Home)
(Home , Intersection)
(Baqala , Masjid)
(Masjid, School)

Intersection (School, Masjid)


Home
(School , Intersection)
(Intersection, School)

Home (Baqala, Masjid, Intersection)


Home 42.5, 142.2
Baqala 42.7, 145.6 Baqala (Home, Masjid)

Masjid 42.7, 145.6 Masjid (Home, School)


School 42.7, 145.5 School (Masjid , Intersection)
Intersection 42.5, 145.4 Intersection (School)

Hash Table
Or
Hash Map
Algorithm to Solve Problem

School
Masjid
Baqala Problem ?
Finding Shortest Path from (Home to School)

Home Intersection

• Find places, you can go from Home to School


• Find all the paths
• Keep track of distance of each path
• Compare the distance, you have travelled
• Find the shortest path

Note: Different options and solutions are possible of same problem


What is a Program?
• A computer program is an instance, or concrete representation, for an
algorithm in some programming language.
• A set of instructions which the computer will follow to solve a problem.

problem

algorithm

input Computer Program output


Informal Definition of an Algorithm
Algorithm Basics
 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 Formatting and syntax complexities
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
Pseudocode

• An informal way of programming


description that
– Does not require any strict programming
language syntax or
– Underlying technology considerations.
• In other words ‘fake code’
Making a Cup of Tea

The following both can be steps of algorithm for


making a cup of tea (Both are correct)
Algorithm Development: Basics
 Where problem can be solved with a series of
actions
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

Order is very important!!!


Steps to find Average of two numbers

Average Of Two Numbers


Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Algorithm Design: Practice

 Example: Determining even/odd number


 A number divisible by 2 is considered an even
number,
 While a number which is not divisible by 2 is
considered an odd number.
 Write pseudo-code to display first N odd/even
numbers.
Even/ Odd Numbers

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

A program is an instance of an algorithm,


written in some specific programming language
Abstract Data Types (ADT)

• Abstract Data Type (ADT) is the logical picture of the data and the
operations to manipulate the component elements of the data
• A definition for a data type solely in terms of a set of
values and a set of operations on that data type.
(Mathematical/logical views only).
• ADTs are entities that are definitions of data and
operations, but don’t have implementation details
• Each ADT operation is defined by its inputs and outputs.

• Encapsulation: Hide implementation details like where to


implement (C# , C++ in our case).
Examples
Abstract Data Type
(ADT)
Abstract Data Types (ADT)

•Basic definitions
 Type: a set of objects.

 Data item or element: a piece of information or


record.
 Member: a data item is said to be a member of a data
type.
 Simple data item: a data item containing no
subparts.
 Aggregate data item: a data item that may contain
several pieces of information.
 ADT: mentions WHAT is to be done, not HOW.
Data Structure

• A data structure is the physical implementation of


an ADT.
• Each operation associated with the ADT is implemented
by one or more subroutines in the implementation.

• Data structure usually refers to an organization of


data in main memory.
• File structure is an organization for data on
peripheral storage, such as a disk drive.
Logical vs. Physical Form
• Data items have both a logical and a physical form.

• Logical form: definition of the data item within an


ADT.
• Ex: Integers in mathematical sense: +, -

• Physical form: implementation of the data item


within a data structure.
• Ex: 16/32 bit integers, overflow.

• ADT is in the logical level and

• Data structure is in the implementation level


Data Type

ADT:
Data Items:
Type
Logical Form
Operations

Data Structure: Data Items:


Storage Space Physical Form
Subroutines

38
Data Structure Philosophy

• Each data structure has costs and benefits.

• Rarely is one data structure better than another


in all situations.
• A data structure requires:

• space for each data item it stores,

• time to perform each basic operation,

• programming effort.
Data Structure Philosophy
(cont.)
• Each problem has constraints on available space and
time.
• Only after a careful analysis of problem characteristics,
we can know the best data structure for the task.
• Bank example:

• Start account: a few minutes

• Transactions: a few seconds

• Close account: overnight


Collections
• Grouping of some variable number of data items that have
some shared significance
– Collection types represent different ways to collect data
– How a data can be accessed from memory.
– Vary in different languages

• Collection: is a structured data type that stores data and


provides operations to manipulate this data (such as: add,
remove and update).

• Collection main types (categories):


 Linear: List of elements where elements follow each other in a
linear order (ordered by position: first, second, … , etc).
 E.g.: Array, ArrayList, String, Struct, Stack, Queue.

 Nonlinear: List of elements which don’t have positional order.


 E.g.: trees, heaps, graphs, and sets
Collection
Operations
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.

Insert() adds a new item to the collection at a specific


location.
Remove() removes a specific item from the collection.

Clear() removes all items from the collection.

Contains() determines if a specific item is a member of a


collection.
IndexOf() determines the index of a specific element in a
collection.
Collection
Subcategories

Collection Subcategories (Types)

• Under both, (Linear and Nonlinear), there are


more subcategories.
• Linear Collections : insertion, deletion possible in
linear fashion. While, it can be either:
 Direct Access Collections, or
 Sequential Access Collections.

• Nonlinear Collections: insertion, deletion not


possible in linear fashion. While, it can be either:
 Hierarchical Collections, or
 Group Collections (unordered).
Direct Access
Collections
Direct Access Collections (linear)

• Array: a collection of elements with the same data


type that are directly accessed via an integer index.
• String: a collection of characters that can be
directly accessed via an index.

• Struct (Structure or record): a composite data type


that holds data that may consist of many different data
types.
 E.g.: Student record (num: int, name: string, avg: float)
Sequential Access Collections (linear

• Is a list that stores its elements in a sequential


order (i.e., linear list).

• Items in a linear list can only be accessed by


their position (i.e. they are not accessed
directly).

• Examples: Linked List, Stacks and


Queues
Sequential Access
Collections

Stack (Sequential Access)


Sequential Access
Collections

Queue(Sequential Access)
Hierarchical
Collections

Hierarchical Collections (non-


linear)

49
Group
Collections

Group Collections (non-


linear)
• A nonlinear collection of items that are
UNORDERED.

• Examples:
 Sets.
 Graphs.
Graph Collection
 Networks.

Sets Collection
Network Collection
Some Questions to Ask

• Are all data inserted into the data structure at


the beginning, or are insertions interspersed
with other operations?
• Can data be deleted?

• Are all data processed in some well-defined


order, or is random access allowed?
Homework
Computing Weekly Wages
Gross pay depends on the pay rate and the
number of hours worked per week.
However, if you work more than 40 hours,
you get paid time-and-a-half for all hours
worked over 40. Write the algorithm to
compute gross pay given pay rate and hours
worked
Problem Solving
For every problem, we need following 4 steps

 Be familiar with problem solving


 Be able to develop and implement algorithms
 Be able to trace algorithms
 Be able to select appropriate data structures and
algorithms for given problems

You might also like