graph theory in computer science greg stoll november 22, 2008

27
Graph Theory in Computer Science Greg Stoll November 22, 2008

Upload: philomena-beasley

Post on 30-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Graph Theory in Computer Science

Greg Stoll

November 22, 2008

Today's Topics

Graph colorings

Huffman encoding

Graph colorings

Graph colorings

Graph colorings

What's the fewest number of colors that are needed to color a given graph?

Graph colorings

A clique of size n is a graph with n vertices where all vertices have edges between them.

Register allocation

Modern computers do operations (+, -, *, etc.) on registers. Fixed number of these (x86 architecture has 8, x64 has 16). When compiling code, need to assign variables to registers to use them.

Register allocation

Register allocation

Register allocation

In fact, this is always true for interval graphs – the number of registers needed (i.e. the number of colors to color the graph) is equal to the size of the largest clique!

Register allocation

Unfortunately, even finding the largest clique in a graph is NP-hard, but a greedy algorithm does pretty well.

Register allocation

Another problem: what if there aren't enough registers?

Worksheet, part 1

Work on questions 1-6 on the worksheet!

Cookies!

Mmmm....

Sending a message

On a computer, all you have are bits (0's and 1's). How can you send a text message to another computer?

ASCII encoding

ASCII is a standard way to turn characters into bits.

A = 65 = 01000001

B = 66 = 01000010

...

Z = 90 = 01011010

Modified ASCII

What if we just want to send letter and a little punctuation? We can use a smaller code:

a = 0 = 00000

b = 1 = 00001

...

<space> = 26 = 11010

5 bits means 2^5 = 32 characters

Modified ASCII

Our modified ASCII uses 5 bits per character.

Sample message: “hello mom, how are you?” is

22 characters * 5 bits/character = 110 bits.

How could we reduce the average number of

bits per character?

Shrinking the message

Not all letters are created equal. “e” is much more common than “x” in most English text.

We could take advantage of this if we made the

code for “e” smaller than the code for “x”.

Shrinking the message

“o” and “ ” are the most commonly used letters in our sample message. Let

“o” = 0

“ “ = 1

“m” = 00

....

Will this work?

Prefix-free code

A prefix-free code is one in which no code word is a prefix of another.

“o” = 0

“ “ = 1

“m” = 00

is not prefix free, since “o”=0 is a prefix of

“m”=00.

Binary tree

A binary tree is a common data structure where each node has up to two children.

Binary tree

What if we assign a character to each leaf?

Huffman encoding

We want a way to build up a binary tree where the least common characters are lower. (i.e. their code words are longer) We can do this by starting at the bottom and always combining the least common two characters, and continuing until we've formed a binary tree.

Huffman encodingExample: “hello mom, how are you?”

Huffman encoding

Using Huffman encoding lets us send the message in 78 bits, as opposed to 110. With longer message, you can get even greater savings.

Worksheet, part 2

Work on questions 7-12 on the worksheet!