The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Tree and Cycle Detection Next: Two-Coloring Graphs Up: Applications of Graph Traversal Previous: Connected Components Tree and Cycle Detection Trees are connected, undirected graphs that do not contain cycles. They are perhaps the simplest interesting class of graphs. Testing whether a graph is a tree is straightforward using depth-first search. During search, every edge will be labeled either a tree edge or a back edge, so the graph is a tree if and only if there are no back edges. Since m=n-1 for any tree, this algorithm can be said to run in time linear in the number of vertices. If the graph is not a tree, it must contain a cycle. Such a cycle can be identified as soon as the first back edge (u,v) is detected. If (u,v) is a back edge, then there must be a path in the tree from v to u. Coupled with edge (u,v), this defines a cycle. Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK2/NODE68.HTM [19/1/2003 1:29:09]

Two-Coloring Graphs Next: Topological Sorting Up: Applications of Graph Traversal Previous: Tree and Cycle Detection Two-Coloring Graphs In vertex coloring, we seek to assign a color to each vertex of a graph G such that no edge links two vertices of the same color. We can avoid all conflicts by assigning each vertex its own color. However, the goal is to use as few colors as possible. Vertex coloring problems arise often in scheduling applications, such as register allocation in compilers. See Section for a full treatment of vertex coloring algorithms and applications. A graph is bipartite if it can be colored without conflicts while using only two colors. Bipartite graphs are important because they arise often in practice and have more structure than arbitrary graphs. For example, consider the ``had-sex-with'' graph in a heterosexual world. Men have sex only with women, and vice versa. Thus gender defines a legal two-coloring. Irrespective of the accuracy of the model, it should be clear that bipartite graphs are simpler to work with than general graphs. But how can we find an appropriate two-coloring of a graph, thus separating the men from the women? Suppose we assume that the starting vertex is male. All vertices adjacent to this man must be female, assuming the graph is indeed bipartite. We can augment either breadth-first or depth-first search so that whenever we discover a new vertex, we color it the opposite of its parent. For each non-discovery edge, we check whether it links two vertices of the same color. Such a conflict means that the graph cannot be two-colored. However, we will have constructed a proper two-coloring whenever we terminate without conflict. We can assign the first vertex in any connected component to be whatever color/sex we wish. Although we can separate the men from the women, we can't tell them apart just using the graph. Next: Topological Sorting Up: Applications of Graph Traversal Previous: Tree and Cycle Detection Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK2/NODE69.HTM [19/1/2003 1:29:09]

Tree and Cycle Detection<br />

Next: Two-Coloring Graphs Up: Applications of Graph Traversal Previous: Connected Components<br />

Tree and Cycle Detection<br />

Trees are connected, undirected graphs that do not contain cycles. <strong>The</strong>y are perhaps the simplest<br />

interesting class of graphs. Testing whether a graph is a tree is straightforward using depth-first search.<br />

During search, every edge will be labeled either a tree edge or a back edge, so the graph is a tree if and<br />

only if there are no back edges. Since m=n-1 for any tree, this algorithm can be said to run in time linear<br />

in the number of vertices.<br />

If the graph is not a tree, it must contain a cycle. Such a cycle can be identified as soon as the first back<br />

edge (u,v) is detected. If (u,v) is a back edge, then there must be a path in the tree from v to u. Coupled<br />

with edge (u,v), this defines a cycle.<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK2/NODE68.HTM [19/1/2003 1:29:09]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!