CSCI 2824: Lecture 28In this lecture, we will look at the following topics:
We will start by revising walks, paths and give examples. WalksLet be a directed graph with vertices and edges . Definition: Walk
A walk over is a sequence of vertices and edges: where
In other words, a walk over a graph is simply a sequence of visits to vertices and edges of the graph wherein
Example-1Take the graph: Here are some walks
PathsSimply stated a path between two nodes and (where ) is a walk that
The length of a path is the number of edges in it. Example-1Take the graph: There are two paths between and :
The following are not paths:
Example-2Consider the undirected graph : There are at least five paths from to :
Are there any more paths between and ? Note that even though the graph is undirected, the path itself is from to . However, since the graph is undirected, we note that any path from to may be reversed to immediately yield a path from to . This is not always true in a directed graph. In the previous example there is a path from to but not vice-versa. CycleA cycle in a graph is a “path” from a vertex back to itself. I place “path” under quotes because we just defined paths so that vertices cannot repeat. Definition: Cycle
Formally, a cycle is a sequence of nodes and edges of the form where other than itself no vertex or edge can repeat. The length of a cycle is the number of edges in it. The choice of a starting point in a cycle is arbitrary in the following sense. We could have written the same cycle above as Example-1Take the graph: Examples of cycles in this graph include:
Example-2Consider the following undirected graph instead: Note that is a cycle in this graph of length . The direction of a cycle does not matter in an undirected graph. The same cycle may be written as . Important Note is not considered a cycle in the undirected graph. The reason is that the undirected edge and are considered the same edge for the undirected graph and is being traversed twice. In other words, a cycle cannot repeat an edge. Connected ComponentsWe will discuss connected components for undirected graphs. The definition for directed graph is possible but is a little harder to understand. Many books also call connected components as strongly connected components. Basic IdeaLet us start with the following example over the set of vertices: . The graph has two “islands”: and . There is no path from any vertex in the second island to the first “island”. The term island is imprecise. The concept of a connected component is useful here. We say that a subset of vertices is a connected component of a graph . I.e., if you choose any pair of vertices in the set then there is a path between them that just involves the vertices in the set . By the same token, the set is not a connected component. Since there is no path from to (since it is an undirected graph, there is no path from to either). Also the set is not a connected component since the only possible path in from to has to pass through which is not included in the set. DefinitionLet be an undirected graph. Let us take a subset of vertices . Definition: Connected Component
We say that is a connected component (also often called a strongly connected component) iff the following hold:
Definition: Maximal Connected Component
A connected component is called a maximal connected component if for every vertex , is not a connected component. In other words, adding any new vertex to the component destroys its connectivity. ExampleConsider the graph below:
Connected Component for Directed GraphsFor directed graphs, the definition is almost the same. Let be a directed graph. A subset of the vertices is a connected component of a directed graph iff
We will not look at this concept any further for now. You will encounter this in your Algorithms class (you will be shown an algorithm to compute the CCs of a graph using depth-first search). Bipartite GraphsBipartite comes from bi meaning two and partite meaning partitioned into. Definition
A graph (may be directed or undirected) is bipartite iff the vertex set can be partitioned into two disjoint parts where
In other words, there can be no edges between vertices in or no edges between vertices in . Often, we think of bipartite graphs as two colorable graphs. I.e, it is possible to assign one of two different colors to the nodes of the graph so that every edge connects nodes of different color (and no edge connects nodes of the same color). It is easy to see that any bipartite graph is two colorable and vice-versa. Simply take the set and color it red and color the set green. Likewise, if the graph can be colored using two colors, define as the red colored nodes and as the green nodes. ExampleHere is an example bipartite graph : The subset is denoted by red squares . The remaining nodes are in subset . Note that any edge goes between these subsets. There are no edges between nodes of the same partition. We can draw the same bipartite graph in a better way to bring out its bipartiteness: Bipartite Graphs and CyclesWe now state the key results involving bipartite graphs. Theorem
A graph is bipartite if and only if it has no odd length cycles The theorem has two parts to it:
Odd Length Cycles Not Bipartite.It is easy to show that a cycle of odd length cannot occur in a bipartite graph. Let us first just take a graph that is itself a single cycle. An interesting pattern emerges:
All odd cycles cannot be two colored, whereas even cycles can. Therefore, we note a key observation. If a graph has an odd cycle someplace inside it, no assignment of two colors will work. This is because the odd cycle cannot be two colored. This proves that a bipartite graph cannot have an odd length cycle inside. Not bipartite odd length cycle.This direction is slightly more complex. Proof sketch
Assume the graph is connected. Otherwise, will prove this separately for each maximally connected component of the graph. Choose an arbitrary start node and make two sets and It is easy to prove that if the graph is bipartite, then , and coloring every node in as 'White’ and coloring every node in as black will provide a partition of the graph. Otherwise, if the graph is not bipartite, then . Therefore, there exists a node that is reachable from by an even length path and an odd length path. Therefore, there is a walk of odd length starting at and ending in obtained by combining the odd and even length paths from to . We can use a well known result that any walk of odd length from to in a graph has an odd length cycle. We will write a simple program that performs a depth-first search, and will try to color the nodes of a graph black and white in an alternating fashion, while performing a depth first search. Pseudo-code to two color a graph G
# Main function that visits a node v and seeks to color it with colorID # It perfoms a depth first search starting from v to color successors with the opposite color. # If it finds a that a node has been colored white and then black, then it reports a CONFLICT def colorNode(G,v,colorID): # Have I already seen node v before? if (alreadyVisitedNode(v)): if (colorNode(v) is not colorID): raise 'CONFLICT' # EXIT: the graph is NOT bipartite else: return # Nothing else to be done. # Mark that node v has been visited markVisited(v) succColor = complement(colorID) # Iterate through all outgoing nodes and # color them with opposite (complement) color foreach u in adjacency(v): colorNode(G,v, succColor) #MAIN LOOP # just start off by coloring every node that has not been # already visited with the White color. # foreach vetex v in G: if (not alreadyVisitedNode(v)): colorNode(G,v,'White') A graph is not bipartite if and only if the function 'colorNode’ returns a 'CONFLICT’. Otherwise, the procedure will color the graph successfully with two colors showing that it is bipartite. You should be able to use your data structures know-how to easily write code that given a graph says whether or not it is bi-partite. |