53
UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis Lecture 17 Class URL: http://vlsicad.ucsd.edu/courses/cse21-s14/

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Embed Size (px)

Citation preview

Page 1: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

UCSD CSE 21, Spring 2014 [Section B00]

Mathematics for Algorithm and System Analysis

Lecture 17

Class URL: http://vlsicad.ucsd.edu/courses/cse21-s14/

Page 2: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Lecture 17 Notes • Last time

• Recurrences revisited• Types of graphs: Complete, Tree, Bipartite, …

• Today and Next Tuesday• Shortest path tree, minimum spanning tree, search trees• Chromatic number• Paths, Trails, Circuits, Cycles: Eulerian and Hamiltonian graphs• (“complexity zoo” – NP, P, NP-hard, NP-complete, …)• (most of the slides are in this slide deck already – please read ahead!)

• Logistics– Final exam review–Sunday June 8, 6-8pm, PCYNH 106

• Practice final exam questions – early Week 10– Week 9 and Week 10 quizzes off-line

• Today’s quiz: open on WeBWorK from 9:30am – 10pm• Can get up to “5 points out of 4”

– Extra credit for PPT slide(s) (up to 2% of course grade)• See next slide

– “Things to Know” – end of Week 9, end of Week 10• SPIS slides on “Trees, Trees, Trees, …”

– Please treat as “assigned reading”

Page 3: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Extra Credit Opportunity: PPT slide(s) on graph(s)– 1% course grade for each slide, up to a max of 2– See thread on Piazza – Must use the given PPT template, and include:

• Image• Source / attribution (URL)• Brief description (what do vertices / edges represent, is the graph

directed / undirected, etc.)• Brief explanation of why the image and the graph are interesting

– Deadline = Thursday, June 5, 5pm Pacific time• Turn-in: email .pptx/.ppt attachment to [email protected]• Filename: LastName_FirstName_LastTwoDigitsofPID[_2].pptx

Page 4: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Special kinds of graphs – complete / clique• Complete (simple) graphs: all possible edges

http://en.wikipedia.org/wiki/File:11-simplex_graph.svghttp://en.wikipedia.org/wiki/File:3-simplex_graph.svghttp://en.wikipedia.org/wiki/File:7-simplex_graph.svg

Page 5: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Special kinds of graphs – bipartite • Bipartite graphs

– Two “kinds” of vertices V1 V2 = V, V1 V2 =

– Each edge is between a vertex of one kind and a vertex of the other kind: any e E has form {u V1 , v V2}

People Projects

Page 6: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Special kinds of graphs – tree • Trees

– Connected– |V| - 1 edges – No cycles

(any two of these properties implies the third !)

Assignment: read the SPIS-2013 “Trees, Trees, Trees” slides that are posted …!

Page 7: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

7

Trees vs. General Graphs

Graph: G = (V,E)

V = set of vertices

E = set of edges

graph with 7 vertices, 6 edges

This graph is not connected, and it has a cycle.

Connected: Between any pair of vertices, there is a path of edges.Cycle: A path of edges that begins and ends at the same vertex.

Page 8: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Tree = Special Kind of Graph

tree with 7 vertices, 6 edges

A tree is a graph with n vertices that:

(i) is connected

(ii) has no cycles

(iii) has n – 1 edges

Fact: Any two of the above properties implies the third.

E.g., “If a graph with n vertices is connected and has no cycles, then it has n – 1 edges.” Can we prove this???

Page 9: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

“You want proof? I’ll give you proof!”

Page 10: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Proof Strategy: ?

Claim: If a graph with n vertices is connected and has no cycles, then it has n – 1 edges.

(I.H.) Assume that the Claim is true for n = 1, …, k – 1. Consider a graph with n = k vertices that is connected and has no cycles.

Strong Induction

k1 k – 1 vertices, connected, no cycles k1 – 1 edges

k2 k – 1 vertices, connected, no cycles k2 – 1 edges

(k1 – 1) + (k2 – 1) + 1 = (k1 + k2) – 1 = k – 1

Page 11: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Trees = Data Structures

Binary Search Tree

Application: Search a set of keys for a given key value

if key = root.key then success

else if key < root.key then search left subtree

else search right subtree

Binary tree:

Has a root vertex

Every vertex has at most 2 children

Search for “2”Search for “17”Search for “8”

Page 12: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

How Many Search Steps Are Needed?How many levels can a binary tree with n nodes have?

(convention: root is at level 0)

At most: ?

At least: ?

n – 1 levels

log2n levels

Page 13: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Trees = Data Structures = Algorithms

• A heap is a binary tree of depth d such that– (1) all nodes not at depth d-1 or d are internal nodes each level is filled before the next level is started

– (2) at depth d-1 the internal nodes are to the left of the leaves and have degree 2, except perhaps for the rightmost, which has a left child each level is filled left to right

• A max heap (min heap) is a heap with node labels from an ordered set, such that the label at any internal node is () the label of any of its children All root-leaf paths are monotone

Page 14: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Minheap Example Data Structure

• Parent-child arithmetic = how we’d store in an array … !

– Parent parent of A[i] has index = i div 2– Left, Right (children) children of A[i] have indices 2i, 2i+1

• Parent Child this is a minheap

12

6

11

12

8

1310

4

952 3

6 7

8 9 10

54

2 6 4 8 11 5 91 2 3 4 5 6 7

108

139

1210

Page 15: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Heap Operation: Insert

2

4

11

12

8

1310

6

97

3

2

4

3

12

8

1310

6

97

11

2

4

1112

8

1310

6

97

3

Insert(S,x): O(height) O(log n)Keep swapping with parent until heap condition satisfied

Page 16: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Heap Operation: ExtractMin

ExtractMin(S): return head, replace head key with the last key, “float down” O(log n)

2

6

11

12

8

1310

4

95

12

6

118

1310

4

95

4

6

118

1310

12

95

4

6

118

1310

5

912

“Float down”: If heap condition violated, swap with smaller child

Page 17: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Insert + ExtractMin = ?

• “Priority queue” abstract data type

– Always have the highest-priority item ready at hand– Useful in algorithm implementation – e.g., Dijkstra’s

algorithm later today

• Unordered list: Insert = O(1) but ExtractMin = O(n) • Ordered list: ExtractMin = O(1) but Insert = O(n)

• Heap: Insert = O(log n), ExtractMin = O(log n)

Page 18: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

O(n log n) Heapsort• Build heap: n x O(log n) time

– for i = 1..n do insert (A[1..i],A[i])• Extract elements in sorted order: n x O(log n) time

– for i = n..2 do [extract-min]• Swap (A[1] A[i])• Heapsize = Heapsize-1• Float down A[1]

4

6

118

1310

5

97

12

12

6

118

1310

5

97

4

12

6

118

1310

5

9

7

4

12

6

118

13

10 5

9

7

4

12

6

11

8

13

10

5

9

7

4

12

6

11

8

13

10

5

9

7

4

12

6

11

8

1310

5

9

7

4

12

6

11

8

13

10

5

9 7

4

Page 19: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Trees = Solutions to Real Problems

(A) Connecting cities with minimum road construction

(when adding new junctions is allowed)

(B) Calculating minimum-cost routes to destinations

Page 20: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

The Minimum Spanning Tree Problem

2

33

2

2

2

34

Given: graph of “cities” and “distances”

Problem: Make all cities reachable from each other with minimum road construction.

2

33

2

2

2

34

E

A

D

B

C

E

A

D

B

C

Page 21: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

The Shortest Paths Problem

2

33

2

2

2

34

E

A

D

B

C

Problem: Compute the shortest routes from A to every other city.

2

33

2

2

2

34

E

A

D

B

C

Page 22: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

The Shortest Paths Problem

2

33

2

2

2

34

E

A

D

B

C

Problem: Compute the shortest routes from B to every other city.

2

33

2

2

2

34

E

A

D

B

C2

5

2

3

0

What do these green numbers represent?

Page 23: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

The Shortest Paths Problem

2

33

2

2

2

34

E

A

D

B

C

“Exploration parties set out from B, always moving at constant speed. When a party is the first to reach some city, they call home, then split up and continue exploring. The times of the phone calls are the shortest-path distances from B.”

2

33

2

2

2

34

E

A

D

B

C2

5

2

3

0

(You learn this as Dijkstra’s Algorithm)

Page 24: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Minimum Spanning Tree, Again

2

33

2

2

2

34

2

33

2

2

2

34

E

A

D

B

C

E

A

D

B

C

“Start from an arbitrary vertex, and add the shortest incident edge to the growing tree that does not complete a cycle.” (If the MST is unique, then the starting vertex doesn’t matter…)(You learn this as Prim’s Algorithm)

Page 25: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Nearly the Same Algorithm !• Prim: Iteratively add the edge e(u,v) to T,

where u is in T and v is not yet in T, tominimize du,v (du,v = (u,v) distance)

• Dijkstra: Iteratively add the edge e(u,v) to T, where u is in T and v is not yet in T, tominimize du,v + lu (lu is u’s shortest path cost)

• Both algorithms build trees, in similar greedy ways!– Prim’s Algorithm constructs a Minimum Spanning Tree– Dijkstra’s Algorithm constructs a Shortest Path Tree

If Prim’s Algorithm and Dijkstra’s Algorithm had a baby… Iteratively add the edge e(u,v) to T, where u is in T and v is not yet in T, tominimize du,v + c lu // 0 c 1 (food for thought !)

Page 26: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Minimum Spanning Tree = A Greedy Problem

• Prim’s algorithm. Start with T = a root node s, and greedily grow the tree T. At each step, add to T the cheapest edge e that has exactly one endpoint in T.

• Reverse-Delete algorithm. Start with T = E (all edges). Consider edges in descending order of cost. Delete edge e from T unless doing so disconnects T.

• Kruskal’s algorithm. Start with T = . Consider edges in ascending order of cost. Insert edge e into T unless doing so creates a cycle.

• All three methods are greedy, and produce a minimum spanning tree.

Page 27: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Kruskal’s Algorithm

Page 28: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

2

From Spanning Trees to Steiner Trees

3

• The red dot is called a Steiner point (= an “intermediate junction”)• The maximum cost savings from adding Steiner points

= min ratio of (min Steiner tree cost) / (min spanning tree cost)depends on the metric, or distance function

• Cf. “Manhattan”, “Euclidean”, “Chebyshev”, … Adapted from Prof. G. Robins, UVA

Page 29: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Minimum Steiner Tree is a Hard Problem (Minimum Spanning Tree was Easy)

• You learn about “hard” vs. “easy” in CSE101, CSE105• How would you approach finding a low-cost Steiner tree?

Adapted from Prof. G. Robins, UVA

Page 30: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Iterated 1-Steiner Algorithm (1990)

Given a pointset S, what point p minimizes MST(S {p})

Algorithmic idea: Iterate! (greedily)

In practice: solution cost is within 0.5% of OPT on average

Adapted from Prof. G. Robins, UVA

Page 31: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Walks - DefinitionLet G = (V,E,φ) be a graph.

A walk in G is a sequence:

e1, e2, ..., en−1

of elements of E (edges of G) for which there is a sequence:

a1, a2, ..., an

of elements of V (vertices of G) such that φ(ei) = {ai, ai+1} for

i = 1, 2, ..., n−1.

Page 32: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Walks on Graphs

Which of the following are walks on the graph?A. a, b, c, dB. 1,2,3,4C. d,b,a,c,dD. a,1,bE. None of the above / more than one of the above

1

2 43ab

cd

Page 33: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Special Walks• (Neither edges nor vertices need be distinct: walk)

• Edges must be distinct: trail

• Vertices must be distinct: path

– Sequence of vertices is the “vertex sequence of the path”

• Trail whose first vertex is same as last vertex: circuit

• Circuit with no repeated vertices (except start/end): cycle

• If the graph is directed:

– Obtain a directed walk, trail, path, circuit, cycle…

– Replace {} with () in vertex set: φ(ei) = (ai, ai+1) now

– Arrows: domain set on the left, codomain set on right

Page 34: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Example

Which of the following statements are true?

A. There are no cycles in this graph.

B. There are no circuits in this graph.

C. There is a path that visits each vertex exactly once.

D. There is a trail that uses each edge exactly once.

E. None of the above/ more than one of the above.

1

2

3

4

56

abc

d e f

Page 35: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

More on graphs…• A graph is connected if there is a path between every pair of

vertices. – Edge case: graph with only one vertex, we say it is connected

• One vertex is reachable from another if there is a path between them.

• A connected component of a graph is a subset of vertices such that (i) every pair of vertices in the set is reachable from one another, and (ii) the subset is not part of a larger set with this property.

– Computing distance in a graph: breadth first search of all walks• Small world phenomenon in social networks

– Analyze structure of the graph by looking at number of paths• Hubs and spokes• Link prediction in social networks

Page 36: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph• An Eulerian trail is a trail that includes every edge.

• An Eulerian circuit is a circuit that includes every edge.

Page 37: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph• An Eulerian trail is a trail that includes every edge.

• An Eulerian circuit is a circuit that includes every edge.

In the previous example there is no Eulerian trail:

How do we prove this?

1

2

3

4

56

abc

d e f

Page 38: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph• An Eulerian trail is a trail that includes every edge.

• An Eulerian circuit is a circuit that includes every edge.

• Observations:– If a graph has an Eulerian circuit then it has an Eulerian trail.– If a graph is not connected, then it does not have an Eulerian trail.

Page 39: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Characterization of Eulerian graphs• Theorem. For a connected simple graph G, the following are

equivalent: – G contains an Eulerian circuit.– Every vertex of G has even degree.– The edges of G can be partitioned into edge-disjoint cycles.

• Corollary. A connected simple graph has an Eulerian trail if and only if the number of vertices with odd degree is either zero or two.

• UCSD connection: “An Eulerian path approach to DNA fragment assembly” Pavel A. Pevzner, Haixu Tang, and Michael S. Waterman (2001).

Page 40: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Characterization of Eulerian graphs• Theorem. For a connected simple graph G, the following are

equivalent: – G contains an Eulerian circuit.– Every vertex of G has even degree.– The edges of G can be partitioned into edge-disjoint cycles.

• Corollary. A connected simple graph has an Eulerian trail if and only if the number of vertices with odd degree is either zero or two.

• Application to previous example:

• Four vertices with odd degree!

1

2

3

4

56

abc

d e f

1

3

2

1

4

1

Page 41: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Characterization of Eulerian graphs• Theorem. For a connected simple graph G, the following are

equivalent: – G contains an Eulerian circuit.– Every vertex of G has even degree.– The edges of G can be partitioned into edge-disjoint cycles.

• Corollary. A connected simple graph has an Eulerian trail if and only if the number of vertices with odd degree is either zero or two.

• Proof Idea: Eulerian circuits enter and leave vertices on distinct edges so there have to be an even number of edges incident with each vertex. – If you can partition the graph into edge-disjoint cycles, you can string

them together to get circuit.

Page 42: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Characterization of Eulerian graphs• Theorem. For a connected simple graph G, the following are

equivalent: – G contains an Eulerian circuit.– Every vertex of G has even degree.– The edges of G can be partitioned into edge-disjoint cycles.

• Corollary. A connected simple graph has an Eulerian trail if and only if the number of vertices with odd degree is either zero or two.

• Tweaking the example:

• Two vertices with odd degree!

1

2 4

56

abc

e f

1 1

2

2 4

Page 43: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph, take 2• A Hamiltonian cycle in a graph is a cycle that visits

every vertex exactly once.

Page 44: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph, take 2• A Hamiltonian cycle in a graph is a cycle that visits

every vertex exactly once.• Example with an Eulerian trail – does it have a

Hamiltonian cycle?

1

2 4

56

abc

e f

1 1

2

2 4

Page 45: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph, take 2• A Hamiltonian cycle in a graph is a cycle that visits

every vertex exactly once.• What about now – does this graph have a

Hamiltonian cycle?

1

2 4

56

abc

e f

2 2

2

2 4

g

Page 46: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Visiting “everywhere” on a graph, take 2• A Hamiltonian cycle in a graph is a cycle that visits

every vertex exactly once.• A Hamiltonian graph is a graph that has a

Hamiltonian cycle

Page 47: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Examples

Which of the following is true:A. Both are Hamiltonian and EulerianB. Neither is Hamiltonian and neither is EulerianC. Both are Hamiltonian and not EulerianD. One is Hamiltonian and not Eulerian;

the other is Eulerian and not Hamiltonian E. None of the above.

1 2

3 4

65

1 2 3

4 5 6

Page 48: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Coloring a graph• A proper coloring of a simple graph G = (V,E) is a function λ : V → C, where C is a set of colors, such that{u,v} ∈ E → λ(u) ≠ λ(v).

• There is a proper coloring of a complete graph on 3 vertices using:

A. 1 colorB. 2 colors (but there aren’t any with 1 color)C. 3 colors (but there aren’t any with 1 or 2 colors)D. 4 colors (but there aren’t any with 1 or 2 or 3 colors) E. None of the above.

Page 49: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Another Tree: 3-Coloring a Graph(from Lecture 11)

1

4

3

2

5

5R

1B,2G

3R

4B4G 4R

3B

4G

A graph is legally colored with k colors if each vertex has a color (label) between 1 and k (inclusive), and no adjacent vertices have the same color.

B

G

B

G

R

Three colors: R, G, BBy symmetry, can use any two colors for vertices 1 and 2

Graph Tree of Colorings

Page 50: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Coloring a graph• A proper coloring of a simple graph G = (V,E) is a function λ : V → C, where C is a set of colors, such that{u,v} ∈ E → λ(u) ≠ λ(v)

• There is a proper coloring of any bipartite graph using:A. 1 colorB. 2 colors (but there aren’t any with 1 color)C. 3 colors (but there aren’t any with 1 or 2 colors)D. 4 colors (but there aren’t any with 1 or 2 or 3 colors) E. None of the above.

Page 51: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Coloring problem and chromatic numbers• The coloring problem: for any c > 2, devise an algorithm whose input can

be any simple graph and whose output is the answer to the question “can the graph be properly colored with c colors?”

• Given G and c, deciding (yes/no) whether G can be properly colored with c colors is an NP-complete problem: no known fast algorithm to come up with solution; but easy to check if candidate solution works.

• The chromatic number of a graph G, (G), is the least number of colors needed to properly color G.

• In the previous example, we saw:(Complete graph, n vertices) = n,

(Bipartite graph) = 2

• The Four Color Theorem says that every planar MAP can be properly colored with four colors. Does this mean that (any planar graph) ≤ 4 ?

– Planar Graph: can be drawn on a plane such that edges intersect only at endpoints

Page 52: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

Hard graph problems … we think• Hamiltonian cycle problem: Given a simple graph G, decide

whether G contains a Hamiltonian cycle. • Coloring problem: Given graph G and c > 2, decide whether G

can be properly colored with c colors.• Traveling salesman problem: Given edge-weighted graph G

and a number B, decide whether there is a Hamilton cycle through all vertices of G with total edge cost B.

• Clique problem: Given a simple graph G and a number s, decide whether there are s vertices in G with all C(s,2) possible edges between them being present in G.

Page 53: UCSD CSE 21, Spring 2014 [Section B00] Mathematics for ...vlsicad.ucsd.edu/courses/cse21-s14/slides/cse21-s14-Lecture17... · Mathematics for Algorithm and System Analysis ... •

• P17.1 Eleven people form five study groups. Prove that two students, A and B, can be found such that every study group that contains A also includes B.

• P17.2 In a game, Player 1 and Player 2 take turns adding to the current number any natural number smaller than it. The game begins with the number 2. Whoever reaches the number 1000 wins. Which player has a winning strategy, and what is the winning strategy?

• P17.3 In a game, Player 1 and Player 2 take turns subtracting from the current number any positive power of 2 less than the current number. The game begins with the number 1000. Whoever reaches the number 0 wins. Which player has a winning strategy, and what is the winning strategy?

Problems 17