Data Structures and Algorithms - Exam 1 Study Guide

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Abstract Data Types

An abstract data type (ADT) is a data type whose creation and update are constrained to specific well-defined operations. It's described by predefined user operations, such as "insert data at rear", without indication how each operation is implemented.

. operator v -> operator

->: member access operator Accessing a class's member functions by first dereferencing a pointer, as in (*myItemPtr1).PrintNums(), is so common that the language includes a second member access operator, in particular the -> operator that allows an alternative to (*a).b: a->b //Equivalent to (*a).b Thus the above program could have used: myItemPtr1->PrintNums();.

Data Structure

A data structure is a way of organizing, storing, and performing operations on data. Operations performed on a data structure include accessing or updating stored data, searching for specific data, inserting new data, and removing data. *see list of basic data structures

Pointers

A pointer is a variable that contains a memory address, rather than containing data like most variables introduced earlier. The following program introduces pointers via example:

Identify a simply memory leak.

A program that allocates memory but then loses the ability to access that memory, typically due to failure to properly destroy/free dynamically allocated memory, is said to have a memory leak.

Recursion

A recursive algorithm solves a problem by breaking that problem into smaller subproblems, solving these subproblems, and combining the solutions. Repeated applications of the same algorithm.

Abstraction v. Implementation

Abstraction: In many languages, abstraction usually involves some sort of interface for a programmer by using an API or library. When we write programs that use these interfaces, we often do not care about how the function is actually implemented, but are just interested about the input and output values. In a word, abstraction is about the overall idea. Implementation: This is where we actually care about how something is done "under the hood". We have to consider how our code's speed and efficiency is determined by the realities of the hardware (e.g. locality is something that needs to be taken into account). There are usually many ways to solve a problem, and implementation is all about which one is the best and how we do it. The implementation can contain calls to interfaces of another API intersecting abstraction and implementation. Figure 1 shows an example of this idea using threads as our abstraction, the pthread library as our interface, and the chain of events that occur when calling a function like pthread_create() as our implementation. Notice how this chain switches between interface calls and implementation details.

.hpp file

Header files should contain function declarations for functions defined in another file.

Code

The region where the program instructions are stored.

Heap Memory

The region where the "new" operator allocates memory, and where the "delete" operator deallocates memory. The region is also called free store. Controlled by the programmer via new & delete commands. New Command: new: allocating memory Sometimes memory should be allocated while a program is running and should persist independently of any particular function. The new operator allocates memory for the given type and returns a pointer (i.e., the address) to that allocated memory. pointerVariable = new type; Delete Command: deallocating memory The delete operator does the opposite of the new operator. The statement delete pointerVariable; deallocates a memory block pointed to by pointerVariable, which must have been previously allocated by new. If pointerVariable is null, delete has no effect. delete pointerVariable;

Break a class across two files

Typical two files per class: ClassName.h Contains the class definition, including data members and member function declarations. ClassName.cpp Contains member function definitions.

recurrence relation T(N)

defines a sequence by giving the nth value in terms of certain of its predecessors

Basic Compilation Commands

g++ main.cpp threenumsfcts.cpp

Compiling a program Modularly

As programs become larger and the number of source files increases, the time required to recompile and link all source files can become very long - often requiring minutes to hours. Instead of compiling an executable using a single step, a modular compilation approach can be used that separates the compiling and linking steps within the compilation process. In this approach, each source file is independently compiled into an object file. An object file contains machine instructions for the compiled code along with placeholders, often referred to as references, for calls to functions or accesses to variables or classes defined in other source files or libraries. For a program involving two files main.cpp and threenumsfcts.cpp, the files can be compiled separately into two object files named main.o and threenumsfcts.o respectively. The resulting object files will include several placeholders for functions that are defined in other files. For example, the main.o object file may contain placeholders for calls to any functions in threenumsfcts.o. After each source file has been compiled, the linker will create the final executable by linking together the object files and libraries. For each placeholder found within an object file, the linker searches the other object files and libraries to find the referenced function or variable. When linking the main.o and threenumsfcts.o files, the placeholder for a call to a function in main.o will be replaced with a jump to the first instruction of that function within the threenumsfcts.o object file. This creates a link between the code within the main.o and threenumsfcts.o object files. Once all placeholders have been linked together, the final executable can be created. The following animation illustrates.

Big(O)

Big O notation is a mathematical way of describing how a function (running time of an algorithm) generally behaves in relation to the input size. In Big O notation, all functions that have the same growth rate (as determined by the highest order term of the function) are characterized using the same Big O notation. In essence, all functions that have the same growth rate are considered equivalent in Big O notation. Given a function that describes the running time of an algorithm, the Big O notation for that function can be determined using the following rules: 1) If f(N) is a sum of several terms, the highest order term (the one with the fastest growth rate) is kept and others are discarded. 2) If f(N) has a term that is a product of several factors, all constants (those that are not in terms of N) are omitted.

.cpp file

Cpp files should contain function definitions for functions declared in another file.

stack frame

Each function call places a new stack frame on the stack, for local parameters, local variables, and more function items. Stack Overflow: Deep recursion could fill the stack region and cause a stack overflow, meaning a stack frame extends beyond the memory region allocated for stack.

Why guard an .hpp file?

Header file guards are preprocessor directives, which cause the compiler to only include the contents of the header file once. Guards: #ifndef FILENAME_H #define FILENAME_H //Header file contents #endif

Dynamic Allocation

Memory space is only allocated when required at runtime.

List of Basic Data Structures

Record: A record is the data structure that stores subitems, with a name associated with each subitem. Array: An array is a data structure that stores an ordered list of items, with each item is directly accessible by a positional index. Linked list: A linked list is a data structure that stores ordered list of items in nodes, where each node stores data and has a pointer to the next node. Binary tree: A binary tree is a data structure in which each node stores data and has up to two children, known as a left child and a right child. Hash table: A hash table is a data structure that stores unordered items by mapping (or hashing) each item to a location in an array. Heap: A max-heap is a tree that maintains the simple property that a node's key is greater than or equal to the node's childrens' keys. A min-heap is a tree that maintains the simple property that a node's key is less than or equal to the node's childrens' keys. Graph: A graph is a data structure for representing connections among items, and consists of vertices connected by edges. A vertex represents an item in a graph. An edge represents a connection between two vertices in a graph.

References

Reference operator: &variableName Dereference operator: *variableName

Big-Omega

T(n) = Ω(f(n)) if ∃ positive constants c & n° such that T(n) >= c * f(n) for all n >= n° Big Omega is for lower bound, Big O is for upper bound. ... Big Omega is used to represent the lower bound, which is also the "best case" for that algorithm.

Create a Makefile

Targets, Commands, Macros When the make command is executed, if any of the prerequisites for the rule have been modified since the target was last created, the commands for the rule will be executed to create the target file. For example, in the above makefile, if main.cpp is modified, then make will first execute the command g++ -Wall -c main.cpp to create the main.o object file. The -c flag is used here to inform the compiler (e.g., g++) that the source file should only be compiled (and not linked) to create an object file. As main.o has now been modified, make will then execute the command g++ main.o threeintsfcts.o -o myprog.exe. The -o flag is used here to inform the linker (e.g., g++) to link the object files into the final executable using the name specified after the -o. In this case, the executable is named myprog.exe. Make rules can also be used to define common operations used when managing larger programs. One common make rule is the clean : rule that is used to execute a command for deleting all generated files such as object files and the program executable. In the above example, this is accomplished by running the command rm *.o myprog.exe. By default make assumes the makefile is named makefile or Makefile. The -f flag can be used to run make using a different filename. For example, make -f MyMakefile will run make using the file named MyMakefile.

recursion base case

The case in which a recursive method does NOT include a recursive call. Every recursive method must have at least one base case. Also known as "stopping case". At some point, a recursive algorithm must describe how to actually do something, known as the base case.

Pass-by-Value

The parameter-passing mechanism by which a copy of the value of the actual parameter is passed to the called procedure. If the called procedure modifies the formal parameter, the corresponding actual parameter is not affected. In Java, all primitives are passed-by-value. See also the definition for "pass-by-reference".

pass by reference

The parameter-passing mechanism by which the address of a variable is passed to the subprogram called. If the subprogram modifies the formal parameter, the corresponding actual parameter is also changed. In Java, all objects, including arrays, are passed-by-reference. See also the definition for "pass-by-value". Ex. Code:

Stack Memory

The region where a function's local variables are allocated during a function call. A function call adds local variables to the stack, and a return removes them, like adding and removing dishes from a pile; hence the term "stack." Because this memory is automatically allocated and deallocated, it is also called automatic memory. Memory is reallocated once the function is complete.

Static Memory

The region where global variables (variables declared outside any function) as well as static local variables (variables declared inside functions starting with the keyword "static") are allocated. The name "static" comes from these variables not changing (static means not changing); they are allocated once and last for the duration of a program's execution, their addresses staying the same.


Kaugnay na mga set ng pag-aaral

Business Law Ch 6 Offer and Acceptance, Complete study guide

View Set

Week 1, Lecture 10 - Phagocytosis

View Set

Forensic and Correctional Nursing

View Set