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

122 Midterm Theory Notes

The document provides notes on algorithms, data structures, pointers, memory allocation, linked lists, recursion, and algorithm analysis. It covers topics like algorithms versus processes, data structures, dynamic memory allocation, linked lists versus sequential lists, recursion, stack frames, and Big O notation for algorithm analysis.

Uploaded by

hughes.julia1120
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)
28 views

122 Midterm Theory Notes

The document provides notes on algorithms, data structures, pointers, memory allocation, linked lists, recursion, and algorithm analysis. It covers topics like algorithms versus processes, data structures, dynamic memory allocation, linked lists versus sequential lists, recursion, stack frames, and Big O notation for algorithm analysis.

Uploaded by

hughes.julia1120
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/ 12

122 Midterm Theory Notes

Lecture One: Algorithms & Data Structs


Algorithms vs Processes
Algorithm:
-A finite sequence of unambiguous instructions performed to achieve a goal or
compute a desired result. A precisely defined procedure for deriving solutions
Examples: making a sandwich, baking a cake
• May contain multiple algorithms as steps
• Has clearly defined termination
Each algorithm should specify each of the following:
1. Name and purpose.
2. Input and output.
3. Unambiguously specified finite sequence of steps
4. Termination condition or terminating state.

Process:
A sequence of operations performed to achieve a goal, do not have to terminate
Examples: Living and breathing
• Process represents higher complexity of work
• Process specification may contain ambiguity
• Process may be a continually ongoing activit

What are Data Structures?


Algorithms operate on various data items (e.g., int, double, string), which are typically
organized in a manner conducive to storage and manipulation.
• Methods that operate on the data items (to access, query and update them quickly
and easily).
• Are main component of the creation of quick and powerful algorithms, and make
codes more readable and understandable.
• is an implementation of an abstract data type (ADT).
Abstract Data Types
A collection of data items given a name, purpose, and a set of functions (INTERFACE) that
operate on the data items
• only the interface is exposed externally, and data organization is hidden
• language independent
Procedural Oriented Programing (For C)
• All data items and structures are public.
• A procedure uses functions to process the data.
• They may be accidentally aSected.
• Big problem when many programmers work on the same project.

Object Oriented Programming (C++)


• Allows a data structure’s members to be private.
• Methods (functions) that operate on these data items are included within the data
structure.
Encapsulation: Users of the data structure use the public methods to achieve their goals,
without accidentally aSecting the private data items in any bad ways.

Dynamic Memory Allocation Selection Sort


Built-in functions for dynamic memory allocation at runtime in C (execution time):
1. malloc()
2. calloc()
3. realloc()
4. free()
Malloc()
Dynamically allocate a single large contiguous memory, according to the size specified, in
the heap and on success return a pointer pointing to the first byte of allocated
memory, else returns NULL.
• Allocates memory requested by the user without knowing the type of data to be
stored inside the memory.
• The void pointer can be type casted to an appropriate type.
• Allocates 4 bytes of memory in the heap and the address of the first byte is
stored in the pointer ptr.
• Memory allocated by malloc is initialized with some garbage value.
Calloc(): Clear Allocation
Is built-in function that is used to dynamically allocate multiple blocks of
memory.
• Memory allocated is initialized to zero.
• Needs two arguments instead of just one argument.
• Use free to deallocate.

Malloc and Calloc both return NULL when su9icient memory is not available in Heap

Realloc(): Clear Allocation


Function is used to change the size of the memory block without losing the old data.
• on failure, realloc returns NULL.
• Used to resize the array; decrease when you don’t need that much memory.

Lecture Two: Review of Pointers & Objects


Pointers
• Pointer variables can be copied either by address or by value.
• Since a pointer itself is stored somewhere in the memory, it also has a memory
address.
Virtual Memory
Virtual Address Space: Set of addresses available to each process (a running program )
• Mapped to main memory by OS (e.g., mapped to RAM)Virtual addresses and their data
represent Virtual Memory (VM)
• Memory is called virtual as it is used to give an illusion of larger memory allocation
• Each process gets a virtual address space to hold its frequently used code and data
Each process gets its own user space. Within each user space, there are blocks for:
1. Instructions (CODE),
2. Global and static variables,
3. Locally-allocated variables (STACK), and Dynamically-allocated variables (HEAP).

Lecture Three: Memory Allocation


More on Virtual Memory Spaces
At the start of a program, the operating system (e.g., Windows, Mac) assigns the program a
memory pool (free space) to use, this memory pool is large, but not infinite.
• At the end of the program, this memory pool will be recollected/de-allocated by the
operating system for other programs to use.
• Address space can be defined by number of bits used to specify each address
Static(automatic) Memory Allocation
Variables defined without using the new operator. Has lifetime within a scope
• At the end of the scope, the variable is automatically destroyed, and memory is
automatically de-allocated for other things in the program to use.

Dynamic(manual) memory Allocation:


Variables defined using the new operator.
• Has lifetime with the entire program, or until user uses delete to manually deallocate
the memory space.
How Does a Memory Leak Occur?
A piece (or pieces) of memory that was previously allocated by a programmer (using new) is
not properly de-allocated (using delete).
• memory is no longer in use by the program, it is still “reserved.”
• Without delete, no new operator can allocate the same address allocated by a previous
new.
• If a program has a lot of memory that hasn’t been de-allocated, it could slow down the
performance.

Constructors & Deconstructions


New operator à calls a constructor for new object
delete operator à calls the deconstructor to de-allocate the object

Constructor
A special kind of class member function, automatically called when an object of the class
is instantiated, thus must be public.
• If no constructor is defined by the user, the system will create a default constructor.
DiSerence from other regular member functions:
1. They must have the same name as the class.
2. They cannot return a value; not even void

Advantages:
1. Initialize some or all member variables of an object.
2. Simply the procedure.
3. Add code to validate the entered initial values.
4. Other actions, e.g., print to system log when an object is instantiated

Constructor Overloading: We can have more than one constructor in a class


with same name, as long as each has a diSerent list of arguments.

Deconstructor
Automatically called when an object of the class is deallocated.
• If the memory is statically allocated, destructor is called when the object’s scope is
closed/ended.
• If the memory is dynamically allocated, destructor is called when explicitly delete.
• Destructor must be named the same as the class, just with a ~ (tilde) sign preceding its
name.
Lecture 4 & 5 : Linked Lists & Sequential List
Abstract Data Types
• With ADT only necessary information is shown externally to the user while the
underlying details are hidden.
Big O notation
• describes the complexity of your code using algebraic terms.
• The running time/number of operations that’s independent of hardware, programming
language, etc
List ADT
A container structure for storing a connected sequence of data items

Linked Lists vs Sequential Lists (w Arrays)


A linked list consists of nodes that are connected by pointers, while a sequential list
consists of elements that are stored in a linear sequence.

• A linked list allows for fast insertions and deletions of nodes anywhere in the list,
while a sequential list can only cheaply add or remove elements at the end of the
list.
• A linked list requires more memory for storing pointers, while a sequential list uses
less memory as no pointers are needed.
• A linked list can have variable length and structure, while a sequential list has a
fixed size and structure.
• A linked list can be used for implementing more complex data structures like trees
and graphs, while a sequential list can be used for simple data structures like stacks
and queues.
• A linked list does not support random access to nodes, while a sequential list
supports random access to elements.
Lecture 7: Introduction to Recursion
Recursion in coding: a function in your code calls itself as part of its own execution.
Is recursion more eSicient than iteration? àNO
Base case: The problem reaches its ending point when there is a trivial answer to the
problem.

Ex. Factorial Function

Stack Frame:
Is a section of the stack dedicated to a particular function call.
• The stack is a region of memory that supports push and pop operations and is used to
store data in a Last-In-First-Out (LIFO) manner.
When a function is called:
• The stack allocates memory to store the function’s variables
• Those variables are stored in frame→ this is called a stack frame
Stack Overflow:
Excessively deep or infinite recursion, in which a function calls itself so many times that the
space needed to store the variables and information associated with each call is more than
can fit on the stack.

Call Tree and Call Traces:


Allow visualization of function calls and recursion, where each call spawns smaller
representations of itself
• This is represented as tree branches that are branching out from the tree root
• To trace the calls, we start oS from the top of node (root) of call tree and go all the
way
• down to the bottom of tree, which
signifies the end of recursive calls.
• Values at base case are defined, which
are used to find the value of upper level
of tree.
• Then the process goes upward.

Lecture 8: More on Recursion


Designing The Fibonacci Algorithm Recursively (0 1 1 2…)

Lecture 9: Algorithm Analysis Big-O Notation


Running Time
How long it takes for a computer to complete a code or a program:
• All operations are aSected by many factors such as hardware, programming language,
memory layout (known as space complexity), etc.
• In the previous code example there are n^2 operations (2 nested loops) → O(n^2)
Big-O or time complexity:
We care about a general estimation of the running time as it scales with the input size
(known as the rate of growth):
this eliminates all external factors (hardware, programming
language, memory layout, etc)
Asymptotic Idea: bypassing constant numbers and lower order terms as the input size
grows to infinity -study the worse case
Analysing Time Complexities
Three ways to analyze the time complexity of an algorithm:
Worst-case: focuses on the worst possible number of operations for, the worst input size
• When n (input size) approaches infinity
• DiSerence between orders of complexity
Average-case: focuses on the average
number of operations for an
average input size n
Best-case: focuses on the best possible
number of operations for the
best possible input size

Asymptotic Analysis: Orders of Complexity


Ignore lower-order terms and constants to achieve the asymptotic analysis of an algorithm
runtime:
1. Focus on only the major things aSecting
performance rather than the minor things.
2. Worst-case, rather than best-case or average-
case
3. N approaching infinity, rather than small n.
DiSerence between orders of complexity Lecture
11: Stack ADT

4. , rather than
diSerences within the
same order
Lecture 10: Algorithm Analysis Big-O Notation Cont.

Iterative or non-recursive algorithms:


1. There is a loop
2. A block of code that is repeated
3. Larger size of code compared to recursive

The strategy is to count the number of operations that are performed in the worse-case as n
approaches infinity (asymptotic analysis):
1. In general no loop, means constants steps (not related to input size) →O(1)
2. In general one loop is repeated (an+ b) in the worse-case, where the constants (a,b) are
for the segments that are repeated in each iteration →O(n)
3. In general for k nested loops each loop is repeated (an+ b) in the worse-case→O(n^k)
4. In general if you have a combination of operations the highest order dominates (e.g.: O
(𝑛^2)+ O (log(𝑛))+ O (𝑛^1.6)+O(𝑛) →O(𝑛^2))
Lecture 11: Stack ADT
A list of data items where items are inserted and, removed based on the last-in first-out
(LIFO) principle.
• That is, you can only insert and remove an item from one end of the stack, which is
called the “top” of the stack.
• The item that you remove from the top of the stack will always be the last item that was
inserted into the stack
• Only the index of the top element is needed.

Stack ADT is characterized by 3 main members:


• data–e.g. int, char
• capacity–the maximum number of elements that can be stored
• size–the number of elements currently in the stack

Main Stack Operators:


Push à is the insert operation
Pop à is the delete operation
Peek à is look at the top item

Linked List vs Linked Stack

Push Operations:
Just like à Insert First (insert first element) in linked List
Input: data value to insert in the list
1. Create a new node to hold the value to insert
2. Check for representative cases; such as if the stack is full
3. If stack is not full
a. start at the top if its empty –insert the node as the top
b. if top is not empty –insert the new node at the top
c. increase the size of the stack
Pop Operations:
DeleteFirst (insert first element) and return the deleted node or value from the linked List
Input:N/A Output: data value or node
1. Check for representative cases, such as if the stack is empty and return some dummy
node or value
2. If stack not empty
a) Start at the top create a new node to hold the current top node
b) Make the next node the new top node
c) Delete and free the old node
d) Decrease the size of the stack
e) Return the value of the deleted node or the node itself

Peek Operation
Just like SelectFirst (select first element) and return the top node value from the linked List
Input: N/A | Output: top node data value
1. check for marginal cases –such as if the stack is empty or top is NULL and return some
dummy node or value.
2. If stack not empty just return the top node value

Sequential Stack Implementation


A Sequential Stack representation –e.g.a sequential list such as a dynamic array
I will here give you some hints and structure setups.
Which end should be used as top? size - 1
What is the time complexity of operations?
When within capacity:
1. Push O(1)
2. Pop O(1)
3. Peak O(1)
When we need to change the capacity:
Push O(n) ßWhen increasing the capacity
Pop O(n) ß When decreasing the capacity

Dynamic Sequential Stack Class Declaration in C++


Let’s do a dynamic sequential stack where we allow the capacity to change dynamically
• We will increase current_capacity when size is too large, and decrease
current_capacity when size is too small.
• We need a pointer to an array since we will need to use dynamic memory allocation
Push Operations
Input: data value to insert in the list
1. Check for representative cases (such as if the stack is full to increase its capacity)
a. Create a new pointer (dynamically) to array holding the new capacity
b. Iterate over the current array to copy all items from it to the new one–O(n)
c. Do not forget to make the new array the main one by simply pointing the main
array pointer (*items) to the new one
2. If stack not full

Lecture 12: Queue ADT


Queue: First-In/First-Out (FIFO) data structure
• Objects are inserted and removed at diSerent end.
Enqueue O(1) à the insert operation
Dequeue O(1) à the delete operation
Peek O(1) à is look at the top item

Linked Queue: representation is a linked list where each node in the linked list holds an
item and a pointer to the next node. It also has a pointer to the front node (head pinter) and
a pointer to the back node (tail pointer).

Refer to VS code for general syntax of Dequeue and Enqueue Operations, and Circular
Queue Array Operations.

Linked Queue vs Sequential Queue

You might also like