Free Essay

Algorithms Notes

In:

Submitted By DMR0585
Words 5019
Pages 21
3PART ONE * Lecture 1 (01/09)
Posted on: Monday, January 28, 2013
Topics: Stable Matching
Reading: class handout * Lecture 2 (01/14)
Posted on: Thursday, January 24, 2013
Topics: Graph Representation, BFS, DFS
Reading: CLRS (Sections 22.1, 22.2, 22.3), KT (3.2, 3.3)

Notes:
2 possible representations of a graph 1. Adjacency Matrix-used for dense graphs (V2 memory space) a. Aij=1 if edge exists between I and J but 0 if not 2. Adjacency List- Used for sparse graphs (V+E memory space or V+2E for undirected) b. Array adj of |V| lists, one for each vertex. c. Adj[u] contains all vertices adjacent (or reachable by one edge) to u
Breadth First Search(G,s) BFS.G; s/
1 for each vertex u in G.V –{s}
2 u.color = WHITE
3 u.disc =∞
4 u.parent= NIL
5 s.color = GRAY
6 s.disc = 0
7 s.parent= NIL
8 Q = ∅;
9 ENQUEUE(Q,s)
10 while Q ≠ ∅
11 u = DEQUEUE(Q)
12 for each v in G.Adj[u]
13 if v.color == WHITE
14 v.color = GRAY
15 v.disc = u.disc + 1
16 v.parent = u
17 ENQUEUE(Q,v)
18 u.color = BLACK

White means not discovered yet, grey mean discovered but not finished, black means finished.
Run time O(V+E)
BFS gives shortest path from s to every vertex
Lemma: x in Li and Y in Lj and edge (x,y) exists Then |i-j| less than or equal to 1

Depth First search
Properties: 1) v is a descendenant of u iff v if discovered when u is gray
2) Parenthesis theorem, u and v in V. either discovery and finish times are disjoint (u nor v are descendents of each other) or one is discovered after and finished before the other (u is a descendent of v or vice versa)

White path theorem In a depth-first forest, vertex v is a descendant of u if and only if at the u is discovered there is a path from u to v consisting entirely of white vertices.

Edge classification Tree edge: edges present in depth first tree Back edge: connects descendant to ancestor in tree (not shown) Forward edges : connects to a descendant not in tree Cross edges : all other edges

DFS(G) O(V+E)
1 for each vertex u in G.V
2 u.color = WHITE
3 u.parents = NIL
4 time = 0
5 for each vertex u in G.V
6 if u.color == WHITE
7 DFS-VISIT.G; u/
DFS-VISIT(G,u)
1 time = time C 1 // white vertex u has just been discovered
2 u.disc = time
3 u.color = GRAY
4 for each v in G.Adj[u] // explore edge (u,v)
5 if v.color == WHITE
6 v.parent = u
7 DFS-VISIT(G,v)
8 u.color = BLACK // blacken u; it is finished
9 time = time + 1
10 u.finished = time

if edge u,v is traversed, and v is white, tree edge and v is gray, back edge and v is black, cross edge or forward edge * Lecture 3 (01/16)
Posted on: Thursday, January 24, 2013
Topics: DFS, Homework 1 out
Reading: CLRS (22.3) * Lecture 4 (01/23)
Posted on: Thursday, January 24, 2013
Topics: Topological Sort, Testing Bipartiteness, Strongly Connected Components
Reading: CLRS (22.4, 22.5), KT (3.4)
Strongly Connected Components
1.Call DFS(G) to compute finishing times for u.f for each vertex u
2. compute Gt, which is the graph with all edges reversed.
3. Call DFS(Gt) but in main loop process vertices in order of decreasing finishing time as computed in part 1
4. output the vertices of each tree in the depth first forest formed in step 3 as a separate SCC

Component graph: just a graph of with each vertex representing an SCC, must be a DAG

Topological Sort O(V+E)
-performed on directed acyclic graph
Linear ordering of all its vertices such that if G contains an edge (u,v) then u appears before v in the order. 1. call DFS(G) to compute finishing times v.f for each vertex v 2. as each vertex is finished insert it onto the front of a linked list 3. return the linked list of vertices 4. Lecture 5 (01/28)
Posted on: Monday, January 28, 2013
Topics: Strongly Connected Components, Activity Selection
Reading: CLRS (22.5, 16.1), KT (4.1)
Scheduling Probelem
Set of n activities which can be served only one at a time, each with start time s and finish time f
Selecct a maximum-size subset of mutually compatible activities (meaning no overlap)

GREEDY ALGORITHM
Note that putting the job with the earliest finish time allows for the most amount of jobs to follow, because it allows the machine to have the most possible time to get to other jobs
Take job with lowest finish time, then reduce set to all job that don’t overlap, then choose lowest finishing time, recursively. * Lecture 6 (01/30)
Posted on: Wednesday, January 30, 2013
Topics: Activity Selection, Coloring Interval Graphs, Scheduling
Reading: CLRS (16.1, 16.2), KT (4.1, 4.2) * Lecture 07 (02/04)
Posted on: Wednesday, February 6, 2013
Topics: Minimizing Maximum Lateness, Sorting (Insertion Sort, Merge Sort, Quick Sort)
Reading: CLRS (Chp 2, 7.1, 7.2), KT (4.2)
Insertion sort
Starting from the second element as key value, while the key value taken is less than the element in the array corresponding to the counter (running down from the index of the key value, initially 2 for first run through), move the value in the array up and then place the key in the counter + 1 space. Recursive biatch

Merge sort 1. Break array in half 2. Call merge sort on left half 3. Call merge sort on right half 4. Call Merge
Merge
1. Take subarrays of input, left and right 2. Compare elements by element and put back in array in right place a. If comparison, easy
Run time = O(n lg n)
Recurrence equations: T(n) = T(f(n)) + O(g(n))

Bubble sort 1. Have a counter that starts one away from the end of the array i and one that starts at the end of the array j 2. Compare element j with element j-1 until j =i a. Change elements if j is less than j-1

Inversions: elements in array out of order, i < j, but A[i] > A[j]

Quick Sort: 1. Find partition or pivot in array 2. Call quicksort on subarray to right and left of pivot

Partition 1. Take last element of input array, key 2. Compare element to key, if less than put in left subarray, if greater put in right subarray
Worst case run time : O(n2) * Lecture 8 (02/06)
Topics: Master Theorem, Selection, Counting Inversions
Reading: CLRS (4.5, 9.1, 9.3), KT (5.3), handout for Simplified Master Theorem

THE MASTER THEOREM
For recurrences of the form T(n) = aT(n/b) + f(n) 1. If f(n)=O(nlog(b) a−ϵ) for some constant ϵ>0, then T(n)=Θ(nlog(b)a) 2. If f(n)=Θ(nlog(b)a), then T(n)=Θ(nlog(b)a lg n) 3. If f(n)=Ω(nlog(b)a+ϵ) for some constant ϵ>0, and if af(n/b)≤cf(n) for some constant c<1 and all sufficiently large n, then T(n)=Θ(f(n))

PART TWO * Lecture 9 (02/11)
Posted on: Monday, February 11, 2013

Topics: Heaps, Lower bound on sorting Reading: CLRS (Chp. 6, 8.1)

Heaps:
Heap_size is the number of elements in the heap array to be included in the heap, while length is the number of elements in the heap array.
Parent: , right child: 2i+1, left child: 2i
Heap property: Max heap prop: A[parent(i)]>A[i]
MAX-HEAPIFY(A,i) O(lg n) or O(h)
1 l LEFT(i) /
2 r RIGHT(i) /
3 if l ≤ A.heap-size and A[l] > A[i]
4 largest l
5 else largest i
6 if r ≤ A.heap-size and A[r] > A[largest]
7 largest r
8 if largest ≠ i
9 exchange A[i] with A[largest]
10 MAX-HEAPIFY(A, largest)

Checks node in question, if either of its children are larger than it, swap the original node with the larger of its children and run max heapify on the subtree that the original node was pushed down to. Maintains heap property.

BUILD-MAX-HEAP.A/
1 A.heap-size <- A.length
2 for i <- downto 1
3 MAX-HEAPIFy(A, i)

every node indexed at an index greater than must be a leaf with no children.
Upper bound: O(n lg n)
But since the heights range from 0 to lg n and the number of nodes at each height is , the run time is

HEAPSORT(A) O(n lg n)
1 BUILD-MAX-HEAP(A)
2 for i <- A.length down to 2
3 exchange A[1] with A[i]
4 A.heap-size <- A.heap-size-1
5 MAX-HEAPIFY(A, 1)

Builds a max heap, then at each iteration, puts the maximum at the index to be removed from the heap size (so a max heap is sorted in ascending order by placing the greatest numbers at the end of the array), then max heapifies all indices less than the exchanged one, until there is only one element left which must be the smallest.

Heap Maximum(A) O(1)
Return A[1]

HEAP-EXTRACT-MAX.A O(lg n)
1 if A.heap-size < 1
2 error “heap underflow”
3 max <- A[1]
4 A[1] <- A[A.heap-size]
5 A.heap-size <- A.heap-size-1
6 MAX-HEAPIFY(A, 1)
7 return max

Saves the value of the max, places the last node in the heap at the top of the heap, then maintains heap property.

HEAP-INCREASE-KEY(A, i, key) O(lg n)
1 if key < A[i]
2 error “new key is smaller than current key”
3 A[i] <- key
4 while i > 1 and A[PARENT(i)] < A[i]
5 exchange A[i] with A[PARENT(i)]
6 i <- PARENT(i)

Only increase the value at i to key if key is greater than the current value there. Then while we haven’t hit the top of the heap and the parent is still less than the new value inserted, exchange the new value with its parent and set i to the value of its parent.

MAX-HEAP-INSERT(A, key) O(lg n)
1 A.heap-size <- A.heap-size - 1
2 A[A.heap-size] <- 1
3 HEAP-INCREASE-KEY[A, A.heap-size, key]

* Lecture 10 (02/13) * 
Posted on: Wednesday, February 13, 2013 * 

Topics: Lower bound on Sorting, Counting Sort, Radix Sort, Randomized Quick Sort * Reading: CLRS (8.1, 8.2, 8.3, 7.3, 7.4)


 Randomized Quicksort: Instead of selecting r as the pivot, select an element at random from A[p…r] Expected run time: Xij=1 if elements i and j are compared X = total comparisons = E[X] = E[]= P(Xij=1) = P(element i is chosen as pivot) + P(element j is chosen as pivot) There are j-i+1 elements in range ij, so Probability of any element being chosen as pivot is 1/(j-i+1), so probability of either being chosen is 2/(j-i+1). So the expected run time is O(n lg n)

Lower Bound on Sorting: Any comparison sort algorithm requires Ω(n lg n) comparisons in the worst case COUNTING-SORT(A,B, k) O(n) if k < n… k is largest integer
1 let C[0… k] be a new array
2 for i <- 0 to k
3 C[i] <- 0
4 for j <- 1 to A.length
5 C[A[j] ] <- C[A[j ] + 1
6 // C[i] now contains the number of elements equal to i .
7 for i <- 1 to k
8 C[i]_<- C[i] + C[i-1]
9 // C[i] now contains the number of elements less than or equal to i .
10 for j <-A.length down to 1
11 B[C[A[j]]] <- A[j ] 12 C[A[j]] <- C[A[j]]-1 Counts the number of elements less than one to determine its sorted place RADIX-SORT(A,d) O(d(n+k))
1 for i <- 1 to d 2 use a stable sort to sort array A on digit i Sort the least significant digits (by shifting entire numbers) up to the most significant

* Lecture 11 (02/18) * 
Posted on: Wednesday, February 20, 2013

Topics: Dijkstra's shortest path algorithm, Minimum spanning trees * Reading: CLRS (24.2, 24.3, 23.1, 23.2), KT (4.4, 4.5)




Minimum Spanning Tree Algorithms
A minimum spanning tree must span every vertex in the graph and have the smallest sum of edge weights of any spanning tree

MST-KRUSKAL(G,w) O(E lg V)
1 A <-Ø
2 for each vertex v ∈G.V
3 MAKE-SET(v)
4 sort the edges of G.E into nondecreasing order by weight w
5 for each edge (u,v) ∈ G.E, taken in nondecreasing order by weight
6 if FIND-SET(u) ≠ FIND-SET(v)
7 A <- A ∪ {(u,v)}
8 UNION(u,v)
9 return A

Each edge is an individual “set” to begin with. Since edges are taking in nondecreasing order, if this edge connects two sets that are not the same, then this edge must be the smallest edge to connect these sets so add it to the min span tree

MST-PRIM(G,w,r) O(E lg V)
1 for each u ∈ G.V
2 u.key <-
3 u.parent <-NIL
4 r.key <- 0
5 Q <- G.V
6 while Q ≠ Ø;
7 u <- EXTRACT-MIN(Q)
8 for each vertex v ∈ G.Adj[u]
9 if v ∈Q and w(u,v) < v.key
10 v.parent <- u
11 v.key <- w(u,v)

Set all keys to infinity for all nodes but the starting point, then remove starting node at the first iteration and update the keys of all adjacent vertices, then go to the smallest of those and then update adjacent vertices and remove the smallest, repeat.

Shortest Path from source vertex algorithms

Shortest path properties
1. Subpaths of shortest paths are also shortest paths
2. Negative weight cycles cause elements on the negative weight cycle (and paths reachable from this cycle) to have a distance of negative infinity
3. Triangular inequality: For any edge (u,v) ∈ G.E, we have d(s,v) ≤ d(s,u) + w(u,v)
4. Upper bound property: We always have v.d ≥ d(s, v) for all vertices in V and once v.d achieves the value d(s,v), it never changes.
5. When there is no path to a vertex its distance is infinity
6. If the shortest path to v is a path from s to u then the edge u to v and u.d = d(s,u) prior to relaxing edge (u,v ) then v.d = d(s, v) at all times after the edge is relaxed.
7. Edges on a path are relaxed in order (not necessarily one after another, but if s goes to t then to u then to v, it relaxes edges (s,t) then (t,u) then (u,v).
8. Once v.d = d(s,v) for all vertices in V , the predecessor subgraph is a shortest-paths tree rooted at s.

* DIJKSTRA(G,W, s) O( VlgV + E) with fibonnaci heap
1 INITIALIZE-SINGLE-SOURCE(G,s) * 2 S <-Ø * 3 Q <- G.V * 4 while Q ≠ Ø ; * 5 u <- EXTRACT-MIN(Q) * 6 S <- S ∪ {u} * 7 for each vertex v ∈ G.Adj[u] * 8 RELAX(u, v, w) *

* DAG-SHORTEST-PATHS(G, w, s) O(V+E) * 1 topologically sort the vertices of G * 2 INITIALIZE-SINGLE-SOURCE.G; s/ * 3 for each vertex u, taken in topologically sorted order * 4 for each vertex v ∈ G.Adj[u] * 5 RELAX(u, v, w)

* Lecture 12 (02/20)
Posted on: Wednesday, February 20, 2013

Topics: Minimum Spanning Trees, Huffman Coding * Reading: CLRS (Chp 23, 16.3), KT (4.4, 4.5, 4.8) * 


Huffman Coding * Prefix coding: no encoding is a prefix of another encoding (allows variable length encoding) *
HUFFMAN(C )
1 n <- |C|
2 Q <- C
3 for i <-1 to n- 1
4 allocate a new node z
5 ´ z.left <- x <- EXTRACT-MIN(Q)
6 ´ z.right <- y <- EXTRACT-MIN(Q)
7 ´ z.freq <- x.freq + y.freq
8 INSERT(Q,z) * 9 return EXTRACT-MIN.Q/ // return the root of the tree

* Lecture 15 (03/11)
Posted on: Monday, March 11, 2013

Topics: Dynamic Programming (Computing powers of 2, Weighted Interval Scheduling) * Readings: KT (6.1, 6.2)




* Lecture 16 (03/13)
Posted on: Wednesday, March 13, 2013

Topics: Dynamic Programming (Knapsack, Longest Common Subsequence) * Readings: CLRS (15.4), KT (6.4) * 4 steps: * 1. Characterize the structure of an optimal solution. * 2. Recursively define the value of an optimal solution. * 3. Compute the value of an optimal solution, typically in a bottom-up fashion. * 4. Construct an optimal solution from computed information. Common subproblems
Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming.
i. The input is x1, x2,…, xn and a subproblem is x1, x2,… xi.
The number of subproblems is therefore linear. ii. The input is x1,…, xn, and y1,…,ym. A subproblem is x1,… ,xi and y1… yj.
The number of subproblems is O(mn). iii. The input is x1…xn and a subproblem is xi,xi+1,…,xj .
The number of subproblems is O(n2). iv. The input is a rooted tree. A subproblem is a rooted subtree. Edit distance example: Example of comparing two different arrays of length n and m Transform String A (array of length n) in B (length m) by the minimum number of transformation (insert, delete, or replace: Cost = 1, else 0) Goal: Minimize total cost of transforming A into B T[i,j] = Min cost to transform A[1…i] into B[1…j] T[i,j] = Min (Cd+T[i-1,j], Ci+T[i,j+1], if A[i] = B[j] then T[i-1,j-1], else T[i-1,j-1]+Cr) Longest Increasing Subsequence: Example of a sequence dyn prog prob Find the longest (not necessarily contiguous subsequence) of increasing numbers L(i) = longest subsequence of all elements up to i L(j) = max for all i<j of { if A[i] < A[j], L[i]+1, else L[i]} Balanced Partition Problem: Similar to knapsack problem, n integers ranging from 0 to k. Partition subsequences so that the difference between the sum of the two is minimized THINK OF EVERYTHING AS AN ARRAY

* Lecture 17 (03/18)
Posted on: Monday, March 18, 2013

Topics: Dynamic Programming (Edit Distance, Matrix Multiplication) * Readings: DPV (6.3), CLRS (15.2, 15.3)

* Lecture 18 (03/20)
Posted on: Wednesday, March 20, 2013
Topics: Network Flows
Readings: CLRS (26.1, 26.2), KT (7.1)

Flow network: G (V,E) is a directed graph in which each edge (u,v) in E has a nonnegative capacity c(u,v) ≥ 0. If (u,v) in E, there is no (v, u) in E, capacity of any edge not in E is 0 and no self loops. Graph is connected and each vertex has at least one incoming edge, so |E| ≥ |V|-1

For each vertex v in V, there exists a path from source (s) to v to sink (t) Flow: capacity constraint: For all u, v in V, the flow on the edge from u to v is greater than or equal to 0 and less than or equal to the capacity of the edge (u,v) Flow conservation: for each vertex v in V- {s,t}

value of flow: anti parallel edges: edges from u to v and v to u. take v to u and create a new vertex with edge from v with same capacity as (v,u) and edge to u with same capacity as (v,u) multi sinks and sources: edges from super source, S, to all s with capacity infinity. Same for T, but edges to it instead Ford Fulkerson: initialize all f(u,v) to 0. Find an augmenting path from s to t, find residual network and repeat until no more augmenting paths exist Residual network: c(u,v) in Gf is c(u,v) from previous residual network – flow pushed through this edges on augmenting path. add this flow to the reverse edge

2|E| ≥ |Ef| Augmenting path: Increase flow on each edge in the residual graph by the minimum capacity edge on the path Cut: partition of V into S and T with s in S and t in T f(S,T) = net flow across cut= c(S,T) = capacity of S-T cut = Max flow min cut thm
If f is a flow in a flow network G (V,E) with source s and sink t , then the following conditions are equivalent:
1. f is a maximum flow in G.
2. The residual network Gf contains no augmenting paths. 3. |f | = c(S,T) for some cut (S,T) of G. Edmond karp alg: ford Fulkerson using BFS to find augmenting path. runs in O(VE2)

* Lecture 19 (03/25)
Posted on: Tuesday, March 26, 2013
Topics: Max Flow-Min Cut Theorem, Bipartite Matching
Readings: CLRS (26.2, 26.3), KT (7.1,7.2,7.5)
Posted by: Rajiv Gandhi
Posted to: CIS 320-001 2013A. Intro To Algorithms

Maximum bipartite matching:
Given an undirected graph G (V,E), a matching is a subset of edges M ⊆ E such that for all vertices v in V , at most one edge of M is incident on v. We say that a vertex v in V is matched by the matching M if some edge in M is incident on v; otherwise, v is unmatched. A maximum matching is a matching of maximum cardinality, that is, a matching M such that for any matching M’, we have |M| ≥ |M’|.

Corresponding flow network: Partition G into L and R, directed edges in G’ are undirected edges from L to R in G, add s with edges to all vertices in L and edges from all vertices in R to added t. each edge has unit capacity.
|E| ≥ |V|/2 so |E| ≤|E’|=|E| + |V| ≤ 3|E|

Run FF on corresponding flow network, |M| = |f|

* Lecture 20 (03/27)
Posted on: Wednesday, March 27, 2013
Topics: Bipartite Matching, Karger's min-cut algorithm, Image Segmentation
Readings: class handout, CLRS (26.3), KT (7.5, 7.10)
Posted by: Rajiv Gandhi
Posted to: CIS 320-001 2013A. Intro To Algorithms

Karger’s min cut alg: contract edge, merge vertices, reattach all edges to merged vertex.

Image segmentation: Graph G of pixels, each pixel has an undirected edge to each of its neighbors. Determine what is in foreground and what is in background. Each pixel i has probability ai that it belongs to foreground, bi that is belongs to background. And for each pair of neighboring pixels ij, there is a penalty pij for putting one in foreground and another in background. Partition pixels into A and B

Max or we can say
Minimize
Alg: create supersource and supersink, edge from s to all vertices with capacity ai and edge from all vertices to supersink t with capacity bi. Between each pair of neighboring vertices is an edge with capacity pij.

Three types of edges that cross cut: from s to a vertex in B which contributes ai, from a vertex in S to t which contributes bi, and an edge between neighbors pij

Lecture 21 (04/01)
Posted on: Tuesday, April 2, 2013
Topics: Image Segmentation, NP-Completeness
Readings: CLRS (34.1 - 34.3), KT (7.10)
Polynomial time verification: select certificate (specific instance of problem) and decide if this certificate is in fact a solution to the problem

A language is NP complete if it can is in NP (has a polynomial time verifier) and can be reduced from another NP problem to the one described

Polynomial time reduction: convert this problem into an instance of another NP complete problem (an instance of the problem will have a solution if and only if its reduction does as well)

PROBLEMS~
Packing: You have some objects, and you want to choose at least of them. You need to deal with conflicts between objects (you can't choose all of them).
Set Packing: given m elements, S is a set of subsets of the universe, find a subset of S that is pairwise disjoint
Independent Set: find maximum set of non-adjacent vertices
Clique: find subset of vertices that there is an edge between each pair of vertices

Covering: You have some objects, and you want to choose at most of them. The constraint is that these objects together should achieve some overall goal.
Set Cover: set of m elements, set S of n sets whose union is all m element, find subset of S whose union is all m elements
Vertex Cover: subset of vertices such that for all edges at least one vertex of each edge is in the vertex cover

Partitioning: You want to search over all ways to divide your collection of objects into non-overlapping subsets.
Partition: Partition S into S1 and S2 such that the sum of each subset is equivalent
3D Matching (Can also think of as a packing problem + a covering problem at the same time): 3 sets X, Y, Z and a set of triplets (x, y, z). Find maximum subset of triplets such that there is no overlap
Graph Coloring (Want to partition with conflicts: conflicting objects can't go in the same set): color vertices such that no to adjacent vertices have the same color

Sequencing: You want to search over all permutations of a sequence.
Hamiltonian Path/Cycle: path/cycle that goes through every vertex exactly once
Traveling Salesman: Hamiltonian cycle that is less than weight k

Numerical Problems
Subset Sum: find a subset of S that sums to t
Knapsack: given a set of items and a bag with capacity C, find subset of items with maximum value with sum of weights less than C

General Purpose: Your problem doesn't fall into the above classes, or you're not sure.
3 SAT:

* Lecture 23 (04/08)
Posted on: Monday, April 8, 2013
Topics: NP-completeness, Reductions
Readings: CLRS (34.3, 34.4, 34.5)

* Lecture 24 (04/10)
Posted on: Wednesday, April 10, 2013
Topics: NP-complete Reductions
Reading: KT (8.6, 8.7, 8.8)
Posted by: Rajiv Gandhi
Posted to: CIS 320-001 2013A. Intro To Algorithms

* Lecture 25 (04/15)
Posted on: Monday, April 15, 2013
Topics: Bellman-Ford, Floyd-Warshall
Readings: KT (6.8), CLRS (24.1, 25.2)
Posted by: Rajiv Gandhi Posted to: CIS 320-001 2013A. Intro To Algorithms Bellman ford: initialize distance from source to source as 0 and from source to all others as infinity, relax all edges on graph V-1 times, at end check whether negative weight cycle exists by checking that for each edge (u,v) , d[u] + w ≥ d[v] Runs in O(VE) Floyd Warshall: let dij(k) be the minimum distance from i to j for which all intermediate vertices are in (1,2,…,k), set dij(0) as wij For k = 1 to n, for i=1 to n, for j=1 to n dij(k)=min(dij(k-1), dik(k-1)+dkj(k-1))

Runs in O(V3)

* Lecture 26 (04/17)
Posted on: Thursday, April 18, 2013
Topics: Approximation Algorithms (Vertex Cover, k-Center, TSP)
Readings: CLRS (35.1, 35.2), class handout
Posted by: Rajiv Gandhi
Posted to: CIS 320-001 2013A. Intro To Algorithms

Vertex cover approximation: no more than 2*Opt
Select an arbitrary edge, add u and v to return set, remove that edge and all edges incident on u or v, repeat until there are no edges left.

Proof of 2*OPT
Let A denote the set of edges selected arbitrarily, to cover these edges in any other vertex cover, specifically C*, at least one end point of the edges in A must be selected and no two edges in A are covered by the same end point in C* by construction of the algorithm

So |C*| ≥ |A|

Since we remove all incident edges, we never select an edge with an end point already in C,
SO |C| = 2|A|

Thus: |C|=2|A|≤2|C*|

TSP approximation:
Select a vertex to be root, find minimum spanning tree, order vertices in preorder walk of MST, return that ordered list

2 time approximation for TSP proof with triangle inequality
The weight of the MST must be less than or equal to the weight of the minimum ham cycle and since every edge in the MST is traversed twice, the weight of the solution from this algorithm must be 2*weight of MST, so the approximation is at most 2*optimal

* Lecture 27 (04/22)
Posted on: Monday, April 22, 2013
Topics: Approximation Algorithms (TSP, Set Cover)
Readings: class handout
Posted by: Rajiv Gandhi
Posted to: CIS 320-001 2013A. Intro To Algorithms

Similar Documents

Premium Essay

Something Borrowed Malcolm Gladwell Analysis

...Those notes can only be sequenced so many times before they are repeated by a new musician and called “original”. Intellectual property has been protected in the courts systems, but has favored personal interest over creativity and borrowing. In the case of Weber vs. Repp for example, Repp was claiming to be the owner of the copied Catholic folk music stolen to create music by Weber. With help from a lawyer, it is proven that Weber wrote a song previous to the music and songs by Repp. It was demonstrated that Weber wrote a song, Repp wrote another song sounding similar, and then Weber wrote the song in question. This showing that Weber borrowed from himself and Repp borrowed from him. The musical notes played in the same sequence were copied by both composers and therefore the courts dismissed the case, musical notes are not owned by any one composer. It does not matter what you copy but how much you choose to take. The idea behind Gladwell’s argument is that borrowing some to be creative is and needs to be acceptable in the eyes of “plagiarism...

Words: 1296 - Pages: 6

Free Essay

Narrative

...to harmonize, considering it was our first year learning an instrument. There was no reading or writing when it came to playing the instruments, but with music, a story can be made. For example, half the class would play our recorders in sync with one another, and other students in the class would play percussion. With the rhythm of the music combined, the feel and sound of the music gives the audience a feel of a different environment, such as feeling as though you are taking a journey through an Indian village, or celebrating the first fourth of July in America. As I progressed through the year, music classes turned into singing as well. In order to know the words that we were singing, we had paperback music, which had music lines, notes, and words for us to...

Words: 1172 - Pages: 5

Free Essay

Integrity

...through the paper. Halfway through the paper, I saw my friend John suspiciously looking at the class. My instincts told me that something was wrong. As a result, I began to keep an eye on John. Suddenly, I saw John taking notes out from his pencil case! My mouth hung wide open and I gasped in shock. How could John do that! I thought should I report him? The devil in my mind said that I should not care about this thing after all, he is still my best friend while the angel said that I should be honest and report him. After thinking for a while, I decided to report him. I raised my hand and told the teacher “ Mr Tan, John is cheating by using notes from his pencil case.” The teacher nodded his head and walked towards John’s table. Mr Tan said “John! Why are you cheating?” John shook his head to deny that he did not cheat. Mr Tan confiscated his pencil case and dumped the contents out. Out came pencils, erasers and pens. But there was no notes inside! John let out a smirk from his mouth. I was shocked! I thought that there was a note? Just when I thought all hope was lost, Mr Tan found another zip at the pencil case and he opened it. Suddenly, John’s smirk began to vanish. Waves of panic overwhelmed him. The hidden note was found there! Mr Tan looked at John sternly. He brought John to the principal’s office to explain what had happened. On the next day, the fiery-tempered Discipline Master caned John during assembly period. After this incident...

Words: 333 - Pages: 2

Premium Essay

Data Minging Fit3002 A2

...144 Peter Road, Monash University south Africa 144 Peter Road, Monash University south Africa FIT 3002 Edson Zandamela and Mbuto Carlos Machili Assignment 2 Report FIT 3002 Edson Zandamela and Mbuto Carlos Machili Assignment 2 Report 08 Fall 08 Fall Table of Contents 1. Introduction 2 2. Problem Definition 2 2.1 Objective 2 2.2 Data Characteristics 2 2.3 Model Evaluation Method 3 2.4 Budgetary Constraints 3 2.5 Response rate without a model 3 3. Data Preparation and Pre-processing 4 3.1 File formatting 4 3.2 Missing Values 4 4. Experiments 4 4.1 Learning Algorithm Selection 4 4.2 Iteration Process 6 4.2.1. Attribute selection: 6 4.2.2. Changing Parameter settings 7 4.2.3. Data Normalization 7 4.2.4. Model Recommendation 8 4.2.4.1 Lift Chart 8 4.2.4.2 Gain Chart 9 5. Campaign suggestions 10 6. Conclusion 12 1. Introduction Global Paper’s prime objective is to analyze and evaluate the market response rate of a new paper product that they are currently exploring by testing the market using a mass mailing campaign. The evaluation is based on how much the product will appeal to people based on their earned salaries (<=$50k, or >$50k) per year. The company has purchased demographic data sets (Adult data set and test data) from a known source, and through market research, it has discovered that the new product is likely to appeal to persons who make over $50K a year. This report documents the data mining processes (using...

Words: 3639 - Pages: 15

Premium Essay

Pt1420 Unit 1 Algorithm Paper

...Incidentally this was difficult for me to understand. I think I get it but correct me if I don't. 1. For what size of problem is algorithm A faster an B Algorithm A: 1000n³ Algorithm B: 2^n Javanotes.com states "the larger the exponent, the greater the rate growth of the function. Exponential functions such as 2^n and 10^n, where the n is in the exponent, have a growth rate that is faster than that of any power function. In fact, exponential functions grow so quickly that an algorithm whose run time grows exponentially is almost certainly impractical even for relatively modest values of n, because the running time is just too long." Note the following table; Run Time n log(n) n*log(n) n squared n/log(n) 16 4 64 256 4.0 64 6 384 4096 10.7 256 8 2048 65536 32.0 1024 10 10240 1048576 102.4 1000000 20 19931568 10^11 50173.1...

Words: 487 - Pages: 2

Free Essay

Info Tech

...2. ALGORITHMS, FLOWCHARTS, DATA TYPES AND PSEUDOCODE 2.1 ALGORITHMS The term algorithm originally referred to any computation performed via a set of rules applied to numbers written in decimal form. The word is derived from the phonetic pronunciation of the last name of Abu Ja'far Mohammed ibn Musa al-Khowarizmi, who was an Arabic mathematician who invented a set of rules for performing the four basic arithmetic operations (addition, subtraction, multiplication and division) on decimal numbers. An algorithm is a representation of a solution to a problem. If a problem can be defined as a difference between a desired situation and the current situation in which one is, then a problem solution is a procedure, or method, for transforming the current situation to the desired one. We solve many such trivial problems every day without even thinking about it, for example making breakfast, travelling to the workplace etc. But the solution to such problems requires little intellectual effort and is relatively unimportant. However, the solution of a more interesting problem of more importance usually involves stating the problem in an understandable form and communicating the solution to others. In the case where a computer is part of the means of solving the problem, a procedure, explicitly stating the steps leading to the solution, must be transmitted to the computer. This concept of problem solution and communication makes the study of algorithms important to...

Words: 4924 - Pages: 20

Free Essay

Ds Java

...A Practical Introduction to Data Structures and Algorithm Analysis Third Edition (Java) Clifford A. Shaffer Department of Computer Science Virginia Tech Blacksburg, VA 24061 April 16, 2009 Copyright c 2008 by Clifford A. Shaffer. This document is the draft of a book to be published by Prentice Hall and may not be duplicated without the express written consent of either the author or a representative of the publisher. Contents Preface xiii I Preliminaries 1 1 Data Structures and Algorithms 1.1 A Philosophy of Data Structures 1.1.1 The Need for Data Structures 1.1.2 Costs and Benefits 1.2 Abstract Data Types and Data Structures 1.3 Design Patterns 1.3.1 Flyweight 1.3.2 Visitor 1.3.3 Composite 1.3.4 Strategy 1.4 Problems, Algorithms, and Programs 1.5 Further Reading 1.6 Exercises 3 4 4 6 8 12 13 14 15 16 17 19 21 2 Mathematical Preliminaries 2.1 Sets and Relations 2.2 Miscellaneous Notation 2.3 Logarithms 2.4 Summations and Recurrences 25 25 29 31 33 iii iv Contents 2.5 2.6 2.7 2.8 2.9 3 II 4 Recursion Mathematical Proof Techniques 2.6.1 Direct Proof 2.6.2 Proof by Contradiction 2.6.3 Proof by Mathematical Induction Estimating Further Reading Exercises Algorithm Analysis 3.1 Introduction 3.2 Best, Worst, and Average Cases 3.3 A Faster Computer, or a Faster Algorithm? 3.4 Asymptotic Analysis 3.4.1 Upper Bounds 3.4.2 Lower Bounds 3.4.3 Θ Notation 3.4.4 Simplifying...

Words: 30587 - Pages: 123

Free Essay

Calorie Management Program

...PRG/211 – Week 5 Team B – Algorithm Planning Visual Logic CALORIE MANAGEMENT PROGRAM Week 2 Algorithm Planning Week 3 Program Variables for Calorie Management Week 4 Verification & workaround for Calorie Management Week 5 Learning Team Assignment ****************************************************** About the Assignment Imagine that your team of software developers won a contract to develop a program that will identify whether a person is balancing calories consumed with those being expended or burned by taking the following into account:   The balance of calories is calculated daily. Calories are consumed in both food and beverages. Calories can be identified from both product labeling and calorie counters located on the Internet. Calories are burned daily in both daily living and exercise. Calories expended or burned can be calculated using calorie calculators located on the Internet. The balance of calories may be displayed in either calories or pounds and ounces. The following are examples of the information that might be provided: Calories are in balance. _ _ _ more calories are consumed than expended. _ _ _ more calories are expended than consumed. No pounds/ounces were gained or lost. _ pounds _ _ ounces may have been gained. _ pounds _ _ ounces may have been lost. Use the following computation: One pound equals 3500 calories. THE PLAN Identify the criteria TEAM B will need to develop the required software. To do this, Team B must...

Words: 1400 - Pages: 6

Free Essay

Pt 1420

...Student Name:__________________ Introduction to Programming Winter 2014/2015 Instructor: Martin Remmele Unit 7 Homework Assignment Due by end of first break February 10, 2015 Learning Objectives and Outcomes NOTE: This section lists concepts and techniques to be understood from this unit. The actual assignment that you are to complete is found in the next section: “Assignment Requirements”. * Be able to Use pseudocode/flowcharts to represent repetition structures. * Be able to Create While, Do-While, and Do-Until conditional loops. * Be able to Describe the implications of an infinite loop. Assignment Requirements Complete the following exercises. An exercise that calls for an algorithm may be written in English as a series of steps. An exercise that calls for program statements may be written in a) the text’s pseudocode, b) your own preferred pseudocode notation or c) Visual Basic. (VB code can simply be typed into your Word document; it does not have to be created in the development environment.) The logic of the statements will be more important to the grade than the syntax. * Short Answer Review Questions 1-5, starting on page 213 (5 pts each) * Algorithm Workbench Review Questions 1, 2, 7, and 8, starting on page 213 (10 pts each) * Programming Exercises 1, 3, and 4, starting on page 214 (10 points each) Required Resources * Textbook Submission Requirements Submit...

Words: 310 - Pages: 2

Premium Essay

Pt1420 Unit 3 Assignment 1 Algorithm Essay

...1. Explain what software you used to create your game To create my game, a software called “Scratch was used. Scratch is a software that is downloadable on most PC’s, people can use this program to share interactive media. This is includes self-made games as well as animations. 2. Explain what an algorithm is  An algorithm is a process, or a set of rules to be followed in calculations or other problem-solving operations, especially by a computer. However, algorithms are used in our day to day life, whether you notice or not. For example; a daily routine, or a cooking recipe. 3. Explain what an if statement is An if statement is a “block” or a piece of code that when one thing happens, it will do that, but if another happens, it will do something else. For example, when...

Words: 653 - Pages: 3

Free Essay

Kolmogorov Algorithm

...CSC 435 DESIGN AND ANALYSIS OF ALGORITHM GROUP THREE(3) ASSIGNMENT THE KOLMOGOROV COMPLEXITY ALGORITHM Computer Science: FMS/0704/11 FMS/0707/11 FMS/0720/11 FMS/0721/11 FMS/0728/11 Computing-with-Accounting: FMS/0818/11 FMS/0643/11 FMS/0749/11 FMS/0722/11 FMS/0729/11 FMS/0741/11 FMS/0829/11 FMS/0784/11 FMS/0812/11 FMS/0652/11 Kolmogorov complexity In algorithmic information theory (a subfield of computer science and mathematics), the Kolmogorov complexity (also known as descriptive complexity, Kolmogorov–Chaitin complexity, algorithmic entropy, or program-size complexity) of an object, such as a piece of text, is a measure of the computability resources needed to specify the object. It is named after Andrey Kolmogorov, who first published on the subject in 1963. For example, consider the following two strings of 32 lowercase letters and digits: abababababababababababababababab 4c1j5b2p0cv4w1x8rx2y39umgw5q85s7 The first string has a short English-language description, namely "ab 16 times", which consists of 11 characters. The second one has no obvious simple description (using the same character set) other than writing down the string itself, which has 32 characters. More formally, the complexity of a string is the length of the shortest possible description of the string in some fixed universal description language (the sensitivity of complexity relative to the choice of description language is discussed below)...

Words: 3373 - Pages: 14

Premium Essay

Lab Solution

...Solutions Lab 1: Input, Processing, and Output Note to Instructor: This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design. Material in the chapter should have been covered prior to lab assignment. In addition, students should have had instruction on using a flowcharting application such as Raptor and instruction on using the IDLE environment for Python. Evaluation: The instructor should be present to answer any questions and observe the student performing the lab. The student should turn in a hard copy (paper) or soft copy (email) of their work. To minimize the number of attachments or individual files created for this lab, space is set aside in the lab for students to insert completed exercises. Directions are provided to students on copying and pasting. Learning Objectives for this lab include: 1. Identify pseudocode keywords. 2. Identify flowcharting symbols. 3. How to declare variables with appropriate data types. 4. How to assign values to variables. 5. How to take in input from the computer user. 6. How display output to the screen. 7. How to document code. 8. How to process calculations. Lab 1.1 through 1.4 uses the following programming problem. Write a program that will take in basic information from a student, including student name, degree name, number of credits taken so far, and the total number of credits required in the degree program. The program will then calculate how many...

Words: 2312 - Pages: 10

Premium Essay

Solving Reader Collision Problem in Large Scale Rfid Systems

...problem in large scale RFID systems : Algorithms, performance evaluation and discussions John Sum, Kevin Ho, Siu-chung Lau Abstract—Assigning neighboring RFID readers with nonoverlapping interrogation time slots is one approach to solve the reader collision problem. In which, Distributed Color Selection (DCS) and Colorwave algorithm have been developed, and simulated annealing (SA) technique have been applied. Some of them (we call them non-progresive algorithms), like DCS, require the user to pre-defined the number of time slots. While some of them (we call them progressive), like Colorwave, determine the number automatically. In this paper, a comparative analysis on both non-progressive and progressive algorithms to solve such a problem in a random RFID reader network is presented. By extensive simulations on a dense network consisting of 250 readers whose transmission rates are 100%, a number of useful results have been found. For those non-progressive type algorithms, it is found that DCS is unlikely to generate a collision-free solution, even the number of time slots is set to 20. On the other hand, heuristic and SAbased algorithms can produce collision-free solutions whenever the number of time slots is set to 16. For the cases when the number of time slots is not specified, heuristic-based, SAbased and Colorwave algorithms are all able to determine the number automatically and thus generate collision-free solution. However, SA-based algorithms require much longer time than the...

Words: 6608 - Pages: 27

Premium Essay

Note Taking

...Improving Your Note Taking ▪ Effective note taking is one of the keys to succeeding in school. Students should devote a considerable amount of time reviewing information discussed during classroom lectures. It is very difficult remembering specific details from classroom lectures without good notes. These note taking strategies will help you to take better notes: ▪ Make clear and accurate notes Make sure to take legible and accurate notes since it is not uncommon to forget key details discussed in class after it has ended. Frequently, students comprehend the teacher's lecture, so they'll neglect to jot down specific details only to forget them later. Students who keep accurate notes can review them later to fully grasp key concepts during personal study time. Additionally, since during classroom lectures teachers frequently cover many topics, effective notes enable students to concentrate on specific topics. ▪ Come to class prepared Students properly prepared for class usually take better notes. Proper preparation includes completing assigned reading prior to class and reviewing notes from previous lectures. Students who do this can ask questions about confusing concepts and be prepared for new topics. ▪ Compare your notes To ensure your notes are as accurate and detailed as possible, compare them with the notes of other students after class is over. This is useful because your colleagues will frequently write down lecture details that you...

Words: 602 - Pages: 3

Free Essay

Michael Meets Mozart

...Side Notes: • I came up with a killer Mozart-style arrangement involving several songs by modern artists. But I ran into a roadblock with getting permissions. So I decided to do variations on a theme by making my arrangement an original tune. Helpful Hints: • Learn the hardest parts first with the correct fingering. Instead of using a slower tempo to practice longer sections, try using the actual tempo to practice overlapping shorts sections (as small as 2 notes...hands alone if needed). • For those who have heard the recording or seen the video on • When I practice, it helps me to realize that it takes up to 300 YouTube, Steven Sharp Nelson laid down over 100 tracks, including (perfect) reps before muscle memory kicks-in. cello textures never before known possible. Every single sound on the video was made using only the instruments shown: piano, cello, • I like to imagine totally soft and relaxed hand muscles as I play... think "soft hand" when approaching hard sections. mouth percussion and kick drum. Of course we put in additional cool effects. For example the U2-style delay on Steve's pizz at the • For a two-minute-edit version, start at measure 109 beginning. (two-minute-edit minus track available at jonschmidt.com). • A recording of the orchestration only (minus piano) is available at jonschmidt.com. This is very fun for live performances with a monitor speaker next to you on stage so you can hear the parts well. Michael meets Mozart = 91 chills up copyright...

Words: 622 - Pages: 3