0% found this document useful (0 votes)
51 views5 pages

Exp9 16010120126 B3 Graph Colouring

The document discusses the implementation of a graph coloring backtracking algorithm. It begins with the objective of learning the backtracking strategy for solving the graph coloring problem. It then provides an example graph that can be colored with 3 colors. The document explains the graph coloring problem and provides pseudocode for the backtracking algorithm. It includes an analysis of the time and space complexity of the backtracking solution for graph coloring as O(m^V) and O(V), respectively.

Uploaded by

Saraunsh Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views5 pages

Exp9 16010120126 B3 Graph Colouring

The document discusses the implementation of a graph coloring backtracking algorithm. It begins with the objective of learning the backtracking strategy for solving the graph coloring problem. It then provides an example graph that can be colored with 3 colors. The document explains the graph coloring problem and provides pseudocode for the backtracking algorithm. It includes an analysis of the time and space complexity of the backtracking solution for graph coloring as O(m^V) and O(V), respectively.

Uploaded by

Saraunsh Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

K. J.

Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)

Batch: B3 Roll No.: 16010120126

Experiment No. 09

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

Title: Implementation of Graph Colouring Backtracking Algorithm

---------------------------------------------------------------------------------------------------------------------
Objective: To learn the Backtracking strategy of problem solving for Graph Colouring problem

CO to be achieved:
CO 2 Analyze and solve problems for divide and conquer strategy, greedy method, dynamic
programming approach and backtracking and branch & bound policies.

Books/ Journals/ Websites referred:


1. Ellis horowitz, Sarataj Sahni, S.Rajsekaran,” Fundamentals of computer
algorithm”, University Press
2. T.H.Cormen ,C.E.Leiserson,R.L.Rivest and C.Stein,” Introduction to
algortihtms”,2nd Edition ,MIT press/McGraw Hill,2001
3. http://www.math.utah.edu/~alfeld/queens/queens.html
4. http://www-isl.ece.arizona.edu/ece175/assignments275/assignment4a/Solving
%208%20queen%20problem.pdf
5. http://www.slideshare.net/Tech_MX/8-queens-problem-using-back-tracking
6. http://www.mathcs.emory.edu/~cheung/Courses/170.2010/Syllabus/
Backtracking/8queens.html
7. http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/
8. http://www.hbmeyer.de/backtrack/achtdamen/eight.htm

Pre Lab/ Prior Concepts:


Data structures, Concepts of algorithm analysis

Historical Profile:
Page 1 of 5 AOA Sem IV Jan-May 2022
K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

Given an undirected graph and a number m, determine if the graph can be colored with at most
m colors such that no two adjacent vertices of the graph are colored with same color. Here
coloring of a graph means assignment of colors to all vertices.
nput:

1) A 2D array graph [V][V] where V is the number of vertices in graph and graph[V][V] is
adjacency matrix representation of the graph.
Output:

An array color [V] that should have numbers from 1 to m. color[i] should represent the color
assigned to the ith vertex. The code should also return false if the graph cannot be colored with
m colors.
Following is an example graph can be colored with 3 colors.

New Concepts to be learned:


Application of algorithmic design strategy to any problem, Backtracking method of problem-
solving Vs other methods of problem-solving problem graph colouring and its applications.

Algorithm Graph colouring Problem: -

Page 2 of 5 AOA Sem IV Jan-May 2022


K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

Example Graph Colouring Problem:


public class Graph_Coloring
{

static int V = 4;

static void printSolution(int[] color)


{
System.out.println("Solution Exists:" +
" Following are the assigned colors ");
for (int i = 0; i < V; i++)
System.out.print(" " + color[i]);
System.out.println();
}

static boolean isSafe(boolean[][] graph, int[] color)


{
for (int i = 0; i < V; i++)
for (int j = i + 1; j < V; j++)
if (graph[i][j] && color[j] == color[i])
return false;
return true;
}

static boolean graphColoring(boolean[][] graph, int m,

Page 3 of 5 AOA Sem IV Jan-May 2022


K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)
int i, int[] color)
{
if (i == V) {

if (isSafe(graph, color))
{

printSolution(color);
return true;
}
return false;
}

for (int j = 1; j <= m; j++)


{
color[i] = j;

// Recur of the rest vertices


if (graphColoring(graph, m, i + 1, color))
return true;
color[i] = 0;
}
return false;
}

public static void main(String[] args)


{

boolean[][] graph = {
{ false, true, true, true },
{ true, false, true, false },
{ true, true, false, true },
{ true, false, true, false },
};
int m = 3;

int[] color = new int[V];


for (int i = 0; i < V; i++)
color[i] = 0;
if (!graphColoring(graph, m, 0, color))
System.out.println("Solution does not exist");
}
}

Page 4 of 5 AOA Sem IV Jan-May 2022


K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

Analysis of Backtracking solution for Graph Colouring Problem:

Time Complexity: O(m^V).


There are total O(m^V) combinations of colours. So, time complexity is O(m^V). The upper
bound time complexity remains the same but the average time taken will be less.
Space Complexity: O(V).
Recursive Stack of graphColoring(…) function will require O(V) space.

Conclusion:
In this experiment, we have learnt Implementation of Graph Colouring using backtracking
approach. We have also understood the algorithm and implemented the same on java.
Additionally, we have compared the time and space complexity of the program.

Page 5 of 5 AOA Sem IV Jan-May 2022

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy