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

Data Structures Overview: Topics

Uploaded by

octoberea6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Structures Overview: Topics

Uploaded by

octoberea6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Topics

• Data Structures Overview


Data Structure Overview, Memory Allocation,
Introduction to Algorithm Analysis • Memory Allocation
• Introduction to Algorithm Analysis

Data Structures Overview


Linear Data Structure Non-linear Data Structure
• Linked list • Tree
Data Structures Overview • Stack • Graph
• Queue
User-Defined Data Structures
• Hash Table

Data Structures Overview Data Structures Overview


• is a sequence of connected nodes, where each node consists Linked list
Linked lists of data and link to the next node

• linear structure represented by a real physical stack, Stack Queue


Stacks insertion / deletion operations perform at single end.

• Similar to Stack but is opened at both end for insertion &


Queues deletion
Tree

Trees • Non-linear Data Structure. It organizes data in a hierarchical


way.

• Non-linear Data Structure. It is a representation of a set of Graph Hash Table


Graphs objects where some pairs of objects (vertices) are connected
by links (edges).

Hash Table • stores data in an array format where each data value has its
own unique index value (in an associative manner).
6

1
Memory Allocation
• Static memory allocation:
– Store variables in stack and it happens at compile time
Memory Allocation based on variable definitions.
– Access to those variables is very fast
Static Memory Allocation – The stack is reserved in a LIFO order (dependent with
each other), the most recently reserved block is always
Dynamic Memory Allocation the next block to be freed. That is make stack simple to
keep track of its storage

Memory Allocation Static memory allocation


• Dynamic memory allocation: int x = 2;
– Allocation of memory storage (all available unused int a[4];
memory called the heap) for use in a computer int *b;
program during the runtime. int main ()
– Accessing this memory is a bit slower than stack. {
– Element of the heap have no dependencies with each
... ... ... ...
other and can always be accessed randomly at any
time, that is make heap much more complex to keep }
track of which parts are allocated or free.

9 10

void *malloc(size_t size);


Dynamic memory allocation
void * a generic pointer type that can point to memory of any type.
int x = 2;
size_t a type defined by the standard library as the type returned by
int a[4];
sizeof. The type is unsigned int. That is why you see (int *) type casting
int *b;
int main ()
{ Be careful to allocate the correct number of bytes, Example:
int *i = (int *) malloc(1); //wrong as it allocates 1 byte, not 1 int
b = (int *) malloc (4 *
sizeof(int) );
b[0] = 10; int *i = (int *) malloc(sizeof(int));
b[1] = 20; char *c = (char *) malloc(NAME_SIZE);
} double *d = (double *) malloc(5*sizeof(*d));
11 12

2
• Static memory allocation Memory Allocation & De-allocation
int arr[10];

• Dynamic memory allocation


• In C++:
int *ptr; // Single element
ptr = (int *) malloc (sizeof (int) * 10); int *X = new int;
delete X;
// Multiple elements
int *X_arr = new int[10];
• Pointer declaration & initialization
delete[ ] X_arr;
int x = 20;
int *Ptr;
... ≡ to .... int *Ptr = &x;
Ptr = &x;
13 14

Allocating an array with variable size Pointers – NULL Pointer


• Stack:
int size; • It points to nothing (“zero”).
scanf(“%d”, &size);
int array[size]; /* array size is determined at
runtime, this is not allowed in standard C, however in Example:
C99, it is allowed as it stores the array in a stack. */ Iptr = new int [1000000];
• Heap if(Iptr == NULL)
int *p; //size is 4 or 8 bytes ... For 32 or 64 bits. printf(“Error allocating memory”);
scanf(“%d”, &size);
p= new int[size];
16

#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int main(int argc, char** argv) {
int main(int argc, char** argv) {
int *Iptr = new (nothrow) int [5000000000]; //18.6 GB
int *Iptr = new (nothrow) int [9000000000]; // 34.3 GB
if(Iptr == NULL)
if(Iptr == NULL)
{
{
cout << "Error: memory could not be allocated " <<endl;
cout << "Error: memory could not be allocated " <<endl;
return 1;
return 1;
}
}
cout << "allocating memory successfully" <<endl;
cout << "allocating memory successfully" <<endl;
return 0;
return 0;
}
}

17 18

3
Question Question
int *P1; int *P1;
P1 = new int[5]; P1 = new int[5];
delete P1; delete P1;
if (P1 != NULL) // Does it works ? P1 = NULL;
cout << "P1 = " <<P1 <<" while *P1 = "<<*P1 <<endl; if (P1 != NULL) // Does it works ?
cout << "P1 = " <<P1 <<" while *P1 = "<<*P1 <<endl;
NO if condition is true & it will print, e.g.:
P1 = 0xc519f0 while *P1 = 12915952
YES if condition is false

19 20

Question
Question #include<stdio.h>
int main()
{ int x = 20, y = 10;
• What happens in this case? int *Ptr = &y;
*Ptr = x; // Are x and *Ptr are equal or not ?
int *Ptr_A; if (x == *Ptr)
Compilation error: invalid conversion from int to int * printf("first if: x and *Ptr are equal, they equal %d\n",*Ptr);
Ptr_A = 20; else
printf("first if: x and *Ptr are not equal, x = %d , *Ptr = %d\n“, x,*Ptr);
• What happens in this case? x = 1000;
Runtime error: memory not allocated if (x == *Ptr)
int *Ptr_A; printf("second if: x and *Ptr are equal, they equal %d",*Ptr);
*Ptr_A = 20; else
printf("second if: x and *Ptr are not equal, x = %d , *Ptr = %d“, x,*Ptr);
}

Dr Hany Hanafy Mahmoud Said 21 22

Important Remark Pointers – common problems


Dangling Pointers Memory Leak
*Ptr = x; // is Wrong • Dereferencing a pointer after the • When losing the pointer to the
memory it refers to has been freed is memory region allocated, so that
/* Ptr does not hold address of x */ called a “dangling pointer”.
• Behavior is undefined. Might:
space is reserved but unreachable
until the program terminates.
– appear to work • Example
– bogus data int *a = (int *)malloc(10 * sizeof(int));
– program crash
Ptr = &x; // is Right • Example
int b[10];
a = b; /* 40 bytes in heap are lost*/
int *a = (int *) malloc(10 * sizeof(int));
free(a);
printf("%d\n", a[0]); /* Error */

23 24

4
Pointers Static & Dynamic memory allocation

Static memory allocation Dynamic memory allocation


• Advantages
Size Fixed Not fixed
– Provide great flexibility for programs and functions (dynamic Done at Compile time Rune time
memory allocation at run time, passing & returning Life time Entire run time of the program It can be allocated or deallocated any
variables) time
– Help in building complex data structures (in this course) E.g. Arrays Linked list using pointers
Adv. Efficient execution time (supports direct Uses exact amount of memory storage
memory access) (not waste of space)
• Disadvantages Dis-adv. Waste of space & not sufficient for Direct memory access is not supported
recursive procedure (support only sequential access)
– Dangerous when doing operations without initializing
Store local Stack Heap
pointers variables in
– Needs to be deleted explicitly
– Slower than normal variables (indirect access)
26

Reference variables Reference variables


• Example:
• No memory is allocated for reference variables, it #include <iostream>
using namespace std;
is similar to aliasing (exist in C++). Pointers on other
int main() {
hand, need a storage for the address. char c = 'z';
char& r = c; // create a reference r
• Reference needs to be initialized, and once it is cout << r << endl; // print z
initialized, it cannot be re-initialized. Pointers can r = 's';
be re-initialized. cout << r << endl; // print s
return 0;
}

27 28

Reference variables
char c = 'z';
char& r = c;
char c2 = 'w';
&r = c2; // Does it compile ?
NO, reference variable cannot be re-initialized.
void add(int& x)
{
int a = 7;
x = &a; // Does it compile ?
} NO, reference variable cannot be re-initialized.
29 Dr Hany Hanafy Mahmoud Said 30

5
Using references & pointers Using references & pointers
• References are more safer (they must be initialized) • Do not initialize reference to constant variable (&var = 10).
• References are more simpler for syntax than pointers (do • Do not return reference from functions (out of scope when
not require dereferencing) function execution is finished).
• References is often used when we want to change variables • Avoid using references for variables with dynamic memory
dynamically through function as pointers, however allocations.
references does not use any extra memory. • Use pointers when NULL could be acceptable value
• Use references for input function parameters (that cannot • Use pointers for dynamically allocated variables
be NULL). • Use pointers for struct & class members
• Use reference for aliasing (int &s = Arr[i])

31 32

Why we need to understand program complexity


• Consider the following ‘Pseudo code’ example :
Def find_sum(given_array):
total = 0
Introduction to Algorithm analysis for each i in given_array:
total = total + i
Return total
Time complexity
We don't know
It Depends on:
•Programming language you use
• Computer specs (how fast computer is)
•Do you run other programs on the computer

What is Time complexity


Rephrase …
Big O Notation
• To know the time needed to complete the function,
we use Big O notation as a way of showing how the
runtime of the function increases when the input
Time … in micro second size increases.
• To give a roughly idea how your function scales
when the input to that function becomes larger &
larger
• Big O notation measures worst-case (Upper bound).
N … length of the array

6
How to calculate Big O notation Example
1. Different steps get added T = c … (constant time)
Def func1(given_array):
2. Drop constants c is different from one
total = 0
computer to anther
3. Different inputs implies different variables Return total
T = O(1)+O(1) = c1 + c2 = c3
4. Find the fastest growing term = O(1)
5. Take out the coefficients
• if T = 5000 = O(1) Def find_sum(given_array): T= O(1) + N x O(1) + O(1)
total = 0 = c1 + N x c2
• if T = aN + b = O(N)
for each i in given_array: =O(N)
• if T = cN2 + dN + e = O(N2)
total = total + i
• if T = xN3 + yN2 + zN + w = O(N3) Return total

Example Example
T= O(1) + N2 x O(1) + O(1) Def sum_product(given_arr): T= O(1) + O(1) + N x O(1) +
Def find_sum(given_arr2d):
= c 1 + N2 x c 2 N x O(1) + O(1)
total = 0 total = 0
= c1 + 2 x N
for each row in given_arr2d: =O(N2) product = 1
= O(N)
for each i in row: for each i in given_arr:
total = total + I
total = total + i
for each i in given_arr:
Return total
product = product x I
print sum, product
Return

Example Time / computational complexity


Def intersection(arrA, arrB): T= O(1) + O(N) x O(M) x O(1) • Constant time O(1)
+ O(1) • Linear time O(n)
count = 0
= c1 + O(N) x O(M) x O(1) • Logarithmic time O(log n)
for each i in arrA:
= O(N x M) • Quasilinear time O(n log n)
for each j in arrB:
if i = = j • Quadratic time O(n2)
Where N & M are the size of
count = count + I • Cubic time O(n3)
arrA & arrB respectively
Return count • Exponential time O(2n)
• Factorial time O(n!)

7
Constant time O(1) Linear time O(n)
• If running time does not depend on the size
of the input (n) for (int i = 0; i < n; i = i + 1) // i++
• O(any constant value), this is equivalent to Example: Linear search
being O(1). Even if there are loops, it doesn’t
depend on the size of n

Logarithmic time O(log2 n) Quasilinear time O(n log2 n)


• for (int i = 0; i < n; i = i + 1) {
• for (int i = 0; i < n; i = i * 2)
for (int j = n; j > 0; j = j / 2)
• Example: Binary search
• Example: merge sort

Quadratic time O(n2) Cubic time O(n3)


• for (int i = 0; i < n; i = i + 1) {
• for (int i = 0; i < n; i = i + 1) {
for (int j = 0; j < n; j = j + 1)
for (int j = 0; j < n; j = j + 1) {
• Example: bubble sort
for (int z = 0; z < n; z = z + 1)
• Example: multiplication of two n*n matrices

8
Exponential time O(2n) Factorial time O(n!)
• function fib(n) • function fact(n) {
{ for(i=0; i<n; i++)
if(n <= 1) {
{ return n } fact(n-1);
else
}
return fib(n-2) + fib(n – 1)
}
}
Example: traversing all nodes in binary tree Example: generating permutations

Source: https://www.slideshare.net/introprogramming/19-algorithms-andcomplexity Source: https://www.slideshare.net/introprogramming/19-algorithms-andcomplexity

Source: https://www.slideshare.net/introprogramming/19-algorithms-andcomplexity Source: https://www.slideshare.net/introprogramming/19-algorithms-andcomplexity

9
Source: https://www.slideshare.net/introprogramming/19-algorithms-andcomplexity
56

10

You might also like