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

IN2010 Structured Notes

Graphs are mathematical structures used to model pairwise relations between objects. There are several graph algorithms described in the document including depth-first search (DFS), breadth-first search (BFS), Dijkstra's algorithm, Bellman-Ford algorithm, topological sorting, Prim's algorithm, and Kruskal's algorithm. These algorithms are used for problems such as finding shortest paths, minimum spanning trees, cycle detection, and topological sorting of nodes. The runtime of common graph algorithms ranges from O(V+E) to O(V^2) where V is the number of vertices and E is the number of edges.

Uploaded by

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

IN2010 Structured Notes

Graphs are mathematical structures used to model pairwise relations between objects. There are several graph algorithms described in the document including depth-first search (DFS), breadth-first search (BFS), Dijkstra's algorithm, Bellman-Ford algorithm, topological sorting, Prim's algorithm, and Kruskal's algorithm. These algorithms are used for problems such as finding shortest paths, minimum spanning trees, cycle detection, and topological sorting of nodes. The runtime of common graph algorithms ranges from O(V+E) to O(V^2) where V is the number of vertices and E is the number of edges.

Uploaded by

Bjorn Birkelund
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

IN2010 – Oppsummering

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.

• A loop is an edge that goes from a node to itself

• Two edges are parallel if they start and end at the same node

A weighted graph is a graph where the edges have different weights

• A simple graph is a straight, unweighted graph without parallel edges and loops.

For a simple graph, the maximum number of edges is (| V | (| V | -1)) / 2

A dense / dense graph has many edges in relation to nodes.

A thin / sparse graph has few edges in relation to nodes

• Path is a path where no node is repeated

• The length of a path is the number of edges in the sequence


A cycle is a path with at least three nodes that begins and ends in the same node, plus

the edge connecting the node. If a graph has more edges than nodes, it has a cycle

A directed graph is acyclic if it does not contain any cycle, loops or

misaligned edges.

An undirected graph is acyclic if it does not contain a cycle or loops.

• Directed graph: Continuous if there is a path between all pairs of nodes.

Directed Graph: Strongly contiguous if there is a (directed) path between all

pair of nodes. Slightly coherent if the underlying uncorrected graph is

connected.

• Separation node is a node in a contiguous graph, so that if the node (and

its edges) are removed, then the graph is no longer coherent.

• 2-consecutive graph is a graph without separation nodes.

• A graph can be divided into contiguous components. If the graph is

connected it consists of one component.

• A weakly connected graph can be divided into strongly coherent

components.

• dinconnected graphs:

• Simple graph (straight, without loops and parallel edges

• Complete graph (| E | = max number of edges)

• Trees (acyclic, misaligned graphs)

• Connected graphs:

• DAG (directed, acyclic graph)

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,

we call this a spanning forest.

• 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

tension wood for the graph.


Graph Algorithms:

Algortime Hva Brukes på Bruksområder Kjøretid

DFS Besøker node, går gjennom alle Alle typer -Finne ut om grafen er O(|V|
+|E|)

Algorithm What Used for Use cases Runtime Best when

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

BFS Takes in Undirected -Find the O (|V|+|E|) If you know a


start node Graph shortest path solution is not
s, and (fewest edges) far from the root
divides the Directed from one node of the tree, a
nodes in Acyclic to another breadth first
the graph Graphs(DAG) -Find out if the search (BFS)
into levels, graph is might be better.
without weight
calling bfs contiguous
on each -Create a span
node’s tree for the
neighbors. graph -Find out
if the graph
contains a cycle
Finds spanning
trees if
unweighted

Dijkstra Priority non-negative -Finds the Queue as heap: Can be used


weighted DAG shortest path O (| E | log | V |) with anything
Queue
from start node Queue as list: but negative
It picks the to all other O (| V | ^2) weights
unvisited nodes in
vertex with weighted graph. Usually:
-Only works on O(E+V log V)
the lowest
graphs with
distance, positive weight.
calculates
the
distance
through it
to each
unvisited
neighbor,
and
updates
the
neighbor's
distance if
smaller

Bellman- Sets the negative or - Finds the O (|V|*|E|) Can be used


Ford distance to positive shortest path with negative
all nodes to weighted DAG from start node weights unlike
infinity, to all other dijkstra
adjusts nodes in the
later. Goes graph
through all - Works in
the edges graphs with
of the graph negative/positive
| V | -1 weight, as long
times, and as there are no
performs negative cycles.
edge
relaxation.
If you can
still reduce
the distance
this | V |
time, we
have a
negative
cycle.
Because a
path of
length | V |
must
contain a
node 2
times

Topological Arrange the DAG(directed, -Used to plan O(V+E)


Sorting nodes so acyclic graph) projects with
that for dependabilities
each edge
(vi, vj) so i <
j. (Is based
on DFS)

Prim Selects any Undirected, -Find minimal O(|E|log|V|)


node, says connected, spanning trees Use Prim's
it is in T. weighted graphs algorithm when
Adds the you have a
cheapest graph with lots
edge that of edges.
connects a
node that is
not in T, to
join T.
Continues
until all
nodes are
in T.

Kruskal Looks at all Undirected, -Find minimal O(|E|log|V|) Kruskal


edges in connected, spanning trees performs better
the entire weighted graphs -works also for in typical
graph, and noncontinuous situations
always graphs, creates (sparse graphs)
chooses the a span tree for because it uses
shortest / eac h simpler data
cheapest component structures.
that does
not make a If you have
bike. access to the
Continue edges sorted by
until all weight, kruskal
edges are is fastest
set.
Borukva First step: Undirected, -Find minimal O(|E|log|V|)
Each node connected, spanning trees
in G is a weighted graph
tree. For all
trees in G:
choose the
edge with
the lowest
weight that
connects
the tree
with the
other tree.
Will
eventually
have a
large span
across the
entire
graph, or
one per
component.

Separation Uses DFS Directed graphs Finds


Nodes to make a separations
DFS- nodes
spanning
tree T for G.
Discovery
edges for
those who
are used to

Strong Uses DFS- Directed, weak Uses DFS to O (|V|+|E|)


Connected full. The connected find strong
Component indexing graphs connected
s takes place components.
when the
DFS call is
completed.
Then
reverses all
edges of
the graph
and calls
DFS again,
from largest
index to
smallest.
Every time
we have to
make a new
DFS call
from DFS-
full, we
know that
we have
visited all
the nodes
that were in
that
component
Seperasjonsnoder:
En node u er en seperasjonsnode hvis:
• u er rot i T og har mer enn ett barn
• u er ikke roten, og har et barn v, slik at ingen etterkommere av v (inkludert v selv)
har en back-edge til en forgjenger av u.
Low-nummeret til en node er definert som indeksen til den noden med lavest indeks
som kan nås ved å følge:
• 0 eller flere kanter i T (discovery edges), etterfulgt av
• 0 eller 1 back-edge
For alle noder u som ikke er rota:
U er en seperasjonsnode hvis det finnes en kant <u,v> i T slik at index (u) <= low (v).

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:

Self balancing tree


● Height cannot be more than 1 difference between left and right at any time.
○ This is done with the use of rotations
Red Black Trees:
● Self balancing tree


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

Insertion into heap


For max-heap:
• Insert the item on the first available index (at the back of the array)
• Find the parent
• While the item is larger than its parent:
• Swap places on the item and the parent
• Find a new parent
• Time complexity: O (log)

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.

Bucket sort Creates an array O(n) O(n) useful when input is


of lists. Must have O(n + k) k uniformly distributed
as many = number over a range, for
deficiency lists as buckets example grades from 1-
potential 6. Then you can put
occurrences of each score in one of 6
numbers. Throws buckets.
the items in the
correct list, then
picks out the
items.
Radix sort Assigns 0’s before O(n) O(n) Use this if the
numbers to make O(d(n+k) d longest integer is
them the same = number shorter than the
length as the of digits array size. It’s better
biggest number, the than quicksort if this
then does a numbers is the case.
bucket sort for consist of
each place in the
largest number.

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)

You might also like