Unit V
Unit V
Graphs
Graph and Graph Models, Graph terminology and special types of graphs
A graph is a mathematical object that is used to model different situations – objects and
processes:
1. Linked list
2. Tree
3. Flowchart of a program
4. Structure chart of a program
5. Finite state automata
6. City map
7. Electric circuits
8. Course curriculum
Definition
A graph is a collection (nonempty set) of vertices and edges
Example:
Vertices: A,B,C,D
Edges: AB, AC, BC, CD
Graph1:
Examples:
Cycles in Graph1: C A B C, C B A C, A B C A, A C B A, B A C B, B C A B
A B A is not a cycle, because the edge A B is the same as B
A Cycles in Graph3: A D A, D A B D
A graph without cycles is called acyclic graph
Loop
An edge that connects the vertex with itself
Connected graphs
Connected graph: There is a path between each two vertices
Graph1, Graph2 and Graph3 are connected graphs.
Disconnected graph: There are at least two vertices not connected by a path.
Examples of disconnected graphs:
Graph4 Graph5
Vertices: A,B,C,D Vertices: A,B,C,D
Edges: AB, CD Edges: AB, AC
Isomorphic graphs
Two graphs which contain the same number of graph vertices connected in the same way are said
to be isomorphic. Formally, two graphs and with graph vertices are said to
be isomorphic if there is a permutation of such that is in the set of graph edges
iff is in the set of graph edges .
Two graphs G1 and G2 are said to be isomorphic if −
Their number of components (vertices and edges) are same.
Their edge connectivity is retained.
Note − In short, out of the two isomorphic graphs, one is a tweaked version of the other. An
unlabelled graph also can be thought of as an isomorphic graph.
There exists a function ‘f’ from vertices of G1 to vertices of G2
[f: V(G1) ⇒ V(G2)], such that
Case (i): f is a bijection (both one-one and onto)
Case (ii): f preserves adjacency of vertices, i.e., if the edge {U, V} ∈ G1, then the edge {f(U),
f(V)} ∈ G2, then G1 ≡ G2.
Note
If G1 ≡ G2 then −
|V(G1)| = |V(G2)|
|E(G1)| = |E(G2)|
Degree sequences of G1 and G2 are same.
If the vertices {V1, V2, .. Vk} form a cycle of length K in G1, then the vertices {f(V1),
f(V2),… f(Vk)} should form a cycle of length K in G2.
All the above conditions are necessary for the graphs G1 and G2 to be isomorphic, but not
sufficient to prove that the graphs are isomorphic.
(G1 ≡ G2) if and only if (G1− ≡ G2−) where G1 and G2 are simple graphs.
(G1 ≡ G2) if the adjacency matrices of G1 and G2 are same.
(G1 ≡ G2) if and only if the corresponding subgraphs of G1 and G2(obtained by deleting
some vertices in G1 and their images in graph G2) are isomorphic.
Example
Which of the following graphs are isomorphic?
In the graph G3, vertex ‘w’ has only degree 3, whereas all the other graph vertices has degree 2.
Hence G3 not isomorphic to G1 or G2.
Taking complements of G1 and G2, you have −
Here, (G1− ≡ G2−), hence (G1 ≡ G2).
Connectivity
Euler graph
A closed walk in a graph G containing all the edges of G is called an Euler line in G. A graph
containing an Euler line is called an Euler graph. We know that a walk is always connected.
Since the Euler line (which is a walk) contains all the edges of the graph, an Euler graph is
connected except for any isolated vertices the graph may contain. As isolated vertices do not
contribute anything to the understanding of an Euler graph, it is assumed now onwards that Euler
graphs do not have any isolated vertices and are thus connected.
Example Consider the graph shown in Figure. Clearly, v1 e1 v2 e2 v3 e3 v4 e4 v5 e5 v3 v6 e7 v1
in (a) is an Euler line, whereas the graph shown in (b) is non-Eulerian.
Hamiltonian graphs
A cycle passing through all the vertices of a graph is called a Hamiltonian cycle. A graph
containing a Hamiltonian cycle is called a Hamiltonian graph. A path passing through all the
vertices of a graph is called a Hamiltonian path and a graph containing a Hamiltonian path is said
to be traceable. Examples of Hamiltonian graphs are given in Figure.
If the last edge of a Hamiltonian cycle is dropped, we get a Hamiltonian path. However, a non-
Hamiltonian graph can have a Hamiltonian path, that is, Hamiltonian paths cannot always be
used to form Hamiltonian cycles. For example, in Figure, G1 has no Hamiltonian path, and so no
Hamiltonian cycle; G2 has the Hamiltonian path v1v2v3v4, but has no Hamiltonian cycle, while
G3 has the Hamiltonian cycle v1v2v3v4v1.
A multigraph or general graph is Hamiltonian if and only if its underlying graph is Hamiltonian,
because if G is Hamiltonian, then any Hamiltonian cycle in G remains a Hamiltonian cycle in the
underlying graph of G. Conversely, if the underlying graph of a graph G is Hamiltonian, then G
is also Hamiltonian.
Let G be a graph with n vertices. Clearly, G is a subgraph of the complete graph Kn. From G, we
construct step by step supergraphs of G to get Kn, by adding an edge at each step between two
vertices that are not already adjacent.
Now, let us start with a graph G which is not Hamiltonian. Since the final outcome of the
procedure is the Hamiltonian graph Kn, we change from a non-Hamiltonian graph to a
Hamiltonian graph at some stage of the procedure. For example, the non-Hamiltonian graph G1
above is followed by the Hamiltonian graph G2. Since supergraphs of Hamiltonian graphs are
Hamiltonian, once a Hamiltonian graph is reached in the procedure, all the subsequent
supergraphs are Hamiltonian.
Planar graph
A planar graph is an undirected graph that can be drawn on a plane without any edges crossing.
Such a drawing is called a planar representation of the graph in the plane. Ex : K4 is a planar
graph
Q3 is a planar graph
K1,n and K2,n are planar graphs for all n
Ex : 2 regions
Euler’s Planar Formula
Let G be a connected planar graph, and consider a planar representation of G.
Let V = # vertices, E = # edges, F = # regions.
Theorem : V + F = E + 2.
Graph Coloring
Graph coloring is the procedure of assignment of colors to each vertex of a graph G such that no
adjacent vertices get same color. The objective is to minimize the number of colors while
coloring a graph. The smallest number of colors required to color a graph G is called its
chromatic number of that graph. Graph coloring problem is a NP Complete problem.
Method to Color a Graph
The steps required to color a graph G with n number of vertices are as follows −
Step 1 − Arrange the vertices of the graph in some order.
Step 2 − Choose the first vertex and color it with the first color.
Step 3 − Choose the next vertex and color it with the lowest numbered color that has not been
colored on any vertices adjacent to it. If all the adjacent vertices are colored with this color,
assign a new color to it. Repeat this step until all the vertices are colored.
Example
In the above figure, at first vertex aa is colored red. As the adjacent vertices of vertex a are again
adjacent, vertex bb and vertex dd are colored with different color, green and blue respectively.
Then vertex cc is colored as red as no adjacent vertex of cc is colored red. Hence, we could color
the graph by 3 colors. Hence, the chromatic number of the graph is 3.
Applications of Graph Coloring
Some applications of graph coloring include −
Register Allocation
Map Coloring
BFS Algorithm
The concept is to visit all the neighbor vertices before visiting other neighbor vertices of
neighbor vertices.
Initialize status of all nodes as “Ready”.
o Remove the first vertex from the queue and mark it as “Visited”.
o Add to the rear of queue all neighbors of the removed vertex whose status is
“Ready”. Mark their status as “Waiting”.
Problem
Let us take a graph (Source vertex is ‘a’) and apply the BFS algorithm to find out the traversal
order.
Solution −
Initialize status of all vertices to “Ready”.
Add a’s neighbors in “Ready” state b, d and e to end of queue and mark them as
“Waiting”.
Remove b from queue, mark it as “Visited”, put its “Ready” neighbor cat end of queue
and mark c as “Waiting”.
Remove d from queue and mark it as “Visited”. It has no neighbor in “Ready” state.
Remove e from queue and mark it as “Visited”. It has no neighbor in “Ready” state.
Remove c from queue and mark it as “Visited”. It has no neighbor in “Ready” state.
Complexity Analysis
Let G(V,E)G(V,E) be a graph with |V||V| number of vertices and |E||E| number of edges. If
breadth first search algorithm visits every vertex in the graph and checks every edge, then its
time complexity would be −
O(|V|+|E|).O(|E|)O(|V|+|E|).O(|E|)
It may vary between O(1)O(1) and O(|V2|)O(|V2|)
Depth First Search
Depth First Search (DFS) algorithm starts from a vertex vv, then it traverses to its adjacent vertex
(say x) that has not been visited before and marks as "visited" and goes on with the adjacent
vertex of xx and so on.
If at any vertex, it encounters that all the adjacent vertices are visited, then it backtracks until it
finds the first vertex having an adjacent vertex that has not been traversed before. Then, it
traverses that vertex, continues with its adjacent vertices until it traverses all visited vertices and
has to backtrack again. In this way, it will traverse all the vertices reachable from the initial
vertex vv.
DFS Algorithm
The concept is to visit all the neighbor vertices of a neighbor vertex before visiting the other
neighbor vertices.
Initialize status of all nodes as “Ready”
o Pop the top vertex from the stack and mark it as “Visited”
o Push onto the top of the stack all neighbors of the removed vertex whose status is
“Ready”. Mark their status as “Waiting”.
Problem
Let us take a graph (Source vertex is ‘a’) and apply the DFS algorithm to find out the traversal
order.
Solution
Initialize status of all vertices to “Ready”.
Push a’s neighbors in “Ready” state e, d and b to top of stack and mark them as
“Waiting”.
Pop b from stack, mark it as “Visited”, push its “Ready” neighbor conto stack.
Digraphs
A graph in which each graph edge is replaced by a directed graph edge, also called a digraph. A
directed graph having no multiple edges or loops (corresponding to a binary adjacency
matrix with 0s on the diagonal) is called a simple directed graph. A complete graph in which
each edge is bidirected is called a complete directed graph. A directed graph having no
symmetric pair of directed edges (i.e., no bidirected edges) is called an oriented graph. A
complete oriented graph (i.e., a directed graph in which each pair of nodes is joined by a single
edge having a unique direction) is called a tournament.
If is an undirected connected graph, then one can always direct the circuit graph
edges of and leave the separating edges undirected so that there is a directed path from any
node to another. Such a graph is said to be transitive if the adjacency relation is transitive.
When drawing a directed graph, the edges are typically drawn as arrows indicating the direction,
as illustrated in the following figure.
Edges are the connections between the nodes. An edge connects two nodes. They are
usually represented by lines, or lines with arrows.
This graph (the thick black line) is acyclic, as it has no cycles (complete circuits).
A connected acyclic graph, like the one above, is called a tree. If one or more of the tree
“branches” is disconnected, the acyclic graph is a called a forest.
A directed acyclic graph is an acyclic graph that has a direction as well as a lack of cycles.
Edge set = {(1,2), (1,3), (2,4), (2,5), (3,6), (4,7), (5,7), (6,7)}.
A directed acyclic graph has a topological ordering. This means that the nodes are ordered so
that the starting node has a lower value than the ending node. A DAG has a unique topological
ordering if it has a directed path containing all the nodes; in this case the ordering is the same as
the order in which the nodes appear in the path.
In computer science, DAGs are also called wait-for-graphs. When a DAG is used to detect a
deadlock, it illustrates that a resources has to wait for another process to continue.
Weighted digraphs
We can assign numbers to the edges or vertices of a graph in order to enable them to be used in
physical problems. Such an assignment is called the weight of the edges or vertices.
Weighted graphs are defined as the quadruples (V, E, f, g) or the triplets (V, E, f)or the triplets (V,
E, g), where V is the set of vertices, E is the set of domains, f is the function with domain V,
which assigns weights to vertices and g is the function with domain E, which assigns weights to
edges
Example
Following diagram is a weighted digraph which represents the communication network among
five individuals v1,v2,v3,v4,v5. The number assigned for each directed edge gives the
probability of their communication.
Planarity – “A graph is said to be planar if it can be drawn on a plane without any edges
crossing. Such a drawing is called a planar representation of the graph.”
Important Note – A graph may be planar even if it is drawn with crossings, because it may be
possible to draw it in a different way without crossings.
For example consider the complete graph k4 and its two possible planar representations –
There are a total of 6 regions with 5 bounded regions and 1 bounded region R6.
All the planar representations of a graph split the plane in the same number of regions. Euler
found out the number of regions in a planar graph as a function of the number of vertices and
number of edges in the graph.
Theorem – “Let G be a connected simple planar graph with e edges and v vertices. Then the
number of regions r in the graph is equal to e-v+2.”
Example – What is the number of regions in a connected planar simple graph with 20
vertices each with a degree of 3?
Solution – Number of vertices and edges in K5 is 5 and 10 respectively. Since 10 > 3*5 –
6, 10 > 9 the inequality e<=3v-6 is not satisfied. Thus the graph is not planar.
In graph theory, graph coloring is a special case of graph labeling; it is an assignment of labels
traditionally called "colors" to elements of a graph subject to certain constraints. In its simplest
form, it is a way of coloring the vertices of a graph such that no two adjacent vertices share the
same color; this is called a vertex coloring. Similarly, an edge coloring assigns a color to each
edge so that no two adjacent edges share the same color, and a face coloring of a planar graph
assigns a color to each face or region so that no two faces that share a boundary have the same
color.
Vertex coloring is the starting point of the subject, and other coloring problems can be
transformed into a vertex version. For example, an edge coloring of a graph is just a vertex
coloring of its line graph, and a face coloring of a planar graph is just a vertex coloring of its
planar dual. However, non-vertex coloring problems are often stated and studied as is. That is
partly for perspective, and partly because some problems are best studied in non-vertex form, as
for instance is edge coloring.
The convention of using colors originates from coloring the countries of a map, where each face
is literally colored. This was generalized to coloring the faces of a graph embedded in the plane.
By planar duality it became coloring the vertices, and in this form it generalizes to all graphs. In
mathematical and computer representations it is typical to use the first few positive or
nonnegative integers as the "colors". In general one can use any finite set as the "color set". The
nature of the coloring problem depends on the number of colors but not on what they are.
Graph coloring enjoys many practical applications as well as theoretical challenges. Beside the
classical types of problems, different limitations can also be set on the graph, or on the way a
color is assigned, or even on the color itself. It has even reached popularity with the general
public in the form of the popular number puzzle Sudoku. Graph coloring is still a very active
field of research.
A proper vertex coloring of the Petersen graph with 3 colors, the
minimum number possible
Vertex coloring
When used without any qualification, a coloring of a graph is almost always a proper vertex
coloring, namely a labelling of the graph’s vertices with colors such that no two vertices sharing
the same edge have the same color. Since a vertex with a loop could never be properly colored, it
is understood that graphs in this context are loopless.
The terminology of using colors for vertex labels goes back to map coloring. Labels like red and
blue are only used when the number of colors is small, and normally it is understood that the
labels are drawn from the integers {1,2,3,...}.
A coloring using at most k colors is called a (proper) k-coloring. The smallest number of colors
needed to color a graph G is called its chromatic number, χ(G). A graph that can be assigned a
(proper) k-coloring is k-colorable, and it is k-chromatic if its chromatic number is exactly k. A
subset of vertices assigned to the same color is called a color class, every such class forms an
independent set. Thus, a k-coloring is the same as a partition of the vertex set into k independent
sets, and the terms k-partite and k-colorable have the same meaning.
The following table gives the chromatic number for familiar classes of graphs.
Note also, that the graph does not specify the order of the children of a given node.
A spanning tree of a graph
A spanning tree of an undirected graph is a subgraph that contains all the vertices, and no cycles.
If we add any edge to the spanning tree, it forms a cycle, and the tree becomes a graph.
It is possible to define a spanning tree for directed graphs, however the definition is rather
complicated and will not be discussed here.
Example:
Spanning trees for Graph1:
Vertices: A,B,C,D
Edges: AB, AC, BD,
CD
A B C D
A 0 1 1 0
B 1 0 0 1
C 1 0 0 1
D 0 1 1 0
Note: Depending on the application sometimes the diagonal is set to 1
For not directed graphs the matrix is symmetrical. Depending on the application only half of the
matrix may be used.
b. Adjacency list structure
Each vertex is associated with a list, that holds all adjacent vertices.
A: B, C
B: A, D
C: A, D
D: B, C
Other information, as weights, labels, names, can be represented using auxiliary arrays.
Tree Traversal Spanning Trees:
In the mathematical field of graph theory, a spanning tree T of a connected, undirected graph G
is a tree composed of all the vertices and some (or perhaps all) of the edges of G. Informally, a
spanning tree of G is a selection of edges of G that form a tree spanning every vertex. That is,
every vertex lies in the tree, but no cycles (or loops) are formed. On the other hand, every bridge
of G must belong to T.
A spanning tree of a connected graph G can also be defined as a maximal set of edges of G that
contains no cycle, or as a minimal set of edges that connect all vertices.
Example:
4. Spanning trees are all minimally connected. That is, if any one edge is removed, the
spanning tree will no longer be connected.
5. Adding any edge to the spanning tree will create a cycle. So, a spanning tree is
maximally acyclic.
6. Spanning trees have |n|-1 edges, where |n| is the number of vertices
Kruskal's Algorithm
Kruskal's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected
weighted graph. It finds a tree of that graph which includes every vertex and the total weight of
all the edges in the tree is less than or equal to every possible spanning tree.
Algorithm
Step 1 − Arrange all the edges of the given graph G(V,E)G(V,E) in non-decreasing order as per
their edge weight.
Step 2 − Choose the smallest weighted edge from the graph and check if it forms a cycle with the
spanning tree formed so far.
Step 3 − If there is no cycle, include this edge to the spanning tree else discard it.
Step 4 − Repeat Step 2 and Step 3 until (V−1)(V−1) number of edges are left in the spanning
tree.
Problem
Suppose we want to find minimum spanning tree for the following graph G using Kruskal’s
algorithm.
Solution
From the above graph we construct the following table −
Edge No. Vertex Pair Edge Weight
E1 (a, b) 20
E2 (a, c) 9
E3 (a, d) 13
E4 (b, c) 1
E5 (b, e) 4
E6 (b, f) 5
E7 (c, d) 2
E8 (d, e) 3
E9 (d, f) 14
Now we will rearrange the table in ascending order with respect to Edge weight −
Edge No. Vertex Pair Edge Weight
E4 (b, c) 1
E7 (c, d) 2
E8 (d, e) 3
E5 (b, e) 4
E6 (b, f) 5
E2 (a, c) 9
E3 (a, d) 13
E9 (d, f) 14
E1 (a, b) 20
Since we got all the 5 edges in the last figure, we stop the algorithm and this is the minimal
spanning tree and its total weight is (1+2+3+5+9)=20(1+2+3+5+9)=20.
Prim's Algorithm
Prim's algorithm, discovered in 1930 by mathematicians, Vojtech Jarnik and Robert C. Prim, is a
greedy algorithm that finds a minimum spanning tree for a connected weighted graph. It finds a
tree of that graph which includes every vertex and the total weight of all the edges in the tree is
less than or equal to every possible spanning tree. Prim’s algorithm is faster on dense graphs.
Algorithm
Initialize the minimal spanning tree with a single vertex, randomly chosen from the
graph.
Repeat steps 3 and 4 until all the vertices are included in the tree.
Select an edge that connects the tree with a vertex not yet in the tree, so that the weight of
the edge is minimal and inclusion of the edge does not form a cycle.
Add the selected edge and the vertex that it connects to the tree.
Problem
Suppose we want to find minimum spanning tree for the following graph G using Prim’s
algorithm.
Solution
Here we start with the vertex ‘a’ and proceed.