Discrete Mathematics - More on Graphs
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.
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 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.
Map Coloring
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.
BFS Algorithm
The concept is to visit all the neighbor vertices before visiting other neighbor vertices of neighbor
vertices.
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.
Solution −
Remove b from queue, mark it as “Visited”, put its “Ready” neighbor c at 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.
a→b→d→e→c
a→b→e→d→c
Or, a→d→b→e→c
Or, a→e→b→d→c
Or, a→b→e→d→c
Or, a→d→e→b→c
Application of BFS
Finding the shortest path
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 −
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.
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.
Solution
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.
a→b→c→d→e
a→e→b→c→d
Or, a→b→e→c→d
Or, a→d→e→b→c
Or, a→d→c→e→b
Or, a→d→c→b→e
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