Docsity

Data Structures and Algorithms: A Comprehensive Guide with Examples, Exams of Advanced Education

Toggle sidebar
bg1
CPSC 350 Final Exam Graded A+
Array benefits - ANSWER-It is better and convenient way of storing the data of same
datatype with same size. It allows us to store known number of elements in it.
Array Big-O runtimes (access, insert, search, delete) - ANSWER-access = O(1)
insert = O(n)
search = O(n)
delete = o(n)
Array disadvantages - ANSWER-- is a static Data structure
-We must know in advance that how many elements are to be stored in array. Only
elements of same data types can be stored in an array. We cannot store elements of
multiple data types in a single array.
Arrays - ANSWER-a collection of items stored in a contiguous memory location and
addressed using one or more indices
AVL trees - ANSWER-- Adelson-Velskii & Landis: Any pair of sibling nodes have a
height difference of at most
- not commonly used in industry
Balanced/Unbalanced BST's - ANSWER-- We will say that a tree is height-balanced if
the heights of the left and right subtrees of each node are within 1
- A tree is perfectly height-balanced if the left and right subtrees of any node are the
same height
Big-O for Linked List (access, insert, search, delete) - ANSWER-access = O(n)
insert = O(1)
search = O(n)
delete = o(1)
Binary Search Tree BST - ANSWER-- Each internal node has at most two children
- Values smaller than the root, left subtree
- Values greater than the root, right subtree
- Each node (vertex) must have exactly one path (edge) to another node
If not, then it's a graph
- The number of children of a given node is called a degree
- Is allows for efficient sorting and searching of data.
- log(N)
Breadth-first search - ANSWER-- We examine all nodes at the same level of the tree
before moving on to the next level
- Finding shortest paths
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Data Structures and Algorithms: A Comprehensive Guide with Examples and more Exams Advanced Education in PDF only on Docsity!

CPSC 350 Final Exam Graded A+

Array benefits - ANSWER-It is better and convenient way of storing the data of same datatype with same size. It allows us to store known number of elements in it. Array Big-O runtimes (access, insert, search, delete) - ANSWER-access = O(1) insert = O(n) search = O(n) delete = o(n) Array disadvantages - ANSWER-- is a static Data structure -We must know in advance that how many elements are to be stored in array. Only elements of same data types can be stored in an array. We cannot store elements of multiple data types in a single array. Arrays - ANSWER-a collection of items stored in a contiguous memory location and addressed using one or more indices AVL trees - ANSWER-- Adelson-Velskii & Landis: Any pair of sibling nodes have a height difference of at most

  • not commonly used in industry Balanced/Unbalanced BST's - ANSWER-- We will say that a tree is height-balanced if the heights of the left and right subtrees of each node are within 1
  • A tree is perfectly height-balanced if the left and right subtrees of any node are the same height Big-O for Linked List (access, insert, search, delete) - ANSWER-access = O(n) insert = O(1) search = O(n) delete = o(1) Binary Search Tree BST - ANSWER-- Each internal node has at most two children
  • Values smaller than the root, left subtree
  • Values greater than the root, right subtree
  • Each node (vertex) must have exactly one path (edge) to another node If not, then it's a graph
  • The number of children of a given node is called a degree
  • Is allows for efficient sorting and searching of data.
  • log(N) Breadth-first search - ANSWER-- We examine all nodes at the same level of the tree before moving on to the next level
  • Finding shortest paths
  • Used for disconverting spanning trees
  • Requires more memory than DFS
  • uses a Queue
  • divides graph into levels
  • better for broader graphs BST advantages - ANSWER-- BST has fast insertion and deletion when balanced
  • very efficient BST disadvantages - ANSWER-- shape of tree determines on insertion and can be degenerated
  • search takes a long time BubbleSort - ANSWER-- compares 2 numbers
  • if the one on the left is bigger, swap
  • performance, pretty lousy O(N^2)
  • best O(n)
  • use when you don't have access to internet and not a large data set circular Queues - ANSWER-The used memory locations, when free are reused so no memory is leaked ex: round Robin schedulers Depth-first search - ANSWER-- useful for finding whether there is a path from one vertex to another and whether or not the graph is connected
  • in terms of running time, DFS is an efficient method for transversing a graph
  • use stack
  • checks graph for cycles
  • traverses deep into graph by visiting children nodes before sibling/neighbor nodes
  • requires less memory difference between Graphs and trees - ANSWER-- trees are a type of graph
  • no loops in trees
  • numerous rules in spelling out connections in trees, graph has not rules dictating connections among nodes Dijkstra's algorithm - ANSWER-- finds the optimal path from a source vertex to and endpoint, Single source
  • graph can be a directed or undirected weight graph
  • graph needs to have a nonnegative weight on every edge
  • used in Google Maps Dijkstra's algorithm disadvantages - ANSWER-- It do blind search so wastes lot of time while processing.

Graph applications (Computer network) - ANSWER-- Local area network

  • Internet
  • Web Graph Applications (Databases) - ANSWER-- Entity-relationship diagram Graph applications (Electronic circuits) - ANSWER-- Printed circuit board
  • Integrated circuit Graph applications (Transportation networks) - ANSWER-- Highway network
  • Flight network Graphs - ANSWER-- A graph is a set of vertices (nodes) and a set of edges that connect pairs of distinct vertices. That is, at most one edge connects any pair of vertices.
  • Note that graphs and trees have a similar look and feel. This is because trees are a specific type of graph. Graphs Adjacency list - ANSWER-- For each vertex, keep a linked list of all the vertices adjacent to it
  • Often implemented using a list of lists
  • Makes better use of memory
  • Fast iteration over all edges, but slow lookups of specific edges Hash collision - ANSWER-- When the key is hashed into an already existing hash value, or bucket Hash Tables - ANSWER-- Is a data structure used to create an associative array, or dictionary
  • A hash table makes use (consists) of a hashing function to compute the index into an array, or buckets This index is then used to find a value Hashing function - ANSWER-- Transforms keys into table addresses, or buckets
  • Maps keys of a given type to integers in a fixed interval [0, N-1]
  • Need to proceed with caution to avoid common issues Hash collision inorder Transversal - ANSWER-- In an inorder transversal a node is visited after its left subtree and before its right subtree Insertion Sort - ANSWER-- If you're dealing with a data set that is partially sorted, insertion Sort will be more efficient than other sorting algorithms.
  • O(n^2)
  • best O(n)
  • copies are much less costly than swaps
  • For random data, it will run 2x as fast as bubble sort, and still faster than selection sort
  • For pre-sorted data, runs much better than others - O(n) because nothing happens in the while loop. Kruskal's Algorithm - ANSWER-- start with edge with least weight
  • greedy algorithm for weighted directed graphs
  • span from edge to edge
  • can function on both disconnected and connected graphs
  • time complexity is O(logV) linear probing - ANSWER-- Perform a sequential search to find a free bucket in the hash table
  • requires more computation
  • hash table may become full
  • used with frequency and number of keys is known
  • run times O(1) Linked List - ANSWER-A linear data structure, much like an array, that consists of nodes, where each node contains data as well as a link to the next node, but that does not use contiguous memory. Linked list benefits - ANSWER-- Linked List is Dynamic data Structure.
  • Linked List can grow and shrink during run time.
  • Insertion and Deletion Operations are Easier
  • Efficient Memory Utilization ,i.e no need to pre-allocate memory
  • Faster Access time,can be expanded in constant time without memory overhead
  • Linear Data Structures such as Stack,Queue can be easily implemeted using Linked list linked list disadvantages - ANSWER-- Pointer Requires extra memory for storage.
  • n Linked list no random access is given to user, we have to access each node sequentially.
  • Suppose we have to access nth node then we have to traverse linked list n times. Lower bound - ANSWER-f(n) >= C*g(n) for all n > n Mathematical analysis - ANSWER-- theoretical analysis
  • Determine factors that determine the run-time of an algorithm
  • Impossible to reverse engineer the input from the output of h(x)
  • One-way hash Quadratic Probing - ANSWER-- similar to linear probing, but avoid clustering problems
  • creates its own problems called secondary clustering
  • where the set of filled array cells "bounce" around around the array in a fixed pattern.
  • if N is not an even number than an open slot may never be found
  • even if N is prime if the array is at least half empty Queue Direct applications - ANSWER-- Critical to OS process management
  • Access to shared resources Priority queue
  • Waiting lists, bureaucracy Queue limitations - ANSWER-- similar to stack Queue operations - ANSWER-- insert()
  • remove()
  • front() each operation runs at O(1) Queues - ANSWER-- FIFO
  • like a line at the movies Queues indirect applications - ANSWER-- Auxiliary data structure for algorithms
  • Component of other data structures QuickSort - ANSWER-- divide and conquer
  • sorts in place using a (pivot)
  • does not require auxiliary array
  • requires more CPU
  • 3 steps
  • divide, pivot
  • recur
  • conquer
  • it is slow in worst case, which is when sorting should be easy, if the sequence is already sorted
  • O(nlog(n))
  • poor choice in real-time applications where we must make guarantees on the time needed to complete a sorting operation
  • Great choice if input sequence can fit entirely in main memory, as a general-purpose, in-memory sorting algorithm
  • worst O(n^2)

RBT advantages - ANSWER-- operations are guaranteed to be O(log(n))

  • when insertion and deletion are relatively frequent RBT disadvantages - ANSWER-- runtime is O(log(n)), sometimes hash-maps can be more efficient depending on the situations RBT violations - ANSWER-- insert and delete cause violations
  • fix them with
    • colorflips
    • rotations Red-Black Tree - ANSWER-- self-balancing
  • each node has a color, red or black
  • The root is always black
  • If a node is red, its children must be black
  • Every leaf null node is black
  • A node in a RBT requires a one bit to store color Selection Sort - ANSWER-- Repeatedly place the next smallest value in it's correct position
  • O(n^2)
  • alot less swaps than bubbleSort, however still slow
  • requires a little less CPU than bubbleSort separate chaining - ANSWER-- Each bucket is independent, each cell in the table points to a linked list of entries that map there.
  • Popular approach die to simple implementations
  • Requires additional memory outside the table
  • hash table never fills up
  • waste of storage because some parts of hash map are left empty
  • if chain becomes long then search time can become O(n) Stack indirect applications - ANSWER-- Auxiliary data structures for algorithms -Component of other data structures Stack limitations - ANSWER-size is static. trying to push causes an implementation- specific exceptions error Stack operations - ANSWER-- push()
  • pop() all operations run at O(1)