CSCI 2824: Lecture 28

In 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.


Let G: (V,E) be a directed graph with vertices V and edges E subseteq V times V.

Definition: Walk

A walk over V is a sequence of vertices and edges:

 v_1 xrightarrow{e_1} v_2 xrightarrow{e_2} v_3 xrightarrow{e_3} cdots xrightarrow{e_{n}} v_{n+1}


  1. v_1, v_2,ldots, v_{n+1} are vertices visited during the walk.

  2. e_i: (v_i,v_{i+1}) in E, for i in [1,n].

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 v_i to visit a vertex v_{i+1}.

  • We end after a finite but arbitrary number of steps.


Take the graph:


Here are some walks

  • a (just start and stop at a).

  • a rightarrow d rightarrow b rightarrow c rightarrow d.

  • a rightarrow c rightarrow b rightarrow d rightarrow a.


Simply stated a path between two nodes u and v (where  u not= v ) is a walk that

  • begins at u,

  • ends at v,

  • 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.


Take the graph:


There are two paths between hello and world:

  • hello rightarrow math rightarrow world (path of length 2).

  • hello rightarrow world (path of length 1).

The following are not paths:

  • hello rightarrow hello rightarrow math rightarrow math rightarrow world.

  • hello rightarrow discrete. In the first case, hello (and math) are repeated vertices which is not allowed. In the second case, the path does not end in world, rather it ends in discrete.


Consider the undirected graph G:


There are at least five paths from a to d:

  • a rightarrow d (length is 1).

  • a rightarrow c rightarrow d (length is 2).

  • a rightarrow b rightarrow d (length is 2).

  • a rightarrow c rightarrow b rightarrow d (length is 3).

  • a rightarrow b rightarrow c rightarrow d (path length is 3).

Are there any more paths between a and d?

Note that even though the graph is undirected, the path itself is from a to d. However, since the graph is undirected, we note that any path from a to d may be reversed to immediately yield a path from d to a.

This is not always true in a directed graph. In the previous example there is a path from hello to world but not vice-versa.


A 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

 u rightarrow v_1 rightarrow v_2 cdots rightarrow u

where other than u 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

 v_1 rightarrow v_2 rightarrow cdots rightarrow u rightarrow v_1


Take the graph:


Examples of cycles in this graph include:

  • hello rightarrow hello (self loop = length 1 cycle).

  • math rightarrow math

  • hello rightarrow math rightarrow hello which is the same cycle as math rightarrow hello rightarrow math (the cycle has length 2).


Consider the following undirected graph instead:


Note that hello rightarrow math rightarrow world rightarrow hello is a cycle in this graph of length 3.

The direction of a cycle does not matter in an undirected graph. The same cycle may be written as hello rightarrow world rightarrow math rightarrow hello.

Important Note hello rightarrow discrete rightarrow hello is not considered a cycle in the undirected graph. The reason is that the undirected edge (hello, discrete) and (discrete,hello) are considered the same edge for the undirected graph and is being traversed twice. In other words, a cycle cannot repeat an edge.

Connected Components

We 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 Idea

Let us start with the following example over the set of vertices: {a,b,c,d,e,f,g,h,i}.


The graph has two “islands”: {a,b,c,d} and {e,f,g,h,i}. 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 S: {a,b,c,d} is a connected component of a graph G. 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 S.

By the same token, the set {a,b,c,d,e} is not a connected component. Since there is no path from a to e (since it is an undirected graph, there is no path from e to a either).

Also the set {a,c} is not a connected component since the only possible path in G from a to c has to pass through b which is not included in the set.


Let G: (V,E) be an undirected graph. Let us take a subset of vertices S subseteq V.

Definition: Connected Component

We say that S is a connected component (also often called a strongly connected component) iff the following hold:

  • for any two vertices u,v in S, if u not= v then there is a path in G from u to v

  • the path involves only those vertices that appear in S (in other words, we cannot have the path use a vertex outside the set S).

Definition: Maximal Connected Component

A connected component S is called a maximal connected component if for every vertex v in V setminus S, S cup {v} is not a connected component.

In other words, adding any new vertex to the component destroys its connectivity.


Consider the graph G below:

Set Connected Component? Maximal Connected Component Remarks
{a,b} Yes No The addition of c,d makes it maximal
{e,i,g} No No The path from e to i has to involve either f or h which are both not in the set
{a,b,c,d} Yes Yes Adding any other vertex to the set stops it from being a conn. comp.
{e,f,g,h,i} Yes Yes Again same as previous example
{a,b,c,d,e,f,g,h,i} No No No path from a to e
{a} Yes No By our definition any empty or singleton set is a connected component vacuously

Connected Component for Directed Graphs

For directed graphs, the definition is almost the same. Let G:(V,E) be a directed graph.

A subset S subseteq V of the vertices is a connected component of a directed graph G iff

  • for any pair of vertices (a,b) in S, there is a path from a to b and also a path from b to a.

  • both paths can only involve vertices in the set S.

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 Graphs

Bipartite comes from bi meaning two and partite meaning partitioned into.


A graph G: (V,E) (may be directed or undirected) is bipartite iff the vertex set V can be partitioned into two disjoint parts V_1, V_2 where

  • V_1 cap V_2 = emptyset and V_1 cup V_2 = V, and

  • any edge in the graph goes from a vertex in V_1 to a vertex in V_2 or vice-versa.

In other words, there can be no edges between vertices in V_1 or no edges between vertices in V_2.

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 V_1 and color it red and color the set V_2 green. Likewise, if the graph can be colored using two colors, define V_1 as the red colored nodes and V_2 as the green nodes.


Here is an example bipartite graph G:


The subset V_1 is denoted by red squares V_1 = {v1,v2,v3,v4,v5}. The remaining nodes are in subset V_2 = V - V_1. 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 Cycles

We now state the key results involving bipartite graphs.


A graph G is bipartite if and only if it has no odd length cycles

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 Rightarrow 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:

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.

Not bipartite Rightarrow 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 v_0 and make two sets

 V_1: { v in V | mbox{there exists a path of even length from} v_0 mbox{to} v }


V_2: { v in V | mbox{there exists a path of odd length from} v_0 mbox{to} v }

It is easy to prove that if the graph is bipartite, then V_1 cap V_2= emptyset, and coloring every node in V_1 as 'White’ and coloring every node in V_2 as black will provide a partition of the graph.

Otherwise, if the graph is not bipartite, then V_1 cap V_2 not= empty. Therefore, there exists a node v that is reachable from v_0 by an even length path and an odd length path.

Therefore, there is a walk of odd length starting at v_0 and ending in v_0 obtained by combining the odd and even length paths from v_0 to v.

We can use a well known result that any walk of odd length from v_0 to v_0 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
            return # Nothing else to be done.

    # Mark that node v has been visited

    succColor = complement(colorID)
    # Iterate through all outgoing nodes and
    # color them with opposite (complement) color

    foreach u in adjacency(v):
    	colorNode(G,v, succColor)

# 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)):

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.