© jalal kawash 2010 graphs peeking into computer science

48
© Jalal Kawash 2010 Graphs Peeking into Computer Science

Upload: charles-nash

Post on 04-Jan-2016

226 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010

GraphsPeeking into Computer Science

Page 2: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Reading Assignment

Mandatory: Chapter 3 – Sections 3.3 & 3.4

2

Page 3: © Jalal Kawash 2010 Graphs Peeking into Computer Science

Graph Coloring

3

Page 4: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Objectives

At the end of this section, you will be able to:

1. Understand (once more) how graphs are used to represent data

2. Learn the graph coloring algorithm3. Apply graph coloring to solve for various

scheduling problems

4

Page 5: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra

Recall: ◦When color is used to draw attention or

otherwise used to communicate information the number of colors used should be minimized.

5

Vs.

Page 6: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: A Subset Of Canada: Color The Map Using As Few Colors As Possible And No Adjacent Colors

6

Page 7: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: Canada (As A Graph)7

YK NWT NU

BC AB SK MB

Page 8: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Simple Intersection8

Based on Aho, Hopcroft, and Ullman, Data Structures and Algorithms, Addison-Wesley

D

B

C

A

Page 9: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Another Example Problem

Design a traffic light for a complex intersection

Identify allowed turns◦Going straight is a turn!

Group turns so that permitted simultaneous turns are in the same group

Find the smallest number of groups◦This means a traffic light with the smallest

number of phases

9

Page 10: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Simple Intersection10

Based on Aho, Hopcroft, and Ullman, Data Structures and Algorithms, Addison-Wesley

D

B

C

A

Page 11: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

9 Possible Turns

From A to B (denoted AB)From A to C (AC)From A to D (AD)BABCBDDADBDC

11

D

B

C

A

Page 12: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Example Simultaneous Turns

ACADBA

12

D

B

C

A

Page 13: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Example Conflicting Turns

AC conflicts withDBBDBCDC

13

D

B

C

A

Page 14: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Objective

Build a graph model

Vertices are turns

An edge connects conflicting turns

14

Page 15: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Graph Model of Conflicting Turns15

D

B

C

A

AB AC AD

BA BC BD

DA DB DC

Page 16: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Graph Coloring

Color the vertices of the graphNo two adjacent vertices can have the

same color◦Adjacent: and edge connects them

Each color represents a group of turns that can be simultaneous

Note: more than one solution

16

Page 17: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Graph Coloring Algorithm

Repeat the following two steps until the graph is colored

1. Select an uncolored vertex and color it with a new color, C

2. For each uncolored vertex,◦ Determine if it has an edge with a vertex that

is colored with color C◦ If not, color it with color C◦ If yes, skip it

17

Page 18: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra, Graph Coloring Example 1: Final Exams

You are to schedule final exams so that a student will not have two final exams scheduled at the same time.

Although you could schedule each exam in it’s own individual time slot (i.e., no two exams run simultaneously) this would be highly inefficient.◦ Another constraint is to schedule exams with the

minimum number of time slots.Examinations for a lecture will be represented

with vertices.A pair of vertices will be connected if there is at

least one student who is registered in both.

Page 19: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer ScienceJT’s Extra, Graph Coloring Example 1: Final Exams (2)

Lecture A:◦ Student 1, student 2

Lecture B:◦ Student 2, student 4, student 6

Lecture C:◦ Student 1, student 3, student 5

Lecture D:◦ Student 4, student 5

Lecture A Lecture B

Lecture C Lecture DStudent 5

Student 4Student 1

Student 2

Page 20: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra, Graph Coloring Example 2: Final Exams

Lecture A:◦ Student 1, student 2, student 4

Lecture B:◦ Student 2, student 4, student 6

Lecture C:◦ Student 1, student 3, student 5

Lecture D:◦ Student 4, student 5

Lecture A Lecture B

Lecture C Lecture DStudent 5

Student 4Student 1

Student 2, 4

Student 4

Page 21: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: Color This Graph

Page 22: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Graph Model of Conflicting Turns22

D

B

C

A

AB AC AD

BA BC BD

DA DB DC

AB AC AD

BA BC BD

DA DB DC

Page 23: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Graph Model of Conflicting Turns23

D

B

C

A

AB AC AD

BA BC BD

DA DB DC

AB AC AD

BA BC BD

DA DB DC

Page 24: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Graph Model of Conflicting Turns24

D

B

C

A

AB AC AD

BA BC BD

DA DB DC

AB AC AD

BA BC BD

DA DB DC

Page 25: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Complex Intersection25

From Aho, Hopcroft, and Ullman, Data Structures and Algorithms, Addison-Wesley

D E

B

C

A

Page 26: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: Graph Coloring Applications

SchedulingMobile radio frequency assignmentSudoku

Page 27: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: Complex Graphs Requiring Only 4 Colors

Page 28: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: Complex Graphs Requiring Only 4 Colors

Page 29: © Jalal Kawash 2010 Graphs Peeking into Computer Science

TreesA popular special case of graphs

29

Page 30: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Objectives

At the end of this section, the student will be able to:

1. Identify when a graph is a tree2. Use tree terminology3. Identify different applications of trees4. Define binary trees

Page 31: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Computer Science Trees

Trees are a special case of graphsA tree is a (directed) graph with the

properties:1. There is a designated vertex, called the

root of the tree2. There is a unique directed path from the

root to every other vertex in the tree

Grow downwards!

31

Page 32: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Example Tree

A

B C D

G

H

FE

32

Page 33: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Example Tree

A tree always “grows” downwardWe omit the arrows

A

B C D

G

H

FE

33

Page 34: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Tree Terminology

RootChildParentAncestorDescendantDistanceSiblingsLeaf

A

B C D

G

H

FE

34

Page 35: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Root35

A

B C D

G

H

FE

Vertex ‘A’ is the root of the tree

Page 36: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Parent-Child (Above/Below), Ancestor-Descendant

A is the parent of B, C, D.

B, C, D are the children of A.

B,C,D,E are descendants of A.

A is ancestor of B, C, D, E

36

A

B C D

E

Page 37: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Parent-Child

All vertices have exactly one parent except for the root (which has none). JT: (by definition – the root is at the ‘top’ and

because there are no vertices that aren’t part of the tree).

37

Page 38: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Siblings

Siblings have the same parent.

B,C,D are siblingsE, F are siblingsA, G have no siblings

38

A

B C D

E F G

Page 39: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Leaf

Leaves are vertices with no children

B, D, E are leaves

“Bottom level”

39

A

B C D

E

Page 40: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Distance

The distance from the root to a vertex is the number of intervening edges

40

A

B C D

E

Distance

Distance

Distances• A to B = 1• A to E = 2

Page 41: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Organizational Trees

President

VPR&D

VP FinanceVP

Marketing

Accounting Director

SalesDirector

DevelopmentDirector

ResearchDirector

41

Page 42: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Game TreesPaper-Rock-Scissors

1S 1R 1P

1R2R

1R2S

1S 2S

1S 2R

1S 2P

1R2P

1P2R

1P2S

1P2P

draw1 wins2 wins2 winsdraw1 wins1 wins2 winsdraw

42

Page 43: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2009Peeking into Computer Science

Game TreesTic-Tac-Toe

X

X …

43

Page 44: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2009Peeking into Computer Science

Game Trees

X

X

X

X

X

X

X X

XX

X

X

44

Page 45: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2009Peeking into Computer Science

X X

X

X X

XX

X X

XX

X X

XX

X X

XX

X X

XX

X X

XX

X X

XX

X X

XXX

Leaf: Draw

X X

XXX

Leaf: O Wins Leaf: O Wins

Leaf: X Wins45

Page 46: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Binary Trees

A binary tree is a tree with the following properties:

1. The edges are labeled with the labels left or right

2. Every vertex has at most two children; if both children exist, then one edge must be labeled with left and the other right.

46

Page 47: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Example Binary Tree

A

CD

G

H

FE

left

left

left

right

right

right

47

Page 48: © Jalal Kawash 2010 Graphs Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science

Example Binary Tree

Drop the labels, if the tree is drawn with no confusion A

CD

G

H

FE

C’sLeft Child

C’sRight Child

D’sRight Child

D has noLeft Child

48