Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Coloring and Hamiltonian cycles Lecture 22 CS 312.

Similar presentations


Presentation on theme: "Graph Coloring and Hamiltonian cycles Lecture 22 CS 312."— Presentation transcript:

1 Graph Coloring and 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 (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

6 M-coloring function void mColoring(int k) {do {//Generate all legal assignments for x[k] NextValue(k); if (!x[k]) break;//No new color possible if (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 color if (!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. vertices break; //have the same color } if (j == n+1) return; //New color found }while (1);//Otherwise try to find another color.}

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 UtahNevada Idaho Colorado New Mexico Wyoming 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) 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 value endWhile

14 Hamiltonian functions void 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 vertex if (!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); }

15 That’s it. Have a good weekend. Midterm 2 is next week.


Download ppt "Graph Coloring and Hamiltonian cycles Lecture 22 CS 312."

Similar presentations


Ads by Google