IN2010 Structured Notes
IN2010 Structured Notes
https://www.studocu.com/no/document/universitetet-i-oslo/algoritmer-og-
datastrukturer/summaries/in2010-oppsumering/7911011/view
Graphs
• A graph is a set of nodes V, and a collection of edges E.
• Undirected graph: degree (v) is the number of edges from the node v.
• Directed graph: degree (v) are edges from the node v, degree (v) are edges to the node
V.
• Two edges are parallel if they start and end at the same node
• A simple graph is a straight, unweighted graph without parallel edges and loops.
the edge connecting the node. If a graph has more edges than nodes, it has a cycle
misaligned edges.
connected.
components.
• dinconnected graphs:
• Connected graphs:
Huffman Coding
Prefix of huffman codes have to be different. For ex: 101 and 10100 won’t work because 101
and 101 are the same.
Spanning Trees
•T contains all the nodes in G, but not necessarily all the edges.
Tension trees are trees that cover all the nodes in a graph.
• If the graph consists of several components, we have a span tree for each
component,
• The minimum span for a weighted graph is the span where the sum of the
weights
until the edges are as small as possible. If the edges have unique weights,
there is a minimum
DFS Besøker node, går gjennom alle Alle typer -Finne ut om grafen er O(|V|
+|E|)
DFS Visits all Undirected -Find out if the O(|V| + |E|) If the tree is
neighbors Graph graph is very deep and
starting at a connected solutions are
node, if the Directed -Create a span rare, depth first
neighbor is Acyclic tree for the search (DFS)
unvisited Graphs(DAG) graph might take an
we call DFS -Find out if a extremely long
without weight
on the node can be time, but BFS
neighbor. reached from could be faster.
another (if there
is a path)
Finds spanning
trees if
unweighted
-Find out if the
graph contains
cycle
Trær
• Urettet graf: er et tre om den er sammenhengende og asyklisk.
• Rettet graf: Finn noden med inngrad 0 (hvis flere/ingen: grafen er ikke et tre).
Hvis du kan nå alle nodene fra denne noden, uten å møte på en node du har
besøkt før, er grafen et tre.
• Rotnode: eneste noden i treet uten forelder
• Subtre: Enhver node i et tre er rotnoden til sitt eget subtre
• Løvnode: En node som ikke har noen barn (peker til null)
• Dybde: Antall «steg» unna rotnoden (hvor mange kanter opp til rotnoden)
• Høyde: Maxdybden (dybden til roten med mest dybde). Høyden til treet er
høyden til rota.
Binære trær:
Binære trær er trær der hver node har maks to barn
AVL trær:
●
Binære søketrær:
• For enhver node i treet:
• Venstre barn har mindre verdi en noden selv
• Høyre barn har større enn eller lik verdi som noden selv.
• Høyde best case: log(n)
• Høyde worst case: n
• Traversering av Binære søketrær:
• DFS:
• Comes quickly down to the leaf nodes.
• Inorder (left, middle, right), preorder (middle, left, right), postorder (left,
right, middle)
• Approach:
• Start at the root
• If the node we are looking at == null, return
• Mark the node as visited
• Call the method recursive on the two child nodes
• BFS:
• Layered traversal
• Visits a node, and then all the children of the node visit before we go further down the
tree.
• Approach:
• Puts the root in a queue
• While the queue is not empty:
• Retrieves an element from the queue
• Visits the element
• Adds the children to the element in the queue (given that they are not
zero)
Insertion in binary search tree:
• Procedure:
• Start in the root
• Is the value of the new larger / smaller than the root we are in?
• Go right / go left
• New node should enter where there is a zero pointer.
• Time complexity:
-Best case: O (log n)
-Worst case: O (n)
Deletion of element in binary search tree
3 scenarios:
• The node to be deleted is a lion node: Set the parent's pointer that should have been to
this node to be zero
• The node to be deleted has 1 child: Set the parent's pointer that should have been to
this node to be the node's node
• The node to be deleted has 2 children: Go to the node's right subre and find the node
with the least value or go to the node's left subre and find the node with the largest value
• If this node is a leaf node:
• Replace the node to be deleted with this node
• If this node has one child:
• Replace this node with its own child
• Replace the node to be deleted with this node.
Time complexity for deleting nodes:
General:
• Worst case: O (n)
• Best case: O (logn)
Red-black trees:
Rules:
• The root must be black
• All zero nodes are black
• All nodes are red when they inserted
• It is not allowed to have two red nodes in a row
• There must be an equal number of black nodes from the root down to any zero node
Insertion in red-black tree:
• The node is inserted as in a normal binary search tree
• The node is highlighted in red
• If the parent is black, we're done
• If the parent is red:
Finds the aunt of the node:
• If the aunt is red:
• Marks grandfather as red and aunt and parent black
• If the aunt is black (remember zero point is also black):
• Must make one or more rotations
• In all cases, finish by marking the root as black.
Code examples:
Find a node with value v, and print path to the node.
void writePathToNode(Node n, int v, String pathSoFar) {
if ( n == null )
System.out.println("Verdien "+ v +" finnes ikke i treet.");
else if ( n.v == v )
System.out.println(pathSoFar + " " + v);
else if ( n.v > v )
writePathToNode(n.l, v, pathSoFar+"L");
else // if (n.v<v)
writePathToNode(n.r, v, pathSoFar+"R"); }
Heaps
• A heap is a tree represented by an array
• No node objects, no child pointers
• The location of the node in relation to each other in the array describes their relationship in the
tree
• Used mostly as priority queues
• Min-heap:
• The «root» / first index in the array is always the smallest
• Every node is smaller than its children
• Max-heap:
• the «root» / first index in the array is always largest
• Every node is at least as big as its children
• To make it easier to find parents / children, we let the first index be 1.
• To find the parent of the item on the index in:
• Parent = i / 2
• To find children of item on index in:
• Left child = i * 2
• Right child = (i * 2) +1
Item retrieval
Always pops from the start of the array
For max-heap:
• Save the item on the first index (X)
Move the last item in the array (Y) to the beginning of the array
• Find the eldest child
• While this child is larger than Y:
• Swap places on Y and the child
• Find the new eldest child
• Return X
• Time complexity: O (log)
Sorting Algorithms:
Algorithm How it works Best case Worst Use cases
Case
Insertion sort Like sorting O(n) O(n^2) Fastest for small
playing cards in arrays or if nearly
hand sorted or list is small
because a minimum
number of elements
will slide over to
insert the unsorted
element
Selection sort Repeatedly finds O(n^2) O(n^2) Good when list is
minimum element small
and puts it in
beginning in
increasing order.
Heap sort First rounds build O(n logn) O(n logn) When you need
max-heap. Second guaranteed
round swaps the consistent
root and last performance (in a
element (lowest) game)
and adds previous
root to sorted list.
Then, bubble
down swapping
with the max value
of children to get
to the correct spot
(vice versa for min
heap).
Quicksort Selects the pivot O(n logn) O(n^2) Best for all around
and partitions the average inputs.
Quicksort's in-place
list around it. Calls algorithm requires the
the algorithms movement (swapping) of
recursive on the data. Best for large
partitions. dataset, has average of
O(nlogn), but only
sometimes O(n^2). Not
best if you absolutely
need consistency
(heapsort is best there).
Merge sort Two sorted lists O(n logn) O(n logn) Mergesort is quicker
merge into one when dealing with linked
lists. This is because
sorted list pointers can easily be
changed when merging
lists. It only requires one
pass (O(n)) through the
list.
Hashing
Hashtabeller
• Brukes i java HashMap og python dicts.
• Både get(key) og put(key, value) er O(1) (som regel)
• I ArrayList og LinkedList er contains() O(n).
• Med hashtabeller kan vi få tak i verdier basert på nøkkel
Hashing
• Hash tables are implemented with arrays.
• The key determines where the object is to be placed. Uses hash function for this.
• Given an object with a key k, the hash function will calculate an index based on
k, and place the object there. put (key, value)
• If we want to retrieve an object, the hash function is used to find the index, and then
the object is returned. get (key).
Open hashing / separate chaining:
• On each index is a list
• When the hash function assigns an object to the object, it is added to the list
Closed hashing / open addressing:
• Tests alternative indices until we find a free index.
• H (x) is given by (hash (x) + f (i)) against tableSize
so that f (0) = 0.
• Has a function f that gives us a number to add, so that we can try another
index.
• There are three strategies for choosing f:
• Linear probing:
Selects f to be a linear function of i, typically f (i) = i.
• Quadratic probing:
Selects f to be a quadratic function of i, most often f (i) = i2.
• Double hashig / double hashing:
Uses two hash functions.
(key) = (hash1 (key) + i * hash2 (key) mod tableSize.
(hash1 is usually x mod tableSize)
hash2 is typically R - (x mod R), where R is a prime number smaller than tableSize.
Secondary accumulation:
With closed hashing, you can go in a loop and never find free space. Has a load factor then
close to 0.5 for closed hashing. The load factor is how full our hash table can be before we have
to re-hashe.
Re-hash
If the table becomes too full, operations begin to take a long time.
Can re-hash, i.e. create a new table that is approx. Twice as big, and use the hash function to
to calculate new indexes (the hash function uses tableSize), and move over all
the elements.
O (n), but must be done relatively infrequently.
Complexity (NP and P)