Approximate Traveling Salesman
Another NP-complete problem that can be solved by a 2-approximation algorithm is traveling
salesman. The traveling salesman problem is given a complete graph with nonnegative weight
edge costs c(u, v), find a minimum weight
Clique
The next NP-complete problem we will explore is a graph problem known as clique.
For an undirected graph G, a clique is a subset of vertices that form a complete
subgraph (i.e. a subset of vertices with every possible edge between all the vertices)
In order to show that a new problem is also NP-complete, we will construct a (polynomial
time) reduction from an instance of a known NP-complete problem (e.g. circuit satisfiability) to
an instance of the new problem. Thus if we could solve the new proble
An augmenting path is a simple path from s t in the residual network. In other
words it is an entire path which can admit additional flow from the source to the sink
(all edges along the path have residual capacity). The residual capacity of an
augmenting
While there is no efficient way of finding the optimal solution to several of them, e.g. traveling
salesman, there are ways to efficiently findapproximate solutions. It is interesting that while we
may not know the optimal value, we can know how far (wors
Often we assume that all (well-posed) problems are computable, even if not efficiently. However
in 1936 Alan Turing proved a problem that is uncomputable known as the Halting Problem.
Halting Problem
Given a description of an algorithm and an initial inpu
Another graph problem that can be shown to be NP-complete is vertex cover. A vertex
cover is a subset of vertices that touch all the edges in an undirected graph.
Given an undirected graph G, find the minimum subset of vertices that touch all the
edges, i
Traveling Salesman
One of the most famous (and most studied) NP-complete problem is known as
the traveling salesman. The name is derived from the problem that a salesman must
visit each of n cities (i.e. make a hamiltonian tour) exactly once using the sho
3-CNF Satisfiability
If the Boolean expression consists of exactly 3 OR'ed variables per clause with all the clauses
being AND'ed, then the expression is said to be in 3-CNF form (conjunctive normal form).
Given a 3-CNF expression, find an assignment of t
NP Completeness
Complexity theory is a field that investigates the run-time of decision problems, i.e. problems
that return true/false as the answer. While many of the problems we have looked at
are optimization problems (e.g.minimum spanning trees, short
In analyzing an algorithm, we are generally interested in its growth as N increases, rather than an
exact number of steps. Big-O refers to characterizing the growth of the exact cost T(n) of an
algorithm in relation to a simpler function f(n). Specificall
The call stack
In order to understand what happens when an exception is thrown we first need to fully examine
how method calls are handled when the program is executed. Each call to a method creates a
new stack frame; the stack frame holds the values of e
A binary search tree is a tree in which each node stores a key/value pair. The keys are ordered,
meaning that for any pair of keys a and b, it is possible to determine whether a<b, a>b, or a=b.
Each node obeys the binary search tree property:
In a binary
General rules:
1. a program statement that is not a method call: 1 step
2. loop executing n iterations: n * m, where m is the cost of a single loop iteration
(NOTE: m may be a function of which loop iteration is being executed)
3. method call: however man
When searching/inserting/removing an element from a hash table, the object's hash code is first
mapped to a hash bucket, which is simply an index into the array used to represent the hash
table. The hash bucket index is found by taking the hash code modul
Deletion
Deleting a key/value pair from the tree is more complicated than inserting. If the deleted key is
in a leaf node of the tree, then there is no problem; we can just delete the node containing the
key.
Deleting a node with a single child is also ea
As a finite series:
1 + 2 + 3 + . + N-2 + N-1
The sum of this series can be found by adding the first and last elements (1+( N-1)
= N), the second and next-to-last elements (2+( N-2) = N), and so forth. There are N/2
such pairs, each of which totals N. So
Recursion works by dividing a large problem into one or more smaller problems, and then
extending the solution to the smaller problem(s) into a solution for the large problem. For this
approach to work, three properties need to hold:
1. The smaller proble
Idealized Hash Table
Before getting into the details of real hash tables, let's first consider an idealized hash table that
has guaranteed worst case running time of O(1) for insert, delete, and find.
All hash tables are based on an object equality predic
Designing a Hash Function
A hash function is a method that inspects the contents of an object and returns an integer value
such that
1. Objects with identical contents will return the same hash code
2. Objects with different contents will, with high proba
A linked list is a data structure for storing a sequence of element values. Each value is stored in
one node in the linked list. The nodes form a chain: each node contains a reference to the node
containing the next element in the list. For a doubly-linke
Bubble sort is an extremely simple sorting algorithm. We will see later in the course that it is not
an efficient sorting algorithm. However, it is easy to understand and implement.
Here is the pseudo-code of bubble sort:
bubbleSort(array) cfw_
for (j = a
In the following method, let the problem size N be rowsCols, which is the number of rows and
columns in the square two-dimensional array matrix.
public static boolean isUpperTriangular(double[][] matrix, int rowsCols) cfw_
for (int j = 1; j < rowsCols; j+
Behind the scenes, Java type parameters work by type erasure. The "real" type of a type
parameter is actually Object: the compiler inserts type casts in the program where necessary to
convert Object references to whatever argument type is appropriate.
Mor
As a concrete example, let's consider an implementation of the generic ArrayList class. An
ArrayList uses an array of references as its internal storage. The methods of the ArrayList class
use this array to store the elements added to the object. When a c
As we've discussed, a collection is an object whose purpose is to store a collection of references
to other objects. Java provides a number of built-in classes to implement collections; we have
used one,ArrayList<E>, already.
Java defines an interface cal
An iterator is a functor whose purpose is to traverse the elements of a collection.
Conceptually, an interface is a "cursor". At any given time, an iterator may be positioned before
any element in the collection, or may be positioned at the end of the col
Today's computers typically have multi-core CPUs. This means that they are able to execute
multiple streams of instructions simultaneously.
We can take advantage of multiple CPU cores by creating threads. A thread is a stream of
instructions executed with
remove(int)
The remove method takes an integer index, and removes the element at that index from the
ArrayList. Any elements to the right of the index must be moved over one position to the left.
The removed element is returned.
Implementation:
public E r