View
219
Download
5
Embed Size (px)
Citation preview
Graph Coloringand
Hamiltonian cycles
Lecture 22
CS 312
Follow up
• 8 queens material.
Objectives
• Use a backtracking algorithm to solve graph coloring.
• Solve the Hamiltonian cycle problem
• Discuss differences between DFS and BFS backtracking algorithms.
Graph Coloring Problem
• Assign colors to the nodes of a graph so that no adjacent nodes share the same color– nodes are adjacent if there is an edge for
node i to node j.
• Find all m-colorings of a graph– all ways to color a graph with at most m
colors.
Graph coloring algorithmmColor (thisNode) while (true) nextColoring (thisNode) if (color[thisNode] == 0) then break // no more colors for thisNode if (thisNode == numNodes) then print this coloring // found a valid coloring of all nodes. else mColor (thisNode + 1) // try to color the next node. endWhile
nextColoring (thisNode)while (true) color[thisNode] = (color[thisNode] + 1) mod (numColors + 1) if (color[thisNode] == 0) then return // no more colors to try. for k = 1 to numNodes+1 if (connected[k,thisNode] and color[k] == color[thisNode]) then break endfor if (k == numNodes+1) return // found a new color because no nodes clashed. endWhile
M-coloring functionvoid mColoring(int k){do {//Generate all legal assignments for x[k]
NextValue(k);if (!x[k]) break;//No new color possibleif (k==n){//At most m colors have been used to color the n vertices.
for (int i=1;i<=n;i++) cout<<x[i]<<‘ ‘;cout << endl;
else mColoring (k+1); }while(1);}void NextValue(int k){do {
x[k] = (x[k]+1)%(m+1);//next highest colorif (!x[k]) return; //All colors have been used.for (int j=1;j<=n;j++) { //Check if this color is distinct
if (G[k][j] //If (k, j) is an edge&& (x[k] == x[j])) //and if adj. verticesbreak; //have the same color
}if (j == n+1) return; //New color found
}while (1);//Otherwise try to find another color.}
Small Example
Aside: Coloring a Map
• Assign colors to countries so that no two countries are the same color.
• Graph coloring as map coloring.
Map coloring as Graph Coloring
UtahNevada
Idaho
Colorado
New Mexico
Wyoming
Arizona
Four color theorem.
• How many colors do you need? – Four.
• Haken and Appel using a computer program and 1,200 hours of run time in 1976. Checked 1,476 graphs.
• First proposed in 1852.
Hamiltonian Cycles
• Given a graph with N vertices.
• Find a cycle that visits all N vertices exactly once and ends where it started.
• Sound familiar?
Example
HamiltonianHamiltonian (k) while (1) x[k] = NextValue(k) if (x[k] = 0) then return if (k == N) then print solution else Hamiltonian (k+1)endWhile
NextValue (k) while (1) value = (x[k]+1) mod (N+1) if (value == 0) then return value if (G[x[k-1],value]) for j = 1 to k-1 if x[j] = value then break if (j==k) and (k < N or k == N and G[x[N],x[1]]) then return valueendWhile
Hamiltonian functionsvoid Hamiltonian(int k){do {//Generate values for x[k]
NextValue(k); //Assign a legal next value to x[k]if (!x[k]) return;if (k==n) {for (int i=1;i<=n;i++) cout<<x[i]<<‘ ‘;cout << “1\n”;} else Hamiltonian(k+1);
}while(1);}void NextValue(int k){do {
x[k] = (x[k]+1)%(n+1);//next vertexif (!x[k]) return;if (G[x[k-1][x[k]) { //Is there an edge?for (int j=1;j<=k-1;j++) if (x[j]==x[k]) break; if (j==k) //if true, then the vertex is distinct.if ((k<n) || (k==n) && G[x[n]][x[1]]))return; }
}while (1);}
That’s it.
• Have a good weekend.
• Midterm 2 is next week.