next up previous
Next: About this document Up: My Home Page

lec5-1.au Find the missing integer from 0 to n using O(n) ``is bit[j] in A[i]'' queries. Note - there are a total of tex2html_wrap_inline137 bits, so we are not allowed to read the entire input!  

Also note, the problem is asking us to minimize the number of bits we read. We can spend as much time as we want doing other things provided we don't look at extra bits.

How can we find the last bit of the missing integer?

Ask all the n integers what their last bit is and see whether 0 or 1 is the bit which occurs less often than it is supposed to. That is the last bit of the missing integer!

How can we determine the second-to-last bit?

Ask the tex2html_wrap_inline141 numbers which ended with the correct last bit! By analyzing the bit patterns of the numbers from 0 to n which end with this bit.  

By recurring on the remaining candidate numbers, we get the answer in T(n) = T(n/2) + n =O(n), by the Master Theorem.

lec15-1.au Graphs

A graph G consists of a set of vertices V together with a set E of vertex pairs or edges.    

Graphs are important because any binary relation is a graph, so graphs can be used to represent essentially any relationship.

Example: A network of roads, with cities as vertices and roads between cities as edges.

f233.0in

Example: An electronic circuit, with junctions as vertices as components as edges.

f242.5in

To understand many problems, we must think of them in terms of graphs! lec15-2.au The Friendship Graph

Consider a graph where the vertices are people, and there is an edge between two people if and only if they are friends.  

f252in

This graph is well-defined on any set of people: SUNY SB, New York, or the world.

What questions might we ask about the friendship graph?

lec15-5.au

Data Structures for Graphs

There are two main data structures used to represent graphs.

Adjacency Matrices

An adjacency matrix is an tex2html_wrap_inline165 matrix, where M[i,j] = 0 iff there is no edge from vertex i to vertex j  

f273in

It takes tex2html_wrap_inline173 time to test if (i,j) is in a graph represented by an adjacency matrix.

Can we save space if (1) the graph is undirected? (2) if the graph is sparse? lec15-6.au

Adjacency Lists

An adjacency list consists of a tex2html_wrap_inline177 array of pointers, where the ith element points to a linked list of the edges incident on vertex i.  

f281f282

To test if edge (i,j) is in the graph, we search the ith list for j, which takes tex2html_wrap_inline187 , where tex2html_wrap_inline189 is the degree of the ith vertex.

Note that tex2html_wrap_inline193 can be much less than n when the graph is sparse. If necessary, the two copies of each edge can be linked by a pointer to facilitate deletions. lec15-7.au Tradeoffs Between Adjacency Lists and Adjacency Matrices

tabular89

Both representations are very useful and have different properties, although adjacency lists are probably better for most problems. lec16-2.au Traversing a Graph

One of the most fundamental graph problems is to traverse every edge and vertex in a graph. Applications include:  

For efficiency, we must make sure we visit each edge at most twice.

For correctness, we must do the traversal in a systematic way so that we don't miss anything.

Since a maze is just a graph, such an algorithm must be powerful enough to enable us to get out of an arbitrary maze.   lec16-3.au Marking Vertices

The idea in graph traversal is that we must mark each vertex when we first visit it, and keep track of what have not yet completely explored.

For each vertex, we can maintain two flags:

We must also maintain a structure containing all the vertices we have discovered but not yet completely explored.

Initially, only a single start vertex is considered to be discovered.

To completely explore a vertex, we look at each edge going out of it. For each edge which goes to an undiscovered vertex, we mark it discovered and add it to the list of work to do.

Note that regardless of what order we fetch the next vertex to explore, each edge is considered exactly twice, when each of its endpoints are explored. lec16-4.au Correctness of Graph Traversal

Every edge and vertex in the connected component is eventually visited.

Suppose not, ie. there exists a vertex which was unvisited whose neighbor was visited. This neighbor will eventually be explored so we would visit it:

f292.5in




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Tue Sep 15 16:45:43 EDT 1998