CSCI 2824: Lecture 28In this lecture, we will look at the following topics:
We will start by revising walks, paths and give examples. WalksLet Definition: Walk
A walk over 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
The length of a path is the number of edges in it. Example-1Take the graph: ![]() There are two paths between
The following are not paths:
Example-2Consider the undirected graph ![]() There are at least five paths from
Are there any more paths between Note that even though the graph is undirected, the path itself is from This is not always true in a directed graph. In the previous example there is a path from 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 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 The direction of a cycle does not matter in an undirected graph. The same cycle may be written as
Important Note 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”: We say that a subset of vertices By the same token, the set Also the set DefinitionLet Definition: Connected Component
We say that
Definition: Maximal Connected Component
A connected component In other words, adding any new vertex to the component destroys its connectivity. ExampleConsider the graph ![]()
Connected Component for Directed GraphsFor directed graphs, the definition is almost the same.
Let A subset
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
In other words, there can be 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 ExampleHere is an example bipartite graph ![]() The subset ![]() Bipartite Graphs and CyclesWe now state the key results involving bipartite graphs. Theorem
A graph The theorem has two parts to it:
Odd Length Cycles
|
n | Bipartite? |
2 | Yes |
3 | No |
4 | Yes |
5 | No |
6 | Yes |
7 | No |
2k | Yes |
2k+1 | No |
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.
This direction is slightly more complex.
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.
# 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.