Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... ·...

83
Graph Theory Part One

Transcript of Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... ·...

Page 1: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Graph TheoryPart One

Page 2: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

http://strangemaps.fles.wordpress.com/2007/02/fullinterstatemap-web.jpg

Page 3: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Chemical Bonds

http://4.bp.blogspot.com/-xCtBJ8lKHqA/Tjm0BONWBRI/AAAAAAAAAK4/-mHrbAUOHHg/s1600/Ethanol2.gif

Page 4: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

http://www.toothpastefordinner.com/

Page 5: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

http://www.prospectmagazine.co.uk/wp-content/uploads/2009/09/163_taylor2.jpg

Page 6: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Me too!

Page 7: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 8: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

What's in Common

● Each of these structures consists of● a collection of objects and● links between those objects.

● Goal: fnd a general framework for describing these objects and their properties.

Page 9: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes (or vertices) connected by edges (or arcs)

Page 10: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes (or vertices) connected by edges (or arcs)

Nodes

Page 11: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes (or vertices) connected by edges (or arcs)

Edges

Page 12: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Some graphs are directed.

Page 13: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

CAT SAT RAT

RANMAN

MAT

CAN

Some graphs are undirected.

Page 14: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Going forward, we're primarily going to focus on undirected graphs.

The term “graph” generally refers to undirected graphs with a fnite number of

nodes, unless specifed otherwise.

Page 15: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Formalizing Graphs

● How might we defne a graph mathematically?

● We need to specify● what the nodes in the graph are, and● which edges are in the graph.

● The nodes can be pretty much anything.● What about the edges?

Page 16: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Formalizing Graphs

● An unordered pair is a set {a, b} of two elements a ≠ b. (Remember that sets are unordered).● {0, 1} = {1, 0}

● An undirected graph is an ordered pair G = (V, E), where● V is a set of nodes, which can be anything, and● E is a set of edges, which are unordered pairs of nodes

drawn from V.● A directed graph is an ordered pair G = (V, E),

where● V is a set of nodes, which can be anything, and● E is a set of edges, which are ordered pairs of nodes

drawn from V.

Page 17: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Self-Loops

● An edge from a node to itself is called a self-loop.● In undirected graphs, self-loops are generally not

allowed.● Can you see how this follows from the defnition?

● In directed graphs, self-loops are generally allowed unless specifed otherwise.

✓×

Page 18: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Standard Graph Terminology

Page 19: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

CAT SAT RAT

RANMAN

MAT

CAN

Two nodes are called adjacent if there is an edge between them.

Page 20: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

CAT SAT RAT

RANMAN

MAT

CAN

Two nodes are called adjacent if there is an edge between them.

Page 21: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Using our Formalisms

● Let G = (V, E) be a graph.● Intuitively, two nodes are adjacent if

they're linked by an edge.● Formally speaking, we say that two

nodes u, v ∈ V are adjacent if {u, v} ∈ E.

Page 22: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

SD Nog

Port

SLC

LA

But

Mon

LV

Bar Flag

Phoe

SF Sac

Sea

From

To A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

SF, Sac, LA, Phoe, Flag, Bar, LV, Mon, SLC, But, SeaSF, Sac, LA, Phoe, Flag, Bar, LV, Mon, SLC, But, Sea

(This path has length 10, but visits 11 cities.)

(This path has length 10, but visits 11 cities.)

Page 23: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

SF Sac

Port

Sea But

SLC

Mon

LV

Bar Flag

LA

SD Nog

Phoe

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

Page 24: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Flag

SF

SD Nog

Phoe

Sac

Mon

LV

Bar

LA

Port

Sea But A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

SLC

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

Sac, Port, Sea, But, SLC, Mon, LV, Bar, LA, SacSac, Port, Sea, But, SLC, Mon, LV, Bar, LA, Sac

(This cycle has length nine and

visits nine diferent cities.)

(This cycle has length nine and

visits nine diferent cities.)

From/To

Page 25: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A simple path in a graph is path that does not repeat any nodes or edges.

A simple path in a graph is path that does not repeat any nodes or edges.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

Port

Sea But

SLC

Mon

LV

SD Nog

SacSF

PhoeLA

FlagBar

SF, Sac, LA, Phoe, Flag, Bar, LASF, Sac, LA, Phoe, Flag, Bar, LA

(A path, not a simple path.)

(A path, not a simple path.)

Page 26: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A simple path in a graph is path that does not repeat any nodes or edges.

A simple path in a graph is path that does not repeat any nodes or edges.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

Port

Sea But

SLC

Mon

LV

SD Nog

SacSF

PhoeLA

FlagBar

SF, Sac, LA, Phoe, Flag, Bar, LASF, Sac, LA, Phoe, Flag, Bar, LA

(This path has length six.)

(This path has length six.)

Page 27: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A simple path in a graph is path that does not repeat any nodes or edges.

A simple path in a graph is path that does not repeat any nodes or edges.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

Port

Sea But

SLC

Mon

LV

SD Nog

SacSF

PhoeLA

FlagBar

Page 28: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A simple path in a graph is path that does not repeat any nodes or edges.

A simple path in a graph is path that does not repeat any nodes or edges.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

Sea But

Mon

LV

SD Nog

SF

PhoeLA

FlagBar

Sac SLC

Port

A simple cycle in a graph is cycle that does not repeat any nodes or edges except the frst/last node.

A simple cycle in a graph is cycle that does not repeat any nodes or edges except the frst/last node.Sac, SLC, Port, Sac, SLC, Port, SacSac, SLC, Port, Sac, SLC, Port, Sac

(A cycle, not a simple cycle.)

(A cycle, not a simple cycle.)

Page 29: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A cycle in a graph is a path from a node back to itself. (By convention, a cycle cannot have length zero.)

A simple path in a graph is path that does not repeat any nodes or edges.

A simple path in a graph is path that does not repeat any nodes or edges.

The length of a path is the number of edges in it.

The length of a path is the number of edges in it.

Sea But

Mon

LV

SD Nog

SF

PhoeLA

FlagBar

Sac SLC

Port

A simple cycle in a graph is cycle that does not repeat any nodes or edges except the frst/last node.

A simple cycle in a graph is cycle that does not repeat any nodes or edges except the frst/last node.Sac, SLC, Port, Sac, SLC, Port, SacSac, SLC, Port, Sac, SLC, Port, Sac

(This cycle has length 6.)

(This cycle has length 6.)

Page 30: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

Sea But

Mon

LV

SD Nog

SF

PhoeLA

FlagBar

Sac SLC

PortFrom

To

Two nodes in a graph are called connected if there is a path between them

Two nodes in a graph are called connected if there is a path between them

(These nodes are not

connected.)

(These nodes are not

connected.)

Page 31: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

Sea But

Mon

LV

SD Nog

SF

PhoeLA

FlagBar

Sac SLC

Port

Two nodes in a graph are called connected if there is a path between them

Two nodes in a graph are called connected if there is a path between them

A graph G as a whole is called connected if all pairs of nodes in G are connected.

A graph G as a whole is called connected if all pairs of nodes in G are connected.

(This graph is not connected.)

(This graph is not connected.)

Page 32: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Time-Out for Announcements!

Page 33: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

0 – 21 22 – 26 27 – 31 32 – 36 37 – 41 42 – 46 47 – 51 52 – 56 57+

Problem Set 2 Grades

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

Page 34: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

0 – 21 22 – 26 27 – 31 32 – 36 37 – 41 42 – 46 47 – 51 52 – 56 57+

Problem Set 2 Grades

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

“Great job! Look over your feedback to fid those last few spots to

patch up.”

“Great job! Look over your feedback to fid those last few spots to

patch up.”

Page 35: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

0 – 21 22 – 26 27 – 31 32 – 36 37 – 41 42 – 46 47 – 51 52 – 56 57+

Problem Set 2 Grades

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

“Nice work! Seems like there are a few spots you may wait to get more practice with. Review your feedback, aid let us kiow how we cai help!”

“Nice work! Seems like there are a few spots you may wait to get more practice with. Review your feedback, aid let us kiow how we cai help!”

Page 36: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

0 – 21 22 – 26 27 – 31 32 – 36 37 – 41 42 – 46 47 – 51 52 – 56 57+

Problem Set 2 Grades

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

“You’re oi the right track, but it looks like somethiig hasi’t quite

clicked yet. Come by ofce hours with questiois – we’re happy to help out!”

“You’re oi the right track, but it looks like somethiig hasi’t quite

clicked yet. Come by ofce hours with questiois – we’re happy to help out!”

Page 37: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

0 – 21 22 – 26 27 – 31 32 – 36 37 – 41 42 – 46 47 – 51 52 – 56 57+

Problem Set 2 Grades

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

75th Percentile: 55 / 63 (87%)50th Percentile: 49 / 63 (78%)25th Percentile: 41 / 63 (65%)

“You’re iot where you ieed to be iow, but we kiow you cai do this. Stop by ofce hours aid let us kiow

what we cai do to help out!”

“You’re iot where you ieed to be iow, but we kiow you cai do this. Stop by ofce hours aid let us kiow

what we cai do to help out!”

Page 38: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Next Steps

● Regardless of how you did on the problem set, make sure you understand all the feedback you’ve received, especially on the frst-order translations and the proofs.● Like, seriously, do this. You don’t want to

make the same mistakes on the midterm!● Ask questions on Piazza or stop by ofice

hours if you have questions – we’re happy to help out.

Page 39: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Problem Set Three

● The Problem Set Three checkpoint has been graded.● Please, please, please review your

feedback! That problem was tricky and a lot of people had a lot of trouble with it.

● Remaining problems are due on Friday at 2:30PM. Be strategic about taking late days.

Page 40: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Midterm Exam Logistics

● The frst midterm exam is next Monday, October 23rd, from 7:00PM – 10:00PM. Locations are divvied up by last (family) name:● Abb – Lop: Go to Cubberly Auditorium.● Mac – Zwa: Go to Hewlett 200.

● You’re responsible for Lectures 00 – 05 and topics covered in PS1 – PS2. Later lectures (relations forward) and problem sets (PS3 onward) won’t be tested here.

● The exam is closed-book, closed-computer, and limited-note. You can bring a double-sided, 8.5” × 11” sheet of notes with you to the exam, decorated however you’d like.

Page 41: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Practice Midterm Exam

● To help you prepare for the midterm, we'll be holding a practice midterm exam tonight from 7PM – 10PM in Hewlett 200.

● The practice midterm exam is an actual midterm we gave out in a previous quarter. It’s probably the best indicator of what you should expect to see.

● Course staf will be on hand to answer your questions.

● Can't make it? We'll release the practice exam and solutions online. Set up your own practice exam time with a small group and work through it under realistic conditions!

Page 42: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Midterm Practice

● On the course website, you’ll fnd● Extra Practice Problems 1, with solutions;● Practice Midterm 1, with solutions;● Practice Midterm 2, with solutions;● a link to the CS103A website, which has

problems with solutions; and● Practice Midterm 3, with solutions (later this

evening).● Need more practice? Let us know how

we can help!

Page 43: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Your Questions

Page 44: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

“Why did you introduce coding into CS103 for this quarter?”

1. They provide iistait feedback. If you mess up a step ii a proof, you may have to wait a full week before you hear back about it. If your code doesi’t pass all our tests, you fid out withii a few miiutes aid cai go back to revise your approach.

2. They solidify core coicepts. You cai’t write code to check for whether S ( (⊆ ℘ ℘ T)) uiless you uiderstaid what a power set is aid what the subset relatioi looks like.

3. They provide a CS perspective oi math coicepts. Writiig code to evaluate a frst-order formula helps you build a better uiderstaidiig for difereit quaitifers aid coiiectives meai, which (ideally!) will help you better uiderstaid iew frst-order defiitiois goiig forward.

4. They separate “do I uiderstaid the core coicepts?” from “cai I execute a mathematical proof?” If you cai get the code workiig for these assigimeits, it meais that you have a solid uiderstaidiig of the major terms that you cai thei build from ii proofwritiig.

1. They provide iistait feedback. If you mess up a step ii a proof, you may have to wait a full week before you hear back about it. If your code doesi’t pass all our tests, you fid out withii a few miiutes aid cai go back to revise your approach.

2. They solidify core coicepts. You cai’t write code to check for whether S ( (⊆ ℘ ℘ T)) uiless you uiderstaid what a power set is aid what the subset relatioi looks like.

3. They provide a CS perspective oi math coicepts. Writiig code to evaluate a frst-order formula helps you build a better uiderstaidiig for difereit quaitifers aid coiiectives meai, which (ideally!) will help you better uiderstaid iew frst-order defiitiois goiig forward.

4. They separate “do I uiderstaid the core coicepts?” from “cai I execute a mathematical proof?” If you cai get the code workiig for these assigimeits, it meais that you have a solid uiderstaidiig of the major terms that you cai thei build from ii proofwritiig.

Page 45: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

“Good frst date ideas? Asking for a fend.”

“Returi to the kiigdom of darkiess from which

you came, you feid! You shall iot coisort with

humaikiid!”

“Returi to the kiigdom of darkiess from which

you came, you feid! You shall iot coisort with

humaikiid!”

Page 46: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

“Good frst date ideas? Asking for a friend.”

The low-cost, sceiic optioi: walk the dish, play arouid with the giait magiet iext to the Mech E buildiig, thei go up the fre escape behiid Buildiig 260 with a laptop, eijoy the suiset, aid watch a movie!

The urbai explorer optioi: Head up to SF (Caltraii to Millbrae, BART to 24th Street Missioi) aid do Urbai Putt or Missioi Bowliig Club, thei stop of at a local

restaurait.

The low-cost, sceiic optioi: walk the dish, play arouid with the giait magiet iext to the Mech E buildiig, thei go up the fre escape behiid Buildiig 260 with a laptop, eijoy the suiset, aid watch a movie!

The urbai explorer optioi: Head up to SF (Caltraii to Millbrae, BART to 24th Street Missioi) aid do Urbai Putt or Missioi Bowliig Club, thei stop of at a local

restaurait.

Page 47: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Back to CS103!

Page 48: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connected Components

Page 49: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Page 50: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connected Components

● Let G = (V, E) be a graph. For each v ∈ V, the connected component containing v is the set

[v] = { x ∈ V | v is connected to x }● Intuitively, a connected component is a “piece”

of a graph in the sense we just talked about.● Question: How do we know that this

particular defnition of a “piece” of a graph is a good one?

● Goal: Prove that any graph can be broken apart into diferent connected components.

Page 51: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

We’re trying to reason about some way of partitioning the nodes in a graph into

diferent groups.

What structure have we studied that captures the idea of a partition?

Page 52: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connectivity

● Claim: For any graph G, the “is connected to” relation is an equivalence relation.● Is it refexive?● Is it symmetric?● Is it transitive?

Page 53: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connectivity

Claim: For any graph G, the “is connected to” relation is an equivalence relation.● Is it refexive?

Is it symmetric?

Is it transitive?

∀v ∈ V. Conn(v, v)∀v ∈ V. Conn(v, v)

vA path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

A path in a graph G = (V, E) is a sequence of one or more nodes v₁, v₂, v₃, …, vₙ such that any two consecutive nodes in the sequence are adjacent.

Page 54: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connectivity

Claim: For any graph G, the “is connected to” relation is an equivalence relation.

Is it refexive?● Is it symmetric?

Is it transitive?

∀x ∈ V. ∀y ∈ V. (Conn(x, y) → Conn(y, x))∀x ∈ V. ∀y ∈ V. (Conn(x, y) → Conn(y, x))

yx

Page 55: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connectivity

Claim: For any graph G, the “is connected to” relation is an equivalence relation.

Is it refexive?● Is it symmetric?

Is it transitive?

∀x ∈ V. ∀y ∈ V. (Conn(x, y) → Conn(y, x))∀x ∈ V. ∀y ∈ V. (Conn(x, y) → Conn(y, x))

yx

Page 56: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Connectivity

Claim: For any graph G, the “is connected to” relation is an equivalence relation.

Is it refexive?

Is it symmetric?● Is it transitive?

∀x ∈ V. ∀y ∈ V. ∀z ∈ V. (Conn(x, y) ∧ Conn(y, z) → Conn(x, z))∀x ∈ V. ∀y ∈ V. ∀z ∈ V. (Conn(x, y) ∧ Conn(y, z) → Conn(x, z))

x

y

z

Page 57: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Theorem: Let G = (V, E) be a graph. Then the connectivity relationover V is an equivalence relation.

Proof: Consider an arbitrary graph G = (V, E). We will prove thatthe connectivity relation over V is refexive, symmetric, andtransitive.

To show that connectivity is refexive, consider any v ∈ V. Then the singleton path v is a path from v to itself. Therefore, v is connected to itself, as required.

To show that connectivity is symmetric, consider any x, y ∈ V where x is connected to y. We need to show that y is connected to x. Since x is connected to y, there is some path x, v₁, …, vₙ, y from x to y. Then y, vₙ, …, v₁, x is a path from y back to x, so y is connected to x.

Finally, to show that connectivity is transitive, let x, y, z ∈ V be arbitrary nodes where x is connected to y and y is connected to z. We will prove that x is connected to z. Since x is connected to y, there is a path x, u₁, …, uₙ, y from x to y. Since y is connected to z, there is a path y, v₁, …, vₖ, z from y to z. Then the pathx, u₁, …, uₙ, y, v₁, …, vₖ, z goes from x to z. Thus x is connected to z, as required. ■

Page 58: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Putting Things Together

● Earlier, we defned the connected component of a node v to be

[v] = { x ∈ V | v is connected to x }● Connectivity is an equivalence relation! So

what’s the equivalence class of a node v with respect to connectivity?

[v] = { x ∈ V | v is connected to x }● Connected components are equivalence

classes of the connectivity relation!

Page 59: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Theorem: If G = (V, E) is a graph, then every node in Gbelongs to exactly one connected component of G.

Proof: Let G = (V, E) be an arbitrary graph and let v ∈ V beany node in G. The connected components of G are justthe equivalence classes of the connectivity relation in G.The Fundamental Theorem of Equivalence Relationsguarantees that v belongs to exactly one equivalenceclass of the connectivity relation. Therefore, v belongs toexactly one connected component in G. ■

Page 60: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Planar Graphs

Page 61: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 62: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 63: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

21

4 3

Page 64: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

2

1

4 3

Page 65: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

This graph is called the utility graph. There is no way to draw it in the plane

without edges crossing.

This graph is called the utility graph. There is no way to draw it in the plane

without edges crossing.

Page 66: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A graph is called a planar graph if there is some way to draw it in a 2D plane without

any of the edges crossing.

Page 67: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A Fun (And Strangely Addicting) Game:http://planarity.net/

Page 68: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 69: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 70: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 71: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 72: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Graph Coloring

Page 73: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 74: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember
Page 75: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

● Intuitively, a k-coloring of a graph G = (V, E) is a way to color each node in V one of k diferent colors such that no two adjacent nodes in V are the same color.

● Formally, a k-coloring of a graph G = (V, E) is a function

f : V → {1, 2, …, k}

such that

∀u ∈ V. ∀v ∈ V. ({u, v} ∈ E → f(u) ≠ f(v))

A graph G is called k-colorable if a k-coloring exists of G.

The smallest k for which G is k-colorable is its chromatic number.

The chromatic number of a graph G is denoted χ(G), from the Greek χρώμα, meaning “color.”

Graph Coloring

1

3

2

4 3

1

Page 76: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

● Intuitively, a k-coloring of a graph G = (V, E) is a way to color each node in V one of k diferent colors such that no two adjacent nodes in V are the same color.

● Formally, a k-coloring of a graph G = (V, E) is a function

f : V → {1, 2, …, k}

such that

∀u ∈ V. ∀v ∈ V. ({u, v} ∈ E → f(u) ≠ f(v))

A graph G is called k-colorable if a k-coloring exists of G.

The smallest k for which G is k-colorable is its chromatic number.

The chromatic number of a graph G is denoted χ(G), from the Greek χρώμα, meaning “color.”

Graph Coloring

Although this is the formal defnition of ak-coloring, you rarely see it used in proofs. It's more common to just talk about assigning colors to nodes. However, this defnition is super useful if you want to write programs to reason about graph colorings!

Although this is the formal defnition of ak-coloring, you rarely see it used in proofs. It's more common to just talk about assigning colors to nodes. However, this defnition is super useful if you want to write programs to reason about graph colorings!

Page 77: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

● Intuitively, a k-coloring of a graph G = (V, E) is a way to color each node in V one of k diferent colors such that no two adjacent nodes in V are the same color.

● Formally, a k-coloring of a graph G = (V, E) is a function

f : V → {1, 2, …, k}

such that

∀u ∈ V. ∀v ∈ V. ({u, v} ∈ E → f(u) ≠ f(v))● A graph G is called k-colorable if a k-coloring of G exists.● The smallest k for which G is k-colorable is its chromatic

number.● The chromatic number of a graph G is denoted χ(G), from the

Greek χρώμα, meaning “color.”

Graph Coloring

Page 78: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Graph Coloring

Page 79: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Theorem (Four-Color Theorem): Every planar graph is 4-colorable.

Page 80: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

● 1850s: Four-Color Conjecture posed.

● 1879: Kempe proves the Four-Color Theorem.

● 1890: Heawood fnds a faw in Kempe's proof.

● 1976: Appel and Haken design a computer program that proves the Four-Color Theorem. The program checked 1,936 specifc cases that are “minimal counterexamples;” any counterexample to the theorem must contain one of the 1,936 specifc cases.

● 1980s: Doubts rise about the validity of the proof due to errors in the software.

● 1989: Appel and Haken revise their proof and show it is indeed correct. They publish a book including a 400-page appendix of all the cases to check.

● 1996: Roberts, Sanders, Seymour, and Thomas reduce the number of cases to check down to 633.

● 2005: Werner and Gonthier repeat the proof using an established automatic theorem prover (Coq), improving confdence in the truth of the theorem.

Page 81: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Philosophical Question: Is a theorem true if no human has ever read the proof?

Page 82: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

A Fantastic Video on a Cool Theorem:https://youtu.be/-9OUyo8NFZg

Page 83: Graph Theory - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1182/... · Formalizing Graphs An unordered pair is a set {a, b} of two elements a ≠ b. (Remember

Next Time

● The Pigeonhole Principle● A simple, powerful, versatile theorem.

● Graph Theory Party Tricks● Applying math to graphs of people!