day10 s16 lecture d - university of california, san diego · 2016. 4. 30. · microsoft powerpoint...

40
Introduction to Graphs http://cseweb.ucsd.edu/classes/sp16/cse21-bd/ April 18, 2016 Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck 1

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Introduction to Graphs

http://cseweb.ucsd.edu/classes/sp16/cse21-bd/April 18, 2016

Russell Impagliazzo and Miles JonesThanks to Janine Tiefenbruck

1

Page 2: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

What is a graph?

A (directed) graph G is • A nonempty set of vertices V, also called nodesand• A set of edges E, each pointing from one vertex

to another (denoted with an arrow)2

Page 3: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Variants of graphsUndirected graph: don't need arrows on edgesif there's an edge from v to w then there's an edge from w to v.Multigraph: undirected graph that may have multiple edges between a pair of nodes. Such edges are called parallel edges.Simple graph: undirected graph with no self-loops (edge from v to v) and no parallel edges.Mixed graph: directed graph that may have multiple edges between a pair of nodes and self loops.

Rosen p. 644

3

Page 4: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Graphs are everywhere

4

Page 5: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Graphs are everywhere

The internet graph5

Page 6: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Graphs are everywhere

Map coloring

6

Page 7: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Graphs are everywhere

Path planning for robots

7

Page 8: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Graphs are everywhere

8

Page 9: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Are these the same graph?

A. Yes: the set of vertices is the same.B. Yes: we can rearrange the vertices so that the pictures look the same.C. No: the pictures are different.D. No: the left graph has a crossing and the right one doesn't.E. None of the above. 9

Page 10: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphs

Diagrams with vertices and edges

How many vertices? For each ordered pair of vertices (v,w)

how many edges go from v to w?

10

Page 11: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphs

Diagrams with vertices and edges

How many vertices? nFor each ordered pair of vertices (v,w)

how many edges go from v to w?

How many ordered pairs of vertices are there?A. nB. n(n-1)C. n2D. n(n-1)/2E. 2n

11

Page 12: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphs

Diagrams with vertices and edges

How many vertices? nFor each ordered pair of vertices (v,w)

how many edges go from v to w?Need to store n(n-1) ints

12

Page 13: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphsAdjacency matrix n x n matrix:

entry in row i and column j is the number of edges from vertex i to vertex j

Rosen p. 66913

Page 14: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphsAdjacency matrix n x n matrix:

entry in row i and column j is the number of edges from vertex i to vertex j

Rosen p. 669

What can you say about the adjacency matrix of a loopless graph?A. It has all zeros. B. All the elements below the diagonal are 1.C. All the elements are even.D. All the elements on the diagonal are 0.E. None of the above.

14

Page 15: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphsAdjacency matrix n x n matrix:

entry in row i and column j is the number of edges from vertex i to vertex j

Rosen p. 669

What can you say about the adjacency matrix of a graph with no parallel edges?A. It has no zeros.B. It is symmetric.C. All the entries above the diagonal are 0.D. All entries are either 0 or 1.E. None of the above.

15

Page 16: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphsAdjacency matrix n x n matrix:

entry in row i and column j is the number of edges from vertex i to vertex j

Rosen p. 669

What can you say about the adjacency matrix of an undirected graph?A. It has no zeros.B. It is symmetric.C. All the entries above the diagonal are 0.D. All entries are either 0 or 1.E. None of the above.

16

Page 17: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing undirected graphsSimple undirected graph:

* Only need to store the adjacency matrix above diagonal.

What's the maximum number of edges a simple undirected graph with n vertices can have?A. n2B. n2/2C. n(n-1)/2D. n(n+1)/2E. n

17

Page 18: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Efficiency?When is an adjacency matrix an inefficient way to store a graph?

High density of edges compared to number of vertices

Low density of edges compared to number of vertices

18

Page 19: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Representing directed graphsAdjacency list (list of lists):

for each vertex v, associate list of all neighbors of v.

Rosen p. 66819

Page 20: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

NeighborsThe neighbors of a vertex v are all the vertices w for which there is an edge whose endpoints are v,w.If two vertices are neighbors then they are called adjacent to one another.

Rosen p. 651

Page 21: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

DegreeThe degree of a vertex in an undirected graph is the total number of edges incident with it, except that a loop contributes twice.

Rosen p. 652

What's the maximum degree of a vertex in this graph?A. 0.B. 1C. 2D. 3E. None of the above.

21

Page 22: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

DegreeThe degree of a vertex in an undirected graph is the total number of edges incident with it, except that a loop contributes twice.

Rosen p. 652

What's the maximum degree of a vertex in this graph?A. 0.B. 1C. 2D. 3E. None of the above.

22

Page 23: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

DegreeWhat's the degree of vertex 5?A. 5B. 3C. 2D. 1E. None of the above.

23

Page 24: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

DegreeWhat's the degree of vertex 0?A. 5B. 3C. 2D. 1E. None of the above.

24

Page 25: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

HandshakesIf there are n people in a room, and each shakes hands with d people, how many handshakes take place?A. nB. dC. ndD. (nd)/2E. None of the above.

25

Page 26: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

HandshakesIf there are n people in a room, and each shakes hands with d people, how many handshakes take place?A. nB. dC. ndD. (nd)/2E. None of the above.

Don't double-count each handshake!26

Page 27: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Handshakes "in" graphsIf a simple graph has n vertices and each vertex has degree d, how many edges are there?

2 |E| = n*d

27

Page 28: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Handshakes "in" graphsIf any graph has n vertices, then

2 |E| = sum of degrees of all vertices

28

Page 29: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Handshakes "in" graphsIf any graph has n vertices, then

2 |E| = sum of degrees of all verticesWhat can we conclude?A. Every degree in the graph is even.B. The number of edges is even.C. The number of vertices with odd degree is even.D. The number self loops is even.E. None of the above.

29

Page 30: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Puzzles

30

Page 31: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Tartaglia's Pouring Problem

You can pour from one cup to another until the first is empty or the second is full.Can we divide the coffee in half? How?A. YesB. No

Large cup: contains 8 ounces, can hold more.Medium cup: is empty, has 5 ounce capacity.Small cup: is empty, has 3 ounce capacity

31

Page 32: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Tartaglia's Pouring Problem

You can pour from one cup to another until the first is empty or the second is full.Can we divide the coffee in half? How?

Hint: configurations (l,m,s) code # ounces in each cupA. YesB. No

Large cup: contains 8 ounces, can hold more.Medium cup: is empty, has 5 ounce capacity.Small cup: is empty, has 3 ounce capacity

32

Page 33: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Die Hard with a vengeance

https://youtu.be/5_MoNu9Mkm4

33

Page 34: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Tartaglia's Pouring Problem

34

Page 35: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Tartaglia's Pouring Problem

Rephrasing the problem:Looking for path from (8,0,0) to (4,4,0)

35

Page 36: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

PathSequence

(v0, e1, v1, e2, v2, … , ek, vk) describes a route through the graph from

start vertex v0

to end vertex vk

36

Page 37: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Tartaglia's Pouring Problem

Rephrasing the problem:(1) Is there a path from (8,0,0) to (4,4,0) ?(2) If so, what's the best path?"Best" means "shortest length"

37

Page 38: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Tartaglia's Pouring Problem

What's the shortest length of a path from (8,0,0) to (4,4,0) ?A. 7B. 8 C. 14D. 16E. None of the above. 38

Page 39: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

Algorithmic questions related to pathsReachability:

Is there a path from vertex v to vertex w?Path:

Find a path from vertex v to vertex w.Distance:

What's the length of the shortest path from vertex v to vertex w?

39

Page 40: day10 S16 Lecture D - University of California, San Diego · 2016. 4. 30. · Microsoft PowerPoint - day10_S16 Lecture D Author: mizej Created Date: 4/29/2016 4:22:09 PM

RemindersHW 4 due tonight 11:59pm via Gradescope.Midterm 1: this Friday, Jan 29 in class covers through Monday’s class* Practice midterm on website/Piazza.* Review session tonight: see website/Piazza.* Seating chart on website/Piazza.* One double-sided handwritten note sheet allowed.* If you have AFA letter, see me as soon as possible.