Sie sind auf Seite 1von 6

Search Problems and algorithms:

Exact-match query search for records whose key value matches a


specified key value
Range query key values fall within a specified range of key values
Search on unsorted arrays:
o Sequential search, check each element until found or exhausted
Search on sorted arrays:
o Jump search: jumps over some items in list until it finds
something bigger than goes back and checks the skipped over
space
o Binary search: traverse a tree eliminating some parts as you go
(know nothing about the distribution of key values)
o Dictionary search: Uses some knowledge about the distribution
of keys to go through the list to the most important part
Hash tables:
o Advantages: easy to make fast, there are no key collisions
o Disadvantages: uses a lot of memory if there are a lot of keys,
keys must be integers ( direct access table)
o Common uses: searchable dictionaries, text search,
spellchecking, web searching, databases, internet routers, file
merge and sync
Hash functions
o Perfect hash functions have equal probability and no collisions
(ie. h(k) = 1 is legal but not useful)
o 12 mod 7 = 5
o Recall probabilities adding up until open slots
o Collision resolution : linked list (open hashing) or move to next
empty slot (closed hashing, linear probing), retrieve, delete, or
retrieve after delete
o Hashtable and Hashmap implement java.util.Map interface
o Hashtable: thread safe, do not permit nulls
Hashtable.put(one, 1), hashtable.get(key),
hashtable.containsValue(), hashtable.containsKey(),
hashtable.isEmpty(), hashtable.keys(), hashtable.values(),
o Hashmap: permits nulls, not thread safe
o unsorted
Trees:

o Tree traversal (inorder, preorder, postorder).

Inorder: left, root, right, BDAECF

Preorder: root, left, right, ABDCEF

Postorder: left, right, root, DBEFCA

o BST: search, insertion, deletion, and time complexity of these


operations; applications using BST; is BST balanced or not.

Left is less than parent, right is greater than parent

private E findNode(BSTNode node, Key k)


{
if(node == null) return null;
if(node.key().compareTo(k) > 0) return
findNode(node.left(), k);
else if(node.key().compareTo(k) == 0)
return node.element();
else return findNode(node.right(), k);
}

Compare to returns 0 if equal, 1 if greater than, and -1 if


less than

Balanced: A binary tree is balanced if for each node it holds


that the number of inner nodes in the left subtree and the
number of inner nodes in the right subtree differ by at most 1.
A binary tree is balanced if for any two leaves the difference
of the depth is at most 1.

AVL trees: Balanced BST trees,

o
o

Rotate to rebalance

Non- binary trees:


2-3 trees: node contains one or two keys, two or three
children, all leaves are same level, always height
balanced
B-trees : (insertion, deletion, key range searches) root is a
leaf or has 2+ children, B tree of order m: each internal
node has b- tween m/2 and m children, all leaves are the
same level
B+-trees: stores records only at the leaf nodes COME
BACK
K-D trees: multidimensional keys, branching is dependent on the
discriminator (eg. x, or y),

to search
keep track of current discriminator, during range search you
may need to split and go down both branches, PR- quad tree has
exactly four children or is a leaf,
Tries: rooted, branches are labeled with letters, child nodes may
be null or have content, suffix trees
Suffix tree is a compressed trie with suffixes as keys and
positions in text as values, extremely efficient for finding
all distinct subsequences,

GRAPHS
A tree is an acyclic connected graph, cardinality of graph
is the number of vertices |G| = 6,
A path is simple if all vertices are distinct, a cycle is a
path with the same vertices starting and ending,
connected or not connected, directed or undirected,
weighted or unweighted paths, undirected, directed, or
directed acyclic graph (DAG)

Isomorphic graphs are two graphs which contain the same


number of vertices connected in the same way.

Applications of graphs: internet packet routing, people


you may know, solving games/puzzles, theorem proof,
garbage collection in programming
Graph traversal: visit the vertices of a graph in some
specific order based on the graphs topology. Begin with a
start vertex and visit remaining. May not be possible to
reach all, infinite loops need to be considered
DFS: recursive, solve maze = leave bread crumbs, at each
node keep track of visited edges and vertices, backtrack if
necessary, do not repeat visiting vertices
BFS: start from a node, visit all nodes that can be reached
by one edge, visit all the nodes that can be reached in
one edge from those, continue until youve visited all
reachable nodes
Topological Sort to lay all vertices of DAG in a linear
order
Recursive:

Queue-based:

Shortest Path:
Source-target: given a weighted digraph, find the
shortest directed path from s to t (cost of path is
minimized), weights are arbitrary, worst case: go
through every vertex. Distance is inf if not
connected
Single source: from source to every other vertex
find all shortest paths. Unweighted graphs can use
BFS.
o Dijkstras (greedy)
All pairs shortest path:

Floyds (dynamic)

MST:
Prims: start with any source, add edge with
smallest weight, greedy, add edges that connect
vertices not yet in tree
Kruskals: sort edges in ascending order, add next
edge unless it creates a cycle. Greedily cluster objects
in increasing order of distance; stop when there are k
clusters
Algorithm Paradigms:
Designing algorithms: no recipe for designing
algorithms, know the problem, use existing
algorithms
Greedy: build up a solution incrementally, optimize
locally. Doesnt always find optimal solution
o Greedy choice property: global optimal is
reached from local greedy
o Optimal solution contains optimal solutions
to sub problems, optimal can be efficiently
reached
o Huffman tree, cashiers, activity selection,
knapsack,
Divide and Conquer: non-overlapping sub problems,
combine
o Quicksort, recursion kind of for computing
fibonacci
Dynamic Programming: series of overlapping subproblems, build up solution to larger problem
o Calculating nth Fibonacci, knapsack, edit
distance, shortest path

Das könnte Ihnen auch gefallen