Professional Documents
Culture Documents
Unit-4: Graph Coloring
Unit-4: Graph Coloring
Graph Coloring
Graph coloring is a method to assign colors to the vertices of a graph so that no two
adjacent vertices have the same color. Some graph coloring problems are −
Vertex coloring − A way of coloring the vertices of a graph so that no two adjacent vertices share the same color.
Edge Coloring − It is the method of assigning a color to each edge so that no two adjacent edges have the same color.
Face coloring − It assigns a color to each face or region of a planar graph so that no two faces that share a common boundary have the same color.
Chromatic Number
Chromatic number is the minimum number of colors required to color a graph. For
example, the chromatic number of the following graph is 3.
Now to assign a particular color to a vertex, determine whether that color is already assigned to the adjacent vertices or not.
If a processor detects same color in the adjacent vertices, it sets its value in the array to 0.
After making n2 comparisons, if any element of the array is 1, then it is a valid coloring.
Adjacency-List Representation:
Adjacency is an array with | V | lists, one for each vertex. u V Adj[u] is the
adjacency list for vertex u and points to all vertices v so that the edge ( u,
v ) E. If G is a directed graph, then:
| Adj [u] | = | E |
u V
If G is an undirected graph, then:
| Adj [u] | = 2 | E |
u V
Some possible spanning trees of the above graph are shown below −
Among all the above spanning trees, figure (d) is the minimum spanning tree. The
concept of minimum cost spanning tree is applied in travelling salesman problem,
designing electronic circuits, Designing efficient networks, and designing efficient
routing algorithms.
To implement the minimum cost-spanning tree, the following two methods are used −
Prim’s Algorithm
Kruskal’s Algorithm
Prim's Algorithm
Prim’s algorithm is a greedy algorithm, which helps us find the minimum spanning tree
for a weighted undirected graph. It selects a vertex first and finds an edge with the
lowest weight incident on that vertex.
Steps of Prim’s Algorithm
Select any vertex, say v1 of Graph G.
Select an edge, say e1 of G such that e1 = v1 v2 and v1 ≠ v2 and e1 has minimum weight among the edges incident on v1 in graph G.
Now, following step 2, select the minimum weighted edge incident on v2.
Continue this till n–1 edges have been chosen. Here n is the number of vertices.
Kruskal’s algorithm is a greedy algorithm, which helps us find the minimum spanning
tree for a connected weighted graph, adding increasing cost arcs at each step. It is a
minimum-spanning-tree algorithm that finds an edge of the least possible weight that
connects any two trees in the forest.
Steps of Kruskal’s Algorithm
Select an edge of minimum weight; say e1 of Graph G and e1 is not a loop.
Continue this till n–1 edges have been chosen. Here n is the number of vertices.
The minimum spanning tree of the above graph is −
Find all unlabeled vertices adjacent to the vertex labeled i. If no vertices are connected to the vertex, S, then vertex, D, is not connected to S. If there
are vertices connected to S, label them i+1.
Introduction:
In a shortest- paths problem, we are given a weighted, directed graphs G = (V, E), with weight function w: E → R mapping edges to real-valued
weights. The weight of path p = (v0,v1,..... vk) is the total of the weights of its constituent edges:
We define the shortest - path weight from u to v by δ(u,v) = min (w (p): u→v), if there is a path from u to v, and δ(u,v)= ∞, otherwise.
The shortest path from vertex s to vertex t is then defined as any path p with weight w (p) = δ(s,t).
The breadth-first- search algorithm is the shortest path algorithm that works on unweighted graphs, that is, graphs in which each edge can be
considered to have unit weight.
In a Single Source Shortest Paths Problem, we are given a Graph G = (V, E), we want to find the shortest path from a given source vertex s ∈ V to
every vertex v ∈ V.
Variants:
There are some variants of the shortest path problem.
o Single- destination shortest - paths problem: Find the shortest path to a given destination vertex t from every vertex v. By shift the
direction of each edge in the graph, we can shorten this problem to a single - source problem.
o Single - pair shortest - path problem: Find the shortest path from u to v for given vertices u and v. If we determine the single - source
problem with source vertex u, we clarify this problem also. Furthermore, no algorithms for this problem are known that run asymptotically
faster than the best single - source algorithms in the worst case.
o All - pairs shortest - paths problem: Find the shortest path from u to v for every pair of vertices u and v. Running a single - source
algorithm once from each vertex can clarify this problem; but it can generally be solved faster, and its structure is of interest in the own right.
1. In all pair shortest path, when a weighted graph is represented by its weight matrix W then objective is to find the distance
between every pair of nodes.
2. We will apply dynamic programming to solve the all pairs shortest path.
3. In all pair shortest path algorithm, we first decomposed the given problem into sub problems.
4. In this principle of optimally is used for solving the problem.
5. It means any sub path of shortest path is a shortest path between the end nodes.
Steps:
i. Let Aki,jAi,jk be the length of shortest path from node i to node j such that the label for every intermediate node will be ≤ k.
ii. Now, divide the path from i node to j node for every intermediate node, say ‘k’ then there arises two case.
a. Path going from i to j via k. b. Path which is not going via k.
iii. Select only shortest path from two cases.
iv. Using recursive method we compute shortest path.
v. Initially: A0=W[i,j]A0=W[i,j]
vi. Next computations: Aki,j=minAk−1i,j,Ak−1i,k,Ak−1k,jAi,jk=minAi,jk−1,Ai,kk−1,Ak,jk−1
Algorithm:
Analysis of Algorithm:
i. The first double for loop takes O (n2) time.
ii. The nested three for loop takes O (n3) time.
iii. Thus, the whole algorithm takes O (n3) time.
Example: Compute all pair shortest path for following figure 7.
Solution:
Or
The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this
algorithm, it will generate a matrix, which will represent the minimum distance from any
node to all other nodes in the graph.
At first the output matrix is same as given cost matrix of the graph. After that the output
matrix will be updated with all vertices k as the intermediate vertex.
The time complexity of this algorithm is O(V3), here V is the number of vertices in the
graph.
Input − The cost matrix of the graph.
0 3 6 ∞ ∞ ∞ ∞
3 0 2 1 ∞ ∞ ∞
6 2 0 1 4 2 ∞
∞ 1 1 0 2 ∞ 4
∞ ∞ 4 2 0 2 1
∞ ∞ 2 ∞ 2 0 1
∞ ∞ ∞ 4 1 1 0
Algorithm
floydWarshal(cost)
.
maximum flow possible is : 23
1) Initialize : max_flow = 0
f(e) = 0 for every edge 'e' in E
3) Return max_flow
Note that the path search just needs to determine whether or not there is an s-t path in the subgraph of edges e with f(e) < C(e). This is
easily done in linear time using BFS or DFS.
There is a path from source (s) to sink(t) [ s -> 1 -> 2 -> t] with maximum flow 3 unit ( path show in blue color )
After removing all useless edge from graph it’s look like
For above graph there is no path from source to sink so maximum flow : 3 unit But maximum flow is 5 unit. to over come form this issue we
use residual Graph.
2. Residual Graphs
The idea is to extend the naive greedy algorithm by allowing “undo” operations. For example, from the point where this algorithm gets stuck
in above image, we’d like to route two more units of flow along the edge (s, 2), then backward along the edge (1, 2), undoing 2 of the 3 units
we routed the previous iteration, and finally along the edge (1,t)