Lecture 1 Introduction
Lecture 1 Introduction
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.
.
• 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
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).
• 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(); } }