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

Lecture 1 Introduction

This document discusses data structures and algorithms. It begins by defining primitive data types, which specify a domain of allowed values and operations on those values, like integers allowing addition. User-defined data types can also specify their own values and operations. Data structures group data using arrays, structures, files, and pointers. Pointers store the address of another data element and can link data structures together in chains. Asymptotic analysis uses Big-O notation to analyze algorithms' runtime growth rates as input size increases. Faster algorithms have lower asymptotic complexity in Big-O ordering.

Uploaded by

Keira Soarin
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)
9 views

Lecture 1 Introduction

This document discusses data structures and algorithms. It begins by defining primitive data types, which specify a domain of allowed values and operations on those values, like integers allowing addition. User-defined data types can also specify their own values and operations. Data structures group data using arrays, structures, files, and pointers. Pointers store the address of another data element and can link data structures together in chains. Asymptotic analysis uses Big-O notation to analyze algorithms' runtime growth rates as input size increases. Faster algorithms have lower asymptotic complexity in Big-O ordering.

Uploaded by

Keira Soarin
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/ 36

Data Structure

202 Comp
Dr. Ghada Nour, and Dr. Wael Zakria
Grading System
• Number of Credit hours 3
• Lecture 2
• Lab 2
• Total grades 150
• Final exam 90
• Mid-term 15
• Lab Work 37 (quiz, assignments , projects, Lab exam)
• Oral 8
Data type VS Abstract Data Type (ADT)
1. Data type:
Two important things about data type:
a. Define a certain domain of values.
b. Define operations allowed in this values.
Example 1:
int type
- takes only integer values.
- operations: addition, subtraction, multiplication, and
% operation etc.
Example 2:
int float
- takes only floating point values.
- operations: addition, subtraction, multiplication, , and
% operation not allowed.
User defined Data types
• In contrast with primitive data types, there a concept of user defined data types
• The operations and values of user defined data types are not specified in the language itself but is
specified by the user.
Example: structure
by using structure, we are defining our own type by combining other data types.
struct point
{ int x;
int y;
};
Abstract Data Types (ADT)
Abstract Data Types (ADT)

data structures, which are collections of variables, possibly of several different data types, connected
in various ways. Such as array or pointers.
Data Structure: cell
• The cell is the basic building block of data structures.
• We can picture a cell as a box that is capable of holding a value
drawn from some basic or composite data type.

• Data structures are created by giving names to aggregates of cells


and (optionally) interpreting the values of some cells as representing
connections (e.g., pointers) among cells.
Data Structure: cells grouping
• There are many mechanism to grouping cells in
programming language:
• Array
• Structure
• File
• pointers
Data Structure: cells grouping using Array
• The simplest aggregating mechanism in many programming
languages is the (one-dimensional) array.
• Array is a sequence of cells of a given type, which we shall often
refer to as the celltype.

• We can think of an array as a mapping from an index set (such as


the integers 1, 2, . . . , n) into the celltype.

• A cell within an array can be referenced by giving the array name


together with a value from the index set of the array.
Cell type
• Example: int A[100]
Data Structure: cells grouping using Structure
• Another common mechanism for grouping cells in programming languages is
the structure.
• A structure is a cell that is made up of a collection of cells, called
fields/members/attributes, of possibly dissimilar types.
• struct personStruct {
String name; float salary; Date birthdate};
• structures are often grouped into arrays; the type defined by the
aggregation of the fields of a record becomes the "celltype" of the
array.
• personStruct personList[100];
Data Structure: cells grouping using Pointers
• A pointer is a cell whose value indicates another cell.
• It is used to represent relationships between cells using pointers

.
• When we draw pictures of data structures, we indicate the fact that
cell A is a pointer to cell B by drawing an arrow from A to B.

struct cell{
int x;
cell* pointer;
}
Data Structure: cells grouping using Pointers
• The chain is pointed to by a variable named header,
which is of type cell*;
• Header points to a cell, a cell has value(s) and
pointer(s), then each pointer points to another cell.
Example on struct Example on pointer
Struct book 00AB134

{ string name; Algorithms and data structure


float price;
Algorithms and data structure 233.55 $
};
b
233.55 $ 00AB134
Book b; book * b; b

b.name = “Algorithms and data structure”; b→name = “Algorithms and data structure”;
b.price = 233.55; b→price = 233.55;
Example on pointers
int x = 10; x 000AA123
10
int * p = &x
000AA1567
cout<<p; // 000AA123 p 000AA123

cout<<&p; // 000AA1567
cout<<*p; // 10
(*p)++;
cout<< x; // 11
• we are interested in the design of “good” data
structures and algorithms.
• We can define a data structure and algorithm in the
following way:
• A data structure is a systematic way of organizing and
accessing data
• An algorithm is a step-by-step procedure for performing
some task in a finite amount of time.
Asymptotic Analysis
• In algorithm analysis, we focus on the growth rate of the running time
as a function of the input size n, taking a “big-picture” approach.
• For example, it is often enough just to know that the running time of an
algorithm grows proportionally to n.
• We analyze algorithms using a mathematical notation for functions
that disregards constant factors.
• We can perform an analysis of an algorithm by estimating the
number of primitive operations executed up to a constant factor.
The “Big-Oh” Notation
The “Big-Oh” Notation
• Example 4.6: The function 8n+5 is O(n).

Dr. Wael Zakaria TIC308 – Data


Sun, Feb 26, 2017 18
Structures and Algorithms-semester 2@UFE 2016-2017
The “Big-Oh” Notation
• Example 4.6: The function 8n+5 is O(n).
• Justification:
❑ By the big-Oh definition, we need to find
o a real constant c>0 and an integer constant n0 ≥ 1 such that
8n+5 ≤ cn for every integer n ≥ n0.
o It is easy to see that a possible choice is c = 9 and n0 = 5.
o Indeed, this is one of infinitely many choices available because there is a trade-off
between c and n0.
o For example, we could rely on constants c = 13 and n0 = 1.
The “Big-Oh” Notation
• Some Properties of the Big-Oh Notation
• The big-Oh notation allows us to ignore constant factors and lower-
order terms and
• focus on the main components of a function that affect its growth.
The “Big-Oh” Notation
• Some Properties of the Big-Oh Notation
• The big-Oh notation allows us to ignore constant factors and lower-
order terms and
• focus on the main components of a function that affect its growth.
The “Big-Oh” Notation
• Some Properties of the Big-Oh Notation
• The big-Oh notation allows us to ignore constant factors and lower-
order terms and
• focus on the main components of a function that affect its growth.
The “Big-Oh” Notation
• Some Properties of the Big-Oh Notation
• The big-Oh notation allows us to ignore constant factors and lower-
order terms and
• focus on the main components of a function that affect its growth.
The “Big-Oh” Notation
• Some Properties of the Big-Oh Notation
• The big-Oh notation allows us to ignore constant factors and lower-
order terms and
• focus on the main components of a function that affect its growth.

Dr. Wael Zakaria TIC308 – Data


Sun, Feb 26, 2017 24
Structures and Algorithms-semester 2@UFE 2016-2017
The “Big-Oh” Notation
• Characterizing Functions in Simplest Terms
• In general, we should use the big-Oh notation to characterize a
function as closely as possible.
• While it is true that the function f (n) = 4n3 +3n2 is O(n5) or even O(n4),
it is more accurate to say that f (n) is O(n3).
Comparative Analysis
• The big-Oh notation is widely used to characterize running times and space
bounds in terms of some parameter n, which is defined as a chosen measure of
the “size” of the problem.
• Suppose two algorithms solving the same problem are available:
• an algorithm A, which has a running time of O(n), and
• an algorithm B, which has a running time of O(n2).
• Which algorithm is better?

Dr. Wael Zakaria TIC308 – Data


Sun, Feb 26, 2017 26
Structures and Algorithms-semester 2@UFE 2016-2017
Comparative Analysis
❑ We know that n is O(n2), which implies that algorithm A is
asymptotically better than algorithm B, although for a small value of
n, B may have a lower running time than A.
• We can use the big-Oh notation to order classes of functions by
asymptotic growth rate.
• Our seven functions are ordered by increasing growth rate in the
following sequence,
• such that f (n) is O(g(n)) if function f (n) precedes function g(n):

Dr. Wael Zakaria TIC308 – Data


Sun, Feb 26, 2017 27
Structures and Algorithms-semester 2@UFE 2016-2017
Comparative Analysis
• We illustrate the growth rates of the seven functions in Table 4.3.

Dr. Wael Zakaria TIC308 – Data


Sun, Feb 26, 2017 28
Structures and Algorithms-semester 2@UFE 2016-2017
Primitive data type

Primitive data type

Numerical primitive Non-numerical data


data type type

Integer data type Floating point Char data type Boolean


data type data type
Byte ( 8 Short ( 16
bits = 1 bits = 2 Int ( 32 bits Long ( 64 bits
= 4 bytes) Float ( 32 bits = 4 bytes) Double ( 64 bits = 8 bytes)
byte) bytes) = 8 bytes)
Space and time Complexity of Algorithms with
Examples
1. Space Complexity of Algorithms with Examples

• You would have come across a term called space complexity when you deal with time complexity.
Similar to time complexity, space complexity is also an important parameter to determine the efficiency
of an algorithm/program.
• Space complexity is the total amount of memory space used by an algorithm/program including
the space of input values for execution. So to find space complexity, it is enough to calculate the space
occupied by the variables used in an algorithm/program.
• But often, people confuse Space complexity with Auxiliary space. Auxiliary space is just a temporary or
extra space and it is not the same as space complexity. In simpler terms,
• Space Complexity = Auxiliary space + Space use by input values

• Important Note: The best algorithm/program should have the lease space complexity. The lesser the space used, the
faster it executes.
Space Complexity of Algorithms

Example #1
#include <iostream.h>
using namespace std;
void main()
{ int a = 5, b = 5, c;
c = a + b; cout<<(c);
}
• In the above program, 3 integer variables are used. The size of the integer data type is 2 or 4 bytes
which depends on the compiler. Now, let’s assume the size as 4 bytes. So, the total space occupied
by the above-given program is 4 * 3 = 12 bytes. Since no additional variables are used, no extra
space is required.
• Hence, space complexity for the above-given program is O(1), or constant.
Space Complexity of Algorithms
Example #2
• #include <iostream.h>
• using namespace std;
• void main()
{ int n, i,x, sum = 0;
cin>> n;
int arr[n];
for(i = 0; i < n; i++)
{ cin>>x;
arr[i] = x;
sum = sum+arr[i];
cout<< sum;
}
In the above-given code, the array consists of ‘n’ integer elements. So, the space occupied by the array is 4 * n.
Also we have integer variables such as n, i and sum. Assuming 4 bytes for each variable, the total space
occupied by the program is 4n + 12 bytes. Since the highest order of n in the equation 4n + 12 is n, so the space
complexity is O(n) or linear.
Space Complexity of Algorithms
What is the space complexity of the following code:
Time complexities for different algorithms
Constant time — O(1)
Example 1:
#include <iostream.h>
int main()
{ printf("Hello World");
}
Logarithmic time — O(log n)
example 2:
#include <iostream.h>
int main()
{int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}
Time complexities for different algorithms
3. Linear time — O(n)
Example3:
#include<iostream.h>
void main()
{ int i, n = 8;
for (i = 1; i <= n; i++)
{ printf("Hello Word !!!"); }
4. O(n log n)
Example 4:
#include<iostream.h>
void main()
{
int i, j, k = 0;
for (i = 1; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n / 2;
}
Time complexities for different algorithms
Quadratic time — O(n2 )
Example 5:
#include<iostream.h>
void main()
{
int a = 0;
for (i = 0; i < N; i++)
{ for (j = 0; j < N; i++)
a = a + rand(); }}
Linear time — O(n + m)
Example 6:
#include<iostream.h>
void main()
{
int a = 0, b = 0;
for (i = 0; i < N; i++) { a = a + rand(); }
for (j = 0; j < M; j++) { b = b + rand(); } }

You might also like