0% found this document useful (0 votes)
78 views51 pages

SAK 3117 Data Structures: Chapter 1: Introduction To Data Structure

This document summarizes Chapter 1 of the textbook "SAK 3117 Data Structures". It introduces key concepts of data structures including: 1) Definitions of data structures, abstract data types, and algorithms. Common data structures include arrays, linked lists, stacks, queues, trees and graphs. 2) Types of algorithm analysis including complexity measures like time and space. Algorithm complexity is expressed using Big-O notation. 3) Examples of simple loops like linear and logarithmic loops, and nested loops which can be linear logarithmic, quadratic, or dependent quadratic.

Uploaded by

Gunblade IV
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views51 pages

SAK 3117 Data Structures: Chapter 1: Introduction To Data Structure

This document summarizes Chapter 1 of the textbook "SAK 3117 Data Structures". It introduces key concepts of data structures including: 1) Definitions of data structures, abstract data types, and algorithms. Common data structures include arrays, linked lists, stacks, queues, trees and graphs. 2) Types of algorithm analysis including complexity measures like time and space. Algorithm complexity is expressed using Big-O notation. 3) Examples of simple loops like linear and logarithmic loops, and nested loops which can be linear logarithmic, quadratic, or dependent quadratic.

Uploaded by

Gunblade IV
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 51

SAK 3117 Data Structures

Chapter 1: INTRODUCTION TO DATA


STRUCTURE
Objective
      To introduce:
Concept and basic terminologies
Operations
Algorithms analysis
CONTENT
1.1       Data Structure?
1.1.1   Type
1.1.2   Operations
1.2       Algorithms
1.3       Algorithms analysis
1.3.1   Algorithm Complexity
1.3.2   Big-O Notation
Introduction
 Look around and you will see that people
organize things:
 Write TO-DO Lists
 Queue at ATMs
 Arrange stacks of books
 Look-up words in dictionary
 Organize directories in computers
 Plan road map to go somewhere
Introduction
 Computer programs also need to organize data, for example using lists,
queues, stacks, etc.
 These ways of organizing data are represented by Abstract Data Types
(ADT).
 ADT is a specification that describes a data set and the operation on
that data.
 Each ADT specifies WHAT data is stored and WHAT operations on the
data
 ADT DOES NOT specify HOW to store or HOW to implement the
operations, so ADT is independent of any programming language.
 In contrast, a Data Structure is an implementation of an ADT within a
programming language.
 Most of the algorithms require the use of correct data representation
for accessing efficiency.
Introduction
 Allowable representation and operations for the algorithm is
called data structure.
 Definition:
 To manage data logically/ as mathematical model.
 To implement in a computer.
 It involves quantitative analysis – memory management
and time for efficiency.
 Two types
 Static data structures – fix size such as array and record.
 Dynamic – varies in size such as link-list, queue, stack.
Introduction

Type of data structures in Java
 Array
 Class
 String

Data structures have to be created:
 Lists
 Stacks
 Queues
 Recursive
 Trees
 Graph
1.1.1 Type of Data Structures
 1. Array
 Is the simplest data structure.
 Can be 1 dim. (linear) and multidim.
Index Student
0 Zaki
1 Dollah
2 Mamat
3 Zack
4 Ali
1.1.1 Type of Data Structures
 2. Linked-list
 A dynamic data structure where size can be changed.
 Using a pointer

Student Pointer PA Pointer


Alfie 3 Dr Azim 1
Daud 1
Dr Ramlan 2
Zack 2
Dollah 2 Dr Ali 3
Ali 3
1.1.1 Type of Data Structures

3. Stack
 Addition and deletion always occur on the top of
the stack. Know as - LIFO (Last In First Out).
 Example: a stack of plates

4. Queue
 Adding at the back and deleting in front. Known
as – FIFO (First In First Out).
 Example: queuing for the bus, taxi, etc.

5. Recursive
 Function calling himself to perform repetition.
 Example: Factorial, Fibonacci, etc.
1.1.1 Type of Data Structures
 6. Tree
 Data in a hierarchical relationship.
STUDENT

Matric Name Address Programme

Road City

 7. Graph
 Like a tree but the relationship can be in any direction.
 Example: Distance between two or more towns.
1.1.2 Operations in a Data Structure
 Operations
 Traversing – to access every record at least
once.
 Searching – to find the location of the record
using key
 Insertion – adding a new record.
 Deletion – remove a record from the structure.
 Combination
 Updating
 Sorting
 Merging
1.2 Algorithm
 Definition: steps to solve a problem.
 Input  Process  Output
 Data structure + Algorithm  program
 Example 1:
 To find a summation of N numbers in an array
of A.
1 Set sum = 0
2 for j = 0 to N-1 do :
3 begin
sum = sum + A[j]
4 end
1.2 Algorithm
 Example 2:
 To find a summation of N numbers in an
array of A.
1 sum = 0
2 for j = 0 to N-1
sum = sum + A[j]
1.2 Algorithm
 Example 3:
 Multiply (Matrix a, Matrix b)
1. If column size of a equal to row size of b.
2. for i = 1 to row size of a
2.1 for j = 1 to column size of b
cij = 0
2.2 for k = 1 to column size of a
cij = cij + aik * bkj
1.3 Algorithm Analysis
 To determine how long or how much spaces are
required by that algorithm to solve the same
problem.
 Other measurements:
 Effectiveness
 Correctness
 Termination
 Effectiveness
 Easy to understand
 Easy to perform tracing.
 The steps of logical execution are well
organized.
1.3 Algorithm Analysis
 Correctness
 Output is as expected or required and
correct.
 Termination
 Step of executions contains ending.
 Termination will happen as being planned
and not because of problems like looping,
out of memory or infinite value.
1.3 Algorithm Analysis
 Measurement of algorithm efficiency.
 Running time
 Memory usage
 Correctness
1.3.1 Algorithm Complexity
 Algorithm M complexity is a function, f(n)
where the running time and/or memory
storage are required for input data of size n.
 In general, complexity refers to running
time.
 If a program contains no loop, f depends on
the number of statements. Else f depends
on number of elements being process in the
loop.
1.3.1 Algorithm Complexity
 Looping Functions can be categorized into 2
types:
 Simple Loops
* Linear Loops
* Logarithmic Loops
 Nested Loops
* Linear Logarithmic
* Dependent Quadratic
* Quadratic
1.3.1 Algorithm Complexity
 Simple Loops
 1. Linear Loops
 Algo. 1.a:
i=1
loop (i <= 1000)
application code
i=1+1
end loop
 The number of iterations directly proportional to the loop
factor (e.g. loop factor = 1000 times). The higher the
factor, the higher the no. of loops.
 The complexity of this loop proportional to no. of
iterations. Determined by the formula: f(n) = n
1.3.1 Algorithm Complexity
 Simple Loops
 1. Linear Loops
 Algo. 1.b:
i=1
loop (i <= 1000)
application code
i=1+2
end loop
 Number of iterations half the loop factor (e.g. 1000/2 = 500
times).
 Complexity is proportional to the half factor. f(n) = n/2
 Both cases still consider Linear Loops - a straight line graphs.
1.3.1 Algorithm Complexity
 Linear Loops
 2. Logarithmic Loops
 Consider a loop which controlling variables - multiplied or
divided:
 Algo. 2.a & 2.b:
Multiply Loops Divide Loops

1 i=1 1 i = 1000
2 loop (i < 1000) 2 loop (i >=1)
1 application code 1 application code
2 i=ix2 2 i=i/2
3 end loop 3 end loop
1.3.1 Algorithm Complexity
Multiply Divide

Iteration Value of i Iteration Value of i

1 1 1 1000

2 2 2 500

3 4 3 250

4 8 4 125

5 16 5 62

6 32 6 31

7 64 7 15

8 128 8 7

9 256 9 3

10 512 10 1

(exit) 1024 (exit) 0


1.3.1 Algorithm Complexity
 No of iterations is 10 in both cases.
Reasons:
 each iteration value of i double for multiply loops.
 iteration is cut half for the divide loop.
 The above loop continues while the below condition is
true:
Multiply 2Iterations < 1000
Divide 1000/2Iterations > 1
 Therefore the iterations in loops that multiply or divide
are determined by the formula: f(n) = [log2 n]
1.3.1 Algorithm Complexity
 Nested Loops
 Loops that contain loops :
Iterations= Outer loop iterations x Inner loop iterations
 3. Linear Logarithmic
 Algo. 3:
i=1
loop (i <= 10)
j=1
loop (j <= 10)
application code inner outer
j=j*2 loop loop
end loop
i=i+1
end loop
* Inner loop  Logarithmic loops (f(n) = log2n)
* Outer loop  Linear loops (f(n) = n)
1.3.1 Algorithm Complexity
 f(n) = Outer Loop x Inner Loop = 10 * [log2 10]
 Generalized the formula as: f(n) = [n log2 n]
1.3.1 Algorithm Complexity
 4. Dependent Quadratic
 Algo. 4:
i=1
loop (i <= 1000)
j=1;
loop (j <= i)
application code inner outer
j=j+1 loop loop
end loop
i=i+1
end loop
* Outer loop  Linear loops (f(n) = n)
1.3.1 Algorithm Complexity
 Inner loop depends on the outer loop, it is executed only
once the first iteration, twice the second iteration…
 No. of iterations in body of inner loops, if n = 10,
1 + 2 + 3 + … + 9 + 10 = 55
average 5.5 = 55/10 times @ = n+1
2

f(n) = Outer Loop x Inner Loop = n * n+1
2
 The formula for dependent quadratic, f(n) = n n+1
2
1.3.1 Algorithm Complexity
 5. Quadratic loop
 Algo. 5:
i=1
loop (i <= 10)
j=1
loop (j <= 10)
application code inner outer
j=j+1 loop loop
end loop
i=i+1
end loop
* Inner loop  Linear Loops (f(n) = n)
* Outer loop  Linear Loops (f(n) = n)
f(n) = Outer Loop x Inner Loop = n * n
The generalized formula, f(n) = n2
1.3.1 Algorithm Complexity
Criteria of Measurement

f(n) can be identified as:
1. worse case: max value of f(n) for any input
2. average case: expected value of f(n)
3. the best case: min value of f(n)
1.3.1 Algorithm Complexity
Criteria of Measurement
 Example 1: Linear Searching
Worse case
Item is last element in an array or none.
f(n) = n
Average case
No item or in anywhere in an array location.
The number of comparison is any item in index 1, 2, 3,...,n.
Best case
Item is in the first position
f(n) = 1
1.3.2 Big-O Notation
 After an algorithm is designed it should be analyzed.
 Usually, there are various ways to design a particular algorithm.
 Certain algorithm take very little computer time to execute, while others take a
considerable amount of time.
 Consider the following problem. The holiday season is approaching and the
gift shop is expecting sales to be double or even triple the regular amount. The
shop has hired extra delivery persons to deliver packages on time. The
company calculates the shortest distance from the shop to a particular
destination and hands the route to the driver. Suppose that 50 packages are to
be delivered to 50 different houses. The company, while creating the route,
finds that 50 houses are one mile apart and are in the same area. The first house
is also one mile from the shop (Figure 1).
1.3.2 Big-O Notation
Gift House House House House House
Shop 1 2 3 49 50
Figure 1: Gift Shop and the 50 houses

To simplify this figure, we use Figure 2:

Gift
Shop

Figure 2: Gift shop and each dot representing a house


Each dot represents a house and the distance between houses, as shown in Figure
2, is 1 mile.
1.3.2 Big-O Notation
To deliver 50 packages to their destinations, one of the drivers picks up all 50
packages, drives one mile to the first house, and delivers the first package.
Then, he drives another mile and delivers the second package, drives another
mile and delivers the third package, and so on. Figure 3 illustrates this delivery
scheme.

Gift
Shop

Figure 3: Package delivery scheme


1.3.2 Big-O Notation
It follows that using this scheme, the distance the driver drives to deliver the packages is:
1 + 1 + 1+ …+ 1 = 50 miles
Therefore, the total distance travelled by the driver to deliver the packages and return to the shop is:
50 + 50 = 100 miles
Another driver has a similar route to deliver another set of 50 packages. The driver looks at the route
and delivers the packages as follows: The driver picks up the first package, drives one mile to the
first house, delivers the package, and then comes back to the shop. Next, the driver picks up the
second package, drives 2 miles, delivers the second package, and then returns to the shop. The
driver then picks up the third package, drives 3 miles, delivers the package, and comes back to the
shop. Figure 4 illustrates this delivery scheme.

Gift
Shop
Figure 4: Another package delivery scheme
1.3.2 Big-O Notation
The driver delivers only one package at a time. After delivering a package, the driver
comes back to the shop to pick up and deliver the next package. Using this scheme, the
total distance travelled by this driver to deliver the packages and return to the store is:
2 * (1 + 2 + 3 + … + 50) = 2550 miles
Now suppose that are n packages to be delivered to n houses, and each house is one mile
apart from each other as shown in Figure 4. If the package are delivered using the first
scheme, the following equation gives the total distance travelled:
1 + 1 + 1 + … + 1 + n = 2n (1)

n times
If the packages are delivered using the second method, the distance travelled is:
2 * (1 + 2 + 3 + … + n) = 2 * (n(n + 1) / 2) = n2 + n (2)
1.3.2 Big-O Notation
In Equation 1, we say that the distance travelled is a function of n. Now consider
Equation 2. In this equation, for large values of n, we find that the term consisting of n 2
becomes the dominant term and the term containing n is negligible. In this case, the
distance travelled is a function of n2. Table 1 evaluates Equations 1 and 2 for certain
values of n. This table also shows the values of n, 2n, n2, and n2 + n.
Table 1: Values of n, 2n, n2, and n2 + n

n 2n n2 n2 + n
1 2 1 2
10 20 100 110
100 200 10000 10100
1000 2000 1000000 1001000
10000 20000 100000000 100010000
1.3.2 Big-O Notation
 Order of magnitude of the result based-on run-time
efficiency
 Expressed as O(N) @ O(f(n))  big-O
N – represents data, instructions, etc.
 Sometimes refer as complexity degree of measurement:
* run-time , complexity
* comparison of fast and slow algorithms for the same
problem statement.
 Time taken= Algorithm Execution = Running Time
1.3.2 Big-O Notation
Table 2: Measures of Efficiency
Nested Efficiency/ Algorithm Name Algorithm Type
Complexity
Degree
No Loop O(k) Constant
Simple Loop O(N) Linear Linear Search
O(log2N) Logarithmic Binary Search
Nested Loop O(N2) Quadratic Bubble Sort,
Selection Sort,
Insertion Sort
O(N log2N) Linear Merge Sort,
Logarithmic Quick Sort, Heap
Sort
1.3.2 Big-O Notation
Table 3: Intuitive interpretations of growth-rate function
1  A problem whose time requirement is constant and therefore,
independent of the problem’s size n.
log2n  The time for the logarithmic algorithm increases slowly as the
problem size increases.
 If you square the problem, you only double its time requirement.
 The base of the log does not affect a logarithmic growth rate, do
you can omit it in a growth-rate function.
 Ex: recursive

N  The time requirement for a linear algorithm increases directly with


the size of the problem.
 If you square the problem, you also square its time requirement.
1.3.2 Big-O Notation
Table 3: Intuitive interpretations of growth-rate function
n log2n  The time requirement increases more rapidly than a linear
algorithm.
 Such algorithms usually divide a problem into smaller problems
that are each solved separately, ex : mergesort
n2  The time requirement increases rapidly with the size of the
problem.
 Algorithm with 2 nested loop.
 Such algorithms are practical only for small problems.

n3  The time requirement increases rapidly with the size of the


problem than the time requirement for a quadratic algorithm.
 3 nested loop.
 Practical only for small problem.
1.3.2 Big-O Notation
 The big-O notation derived from f(n) using the following steps:
1. Set the coefficient of the term to 1.
2. Keep the largest term in the function and discard the others.
 Ex. 1:
f(n) = n * n + 1
2
= 1n2 + 1n
2 2
= n2 + n
= n2
O(f(n)) = O(n2)
 Ex. 2:
f(n) = 8n3 – 57n2 + 832n – 248
= n3 – n2 + n – 1
= n3
O(f(n)) = O(n3)
1.3.2 Big-O Notation

While analyzing a particular algorithm, we usually count the


number of operations performed by the algorithm. We focus on
the number of operations, not on the actual computer time to
execute the algorithm. This is due to the fact that a particular
algorithm can be implemented on a variety of computers and the
speed of the computer can effect the execution time. However, the
number of operations performed by the algorithm would be the
same on each computer.
1.3.2 Big-O Notation
 Ex. 3:
Algorithm to calculate average
1. initialize sum = 0
2. initialize i = 0
3. while i < n do the following :
4. a. add x[i] to sum
5. b. increment i by 1
6. calculate and return mean

1st Method
f(n) = Linear loops
=n
Big-O  O(n)
1.3.2 Big-O Notation
 2nd Method

Statement of time executed


----------------------------------------------------
1 1
2 1 f(n) = 3n + 4
3 n+1 =n+1
4 N =n
5 N big-O  O(n)
6 1
----------------------------------------------------
Total 3n + 4
1.3.2 Big-O Notation
 The comparison on run-time efficiency of different f(n) where
n = 256 and 1 instruction  1 microsec. (10-6 sec.)

f(n) Estimation Time


n 0.25 milisec.
n2 65 milisec.
n3 17 sec.
log2n 8 microsec.
n log2 n 2 milisec.
Note:
1 sec.  1000 miliseconds
1 milisec.  1000 microsec.
1.3.2 Big-O Notation
Ex. of calculation:
f(n) =n
= 256 x 10-6 sec.
= (256 x 10-6) x 103 milisec.
= 256 x 10-3
= 0.256 milisec.
f(n) = n2
= 2562
= 65536 x 10-6 sec.
= (65536 x 10-6) x 103 milisec.
= 65536 x 10-3
= 65 milisec.
 The run-time efficiency order of magnitude:

(1) < O(log2n) < O(n) < O(nlog2n) < O(n2) < O(n3)
Exercise 1

1. Reorder the following efficiency from smallest to largest:


a) 2n
b) n!
c) n5
d) 10,000
e) nlog2(n)

2. Reorder the following efficiency from smallest to largest:


a) nlog2(n)
b) n + n2 + n3
c) n0.5
Exercise 1
3. Calculate the run-time efficiency for the following program segment: (doIT
has an efficiency factor 5n).
1 i=1
2 loop i < = n
1 doIT(…)
2 i=i+1
3 end loop

4. Efficiency of an algo. is n3, if a step in this algo. takes 1 nanosec. (10-9


sec.). How long does it take the algo. to process an input of size 1000?
Exercise 1
5. Find the run-time efficiency of the following program segments
i) // Calculate mean
n = 0;
sum = 0;
x = System.in.read();
while (x != -999) {
n++;
sum += x;
x = System.in.read();
}
mean = sum / n;
Exercise 1
ii) // Matrix addition
for (int i = 0; i < n; i++)
for (int j = 0; j < n ; j++)
c[i][j] = a[i][j] + b[i][j];

You might also like