## CSCI 2824: Lecture 28In this lecture, we will look at the following topics: Walks, Paths, and Cycles (revision} Connectedness and Connected Components. Bipartite Graphs. Colorability of Graphs.
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 are vertices visited during the walk. , for .
In other words, a walk over a graph is simply a sequence of visits to vertices and edges of the graph wherein We may start anywhere in the graph. A single step of the walk consists of taking an outgoing edge from the current vertex to visit a vertex . We end after a finite but arbitrary number of steps.
## Example-1Take the graph: Here are some walks (just start and stop at ). . .
## PathsSimply stated a path between two nodes and (where ) is a walk that begins at , ends at , No vertex is repeated, i.e, each vertex is visited at most once.
The length of a path is the number of edges in it. ## Example-1Take the graph: There are two paths between and : (path of length 2). (path of length 1).
The following are not paths: . . In the first case, (and ) are repeated vertices which is not allowed. In the second case, the path does not end in , rather it ends in .
## Example-2Consider the undirected graph : There are at least five paths from to : (length is 1). (length is 2). (length is 2). (length is 3). (path length is 3).
Are there any more paths between and ? Note that even though the graph is undirected, the path itself is 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: (self loop = length 1 cycle). which is the same cycle as (the cycle has length 2).
## 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 .
## Connected ComponentsWe will discuss connected components for Many books also call connected components as ## 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: for any two vertices , if then there is a path in from to the path involves only those vertices that appear in (in other words, we cannot have the path use a vertex outside the set ).
Definition: Maximal Connected Component
A connected component is called a 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 for any pair of vertices , there is a path from to and also a path from to . both paths can only involve vertices in the set .
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 Definition
A graph (may be directed or undirected) is bipartite iff the vertex set can be partitioned into two disjoint parts where and , and any edge in the graph goes from a vertex in to a vertex in or vice-versa.
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 The theorem has two parts to it: Any graph with an odd length cycle **cannot**be bipartite.Any graph that does not have odd length cycles must be bipartite.
## 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 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. |