Graph Coloring and Hamiltonian cycles - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Coloring and Hamiltonian cycles

Description:

Aside: Coloring a Map. Assign colors to countries so that no two countries are the same color. ... Wyoming. Arizona. Four color theorem. How many colors do you ... – PowerPoint PPT presentation

Number of Views:1257
Avg rating:3.0/5.0
Slides: 16
Provided by: mike437
Category:

less

Transcript and Presenter's Notes

Title: Graph Coloring and Hamiltonian cycles


1
Graph Coloringand Hamiltonian cycles
  • Lecture 22
  • CS 312

2
Follow up
  • 8 queens material.

3
Objectives
  • Use a backtracking algorithm to solve graph
    coloring.
  • Solve the Hamiltonian cycle problem
  • Discuss differences between DFS and BFS
    backtracking algorithms.

4
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.

5
Graph 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
6
M-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

7
Small Example
8
Aside Coloring a Map
  • Assign colors to countries so that no two
    countries are the same color.
  • Graph coloring as map coloring.

9
Map coloring as Graph Coloring
Idaho
Wyoming
Utah
Nevada
Colorado
New Mexico
Arizona
10
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.

11
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?

12
Example
13
Hamiltonian
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
14
Hamiltonian 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)

15
Thats it.
  • Have a good weekend.
  • Midterm 2 is next week.
Write a Comment
User Comments (0)
About PowerShow.com