You are on page 1of 11

1

Divide and conquer

• Divide the problem into a number of sub-problems that are smaller instances of the
same problem.
• Conquer the sub-problems by solving the recursively.
• Combine the solutions to the sub-problems into the solution for original problem

Maximum Sub-array

• Find max sub-array of the sub-array A [low...high]


• Divide the sub-array into two sub-arrays of equal sizes – find the midpoint
• Any contiguous sub-array a [i...j] must lie exactly in one of the following places –
entirely from low to mid , or , entirely from mid + 1 to high or , crosses the midpoint.
• Find the max of these 3 arrays.

Merge Sort

• Find the middle point to divide the array into two halves:
• Call mergeSort for first half:
• Call mergeSort for second half:
• Merge the two halves sorted in step 2 and 3:

Quick Sort

• Compute a pivot. Divide the array into 2 sub-arrays such that left part contains all
elements less than pivot and right part contains greater than pivot.
• Sort the two sub-arrays recursively.
• Combine the already sorted sub-arrays.

Greedy Method

o Applies to optimization problems


o Makes each choice in a locally optimal manner

Dynamic Programming

o Applies to optimization problems


o When a set of choices is to be made to arrive at an optimal solution
o Effective when a given sub-problem may arise from more than one partial set of
choices
o Store the solution to each sub-problem in case it should reappear.
o Combines solutions to sub-problems.
2

o Applies when sub-problems overlap

Can every recursive be converted to iterative?

Yes both are same

What is better – recursive or iterative

✓ Loops may achieve a performance gain for your program. Recursion may achieve a
performance gain for your programmer.
✓ Recursion makes the algorithm briefer and easier to understand (therefore shareable
and reusable)

Difference b/w calloc malloc

calloc() zero-initializes the buffer, while malloc() leaves the memory uninitialized
Transitive closure

Check if there is a path from vertex u to v. The reach-ability matrix is called transitive closure
of a graph. Uses dfs

Bfs

o Crawlers in search engine


o Gps uses bfs to find neighboring locations

Dfs

o Go as far down one path until you hit node you've already visited; backtrack until
there is unexplored node.
o Produces a MST and all pair shortest path tree
o Detects cycle – find back edges
o Find path b/w 2 vertices
o Topological sort – scheduling jobs from given dependencies among jobs

Bfs vs dfs

Dfs - in simulations of games (and game-like situations in the real world). In a typical game
you can choose one of several possible actions. Each choice leads to further choices, each of
which leads to further choices, and so on into an ever-expanding tree-shaped graph of
possibilities.

Bfs -this is useful if you’re trying to find the shortest path from the starting vertex to a given
vertex. You start a BFS, and when you find the specified vertex, you know the path you’ve
3

traced so far is the shortest path to the node. If there were a shorter path, the BFS would
have found it already.

Breadth-first search can be used for finding the neighbor nodes in peer to peer networks like
Bit Torrent, GPS systems to find nearby locations, social networking sites to find people in the
specified distance and things like that.

o Given a family tree if one were looking for someone on the tree who’s still alive, then
it would be safe to assume that person would be on the bottom of the tree. This
means that a BFS would take a very long time to reach that last level. A DFS, however,
would find the goal faster. But, if one were looking for a family member who died a
very long time ago, then that person would be closer to the top of the tree. Then, a
BFS would usually be faster than a DFS. So, the advantages of either vary depending
on the data and what you’re looking for.
o One more example is Facebook; Suggestion on Friends of Friends. We need immediate
friends for suggestion where we can use BFS. May be finding the shortest path or
detecting the cycle (using recursion) we can use DFS.

What is spanning tree

Given a connected and undirected graph, a spanning tree of that graph is a sub graph that is a
tree and connects all the vertices together

What is sub graph?

A sub graph S of a graph G is a graph whose set of vertices and set of edges are all subsets of
G

What is MST

Minimum weight spanning tree for a weighted, connected and undirected graph is a spanning
tree with weight less than or equal to the weight of every other spanning tree.

Prims

o Greedy algorithm
o Starts with an empty spanning tree
o Maintains 2 sets of vertices – one contains vertices already included in MST; other
contains vertices not yet included.
o At each step, it considers all edges that connect two sets (also called a CUT) – picks the
min weight edge
o Then adds the other end point of edge to set containing mst
4

o Initially make a set mstSet – empty


o Keys assigned to all vertices are {0, inf,inf… v times}
o Pick key with min value – first vertex is picked
o Include it in mstSet
o Update key values of adjacent vertices.
o Again pick the key with min value and not already in mstSet.
o Repeat till mstSet contains all vertices.
o If the input graph is represented using adjacency list, then the time complexity of
Prim’s algorithm can be reduced to O(E log V) with the help of binary heap.
o With adj matrix t.c. is O(V^2)

Kruskal

1. Sort all the edges in non-decreasing order of their weight.


2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far.
If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.

Time complexity: Sorting of edges takes O (ELogE) time.


After sorting, we iterate through E edges and apply find-union algorithm. The find and
union operations can take at most O (LogV) time.
So overall complexity is O (ELogE + ELogV) time.
5

The value of E can be at most O (V2), so O (LogV) are O (LogE) same. Therefore, overall
time complexity is O (ElogE) or O (ElogV)

Inorder preorder postorder

inorder traversal gives nodes in non-decreasing order.

Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get
prefix expression on of an expression tree.

Postorder traversal is also useful to get the postfix expression of an expression tree. – also to
delete a tree -- because before deleting the parent node we should delete its children nodes
first

Single Source Shortest Paths

Given a weighted directed graph.

A weight function, maps edges to real valued weights

Shortest path weight d (u, v) from u to v is min {w (p)} if there is a path from u to v, else INF.

Given a source vertex s, find a shortest path from s to each vertex.

-Bfs is a shortest paths algorithm that works on un weighted graphs – with unit weight

- negative weight edges – if the graph does not contain any negative weight cycle reachable
from source, then the shortest path weight remains well defined.

- If it contains a negative weight cycle, shortest path weight is not defined. If there is a
negative weight cycle then d(s, v) = -INF.

-Should not contain positive weight cycle – removing that cycle produces a path with lower
weight.

- Only 0 weight cycles allowed


6

1. Initialize the shortest path estimates and parents

Takes O (V) time


2. Relaxation – tests whether a shortest path can be added.

Takes O (1) time

Algorithms differ how many times they relax each edge and in order which they relax.

A. Bellman Ford
- Allows negative edge weights
- Return bool indicating whether a negative weight cycle is reachable from source

Lines 5 to 8 check for negative weight cycle. – O (E)


Line 3 runs in O (E) time for each vertex. Total Time O (VE)
B. Dijkstra Algorithm
-all weights non negative
- maintains a set S of vertices whose final shortest path weights from s have been
found
- use a min priority queue Q of vertices
7

Always chooses the lightest vertex to add to S. – greedy


Runs in O (V2). Using min heap O (ElgV)

All pair shortest Path


Floyd Warshall

The optimum substructure with overlapping sub problems for shortest paths is that
for all k on an s-t shortest path, d(s, t) = d(s, k) + d (k, t). We will refine this: suppose
that the nodes of the graph are identified with the integers from 1 to n, then if k is the
maximum node on an s-t shortest path, then d(s, t) = d(s, k) + d (k, t) and moreover the
sub paths from s to k and from k to t only use nodes ≤ k − 1 internally.

Check the main-diagonal entries of the result matrix for a negative value. There is a
negative weight cycle if and only if d (n) ii < 0 for some vertex I:

Sorting
insertion selection bubble merge heap quick
Random Slower 3rd All same 2nd All same 2nd same
Nearly Fastest 1st Fastest 1st Slowest
sorted 4th
Reversed Slowest Slowest Fastest 1st
4th 4th
8

Few Faster 2nd 3rd 3rd Slowest


unique 4th

Insertion • Stable Insertion sort is the algorithm


• O(1) extra space of choice either when the
• O(n2) comparisons data is nearly sorted
and swaps (because it is adaptive) or
• Adaptive: O(n) time when the problem size is
when nearly sorted small (because it has low
• Very low overhead overhead).
Selection • Not stable Selection sort has the
• O(1) extra space property of minimizing the
• Θ(n2) comparisons number of swaps.
• Θ(n) swaps
• Not adaptive
Bubble • Stable In the case of nearly sorted
• O(1) extra space data, bubble sort takes O(n)
• O(n2) comparisons time, but requires at least 2
and swaps passes through the data
• Adaptive: O(n) when
nearly sorted
Merge • Stable It is simple to implement, and
• Θ(n) extra space for it is the only stable O (n·lg
arrays (as shown) (n)) sorting algorithm. Note
• Θ(lg(n)) extra space that when sorting linked lists,
for linked lists merge sort requires only Θ
• Θ(n·lg(n)) time (lg(n))
• Not adaptive
• Does not require
random access to data
Heap • Not stable Simple to implement,
• O(1) extra space performs an O (n·lg (n)) in-
• O(n·lg(n)) time place sort, but is not stable.
• Not really adaptive

Quick • Not stable Quick sort is robust and has


• O(lg(n)) extra space low overhead. When a stable
• O(n2) time, but sort is not needed, quick sort
typically O(n·lg(n)) is an excellent general-
time purpose sort
• Not adaptive
9

B tree – B+ tree
Btree B+tree
- Contain data with each key - No data associated with
- Leaf nodes are not linked – internal nodes
requires a traversal of every - More keys can fit on a page of
level in the tree memory
- Frequently accessed nodes can - Leaf nodes are linked –
lie closer to the root – quicker requires one linear pass to do
access a full scan
- Internal nodes contain a - A B+ tree can be viewed as
number of keys -- The keys act a B-tree in which each node
as separation values which contains only keys (not key–
divide its subtrees. For value pairs), and to which an
example, if an internal node additional level is added at the
has 3 child nodes (or subtrees) bottom with linked leaves.
then it must have 2
keys: a1 and a2. All values in the
leftmost sub tree will be less
than a1, all values in the middle
sub tree will be
between a1 and a2, and all
values in the rightmost sub tree
will be greater than a2.
10

Check for prime number

Find cube
Check for palindrome
Longest Common Subsequence

Longest Increasing Subsequence

Bottom up versus top down dynamic


When to use dynamic and when greedy
let's say that you have to get from point A to point B as fast as possible, in a given city,
during rush hour. A dynamic programming algorithm will look into the entire traffic
11

report, looking into all possible combinations of roads you might take, and will only
then tell you which way is the fastest. Of course, you might have to wait for a while
until the algorithm finishes, and only then can you start driving. The path you will take
will be the fastest one (assuming that nothing changed in the external environment).
Sorting algorithms
Bubble Sort

Insertion Sort
Heap Sort

You might also like