64
CS 106B, Lecture 23 Graphs This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others

CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.

CS 106B, Lecture 23Graphs

This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others

Page 2: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

2

Plan for Today• Graphs!

– How to model problems using a graph

Page 3: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

3

ADT Flowchart

Page 4: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

4

Google Maps

Source: https://www.google.com/maps

Page 5: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

5

Molecules

http://pngimg.com/uploads/molecule/molecule_PNG50.png

Page 6: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

6

Introducing: The Graph• A graph is a mathematical structure for representing relationships• Consists of nodes (aka vertices) and edges (aka arcs)

– edges are the relationships, nodes are the items

• Examples:– Map: cities (nodes) are connected by roads (edges)– Molecules: atoms (nodes) are connected by bonds (edges)

Page 7: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

7

Graph examples• For each, what are the nodes and what are the edges?

– Web pages with links– Functions in a program that call each other– Airline routes– Facebook friends– Course pre-requisites– Family trees– Paths through a maze

Page 8: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

8

Boggle as a graph• Q: If a Boggle board is a graph, what is a node? What is an edge?

A. Node = letter cube, Edge = Dictionary (lexicon)B. Node = dictionary word; Edge = letter cubeC. Node = letter; Edge = between each letter that is part of a wordD. Node = letter cube; Edge = connection to neighboring cubeE. None of the above

Page 9: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

9

Undirected vs. Directed• Some relationships are

mutual– Facebook

• Some are one-way– Twitter/Instagram– Doesn't mean that all

relationships are non-mutual

Page 10: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

10

Representing Graphs• Two main ways:

– Have each node store the nodes it's connected to (adjacency list)– Have a list of all the edges (edge list)

• The choice depends on the problem you're trying to solve• You can sometimes represent graphs implicitly instead of explicitly

storing the edges and nodes– e.g. Boggle, WordLadder– draw a picture to see the graph more clearly!

• Was the backtracking (wiki links) problem on the midterm a graph problem? How did we represent the graph?

Page 11: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

11

Adjacency List• Map<Node, Vector<Node>>

– or Map<Node, Set<Node>> Node Set<Node>

Page 12: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

12

Adjacency Matrix• Store a boolean grid, rows/columns correspond to nodes

– Alternative to Adjacency List

F T F T F

F F F F T

T F F F T

F F T F F

F F T F F

Page 13: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

13

Edge List• Store a Vector<Edge> (or Set<Edge>)

– Edge struct would have the two nodesVector<Edge>

Page 14: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

14

Edge Properties• Not all edges are created equally

– Some have greater weight• Real life examples:

– Flight costs– Miles on a road– Time spent on a road

• Store a number with each edge corresponding to its weight

Source: https://www.google.com/maps

Page 15: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

15

Paths• I want a job at Google. Do I know anyone who works there? What

about someone who knows someone?• I want to find this word on a board made of letters "next to" each

other (Boggle)• A path is a sequence of nodes with edges between them

connecting two nodes– Could store edges instead of nodes (why?)– You know Jane. Jane knows

Sally. Sally knows knows Sergey Brin, the founder of Google, so the path is:You->Jane->Sally->Sergey

Page 16: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

16

Other graph properties• reachable: Vertex u is reachable from v

if a path exists from u to v.

• connected: A graph is connected if everyvertex is reachable from every other.

• complete: If every vertex has a directedge to every other.

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

a

c

b

d

a

c

b

d

e

Page 17: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

17

Loops and cycles• cycle: A path that begins and ends at the same node.

– example: {b, g, f, c, a} or {V, X, Y, W, U, V}.– example: {c, d, a} or {U, W, V, U}.

– acyclic graph: One that doesnot contain any cycles.

• loop: An edge directly froma node to itself.– Many graphs don't allow loops.

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

Page 18: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

18

Types of Graphs• Boggle?

– undirected, unweighted, cyclic, connected• A molecule?

– undirected, weighted, potentially cyclic, connected• A map of flights?

– directed, weighted, cyclic, perhaps not connected• A tree?

– directed, acyclic graph, not connected

Page 19: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

19

Announcements• Assn. 6 is due Thursday

Page 20: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

20

Finding Paths• Easiest way: Depth-First Search (DFS)

– Recursive backtracking!• Finds a path between two nodes if it exists

– Or can find all the nodes reachable from a node• Where can I travel to starting in San Francisco?• If all my friends (and their friends, and so on) share my post, how many will

eventually see it?

Page 21: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

21

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 22: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

22

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 23: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

23

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 24: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

24

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 25: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

25

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 26: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

26

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 27: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

27

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 28: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

28

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 29: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

29

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 30: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

30

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 31: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

31

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 32: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

32

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 33: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

33

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 34: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

34

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 35: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

35

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 36: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

36

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 37: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

37

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 38: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

38

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 39: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

39

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 40: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

40

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 41: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

41

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 42: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

42

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 43: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

43

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 44: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

44

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 45: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

45

DFS

If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node

Page 46: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

46

DFS Details• In an n-node, m-edge graph, takes O(m + n) time with an adjacency

list– Visit each edge once, visit each node at most once

• Pseudocode:dfs from v1:

mark v1 as seen.for each of v1's unvisited neighbors n:dfs(n)

• How could we modify the pseudocode to look for a specific path?– Recursive Backtracking– Look at maze example from earlier in the course

Page 47: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

47

Finding Shortest Paths• We can find paths between two nodes, but how can we find the

shortest path?– Fewest number of steps to complete a task?– Least amount of edits between two words?

• When have we solved this problem before?

Page 48: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

48

Breadth-First Search (BFS)• Idea: processing a node involves knowing we need to visit all its

neighbors (just like DFS)• Need to keep a TODO list of nodes to process• Which node from our TODO list should we process first if we want

the shortest path?– The first one we saw?– The last one we saw?– A random node?

Page 49: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

49

Breadth-First Search (BFS)• Keep a Queue of nodes as our TODO list• Idea: dequeue a node, enqueue all its neighbors• Still will return the same nodes as reachable, just might have

shorter paths

Page 50: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

50

BFS

a b c d

fe

g h i

queue: a

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 51: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

51

BFS

a b c d

fe

g h i

queue: e, g

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 52: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

52

BFS

a b c d

fe

g h i

queue: e, g

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 53: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

53

BFS

a b c d

fe

g h i

queue: g, f

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 54: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

54

BFS

a b c d

fe

g h i

queue: g, f

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 55: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

55

BFS

a b c d

fe

g h i

queue: f, h

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 56: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

56

BFS

a b c d

fe

g h i

queue: f, h

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 57: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

57

BFS

a b c d

fe

g h i

queue: h

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 58: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

58

BFS

a b c d

fe

g h i

queue: h

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 59: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

59

BFS

a b c d

fe

g h i

queue: i

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 60: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

60

BFS

a b c d

fe

g h i

queue: i

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 61: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

61

BFS

a b c d

fe

g h i

queue: c

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 62: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

62

BFS

a b c d

fe

g h i

queue: c

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

Page 63: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

63

BFS

Dequeue a nodeOtherwise, add all its unseen neighbors to the queue

a b c d

fe

g h i

queue: c

Page 64: CS 106B, Lecture 23 Graphs - Stanford University–Twitter/Instagram –Doesn't mean that all relationships are non-mutual 10 Representing Graphs •Two main ways: –Have each node

64

BFS Details• In an n-node, m-edge graph, takes O(m + n) time with an adjacency

list– Visit each edge once, visit each node at most once

• Pseudocode:bfs from v1:

add v1 to the queue.while queue is not empty:dequeue a node nenqueue n's unseen neighbors

• How could we modify the pseudocode to look for a specific path?