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

Course Instructor Dr.M.K.Kavitha Devi, Professor/CSE, TCE

The document provides information about a course on data structures. It introduces the instructor and covers topics like the definition of data structures, abstract data types, and performance measurement. It discusses how data structures are used to organize data and the relationship between algorithms and data structures. Examples are given of abstract data types in C using a "nat" (natural number) type. The importance of efficient data structures for algorithm design is emphasized. In 3 sentences or less: The document introduces a course on data structures that covers topics such as definitions, abstract data types, and performance measurement. It discusses how data structures organize data and their relationship to algorithms. Examples are provided of implementing an abstract data type for natural numbers in C to illustrate

Uploaded by

baby sivakumar
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)
78 views43 pages

Course Instructor Dr.M.K.Kavitha Devi, Professor/CSE, TCE

The document provides information about a course on data structures. It introduces the instructor and covers topics like the definition of data structures, abstract data types, and performance measurement. It discusses how data structures are used to organize data and the relationship between algorithms and data structures. Examples are given of abstract data types in C using a "nat" (natural number) type. The importance of efficient data structures for algorithm design is emphasized. In 3 sentences or less: The document introduces a course on data structures that covers topics such as definitions, abstract data types, and performance measurement. It discusses how data structures organize data and their relationship to algorithms. Examples are provided of implementing an abstract data type for natural numbers in C to illustrate

Uploaded by

baby sivakumar
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/ 43

Introduction

Course Instructor
Dr.M.K.Kavitha Devi,
Professor/CSE, TCE
Course Content
• Data Structure
• Definition
• Importance
• Abstract Data Type
• Definition
• Example in C
• Performance Measure
• Asymptotic Measure
• Compute Time Complexity
• Example
• Quiz
Data Structure - Definition
• Data structure is a data organization, management, and storage format
that enables efficient access and modification.

• It is a collection of data values, the relationships among them, and the


functions or operations that can be applied to the data.
Data Structures - Importance
• Data structures serve as the basis for abstract data types (ADT).
• ADT defines the logical form of the data type. The data structure implements
the physical form of the data type.
• Different types of data structures are suited to different kinds of
applications, and some are highly specialized to specific tasks.
• Relational databases commonly use B-tree indexes for data retrieval
• Compiler implementations usually use hash tables to look up identifiers.
• Data structures provide a means to manage large amounts of data
efficiently for uses such as large databases and internet indexing
services.
• Efficient data structures are key to designing efficient algorithms.
• Some formal design methods and programming languages emphasize
data structures, rather than algorithms, as the key organizing factor in
software design.
• Data structures can be used to organize the storage and retrieval of
information stored in both main memory and secondary memory
Data Structures
• Data structure studies the organization of data in computers,
consisting of
• the (abstract) data types (definition and repr’)
• relationship between elements of this type
• operations on data types

• Algorithms:
• methods to operate on data structures
• tradeoff between efficiency and simplicity
• subtle interplay with data structure design

• Slogan: program = data structures + algorithm


Data Types
• A data type consists of:
• A collection of data elements (a type)
• A set of operations on these data elements

• Data types in languages:


• predefined:
• any language defines a group of predefined data types
• (In C) int, char, float, double, …

• user-defined:
• allow programmers to define their own (new) data types
• (In C) struct, union, …
Data Type Examples
• Predefined:
• type: int
• elements: …, -2, -1, 0, 1, 2, …
• operations: +, -, *, /, %, …

• User-defined:
• type: complex
• elements: 1+3i, -5+8i, …
• operations: newComplex, add, distance, …
Abstract Data Types
• An abstract data type:
• separates data type definition from representation
• separates function declaration (prototypes) from implementation

• Example of abstract data types in languages


• interfaces in Java
• (roughly) header files & typedef in C
Abstract Data Types
• Consider a data type to represent natural number n:
• a data type called “nat”
• elements: 0, 1, 2, 3, …
• operations: newNat, add, sub, …
• How to represent this (abstract) data type in C?
“nat” ADT in C (Interface)
// in file “nat.h”
#ifndef NAT_H
#define NAT_H

typedef struct nat *nat;

nat newNat (int i);


nat natAdd (nat n1, nat n2);
// other function prototypes are similar

#endif
Client Code
// in file “main.c”
#include “nat.h”

int main ()
{
nat n1, n2, n3;

n1 = newNat (8);
n2 = newNat (9);
n3 = natAdd (n1, n2);

return 0;
}
“nat” Implementation
// in file “nat.c”
#include <stdlib.h>
#include “nat.h”
struct nat
{
int i; // the concrete internal representation
};
nat newNat (int i)
{
if (i<0)
error (“invalid arg\n”); n
nat n = malloc (sizeof (*nat));
i
n->i = i;
return n;
}
“nat” Implementation
n1
// in file “nat.c”
#include <stdlib.h> i
#include “nat.h”
n2
struct nat
{ i
int i; // the concrete internal representation
};
nat natAdd (nat n1, nat n2) n
{ n1->i +
nat n = malloc (sizeof (*n)); n2->i
n->i = n1->i + n2->i;
return n;
}
Client Code
// in file “main.c”
#include “nat.h”

int main ()
{
nat n1, n2, n3;

n1 = newNat (8);
n2 = newNat (9);
n3 = natAdd (n1, n2);

// but what if we want to print nat? Is it:


// printf (“”, n3) ????

return 0;
}
Change to the Interface
// in file “nat.h”
#ifndef NAT_H
#define NAT_H

typedef struct nat *nat;

nat newNat (int i);


nat natAdd (nat n1, nat n2);
void natPrint (nat n);
// other function prototypes are similar

#endif
Change to the Implementation
// in file “nat.c”
#include <stdlib.h>
#include <stdio.h>
#include “nat.h”

struct nat
{
int i; // the concrete internal representation
};

void natPrint (nat n)


{
printf (“%d”, n->i);

return;
}
Client Code
// in file “main.c”
#include “nat.h”

int main ()
{
nat n1, n2, n3;

n1 = newNat (8);
n2 = newNat (9);
n3 = natAdd (n1, n2);

natPrint (n3);

return 0;
}
Performance Measure
• There are more than one way to solve a problem.
• How to compare the performance different algorithms and
choose the best one to solve a particular problem?
• Time complexity of an algorithm quantifies the amount of time
taken by an algorithm to run as a function of the length of the
input.
• Space complexity of an algorithm quantifies the amount of space
or memory taken by an algorithm to run as a function of the
length of the input.
• Time and space complexity depends on lots of things like
hardware, operating system, processors, etc.
• These factors are not considered, while analyzing the algorithm.
Only execution time of an algorithm is considered.
An Example
• Suppose you are given an array A of length N and an integer x and you have to
find if exists in array .
• Simple solution to this problem is traverse the whole array A and check if the
any element is equal to x.
for i = 1 to N
if A[i] is equal to x
return TRUE
return FALSE
• Each of the operation in computer take approximately constant time. Let each
operation takes 𝑐 time. The number of lines of code executed is actually
depends on the value of x.
• During analyses of algorithm, mostly worst case scenario is considered, i.e.,
when x is not present in the array .
• So, total execution time will be 𝑁 + 1 ∗ 𝑐 + 𝑁 ∗ 𝑐 + 𝑐
where 𝑁 + 1 ∗ 𝑐 for the for statement, 𝑁 ∗ 𝑐 for the if condition and c for the
return statement ( ignoring some operations like assignment of ).
• The total time depends on the length of the array . If the length of the array will
increase the time of execution will also increase.
Asymptotic Measures – Big-oh
Asymptotic Measures – Omega
Asymptotic Measures – Theta
Example 1
Example 2
Practice…
Solution
General Rules
• Rule 1—FOR loops
• The running time of a for loop is at most the running time of the
statements inside the for loop (including tests) times the number of
iterations.

• Rule 2—Nested loops


• Analyze these inside out. The total running time of a statement
inside a group of nested loops is the running time of the statement
multiplied by the product of the sizes of all the loops.
General Rules
• Rule 3—Consecutive Statements
• These just add

• Rule 4—If/Else
• For the fragment

• the running time of an if/else statement is never more than the


running time of the test plus the larger of the running times of S1
and S2.
General Rules
• A basic strategy of analyzing from the inside (or deepest part) out
works.
• If there are function calls, these must be analyzed first.
• If there are recursive functions, there are several options.
Quiz 1 - Question
• What is the time complexity of following code:
• int a = 0, b = 0;
• for (i = 0; i < N; i++)
• {
• a = a + rand();
• }
• for (j = 0; j < M; j++)
• {
• b = b + rand();
• }
• Options
1. O(N ) time
2. O(N + M) time
3. O(M) time
4. O(N * M) time
Quiz 1 - Answer
• What is the time complexity of • Explanation
following code: • The first loop is O(N)
• int a = 0, b = 0;
• for (i = 0; i < N; i++) • The second loop is O(M)
• { • Since we don’t know which is
• a = a + rand(); bigger, we say this is O(N + M).
• } • This can also be written as
• for (j = 0; j < M; j++) O(max(N, M)).
• {
• b = b + rand();
• }
• Options
1. O(N ) time
2. O(N + M) time
3. O(M) time
4. O(N * M) time
Quiz 2 - Question
• What is the time complexity of following code:
• int a = 0;
• for (i = 0; i < N; i++)
• {
• for (j = N; j > i; j--)
• {
• a = a + i + j;
• }
• }

• Options:
1. O(N)
2. O(N*log(N))
3. O(N * Sqrt(N))
4. O(N*N)
Quiz 2 - Answer
• What is the time complexity of • Explanation:
following code: • The above code runs total no of
• int a = 0; times
• for (i = 0; i < N; i++)
• { • = N + (N – 1) + (N – 2) + … 1 + 0
• for (j = N; j > i; j--) • = N * (N + 1) / 2
• {
• a = a + i + j;
• = 1/2 * N^2 + 1/2 * N
• } • O(N^2) times.
• }

• Options:
1. O(N)
2. O(N*log(N))
3. O(N * Sqrt(N))
4. O(N*N)
Quiz 3 - Question
• What is the time complexity of following code:
• int i, j, k = 0;
• for (i = n / 2; i <= n; i++)
• {
• for (j = 2; j <= n; j = j * 2)
• {
• k = k + n / 2;
• }
• }

• Options:
1. O(n)
2. O(nLogn)
3. O(n^2)
4. O(n^2Logn)
Quiz 3 - Answer
• What is the time complexity of • Explanation:
following code:
• int i, j, k = 0;
• j keeps doubling till it is less than or
equal to n.
• for (i = n / 2; i <= n; i++)
• { • Number of times, we can double a
• for (j = 2; j <= n; j = j * 2) number till it is less than n would be
• {
log(n).
• k = k + n / 2; • Let’s take the examples here.
• } • for n = 16, j = 2, 4, 8, 16
• }
• for n = 32, j = 2, 4, 8, 16, 32
• So, j would run for O(log n) steps.
• Options:
• i runs for n/2 steps.
1. O(n)
• So, total steps = O(n/ 2 * log (n)) =
2. O(nLogn) O(n*logn)
3. O(n^2)
4. O(n^2Logn)
Quiz 4 - Question
• What does it mean when we say that an algorithm
X is asymptotically more efficient than Y?

• Options:
1. X will always be a better choice for small inputs
2. X will always be a better choice for large inputs
3. Y will always be a better choice for small inputs
4. X will always be a better choice for all inputs
Quiz 4 - Answer
• What does it mean when • Explanation:
we say that an algorithm X • In asymptotic analysis we
is asymptotically more consider growth of
efficient than Y? algorithm in terms of input
size.
• Options: • An algorithm X is said to be
1. X will always be a better asymptotically better than Y
choice for small inputs :
2. X will always be a better • if X takes smaller time than y
choice for large inputs for all input sizes n larger
3. Y will always be a better than a value n0 where n0 >
choice for small inputs 0.
4. X will always be a better
choice for all inputs
Quiz 5 - Question
• What is the time complexity of following code:
• int a = 0, i = N;
• while (i > 0)
• {
• a += i;
• i /= 2;
• }

• Options:
1. O(N)
2. O(Sqrt(N))
3. O(N / 2)
4. O(log N)
Quiz 5 - Question
• What is the time complexity • Explanation
of following code: • For i = 16
• int a = 0, i = N; • In loop i value 8, 4, 2, 1, 0
• while (i > 0)
• { • log 2 16 = 4
• a += i;
• i /= 2;
• }

• Options:
1. O(N)
2. O(Sqrt(N))
3. O(N / 2)
4. O(log N)
Summary
• Data structure studies data representation and operations
• direct interplay with algorithm design
• Abstract data types enable modular programming
• clear separation between interface and implementation
• interface and implementation should design and evolve together
• Polymorphism enables code reuse
• Performance Analysis of the Algorithm
• Time and Space Complexity
• Asymptotic Notation
• Omega, Theta and Big-oh
Homework
Homework

You might also like