Skip to main content

Discrete Mathematics - More On Graphs

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
Graph coloring
In the above figure, at first vertex a is colored red. As the adjacent vertices of vertex a are again adjacent, vertex b and vertex d are colored with different color, green and blue respectively. Then vertex c is colored as red as no adjacent vertex of c 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 −

Graph Traversal

Graph traversal is the problem of visiting all the vertices of a graph in some systematic order. There are mainly two ways to traverse a graph.
  • Breadth First Search
  • Depth First Search

Breadth First Search

Breadth First Search (BFS) starts at starting level-0 vertex X of the graph G. Then we visit all the vertices that are the neighbors of X. After visiting, we mark the vertices as "visited," and place them into level-1. Then we start from the level-1 vertices and apply the same method on every level-1 vertex and so on. The BFS traversal terminates when every vertex of the graph has been visited.
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”.
  • Put source vertex in a queue and change its status to “Waiting”.
  • Repeat the following two steps until queue is empty −
    • Remove the first vertex from the queue and mark it as “Visited”.
    • 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.
Breadth First Search graph
Solution −
  • Initialize status of all vertices to “Ready”.
  • Put a in queue and change its status to “Waiting”.
  • Remove a from queue, mark it as “Visited”.
  • 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.
  • Queue is empty so stop.
So the traversal order is −
abdec
The alternate orders of traversal are −
abedc
Or, adbec
Or, aebdc
Or, abedc
Or, adebc
Application of BFS
  • Finding the shortest path
  • Minimum spanning tree for un-weighted graph
  • GPS navigation system
  • Detecting cycles in an undirected graph
  • Finding all nodes within one connected component
Complexity Analysis
Let G(V,E) be a graph with |V| number of vertices and |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|)

It may vary between O(1) and O(|V2|)

Depth First Search

Depth First Search (DFS) algorithm starts from a vertex v, then it traverses to its adjacent vertex (say x) that has not been visited before and mark as "visited" and goes on with the adjacent vertex of x 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 v.
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”
  • Put source vertex in a stack and change its status to “Waiting”
  • Repeat the following two steps until stack is empty −
    • Pop the top vertex from the stack and mark it as “Visited”
    • 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.
Depth First Search graph
Solution
  • Initialize status of all vertices to “Ready”.
  • Push a in stack and change its status to “Waiting”.
  • Pop a and mark it as “Visited”.
  • 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 c onto stack.
  • Pop c from stack and mark it as “Visited”. It has no “Ready” neighbor.
  • Pop d from stack and mark it as “Visited”. It has no “Ready” neighbor.
  • Pop e from stack and mark it as “Visited”. It has no “Ready” neighbor.
  • Stack is empty. So stop.
So the traversal order is −
abcde
The alternate orders of traversal are −
aebcd
Or, abecd
Or, adebc
Or, adceb
Or, adcbe
Complexity Analysis
Let G(V,E) be a graph with |V| number of vertices and |E| number of edges. If DFS algorithm visits every vertex in the graph and checks every edge, then the time complexity is −
(|V|+|E|)

Applications
  • Detecting cycle in a graph
  • To find topological sorting
  • To test if a graph is bipartite
  • Finding connected components
  • Finding the bridges of a graph
  • Finding bi-connectivity in graphs
  • Solving the Knight’s Tour problem
  • Solving puzzles with only one solution

Comments

Popular posts from this blog

Discrete Mathematics - Rules of Inference

To deduce new statements from the statements whose truth that we already know,  Rules of Inference  are used. What are Rules of Inference for? Mathematical logic is often used for logical proofs. Proofs are valid arguments that determine the truth values of mathematical statements. An argument is a sequence of statements. The last statement is the conclusion and all its preceding statements are called premises (or hypothesis). The symbol “ ∴ ∴ ”, (read therefore) is placed before the conclusion. A valid argument is one where the conclusion follows from the truth values of the premises. Rules of Inference provide the templates or guidelines for constructing valid arguments from the statements that we already have. Table of Rules of Inference Rule of Inference Name Rule of Inference Name P ∴ P ∨ Q P ∴ P ∨ Q Addition P ∨ Q ¬ P ∴ Q P ∨ Q ¬ P ∴ Q Disjunctive Syllogism P Q ∴ P ∧ Q P Q ∴ P ∧ Q Conjunction P → Q Q → R ∴ P → R P → Q Q → R ∴ P → R ...

Digital Circuits - Shift Registers

We know that one flip-flop can store one-bit of information. In order to store multiple bits of information, we require multiple flip-flops. The group of flip-flops, which are used to hold (store) the binary data is known as  register . If the register is capable of shifting bits either towards right hand side or towards left hand side is known as  shift register . An ‘N’ bit shift register contains ‘N’ flip-flops. Following are the four types of shift registers based on applying inputs and accessing of outputs. Serial In − Serial Out shift register Serial In − Parallel Out shift register Parallel In − Serial Out shift register Parallel In − Parallel Out shift register Serial In − Serial Out (SISO) Shift Register The shift register, which allows serial input and produces serial output is known as Serial In – Serial Out  (SISO)  shift register. The  block diagram  of 3-bit SISO shift register is shown in the following figure. This block d...

discrete mathematics: Venn Diagrams

Venn Diagrams Venn diagram, invented in 1880 by John Venn, is a schematic diagram that shows all possible logical relations between different mathematical sets. Examples Set Operations Set Operations include Set Union, Set Intersection, Set Difference, Complement of Set, and Cartesian Product. Set Union The union of sets A and B (denoted by  A ∪ B A ∪ B ) is the set of elements which are in A, in B, or in both A and B. Hence,  A ∪ B = { x | x ∈ A   O R   x ∈ B } A ∪ B = { x | x ∈ A   O R   x ∈ B } . Example  − If  A = { 10 , 11 , 12 , 13 } A = { 10 , 11 , 12 , 13 }  and B =  { 13 , 14 , 15 } { 13 , 14 , 15 } , then  A ∪ B = { 10 , 11 , 12 , 13 , 14 , 15 } A ∪ B = { 10 , 11 , 12 , 13 , 14 , 15 } . (The common element occurs only once) Set Intersection The intersection of sets A and B (denoted by  A ∩ B A ∩ B ) is the set of elements which are in both A and B. Hence,  A ∩ B = { x | x ∈ A   A N D...