Introduction to Graphs Russell Impagliazzo and Miles Jones Thanks - - PowerPoint PPT Presentation

introduction to graphs
SMART_READER_LITE
LIVE PREVIEW

Introduction to Graphs Russell Impagliazzo and Miles Jones Thanks - - PowerPoint PPT Presentation

Introduction to Graphs Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck http://cseweb.ucsd.edu/classes/sp16/cse21-bd/ April 18, 2016 1 What is a graph? A (directed) graph G is A nonempty set of vertices V, also called


slide-1
SLIDE 1

Introduction to Graphs

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

1

slide-2
SLIDE 2

What is a graph?

A (directed) graph G is

  • A nonempty set of vertices V, also called nodes

and

  • A set of edges E, each pointing from one vertex

to another (denoted with an arrow)

2

slide-3
SLIDE 3

Variants of graphs

Undirected graph: don't need arrows on edges if 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

slide-4
SLIDE 4

Graphs are everywhere

4

slide-5
SLIDE 5

Graphs are everywhere

The internet graph

5

slide-6
SLIDE 6

Graphs are everywhere

Map coloring

6

slide-7
SLIDE 7

Graphs are everywhere

Path planning for robots

7

slide-8
SLIDE 8

Graphs are everywhere

8

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Representing directed graphs

J

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

slide-11
SLIDE 11

Representing directed graphs

J

Diagrams with vertices and edges How many vertices? n For each ordered pair of vertices (v,w) how many edges go from v to w?

How many ordered pairs

  • f vertices are there?
  • A. n
  • B. n(n-1)
  • C. n2
  • D. n(n-1)/2
  • E. 2n

11

slide-12
SLIDE 12

Representing directed graphs

J

Diagrams with vertices and edges How many vertices? n For each ordered pair of vertices (v,w) how many edges go from v to w? Need to store n(n-1) ints

12

slide-13
SLIDE 13

Representing directed graphs

Adjacency 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

13

slide-14
SLIDE 14

Representing directed graphs

Adjacency 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

slide-15
SLIDE 15

Representing directed graphs

Adjacency 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

slide-16
SLIDE 16

Representing directed graphs

Adjacency 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

slide-17
SLIDE 17

Representing undirected graphs

Simple 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. n2
  • B. n2/2
  • C. n(n-1)/2
  • D. n(n+1)/2
  • E. n

17

slide-18
SLIDE 18

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

slide-19
SLIDE 19

Representing directed graphs

Adjacency list (list of lists): for each vertex v, associate list of all neighbors of v.

Rosen p. 668

19

slide-20
SLIDE 20

Neighbors

The 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

  • ne another.

Rosen p. 651

slide-21
SLIDE 21

Degree

The 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. 1
  • C. 2
  • D. 3
  • E. None of the above.

21

slide-22
SLIDE 22

Degree

The 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. 1
  • C. 2
  • D. 3
  • E. None of the above.

22

slide-23
SLIDE 23

Degree

What's the degree of vertex 5?

  • A. 5
  • B. 3
  • C. 2
  • D. 1
  • E. None of the above.

23

slide-24
SLIDE 24

Degree

What's the degree of vertex 0?

  • A. 5
  • B. 3
  • C. 2
  • D. 1
  • E. None of the above.

24

slide-25
SLIDE 25

Handshakes

If there are n people in a room, and each shakes hands with d people, how many handshakes take place?

  • A. n
  • B. d
  • C. nd
  • D. (nd)/2
  • E. None of the above.

25

slide-26
SLIDE 26

Handshakes

If there are n people in a room, and each shakes hands with d people, how many handshakes take place?

  • A. n
  • B. d
  • C. nd
  • D. (nd)/2
  • E. None of the above.

Don't double-count each handshake!

26

slide-27
SLIDE 27

Handshakes "in" graphs

If a simple graph has n vertices and each vertex has degree d, how many edges are there? 2 |E| = n*d

27

slide-28
SLIDE 28

Handshakes "in" graphs

If any graph has n vertices, then 2 |E| = sum of degrees of all vertices

28

slide-29
SLIDE 29

Handshakes "in" graphs

If any graph has n vertices, then 2 |E| = sum of degrees of all vertices

What 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

slide-30
SLIDE 30

Puzzles

30

slide-31
SLIDE 31

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. Yes B. 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

slide-32
SLIDE 32

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 cup A. Yes B. 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

slide-33
SLIDE 33

Die Hard with a vengeance https://youtu.be/5_MoNu9Mkm4

33

slide-34
SLIDE 34

Tartaglia's Pouring Problem

34

slide-35
SLIDE 35

Tartaglia's Pouring Problem

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

35

slide-36
SLIDE 36

Path

Sequence (v0, e1, v1, e2, v2, … , ek, vk) describes a route through the graph from start vertex v0 to end vertex vk

36

slide-37
SLIDE 37

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

slide-38
SLIDE 38

Tartaglia's Pouring Problem

What's the shortest length of a path from (8,0,0) to (4,4,0) ?

  • A. 7
  • B. 8
  • C. 14
  • D. 16
  • E. None of the above.

38

slide-39
SLIDE 39

Algorithmic questions related to paths

Reachability: 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

slide-40
SLIDE 40

Reminders

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