40
Lecture outline Graph coloring I Examples I Applications I Algorithms

Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Lecture outline

Graph coloring

I Examples

I Applications

I Algorithms

Page 2: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Graph coloring

Adjacent nodes must have different colors.

How many colors do we need?

Page 3: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Graph coloring

Neighbors must have different colors

How many colors do we need?

Page 4: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Graph coloring

Neighbors must have different colors

Not valid

Page 5: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Definitions

I k-Colorable: can be colored with k colors

I chromatic number: min number of colorsneeded

Page 6: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Cycle graph

Page 7: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Cycle graph

Odd-length cycles require 3 colors

Page 8: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Cycle graph

No way to color with only 2 colors

Page 9: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Bipartite graph

Page 10: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Bipartite graph

Bipartite ⇔ 2-colorable

Page 11: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Tree

e

c

d

a

f

i

g

k

h

l

j

b

c

e d

a g i

b

f

jh k l

Page 12: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Tree

e

c

d

a

f

i

g

k

h

l

j

b

c

e d

a g i

b

f

jh k l

Page 13: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Clique

Page 14: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Clique

If G contains a Clique of size k , then it requires atleast k colors

Page 15: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Planar graph

Page 16: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Planar graph

exam-like question: draw the smallest planar graphwith chromatic number 4

Page 17: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Planar graph coloring

I Some planar graphsrequire 4 colors

I Any planar graph is5-colorable (1890)

I Can every planargraph be coloredwith 4 colors?

(Stated in 1852, solved in 1976)

Page 18: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

map coloring

map coloring ⇔ planar graph coloring

Page 19: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

4 color theorem

Every planar graph (and map) can be colored with 4colors.

Page 20: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Graph coloring

I ExamplesI Applications

I Register allocationI Logistics problemsI Scheduling problemsI Puzzles

I Algorithms

Page 21: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Applications

K9

K9

Page 22: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Applications

Page 23: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Sudoku

5 3

8

7

91

89

6

6

6

8 3

27

4

8 3

1

6

82

5

978

1 94

Page 24: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Register allocation

I Limited registers (16 in ax86-64 CPU)

I Goal: avoid storing localvariables in main memory

I If a variable X is put in aregister, other variables can’tuse that register until the lastuse of X

I Need to decide which variablegoes to which register

for (i=0; i<n; i++){

...

}

for (j=0; j<n; j++){

...

}

i and j can go in thesame register, but i andn can’t

Page 25: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Interval graphI Nodes: intervals

I Edges: overlapping intervals

Page 26: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Register allocation

same color = same register

Source: scienceblogs.com/goodmath/2007/06/28/graph-coloring-algorithms-1/

Page 27: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Logistics problems

Grouping things into as few groups as possible

I Nodes: things

I Edges: things that can’t be together

I Each color is a group

Examples: register allocation, chemical storage, ...

Page 28: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Scheduling problems

Doing a set of tasks in the shortest time

I Nodes: tasks

I Edges: tasks that can’t be done at the sametime

I Each color is a time slot

Examples: exam scheduling, ...

Page 29: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Graph coloring

I Examples

I ApplicationsI Algorithms

I Can this graph be colored with k colors?I Find an optimal coloringI Or suboptimal coloring more quickly

Page 30: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

2-colorability

Is a graph 2-colorable? (is a graph bipartite?)

I Run BFS

I Color nodes by levelin the BFS tree

I If there are crossedges betweensame-parity levelss⇒ not 2-colorable BFS Tree edges

Cross edges between even and odd

Cross edges between same-parity levels

Page 31: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

3-colorability

Is a graph 3-colorable?

I different colorings with n nodes and 3 colors:

3n or n3 ?

In general: n nodes and k colors ⇒ kn differentcolorings

Exponential!

I Next lecture: 3-colorability is NP-complete

(even for planar graphs)

Page 32: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Greedy coloringAlgorithm:

1. Consider the nodes in some order

2. Give each node in this order the first color notused by its neighbors

I Not optimal: depends on order

1 3 4 2

1 3 2 4

Order Coloring

Page 33: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Greedy coloring

It can be really bad

1

2

3

4

5

6

7

8

1

3

5

7

2

4

6

8

Page 34: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

k-regular graph

How many colors are needed?

Page 35: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Greedy upper boundAlgorithm:

1. Consider the nodes in some order

2. Give each node in this order the first color notused by its neighbors

If the max degree of any node is r ,the graph is (r + 1)-colorable

Proof: let u be any node. When it’s time to color u,even if all its neighbors are already colored, they canuse at most r colors, so r + 1 colors suffice to coloru.

Page 36: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

k-regular graph

Greedy coloring ⇒ (k+1)-colorable

Page 37: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Greedy upper bound

If the max degree of any node is r ,the graph is (r + 1)-colorable

Can still be a bad upper bound

Page 38: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Greedy for interval graphs

I Suppose the biggest clique has size k

I The chromatic number must be at least k

I If nodes are sorted by starting point, greedycoloring finds a k-coloring

I Therefore, it is optimal

Page 39: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Greedy for interval graphsIf nodes are sorted by starting point, greedy coloring finds ak-coloring. Proof:

1. Let I = (Is , Ie) be any interval

2. Any neighbor of I must end after Is

3. Any already-colored neighbor of I must start before Is

4. (2. and 3.) ⇒ I and the already-colored neighbors of Iintersect at Is

5. If the max clique size is k , there are at most k − 1already-colored neighbors of I , and I can use color k

Page 40: Lecture outlinegoodrich/teach/graph/notes/graphColoringSlides.pdfLecture outline Graph coloring I Examples I Applications I Algorithms. Graph coloring Adjacent nodes must have di erent

Overview

Graph coloringI Examples

I Bipartite graphsI CliquesI Planar graphs

I ApplicationsI SudokuI Register allocationI Logistics problemsI Scheduling problems

I AlgorithmsI 2-coloringI Greedy coloring

Thanks!