Title: Graph Coloring and Hamiltonian cycles
1Graph Coloringand Hamiltonian cycles
2Follow up
3Objectives
- Use a backtracking algorithm to solve graph
coloring. - Solve the Hamiltonian cycle problem
- Discuss differences between DFS and BFS
backtracking algorithms.
4Graph 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.
5Graph coloring algorithm
mColor (thisNode) while (true) nextColoring
(thisNode) if (colorthisNode 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) colorthisNode
(colorthisNode 1) mod (numColors 1) if
(colorthisNode 0) then return // no more
colors to try. for k 1 to numNodes1 if
(connectedk,thisNode and colork
colorthisNode) then break endfor if
(k numNodes1) return // found a new color
because no nodes clashed. endWhile
6M-coloring function
- void mColoring(int k)
- do //Generate all legal assignments for xk
- NextValue(k)
- if (!xk) break//No new color possible
- if (kn)//At most m colors have been used to
color the n vertices. - for (int i1iltni) coutltltxiltlt
- cout ltlt endl
- else mColoring (k1)
- while(1)
- void NextValue(int k)
- do
- xk (xk1)(m1)//next highest color
- if (!xk) return //All colors have been used.
- for (int j1jltnj) //Check if this color
is distinct - if (Gkj //If (k, j) is an edge
- (xk xj)) //and if adj. vertices
- break //have the same color
-
- if (j n1) return //New color found
7Small Example
8Aside Coloring a Map
- Assign colors to countries so that no two
countries are the same color. - Graph coloring as map coloring.
9Map coloring as Graph Coloring
Idaho
Wyoming
Utah
Nevada
Colorado
New Mexico
Arizona
10Four 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.
11Hamiltonian Cycles
- Given a graph with N vertices.
- Find a cycle that visits all N vertices exactly
once and ends where it started. - Sound familiar?
12Example
13Hamiltonian
Hamiltonian (k) while (1) xk
NextValue(k) if (xk 0) then return if
(k N) then print solution else
Hamiltonian (k1) endWhile
NextValue (k) while (1) value (xk1) mod
(N1) if (value 0) then return value if
(Gxk-1,value) for j 1 to k-1 if
xj value then break if (jk) and (k lt
N or k N and GxN,x1) then
return value endWhile
14Hamiltonian functions
- void Hamiltonian(int k)
- do //Generate values for xk
- NextValue(k) //Assign a legal next value to
xk - if (!xk) return
- if (kn)
- for (int i1iltni) coutltltxiltlt
- cout ltlt 1\n
- else Hamiltonian(k1)
- while(1)
- void NextValue(int k)
- do
- xk (xk1)(n1)//next vertex
- if (!xk) return
- if (Gxk-1xk) //Is there an edge?
- for (int j1jltk-1j) if (xjxk) break
if (jk) //if true, then the vertex is
distinct. - if ((kltn) (kn) Gxnx1))
- return
-
- while (1)
15Thats it.
- Have a good weekend.
- Midterm 2 is next week.