Sie sind auf Seite 1von 2

Position- stores the place where something is.

HashTable ArrayList
ADT: size(), isEmpty(), first(), last(), before(p), after(p), ADT: same as map Cells in an array are associated with particular pieces of
set(p, e), remove(p), addBefore(p, e), addAfter(p, e), Use an array with index determined by hashfunction to allocated memory.
addFirst€, addLast€ store key value pairs get(i)-O(1). Return data[i], deals with exceptional
Singley Linked List, Doubley Linked List. Operations Expected run time is O(1) but worst case is O(n) classes(less than or greater than size).
O(1). Separate Chaining: each cell in table points to a linked set(i,e)-O(1). Return the replaced value, update the
Stack – like an actual stack of things list of entries that map there. Alternative is open value in memory with index i so it now points to e.
ADT: push(e), pop(), top() , size(), isEmpty() addressing-building Map based on hashing add(i,e)-O(n). Insert element e at index i, existing
Last-in, First-out Linear Probing: going through array until space. When elements with index >=i are shifted up.
Queues – like a real life queue remove leave defunct remove(i)-O(n). Return the removed item. Iterate over
ADT: enqueue(e), dequeue(), front(), size(), isEmpty() Quadratic Probing: go through series (key+j^2 modN) indices >=i, shifting values down.
Deque - a stack and a queue combined for j=1,2,3, .... N must be prime Singley Linked list
ADT: addFirst(e), addLast(e), removeFirst(), Double Hashing: Use a second hashing function d(k) in Each Node in a Singley Linked List stores its element and
removeLast(), getFirst(), getLast(), size(), isEmpty(), series: k+j*d(k) modN a link to the next node.
offerFirst(e), inserts e at front unless full. Returns T/F Selection Sort – Priority Queue with an unsorted list. addFirst(e)-O(1). New node, 'n', set e as element of n,
based on success, offerLast(e), pollFirst(), removes and O(n^2) set n.next to old head, update head to point to n.
returns the first element, pollLast(), peekFirst(), Graphs are a pair(collection of Vertices, collection of removeFirst()-O(1). Update list's head to point to the
peekLast() Edges) next node. Make the former first one null.
Tree – nodes with a parent-child relationship ADT: numVertices(), vertices(), numEdges(), edges(), addLast(e)-O(n). New node, 'n', set e as element of n,
ADT: int size(), boolean isEmpty(), iterator iterator(), getEdge(u, v), endVertices(e), opposite(v,e), set n.next to null, set old tail to point to n.
iterable positions(), position root(), position parent(p), outDegree(v), inDegree(v), outgoingEdges(v), removeLast()-O(n). Update list's tail to point to the
iterable children(p), integer numChildren(p), boolean incomingEdges(v), insertVertex(x), insertEdge(u, v, x), node before, make former tail null.
isInternal(p), boolean isExternal(p), boolean isRoot(p) removeVertex(v), removeEdge(e) Doubley Linked list
Definition: each node v of T different from the root has Incident edges are the ones connected to the vertex Each Node in a Doubley Linked list stores its element
a unique parents, there is a path from the root to any Simple graph No parallel edges or self loops and a link to the previous and next nodes.
other node Cycle circular sequence of alternating vertices and addAfter(p,e)-O(1).Return new node, 'n',
Can be ordered (linear ordering of children) edges n.setElement(e), n.setPrev(p),n.setNext(p.getNext()),
Preorder Traversal: visit n, for each child n, n = number of vertices, m = number of edges p.getNext().setPrev(n), p.setNext(n).
preOrder(child) Subgraph the vertices and edges of S are a subset of the remove(p)-O(1)-Return value removed,
Postorder Traversal: for each child of n: vertices and edges of G. Spanning subgraph vertices S = p.getPrev().setNext(p.getNext()),
postOrder(child), visit(n) vertices G p.getNext().setPrev(p.getPrev()), p.setPrev(null),
Binary Tree – ordered tree with nodes having at most Undirected simple graph: m <= n(n-1)/2 p.setNext(null)
two children which are labelled left and right Can be implemented through an unordered list holding O(1) - size, isEmpty, first, last, before, after, set,
ADT: same as tree but with position left(p), position incident edges for each vertex OR through an adjacency remove, addBefore, addAfter
right (p), position sibling(p). map which uses a hash-based map for storing incident Iterators
Proper: each node has exactly 0 or 2 children edges for each vertex, OR through an edge list which Abstracts the process of stepping through a collection
Complete: each level is completely filled but the last holds its origin and destination vertex objects OR of elements one at a time.
one which is filled left to right through a matrix which stores the edge in the correct hasNext()- returns boolean, checkes whether the
Balanced: if the height of any right subtree differs from cell between vertices. collection is non-empty and the cursor is not after the
the left subtree by no more than 1 Subgraph vertices and edges of S are a subset of the last element
inorder Traversal: inOrder(left child), visit(n), ones in G next()- returns the element after the cursor(cursor
inOrder(right child) Spanning supgraph: vertices S = vertices G advances)
Can be implemented with a linked structure (like a tree) DFS: Check the children before siblings, run in O(m+n) For-Each- convenient syntax for using the iterator
OR an array. If array leftchild = 2i + 1, rightchild = 2i+2, time, use stack. associated with an Iterable class
parent =(i+1)/2 floored. BFS: check siblings before children, runs in O(m+n) time, Inorder
Priority Queues – stores a collection of prioritised use queue.
entries (key-value pairs)
ADT: insert(k, v), removeMin(), min(), size(), isEmpty()
Comparator encapsulates the action of comparing two
objects. External to the objects being compared (unlike
Comparable).
Implemented with a sorted (insert O(n) but removeMin
O(1)) or unsorted (insert O(1) removeMin O(n)) list
Preorder
Heap – implements priority queue where each child is Directed DFS we
smaller than its parent have discovery,
ADT: same as priority queue back, forward and
Has height O(log(n)) cross edges
To insert, add element at the bottom and restore the
heap property by swapping nodes upwards. To remove,
switch the node being removed with the last node, and Strongly connected graph: each vertex has a directed
then downheap from the new position of the last node. path to every other vertex
Use O(n) space, everything O(1) except insert and Transitive Closure: Floyd-Warshall algorithm, digraph of
removeMin, which are O(log(n)) G where if there is a directed path between u,v there is
Map – searchable collection of unique key-value pairs a directed edge (FW: number the vertices, construct
ADT: get(k), put(k, v), remove(k), size(), isEmpty(), diagram incrementally @ round k+1 only consider paths
iterable entrySet(), keyset(), values() that use only vertices numbered 1, 2, …k as
List Based: use unsorted list. Takes space O(n) and put, intermediate vertices). O(n^3)
Postorder
get and remove take O(n). Directed Acyclic Graph (DAG) has no directed cycles
Ordered Map ADT: everything above plus, firstEntry(k), Topological Ordering; numbering vertices so every edge
lastEntry(k), ceilingEntry(k), floorEntry(k), lowerEntry(k), (u,v) u<v. (must be a DAG)
higherEntry(k) Weighted Graph Dijkstra’s algorithm does shortest
Implemented using a sorted array – space O(n), insert distance in O(mlog(n)). Grow cloud of vertices each
and delete are O(n), but search is O(log(n)) storing the shortest distance to it from v. Each step, add
Binary Search Tree the vertex with the smallest distance to the cloud
Searches in O(log(n)) worst case could be O(n) if
everything in row
Deletion: if one child, swap with that child (repeatedly
until no children). If two children, swap with the inorder
predecessor and then do the first step
1. Edge List structure-Simple, but inefficient-Unordered Sorting Amortised Analysis
list V,E (Vertex and Edge object) Insertion Sort – Priority Queue with a sorted list. Aggregate Analysis: O(n)/n = O(1) find the worst case
O(m)-incidentEdges(v), areAdjacent(v,w), O(n^2). Space is O(n). We move elements into the pq, time T(n) for any sequence of n operations. Amortized
removeVertex(v), m edges and then move them from the pq to the output cost of an operation is T(n)/n.
O(1)-insertVertex(o),insertEdge(v,w,o), removeEdge(e) Accounting Method: overcharge certain operations to
pay for future ones. Total amount charged must be
enough to pay for the entire sequence of operations
Potential Method: Start with initial data structures D0,
Potential function: Φ(Di ), Real cost: ci and ai = ci +
Φ(Di) – Φ(Di-1) for i=1, 2, …n.
Average Case Costs
Expected value – Weighted average of random variable
Heap Sort – sorting with a heap. O(nlog(n))
among the various cases – E(X) = ∑ i X(case i)*P(case i)
Bubble Sort – Scan the sequence n-1 times – In each
Binomial distribution (discrete cases)- P(X=k) = (k!(n-
step of a scan, compare the current element with the
k)!/n!) pk (1-p)n-k – Expected value E(X)= np
next and swap them if they are out of order. O(n^2)
Red-Black Trees – Balanced binary tree
Properties: Root is black, the children of a red node are
2. Adjacency List structure-Unordered list with black, all leaves have the same black depth
additional list structure To insert/remove, do as normal BST with new node Red
Incidence sequence l(v) for each vertex v- sequence of and then if violation, push upwards and rotations until
references to edge objects of incident edges solved. Runs in O(log(n)) search insert delete.
Augmented edge objects- references to associated AVL tree – a BST
positions in incidence sequence of end vertices
Merge Sort – divide and conquer. O(nlog(n)). Partition S Each node stores height of subtree
All methods O(1) with Edge list structure are still O(1).
into half, and recursively sort (base: size == 1), and then Constraint: left and right children of x have heights that
O(deg(v))-incidentEdges(v), removeVertex(v), differ at most 1
merge the sorted lists together
O(min(deg(v),deg(w))-areAdjacent(v,w) The height of an AVL tree with n nodes is O(log n)
Binomial Queue – heap but faster merging or melding
Merge: compare roots of the binomial trees
Only has at most 1 tree for each k. Of order k has 2^k
nodes and height k. O(log(n) for everything
Treap - A BST and a heap
Heap order maintained with priorities, BST with the
keys
When added/constructing, heapify and then restore the
Divide BST properties (downwards)
Search, delete and insert average O(log(n)), worst O(n)
3. Adjacency Map structure-Same as adjacency list, but Splay Tree – Move elements up
uses a hash-based map for storing incident edges When node is accessed, it is splayed to the top of the
Incidence map for each vertex v- Key=opposite tree (inserted or searched for). When deleting, splay up
endpoint value=edge its parent.
getEdge(u,v) now is in expect O(1)-although still worst To insert, do normal BST and splay up
case O(min(deg(u),deg(v))) Skip List
Insertion - to insert an entry(x,o) into a skip list, we use
Conquer a randomized algorithm. Get a random number that is
Quick Sort – divide and conquer with pivot. Worst case less than the height, search for x in the skip list by going
is O(n^2) when worst pivot every time, BUT as good right, then down if too small.
pivot picked 50^ of the time, expected run time is Deletion – To remove an entry with key x from a skip
O(nlog(n)). Pick pivot and sort into groups less than, list, search for the skip list, going right then down if not
equal and greater than, recursively repeat on these found. Remove it if found.
groups. Join them together Space usage is O(n)
Search, insertion and deletion takes O(log(n))
Sorting Lower Bound
Any comparison-based sorting algorithm must run in
Summary of sorting algorithms Sort algorithm Time cost Ω(nlog(n)).
Comments Selection-sort O(n^2) can be done in-place Selection
Insertion-sort O(n^2) can be done in-place Heap-sort Quick-Select is a randomized selection algorithm based
4. Adjacency Matrix Structure- Augmented vertex
O(n log n) can be done in-place Bubble-sort O(n^2) can on the prune-and-search paradigm.
objects- integer key(index) associated with vertex
be done in-place sequential access, so works well with Prune: pick a random element x (called pivot) and
2D-array adjacency array A- reference to edge object
data on disk Merge-sort O(n log n) can be done in-place partition S into less than, greater than, equal to x.
for adjacent vertice, null for nonadjacent vertices sequential access, so works well with data on disk Search: depending on k, either answer is in E, or we
Dense graphs have O(n^2) edges
Quick-sort worst: O(n^2) Average/expected: O(n log n) need to recur in either L or G.
can be done in-place requires randomization Partition takes O(n) time

n vertices, m edges, no parallel edges/self-loops

Das könnte Ihnen auch gefallen