54
Graph Theory Part One

Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Graph TheoryPart One

Page 2: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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

Page 3: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Chemical Bonds

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

Page 4: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

http://www.toothpastefordinner.com/

Page 5: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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

Page 6: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence
Page 7: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence
Page 8: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

What's in Common

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

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

Page 9: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Some graphs are directed.

Page 11: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

CAT SAT RAT

RANMAN

MAT

CAN

Some graphs are undirected.

Page 12: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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

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

nodes, unless specified otherwise.

Page 13: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Formalizing Graphs

● How might we define 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 14: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 15: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

● An unordered pair is a set {a, b} of two elements a ≠ b.● 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.

● An unordered pair is a set {a, b} of two elements a ≠ b.● 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.

How many of these drawings are of valid undirected graphs?How many of these drawings are of valid undirected graphs?

A

C

B

D

A

C

B

D

A

C

B

D

A

C

B

D

ABBBC

Page 16: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 definition?

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

✓×

Page 17: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Standard Graph Terminology

Page 18: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

CAT SAT RAT

RANMAN

MAT

CAN

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

Page 19: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 20: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 the path v₁, …, vₙis n – 1.

The length of the path v₁, …, vₙis n – 1.

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 21: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 the path v₁, …, vₙis n – 1.

The length of the path v₁, …, vₙis n – 1.

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 different cities.)

(This cycle has length nine and

visits nine different cities.)

From/To

Page 22: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 the path v₁, …, vₙis n – 1.

The length of the path v₁, …, vₙis n – 1.

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 23: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 the path v₁, …, vₙis n – 1.

The length of the path v₁, …, vₙis n – 1.

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 24: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 the path v₁, …, vₙis n – 1.

The length of the path v₁, …, vₙis n – 1.

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 first/last node.

A simple cycle in a graph is cycle that does not repeat any nodes or edges except the first/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 25: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 the path v₁, …, vₙis n – 1.

The length of the path v₁, …, vₙis n – 1.

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 first/last node.

A simple cycle in a graph is cycle that does not repeat any nodes or edges except the first/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 26: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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. No Grand Canyon for

you.)

(These nodes are not connected. No Grand Canyon for

you.)

Page 27: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 28: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Time-Out for Announcements!

Page 29: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

0 – 56 57 – 60 61 – 64 65 – 68 69 – 72 73 – 76 77 – 80 81 – 84 85 –

Problem Set 2 Grades

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

Average grades are malicious lies. Ignore them.

Standard deviations are malicious lies. Ignore them.

Average grades are malicious lies. Ignore them.

Standard deviations are malicious lies. Ignore them.

Page 30: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

0 – 56 57 – 60 61 – 64 65 – 68 69 – 72 73 – 76 77 – 80 81 – 84 85 –

Problem Set 2 Grades

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

“Excellent job! Look over your feedback to find those last few spots to

patch up.”

“Excellent job! Look over your feedback to find those last few spots to

patch up.”

Page 31: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

0 – 56 57 – 60 61 – 64 65 – 68 69 – 72 73 – 76 77 – 80 81 – 84 85 –

Problem Set 2 Grades

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

“Good job! Take a look at your feedback to see what areas you need to focus

on for next time.”

“Good job! Take a look at your feedback to see what areas you need to focus

on for next time.”

Page 32: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

0 – 56 57 – 60 61 – 64 65 – 68 69 – 72 73 – 76 77 – 80 81 – 84 85 –

Problem Set 2 Grades

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

“A solid performance! Seems like there are a few spots you may want to get more practice with. Review your feedback, and

let us know how we can help!”

“A solid performance! Seems like there are a few spots you may want to get more practice with. Review your feedback, and

let us know how we can help!”

Page 33: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

0 – 56 57 – 60 61 – 64 65 – 68 69 – 72 73 – 76 77 – 80 81 – 84 85 –

Problem Set 2 Grades

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

“You’re on the right track, but it looks like something hasn’t quite

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

“You’re on the right track, but it looks like something hasn’t quite

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

Page 34: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

0 – 56 57 – 60 61 – 64 65 – 68 69 – 72 73 – 76 77 – 80 81 – 84 85 –

Problem Set 2 Grades

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

75th Percentile: 84 / 88 (96%)50th Percentile: 80 / 88 (91%)25th Percentile: 73 / 88 (83%)

“You’re not where you need to be now, but we know you can do this. Stop by office hours and let us know

what we can do to help out!”

“You’re not where you need to be now, but we know you can do this. Stop by office hours and let us know

what we can do to help out!”

Page 35: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Problem Sets● Regardless of how you did on the problem set, make sure you understand

all the feedback you’ve received.● Your action items:

● Review your feedback on PS2. The exam tests these topics; make sure you understand where you need to improve.

● Review your feedback on the PS3 checkpoint. The rest of PS3 builds on these ideas.

● Read the PS2 solutions. Make sure you understand why each question was there!

● Read the PS3 checkpoint solutions. The first two problems, in particular, have some subtleties to them.

● Follow up with questions. Having trouble understanding a particular problem? Not sure how to solve something? Confused by your feedback? Ping us on Piazza or stop by office hours!

● A quick note: please double-check that, if you worked with a partner, you have scores recorded under your name. If not, contact us and we can get that sorted out.

Page 36: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Midterm Exam Logistics

● Our first midterm exam is next Monday, October 21st, from 7:00PM – 10:00PM. Locations are divvied up by last (family) name:● A – G: Go to Hewlett 201.● H – Z: 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 may ask follow-up questions on both written and coding problems.

● 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.

● Students with OAE accommodations: you should have heard back from us with an alternate exam time. If you didn’t hear back – or that alternate time doesn’t work for you – please contact us as soon as possible.

Page 37: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Practice Midterm Exam

● We will be holding a practice midterm exam tonight in Cemex Auditorium from7:00PM – 10:00PM.

● You should go to this exam unless you have a hard conflict. It’s the most realistic practice you can get.

● Can’t make it? No worries! We’ll post the exam and solutions up on the course website.

Page 38: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

More Practice

● Want more practice? We’ve posted online● Extra Practice Problems 1;● three practice midterms, with solutions; and● the CS103A materials for the past few

weeks.● We’ll also post the fourth and final

practice midterm (the one we’re giving out tonight).

Page 39: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Your Questions

Page 40: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

“What are some of the problems you see minorities facing in computer science, and

how do you think we can address the underrepresentation of minorities in CS?”

As our society continues to become more heterogeneous, there’s going to be a lot more variance in people’s life experiences, backgrounds, interests, preferences, and constraints. Much of what needs to be done, I think, is going to be in critically

reassessing implicit assumptions at each stage in the pipeline (what do we assume people know when we teach them? what skills should

folks have when going into industry? what contributions are valuable? what financial considerations must be made?)

I will extend an open invite – about once a year I’ll meet with student groups like SOLE, SBSE, FLIP, etc. to get input about

what we’re doing that works and what we need to fix. Interested in chatting about this? Hit me up over email!

As our society continues to become more heterogeneous, there’s going to be a lot more variance in people’s life experiences, backgrounds, interests, preferences, and constraints. Much of what needs to be done, I think, is going to be in critically

reassessing implicit assumptions at each stage in the pipeline (what do we assume people know when we teach them? what skills should

folks have when going into industry? what contributions are valuable? what financial considerations must be made?)

I will extend an open invite – about once a year I’ll meet with student groups like SOLE, SBSE, FLIP, etc. to get input about

what we’re doing that works and what we need to fix. Interested in chatting about this? Hit me up over email!

Page 41: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Back to CS103!

Page 42: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Connected Components

Page 43: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence
Page 44: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence
Page 45: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Page 46: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 definition of a “piece” of a graph is a good one?

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

Page 47: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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

different groups.

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

Page 48: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Connectivity

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

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 49: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Connectivity

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

Is it reflexive?● 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 50: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Connectivity

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

Is it reflexive?

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 51: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

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 reflexive, symmetric, andtransitive.

To show that connectivity is reflexive, 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 52: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Putting Things Together

● Earlier, we defined 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]conn = { x ∈ V | v is connected to x }

● Connected components are equivalence classes of the connectivity relation!

Page 53: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Putting Things Together

● Think back – what does an equivalence relation R do to a set A?

● Answer: It partitions the elements into equivalence classes.

● Since connectivity is an equivalence relation over nodes, its connected components partition the nodes of a graphs.

● Stated differently – every node in a graph belongs to exactly one connected component!

Page 54: Graph Theory - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/10/Sm… · LV Bar Flag Phoe SF Sac Sea F r o m To A path in a graph G = (V, E) is a sequence

Next Time

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

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