0% found this document useful (0 votes)
3 views

Graph theory_notes after mst

The document discusses various graph algorithms including Dijkstra's algorithm for finding the shortest paths in weighted graphs, the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices, and methods for finding minimum spanning trees using Prim's and Kruskal's algorithms. It also covers graph traversal techniques like Depth-First Search (DFS) and Breadth-First Search (BFS), and introduces concepts related to planar graphs and Euler's formula. Additionally, it provides examples and step-by-step execution of these algorithms.

Uploaded by

jaggarwal1be22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Graph theory_notes after mst

The document discusses various graph algorithms including Dijkstra's algorithm for finding the shortest paths in weighted graphs, the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices, and methods for finding minimum spanning trees using Prim's and Kruskal's algorithms. It also covers graph traversal techniques like Depth-First Search (DFS) and Breadth-First Search (BFS), and introduces concepts related to planar graphs and Euler's formula. Additionally, it provides examples and step-by-step execution of these algorithms.

Uploaded by

jaggarwal1be22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

28 M.

RANA AND ARUN MAITI

7. Graph Algorithms (Minimum path, Minimum spanning tree (MST),


Network, Flow Problems)
7.1. Dijkstra Algorith. Dijkstra’s algorithm for finding the shortest paths between vertices
in a weighted graph. It was conceived by W. Dijkstra in 1956.
It can also be used to find the shortest path to a specific destination vertices, by terminating
the algorithm once the shortest path to the destination node is known. Given a weighted
graph and a source vertex in the graph, find the shortest paths from the source to all the
other vertices in the given graph.
Description of the Algorithm :
Create a set spSet (shortest path tree set) that keeps track of vertices included in the shortest
path tree, i.e., whose minimum distance from the source is calculated and finalized. Initially,
this set is empty. Assign a distance value to all vertices in the input graph. Initialize all
distance values as infinite . Assign the distance value as 0 for the source vertex so that it
is picked first. While sptSet doesn’t include all vertices Pick a vertex u that is not there in
sptSet and has a minimum distance value. Include u to sptSet . Then update the distance
value of all adjacent vertices of u . To update the distance values, iterate through all adjacent
vertices. For every adjacent vertex v, if the sum of the distance value of u (from source) and
weight of edge u-v , is less than the distance value of v , then update the distance value of v.
Note: The algorithm is applicable to only the graphs that does not contain any edge with
negative weight.
Visualisation: click here

7.2. Example. The following graph contains 9 vertices (0 to 8) with weighted edges as
shown in the figure below. We will be using Dijkstra’s algorithm to find the shortest path
from vertex "0" to vertex "4".
8 7
1 2 3
9
4 2
11 14
0 8 4 4
8
7 7 6 10
1 6 2 5

Step-by-Step Execution:
We initialize the distances to all vertices as infinity, except for the source vertex (0), which
is set to 0. The table below shows the progress of the algorithm at each step.
Initial Setup
• Source: Vertex 0
• Distances: dist(0) = 0, all other vertices set to 1.
• Unvisited Set: {0, 1, 2, 3, 4, 5, 6, 7, 8}
Step 1: Visit Vertex 0
• Neighbors: {1, 7}
• Update distances:
dist(1) = min(1, 0 + 4) = 4
dist(7) = min(1, 0 + 8) = 8
GRAPH THEORY LECTURE NOTES 29

Unvisited Set: {1, 2, 3, 4, 5, 6, 7, 8}


Step 2: Visit Vertex 1
• Current vertex: 1 (minimum distance = 4)
• Neighbors: {0, 2, 7}
• Update distances:
dist(2) = min(1, 4 + 8) = 12
dist(7) = min(8, 4 + 11) = 8
Unvisited Set: {2, 3, 4, 5, 6, 7, 8}
Step 3: Visit Vertex 7
• Current vertex: 7 (minimum distance = 8)
• Neighbors: {0, 1, 6, 8}
• Update distances:
dist(6) = min(1, 8 + 1) = 9
dist(8) = min(1, 8 + 7) = 15
Unvisited Set: {2, 3, 4, 5, 6, 8}
Step 4: Visit Vertex 6
• Current vertex: 6 (minimum distance = 9)
• Neighbors: {5, 7, 8}
• Update distances:
dist(5) = min(1, 9 + 2) = 11
dist(8) = min(15, 8 + 6) = 15
Unvisited Set: {2, 3, 4, 5, 8}
Step 5: Visit Vertex 5
• Current vertex: 5 (minimum distance = 11)
• Neighbors: {2, 3, 4, 6}
• Update distances:
dist(2) = min(1, 11 + 1) = 12
dist(3) = min(1, 11 + 14) = 25
dist(4) = min(1, 11 + 10) = 21
dist(4) = min(15, 11 + 6) = 15
Unvisited Set: {2, 3, 4, 8}
Step 6: Visit Vertex 2
• Current vertex: 2 (minimum distance = 12)
• Neighbors: {1, 3, 5, 8}
• Update distances:
dist(3) = min(1, 12 + 7) = 19
Unvisited Set: {3, 4, 8}
Step 7: Visit Vertex 3
• Current vertex: 3 (minimum distance = 19)
• Neighbors: {2, 4, 5}
• Update distances:
dist(4) = min(21, 19 + 9) = 21
30 M. RANA AND ARUN MAITI

Unvisited Set: {4, 8}


Step 8: Visit Vertex 4
• Current vertex: 4 (minimum distance = 21)
• Neighbors: {3, 5}
The destination vertex "4" has been reached. Hence the distance from vertex 0 to vertex 4
is 21. that the shortest path from vertex 0 to vertex 4 is 0 ! 7 ! 6 ! 5 ! 2 ! 3 ! 4 and
7.3. Floyd-Warshall Algorithm. It is an algorithm for finding shortest paths in a directed
weighted graph. There can be edges with positive or negative weights but no negative cycles.
The algorithm finds the lengths (summed weights) of shortest paths between all pairs of
vertices. Although it does not directly gives the details of the paths themselves, it is possible
to reconstruct the paths with simple modifications to the algorithm.
Description: Given a a directed weighted graph (equivalently its adjacency matrix), initialize
the solution matrix same as the given adjacency matrix as a first step. Then update the
solution matrix by considering all vertices as an intermediate vertex. The idea is to pick
all vertices one by one and updates all shortest paths which include the picked vertex as an
intermediate vertex in the shortest path. When we pick vertex number k as an intermediate
vertex, we already have considered vertices {0, 1, 2, · · · , k 1} as intermediate vertices. For
every pair (i, j) of the source and destination vertices respectively, there are two possible
cases. k is not an intermediate vertex in shortest path from i to j. We keep the value of
dist[i][j] as it is. k is an intermediate vertex in shortest path from i to j. We update the value
of dist[i][j] as dist[i][k] + dist[k][j], if dist[i][j]> dist[i][k] + dist[k][j]
Example: Let G be the weighted graph given on the left of the following figure.

k = 0: k = 1: k = 4:
2 4 2 2 1
1 3 2 1 3 3 4 2
4 2 1 4
2 1 k = 2: 3 4 2 1
1 3
1 4
4 2
2 3 4 2 1 2 2 1
3 1 3 4 2
2 3 2 1 4 2
3 4 4 2 1 3
1 2
4 1 k = 3:
4 2 2 2
1 3 4
G 4 2 2
2 1 3 4

The distance matrix at each iteration of k, with the updated distances in bold is shown
below.

Matrix for k=0


A B C D
A 0 1 2 1
B 4 0 3 1
C 1 1 0 2
D 1 1 1 0
GRAPH THEORY LECTURE NOTES 31

Matrix for k = 1
A B C D
A 0 1 2 1
B 4 0 2 1
C 1 1 0 2
D 1 1 1 0

Matrix for k = 2
A B C D
A 0 1 2 1
B 4 0 2 1
C 1 1 0 2
D 3 1 1 0

Matrix for k = 3
A B C D
A 0 1 2 0
B 4 0 2 4
C 1 1 0 2
D 3 1 1 0

Matrix for k = 4
A B C D
A 0 1 2 0
B 4 0 2 4
C 5 1 0 2
D 3 1 1 0
Comparison: For graphs with non-negative edge weights, Dijkstra’s algorithm can be
used to find all shortest paths from a single vertex with running time ⇥(|E| + |V | log |V |).
Thus, running Dijkstra starting at each vertex takes time ⇥(|E||V | + |V |2 log |V |). Since
|E| = O(|V |2 ), this yields a worst-case running time of repeated Dijkstra of O(|V |3 ). While
this matches the asymptotic worst-case running time of the Floyd-Warshall algorithm, the
constants involved matter quite a lot. When a graph is dense (i.e., |E| ⇡ |V |2 ), the Floyd-
Warshall algorithm tends to perform better in practice. When the graph is sparse (i.e., |E|
is significantly smaller than |V |2 ), Dijkstra tends to dominate.

7.4. DFS and BFS. Breadth-First Search (BFS) and Depth-First Search (DFS) are two
fundamental algorithms used for traversing or searching graphs and trees.
DFS The algorithm starts at the root node and explores as far as possible along each
branch before backtracking.
Breath-first search (BFS) It starts at the tree root and explores all nodes at the present
depth prior to moving on to the nodes at the next depth level.
32 M. RANA AND ARUN MAITI

Fig. 7.1. Comparison between BFS and DFS

7.5. Algorithms to find minimum spanning tree. Minimum spanning tree is a spanning
tree whose sum of edge weights is the smallest possible. Prim’s algorithm is a greedy algo-
rithm that finds a minimum spanning tree for a weighted undirected graph. The algorithm
for MST is as follows :
Step 1. Intialize with an arbitrary vertex.
Step 2. Perform the following steps till there are vertices that are not included in the MST
. This vertices are known as fringe vertex
Step 3. Find edges connecting any tree vertex with the fringe vertices and find the minimum
among them.
Step 4. Add the chosen edge to the MST if it does not form any cycle.

Visualisation: click here


Kruskal’s Algorithm:
Step 1.Sort all the edges in non-decreasing order of their weight.
Step 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so
far. If the cycle is not formed, include this edge. Else, discard it.
Step 3. Repeat step 2. until there are edges in the spanning tree.
Visualisation: click here

8. Planar graph
A graph that can be drawn in the plane without edges crossing is planar. A graph drawn
in the plane is a plane graph. A face or region of a plane graph is a connected region of
R2 \ G. We define the size of a region to be the number of edges it has.
The set of regions of a planer graph G is denoted by R(G).
Consider the following graph:
GRAPH THEORY LECTURE NOTES 33

D C

A B

It is clearly planar. It has five vertices, eight edges: AB, BC, CD, DA, AE, BE, CE, and
DE. There are 5 regions in total: Face 1: The outer unbounded region around the entire
graph, Face 2: The triangle ABE, Face 3: The triangle BCE, Face 4: The triangle CDE,
Face 5: The triangle DAE.
Theorem 8.1 (Euler’s Formula). For a connected planar graph G,
|V (G)| |E(G)| + |R(G)| = 2
Proof. Proof by induction on the number of edges.
The statement is clearly true for a graph with 1 edges.
Given a graph G with n+1 edges. We can remove its one edge say e. By induction hypothesis
|V (G \ e)| |E(G \ e)| + |R(G \ e)| = 2
It is easy to see that |V (G \ e)| = |V (G)|, |E(G \ e)| = |E(G)| 1 and |R(G \ e)| = |R(G)| 1.
Substituting the values in the above equality, we obtain
|V (G)| |E(G)| + |R(G)| = 2
This completes the induction, hence the proof??

The following result gives criteria to verify planarity of graphs.
Theorem 8.2. For a planar graph G, |E(G)| 3|G| 3. Furthermore, if there is no triangle
in G, then |E(G)| 2|G| 4.
Let us denote |E(G)| = e and |G| = n. Let R(G) = {R1 , R2 , · · · , Rr } denote all the regions
of G. We know that for a planar graph
deg(Ri ) 3
This implies X
deg(Ri ) 3m (1)
By handshake lemma for planar graph
X
deg(Ri ) = 2e (2)
From (1) and (2), we get
3r  2e. (3)
From Euler’s formula, we know that
r=e n+2
Using (3) we get
2
=) (e n + 2)  e
3
=) e  3n 6 (4)
34 M. RANA AND ARUN MAITI

The complete graph K4 on 4 vertices is planar and can be drawn without edge crossings,
as shown below.
2

1 3

4
A subdivision of a graph is formed by inserting vertices along its edges. For example,
subdividing an edge (u, v) by adding a vertex w gives two edges (u, w) and (w, v).
Consider the complete graph K3 and it’s subdivision by adding one vertex in the middle
of each edge of K3 . This transforms each edge into two edges.
C

A B

F E

A D B

Subdivision does not reduce the inherent "complexity" of a graph. Any attempted planar
embedding of a subdivision of a graph would still require the same non-crossing requirements
as the original. Adding vertices along edges does not create additional faces or change the
essential connectivity of the graph.
Theorem 8.3. A graph G is planar if and only if G has a subgraph that is a subdivision (or
homemorphic) of K3,3 or K5 .
"Only if " part only. . To show t K5 , K3,3 are not planar, we can use Theorem 8.3. ⇤

8.1. Practice problem. Check which of these graphs are planar.

9. Graph Coloring
In this notes coloring of a graph will always refer to a proper vertex coloring, namely a
labeling of the graph’s vertices with colors such that no two adjacent vertices 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 smallest number of colors needed to color a graph G is called its chromatic number,
and is denoted by (G) (often denoted by (G) as well).
GRAPH THEORY LECTURE NOTES 35

Example 9.1. We will color the states of India (or any other country) with minimum number
of colors so that no two neghbouring states have the same color. For that we can first construct
the dual graph, and then use graph coloring algorithms described further below. The following
images show states of India can be colored using only 4 colors. Can you do it with lesser?

Fig. 9.1. States of India and the dual graph

The following observations are the immediate consequences of the above definitions.
1. A graph is 1-chromatic ( (G) = 1) if and only if it is a null graph.
2. A graph having at least one edge is at least 2-chromatic.
3. A graph G having n vertices has (G)  n.
4. If H is subgraph of a graph G, then (H)  (G).
5. A complete graph with n vertices is n-chromatic
6. A cycle of length n 3 is 2-chromatic if n is even and 3-chromatic if n is odd.

Theorem 9.2. Every tree with n 2 vertices is bicolorable (2-chromatic or (G) = 2).
Proof. Let T be a tree with n 2 vertices. Consider any vertex v of T and assume T to be
rooted at vertex v . Assign color 1 to v. Then assign color 2 to all vertices which are adjacent
to v. Let v1 , v2 , · · · , vr be the vertices which have been assigned color 2. Now assign color 1
to all the vertices which are adjacent to v1 , v2 , · · · , vr . Continue this process till every vertex
in T has been assigned the color. We observe that in T all vertices at odd distances from
v have color 2, and v and vertices at even distances from v have color 1. Therefore along
any path in T , the vertices are of alternating colors. Since there is one and only one path
between any two vertices in a tree, no two adjacent vertices have the same color. Thus T is
colored with two colors. Hence T is 2-chromatic. ⇤
The converse of the above theorem is not true, i. e., every 2-chromatic graph need not be
a tree. The next result charaterises 2-chromatic graphs.
Theorem 9.3. A graph is 2-chromatic ( (G) = 2) if and only if it has no odd cycles.
Proof. Let G be a connected graph with cycles of only even length and let T be a spanning
tree in G. Then, by Theorem 9.2, T can be coloured with two colours. Now add the chords
36 M. RANA AND ARUN MAITI

to T one by one. As G contains cycles of even length only, the end vertices of every chord
get different colours of T . Thus G is coloured with two colours and hence is 2-chromatic. If
G is disconnected we can use the same argument for each of its components.
Conversely, let G be 2-chromatic then G cannot contain an odd-cycle as to color an odd cycle
one requires at least 3 colors. ⇤
A greedy algorithm for graph coloring. Let (G) denote the maximum degree of
vertices of a given graph G. For a given vertex ordering {v1 , v2 , · · · , vn } of G, and ordering
of (G) + 1 many colors C1 , C2 , · · · , C (G)+1 , we color first vertex (v1 ) with the first color
(C1 ), and then each vertex is given the color with the smallest number that is not already
used by one of its neighbors.
Remark 9.4. This algorithm never uses more than d(vi ) + 1 colors to color i-th vertex vi .
Therefore, we have

1  (G)  (G) + 1.
The following result due to Brooks improves the above bound in most cases.
Theorem 9.5 (Only the statement). For a connected graph G,
(G)  (G)
unless G is a complete graph or an odd cycle.
For planar graphs, e.g., dual graphs of maps of the states of a country, we have a much
better result to determine the chromatic numbers. Here we state and prove a weaker version
of such a result.
Theorem 9.6 (Five color theorem). For any planar graph G, (G)  5
The proof uses the following lemma.
Lemma 9.7. A planar graph G contains a vertex of degree at most 5.
Proof. We may assume that G is connected. Suppose all the vertex of G has degree 6 or
more, then by hand-shake lemma
2|E(G)| 6|V (G)|.
On the other hand, for a planar graph G we have seen earlier that
|E(G)|  3|V (G)| 3 =) 2|E(G)|  6|V (G) 6|
So there is a contradiction. Hence the proof of the lemma.

Proof of five color theorem. Proof by induction on the number of vertices. Let us assume
that the statement is true for all graphs with n vertices. Then we will show that it is true
for a given graph G with n + 1 vertices
By the above lemma, there exist a vertex v in G such that deg(v)  5.
Case 1. deg(v)  4. So, there are at most 4 colors that have been used on the neighbors of
v. There is at least one color then available for v, see below pic. So G can be colored with
five colors.
Case 2. deg(v) = 5. If two of the neighbors of v are colored with the same color, then
there is a color available for v. So we may assume that all the neighbors v, v1 , v2 , v3 , v4 , v5
GRAPH THEORY LECTURE NOTES 37

are colored with colors C1 , C2 , C3 , C4 , C5 in the clockwise order. Rest of the proof will be
presented later on.

The following stronger version of the above theorem is known to be true.
Theorem 9.8 (four color theorem). For any planar graph G, (G)  4
Exercise Find the chromatic number of the Petersen graph?

9.1. Chromatic polynomial. Chromatic polynomialx is a special function that de-


scribes the number of ways we can achieve a proper coloring on a graph G given k colors.
For a graph G, PG (k) counts the number of its vertex k-colorings (coloring of its vertices by
k colors), if k < (G), then by convention PG (k) = 0.
Homework: What is value of PG ( (G))?
Proposition 9.9. The chromatic polynomial of the complete graph with n-vertices, Kn , is
given by
PKn (k) = k(k 1)(k 2) · · · (k n + 1)
.

Proof. We know that for (Kn ) = n. Therefore, for k < n the proposition follows from the
definition of PG . For k n, for an arbitrary ordering of vertices v1 , v2 , v3 , · · · vn of Kn , we
have k choices of colors for the first vertex v1 , and next, (k 1) choices for v2 and so on.
Continuing this way, we have (k n + 1) choices of colors for the vertex vn . Thus, the total
number of choices to color for all the n vertices is k(k 1)(k 2) · · · (k n + 1). ⇤
The fact that the number of k-colorings is a polynomial in k follows from a recurrence
relation called the deletion–contraction recurrence or Fundamental Reduction Theorem.
Theorem 9.10. Let G be a simple graph, for a pair of vertices u and v the graph G/uv
is obtained by merging the two vertices and removing any edges between them. If u and v
are adjacent in G, let G uv denote the graph obtained by removing the edge uv. Then the
numbers of k-colorings of these graphs satisfy:

PG (k) = PG uv (k) PG/uv (k)


Equivalently, if u and v are not adjacent in G and G + uv is the graph with the edge uv
added, then

PG (k) = PG+uv (k) + PG/uv (k)


By systematically applying Theorem 9.10 to pairs of nonadjacent vertices in a graph G, we
eventually arrive at a collection of complete graphs. Thus, by Proposition 9.1, the chromatic
polynomial is sum of polynomials, therefore itself is a polynomial.
We now illustrate this. Suppose that we wish to compute the chromatic polynomial of the
graph G of Figure 9.2. For the nonadjacent vertices u and v of G and the graph H obtained
by identifying u and v, it follows that by Theorem 9.10 that the chromatic polynomial of G
is the sum of the chromatic polynomials of G + uv and H.
38 M. RANA AND ARUN MAITI

Fig. 9.2

To compute the chromatic polynomial of G, we apply this procedure recursively below.

Thus, we obtain
PG (k) = k(k 1)(k 2) · · · (k 5+1) + 4k(k 1)(k 2)(k 4+1) + 3k(k 1)(k 3+1)
= k5 6k 4 + 14k 3 15k 2 + 6k
References
[RD] Reinhard Diestel, Graph Theory, Springer, Graduate text book.
[ND] Narshingh Deo. Graph Theory with Applications to Engineering and Computer Science, 1974.

You might also like