0% found this document useful (0 votes)
29 views30 pages

UNIT 1 - DATA STRUCTURES (1)

This document provides an overview of data structures, algorithms, and performance analysis in computer science. It covers definitions, types of data structures, algorithm specifications, recursive algorithms, data abstraction, and performance evaluation metrics such as time and space complexity. Additionally, it discusses asymptotic notation for analyzing algorithm efficiency.

Uploaded by

syeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views30 pages

UNIT 1 - DATA STRUCTURES (1)

This document provides an overview of data structures, algorithms, and performance analysis in computer science. It covers definitions, types of data structures, algorithm specifications, recursive algorithms, data abstraction, and performance evaluation metrics such as time and space complexity. Additionally, it discusses asymptotic notation for analyzing algorithm efficiency.

Uploaded by

syeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Structures – Unit 1

• Overview - Data Structure


• Algorithm Specification
• Data Abstraction
• Performance Analysis
Data Structures
• A data structure is a storage that is used to
store and organize data.
• It is a way of arranging data on a computer so
that it can be accessed and updated
efficiently.
Types of Data Structures
Algorithm Specification
• Definition
– An algorithm is a finite set of instructions that, if followed,
accomplishes a particular task. In addition, all algorithms must satisfy
the following criteria:
(1)Input. There are zero or more quantities that are externally supplied.
(2)Output. At least one quantity is produced.
(3)Definiteness. Each instruction is clear and unambiguous.
(4)Finiteness. If we trace out the instructions of an algorithm, then for all
cases, the algorithm terminates after a finite number of steps.
(5)Effectiveness. Every instruction must be basic enough to be carried
out, in principle, by a person using only pencil and paper. It is not
enough that each operation be definite as in (3); it also must be
feasible.
Describing Algorithms
• Natural language
– English, Chinese
• Instructions must be definite and effectiveness
• Graphic representation
– Flowchart
• work well only if the algorithm is small and simple
• Pseudo language
– Readable
– Instructions must be definite and effectiveness
• Combining English and C
– In this text
Translating a Problem into an
Algorithm
• Problem
– Devise a program that sorts a set of n>= 1 integers
• Step I - Concept
– From those integers that are currently unsorted, find
the smallest and place it next in the sorted list
• Step II - Algorithm
– for (i= 0; i< n; i++){
Examine list[i] to list[n-1] and suppose that the
smallest integer is list[min];
Interchange list[i] and list[min];
}
Translating a Problem into an Algorithm(Cont.)
• Step III - Coding

void sort(int *a, int n)


{
for (i= 0; i< n; i++)
{
int j= i;
for (int k= i+1; k< n; k++){
if (a[k ]< a[ j]) j= k;
int temp=a[i]; a[i]=a[ j]; a[ j]=temp;
}
}
Recursive Algorithms
• Direct recursion
– Functions call themselves
• Indirect recursion
– Functions call other functions that invoke the calling
function again
• When is recursion an appropriate mechanism?
– The problem itself is defined recursively
– Statements: if-else and while can be written
recursively
– Art of programming
• Why recursive algorithms ?
– Powerful, express an complex process very clearly
Recursive Implementation of Binary
Search
int binsearch(int list[], int searchnum, int left, int right)
{// search list[0]<= list[1]<=...<=list[n-1] for searchnum
int middle;
while (left<= right){
middle= (left+ right)/2;
switch(compare(list[middle], searchnum)){
case -1: left= middle+ 1;
break; int compare(int x, int y)
case 0: return middle; {
case 1: right= middle- 1; break; if (x< y) return -1;
}} else if (x== y) return 0;
else return 1;
return -1;} }
Recursive Implementation of Binary
Search
int binsearch(int list[], int searchnum, int left, int right)
{// search list[0]<= list[1]<=...<=list[n-1] for searchnum
int middle;
while (left<= right){
middle= (left+ right)/2;
switch(compare(list[middle], searchnum)){
case -1:return binsearch(list, searchnum, middle+1, right);
case 0: return middle;
case 1: return binsearch(list, searchnum, left, middle- 1);
}
}
return -1;
}
Data Abstraction
• Types of data
– All programming language provide at least
minimal set of predefined data type, plus user
defined types
• Data types of C
– Char, int, float, and double
• may be modified by short, long, and unsigned
– Array, struct, and pointer
Data Type
• Definition
– A data type is a collection of objects and a set of
operations that act on those objects
• Example of "int"
– Objects: 0, +1, -1, ..., Int_Max, Int_Min
– Operations: arithmetic(+, -, *, /, and %),
testing(equality/inequality), assigns, functions
• Define operations
– Its name, possible arguments and results must be
specified
• The design strategy for representation of objects
– Transparent to the user
Abstract Data Type
• Definition
– An abstract data type(ADT) is a data type that is
organized in such a way that the specification of
the objects and the specification of the
operations on the objects is separated from the
representation of the objects and the
implementation of the operation.#
• Why abstract data type ?
– implementation-independent
Classifying the Functions of a Data
Type
• Creator/constructor:
– Create a new instance of the designated type
• Transformers
– Also create an instance of the designated type by
using one or more other instances
• Observers/reporters
– Provide information about an instance of the type, but
they do not change the instance
• Notes
– An ADT definition will include at least one function
from each of these three categories
An Example of the ADT
structure Natural_Number is
objects: an ordered subrange of the integers starting at zero and
' ending at the maximum integer (INT_MAX) on the computer
functions:
for all x, y is Nat_Number, TRUE, FALSE is Boolean and where .
+, -, <, and == are the usual integer operations
Nat_NoZero() ::= 0
Boolean Is_Zero(x) ::= if (x) return FALSE
Nat_No Add(x, y) ::= if ((x+y)<= INT_MAX) return x+ y
else return INT_MAX
Boolean Equal(x, y) ::= if (x== y) return TRUE
else return FALSE
Nat_No Successor(x) ::= if (x== INT_MAX) return x
else return x+ 1
Nat_No Subtract(x, y) ::= if (x< y) return 0
else return x-y
end Natural_Number
Performance Analysis
• Performance evaluation
– Performance analysis
– Performance measurement
• Performance analysis - prior
– an important branch of CS, complexity theory
– estimate time and space
– machine independent
• Performance measurement -posterior
– The actual time and space requirements
– machine dependent
Performance Analysis(Cont.)
• Space and time
– Does the program efficiently use primary and
secondary storage?
– Is the program's running time acceptable for the task?

• Evaluate a program generally


– Does the program meet the original specifications of the task?
– Does it work correctly?
– Does the program contain documentation that show how to use it and
how it works?
– Does the program effectively use functions to create logical units?
– Is the program's code readable?
Space Complexity
• Definition
– The space complexity of a program is the amount of
memory that it needs to run to completion
• The space needed is the sum of
– Fixed space and Variable space
• Fixed space
– Includes the instructions, variables, and constants
– Independent of the number and size of I/O
• Variable space
– Includes dynamic allocation, functions' recursion
• Total space of any program
– S(P)= c+ Sp(Instance)
Examples of Evaluating Space
Complexity
float abc(float a, float b, float c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.00;
}
Sabc(I)= 0 float rsum(float list[], int n)
{
if (n) return rsum(list, n-1)+ list[n-1];
float sum(float list[], int n) return 0;
{ }
float fTmpSum= 0; Srsum (n)= 4*n
int i;
for (i= 0; i< n; i++) parameter:float(list[]) 1
fTmpSum+= list[i]; parameter:integer(n) 1
return fTmpSum;
return address 1
}
return value 1
Ssum(I)= Ssum (n)= 0
Time Complexity
 Definition
 The time complexity, T(p), taken by a program P is the sum of the
compile time and the run time
 Total time
 T(P)= compile time + run (or execution) time
= c + tp(instance characteristics)
Compile time does not depend on the instance characteristics
 How to evaluate?
 Use the system clock
 Number of steps performed
 machine-independent
 Definition of a program step
 A program step is a syntactically or semantically meaningful program segment
whose execution time is independent of the instance characteristics
(10 additions can be one step, 100 multiplications can also be one step)
Examples of Determining Steps(Cont.)
 The second method: build a table to count
s/e: steps per execution
frequency: total numbers of times each statements is executed
Statement s/e Frequency Total Steps

void add(int a[][MaxSize], . . . 0 0 0


{ 0 0 0
int i, j; 0 0 0
for (i=0; i< rows; i++) 1 rows+ 1 rows+ 1
for (j=0; j< cols; j++) 1 rows*(cols+1) rows*cols+ rows
c[i][j]= a[i][j] + b[i][j]; 1 rows*cols rows*cols
} 0 0 0

Total 2rows*cols+2rows+1
Remarks of Time Complexity
 Difficulty: the time complexity is not dependent solely on
the number of inputs or outputs
 To determine the step count
 Best case, Worst case, and Average
 Example – binary search program
Asymptotic Notation(O, , )
• motivation
– Target: Compare the time complexity of two programs that
computing the same function and predict the growth in run time
as instance characteristics change
• Asymptotic notation
– Big "oh“ O
• upper bound(current trend)
– Omega 
• lower bound
– Theta 
• upper and lower bound
Asymptotic Notation O
• Definition of Big "oh"
– f(n)= O(g((n)) iff there exist positive constants c
and n0 such that f(n)<= cg(n) for all n, n>= n0
– g(n) is the least upper bound
Asymptotic Notation 
• Definition
– f(n)= (g(n)) iff there exist positive constants c
and n0 such that f(n)>= cg(n) for all n, n>= n0
– The largest lower bound
Asymptotic Notation 
• Definition
– f(n)= (g(n)) iff there exist positive constants c1, c2,
and n0 such that c1g(n)<= f(n) <= c2g(n) for all n,
n>= n0
Example of Time Complexity Analysis
Statement Asymptotic complexity

void add(int a[][Max.......) 0


{ 0
int i, j; 0
for(i= 0; i< rows; i++) (rows)
for(j=0; j< cols; j++) (rows*cols)
c[i][j]= a[i][j]+ b[i][j]; (rows*cols)
} 0

Total (rows*cols)
Example of Time Complexity
Analysis(Cont.)
The more global approach to count steps:
focus the variation of instance characterics.
int binsearch(int list[], int .....)
{ int middle;
while (left<= right){
middle= (left+ right)/2;
switch(compare(list[middle],
searchnum)){
case -1: left= middle+ 1;
break; worst case (log n)
case 0: return middle;
case 1: right= middle- 1;
}
}
return -1;
}
Table 1.7 Function values
Instance characteristic n

Time Name 1 2 4 8 16 32

1 Constant 1 1 1 1 1 1
log n Logarithmic 0 1 2 3 4 5
n Linear 1 2 4 8 16 32
nlog n Log Linear 0 2 8 24 64 160
n2 Quadratic 1 4 16 64 256 1024
n3 Cubic 1 8 61 512 4096 32768
2n Exponential 2 4 16 256 65536 4294967296
n! Factorial 1 2 54 40326 20922789888000 26313*1033
Reference

Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed


“Fundamentals of Data Structures in C”,
Computer Science Press, 1992.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy