Top Banner
This document is copyright (C) Stanford Computer Science and Nick Troccoli, 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, Marty Stepp, Ashley Taylor and others. CS 106X, Lecture 22 Graphs; BFS; DFS reading: Programming Abstractions in C++, Chapter 18
80

CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

May 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

This document is copyright (C) Stanford Computer Science and Nick Troccoli, 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, Marty Stepp, Ashley Taylor and others.

CS 106X, Lecture 22Graphs; BFS; DFS

reading:Programming Abstractions in C++, Chapter 18

Page 2: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

2

Plan For Today• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Page 3: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

3

Plan For Today• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Page 4: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

4

Graphs

Graphs can model:- Sites and links on the web- Disease outbreaks- Social networks- Geographies- Task and dependency graphs- and more…

A graph consists of a set of nodes connected by edges.

Page 5: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

5

GraphsA graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 6: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

6

Graphs

5

3

62

11

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 7: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

7

Graphs

2

1

22

11

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 8: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

8

Graphs

3

2

40

00

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 9: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

9

Graphs

A

C

DB

EF

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 10: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

10

Graphs

A

C

DB

EF

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 11: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

11

Graphs

A

C

DB

EF

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 12: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

12

Graphs

A

C

DB

EF

A graph consists of a set of nodes connected by edges.

Nodes: degree (# connected edges)Nodes: in-degree (directed, # in-edges)Nodes: out-degree (directed, # out-edges)

Path: sequence of nodes/edges from one node to another Path: node x is reachable from node yif a path exists from y to x.Path: a cycle is a path that starts and ends at the same nodePath: a loop is an edge that connects a node to itself

Page 13: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

13

Graph PropertiesA graph is connected if every node is reachable from every other node.

Page 14: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

14

Graph PropertiesA graph is complete if every node has a direct edge to every other node.

Page 15: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

15

Graph PropertiesA graph is acyclic if it does not contain any cycles.

Page 16: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

16

Graph PropertiesA graph is directed if its edges have direction, or undirected if its edges do not have direction (aka are bidirectional).

directed undirected

Page 17: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

17

Graph Properties• Connected or unconnected• Acyclic• Directed or undirected• Weighted or unweighted• Complete

Page 18: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

18

Plan For Today• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Page 19: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

19

Twitter Influence• Twitter lets a user follow another user to see their

posts.• Following is directional (e.g. I can follow you but you

don’t have to follow me back L)• Let’s define being influential as having a high number

of followers-of-followers.– Reasoning: doesn’t just matter how many people follow

you, but whether the people who follow you reach a large audience.

• Write a function mostInfluential that reads a file of Twitter relationships and outputs the most influential user.

https://about.twitter.com/en_us/company/brand-resources.html

Page 20: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

20

BasicGraph members#include "basicgraph.h" // a directed, weighted graph

g.addEdge(v1, v2); adds an edge between two vertexesg.addVertex(name); adds a vertex to the graphg.clear(); removes all vertexes/edges from the graphg.getEdgeSet()g.getEdgeSet(v)

returns all edges, or all edges that start at v,as a Set of pointers

g.getNeighbors(v) returns a set of all vertices that v has an edge tog.getVertex(name) returns pointer to vertex with the given nameg.getVertexSet() returns a set of all vertexesg.isNeighbor(v1, v2) returns true if there is an edge from vertex v1 to v2g.isEmpty() returns true if queue contains no vertexes/edgesg.removeEdge(v1, v2); removes an edge from the graphg.removeVertex(name); removes a vertex from the graphg.size() returns the number of vertexes in the graph

g.toString() returns a string such as "{a, b, c, a -> b}"

Page 21: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

21

Plan For Today• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Page 22: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

22

Searching for paths• Searching for a path from one vertex to another:

– Sometimes, we just want any path (or want to know there is a path).– Sometimes, we want to minimize path length (# of edges).– Sometimes, we want to minimize path cost (sum of edge weights).

ORD PVD

MIADFW

SFO

LAX

LGAHNL

$50

$80

$140$170

$70

$100$110

$120$60

$250

$200

$500

$130

Page 23: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

23

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 24: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

24

Depth-first search (18.4)• depth-first search (DFS): Finds a path between two vertices by

exploring each possible path as far as possible before backtracking.– Often implemented recursively.– Many graph algorithms involve visiting or marking vertices.

• DFS from a to h (assuming A-Z order) visits:– a

• b• e

• fc

i• d

• g• h

– path found: {a, d, g, h}

a

e

b c

hg

d f

i

Page 25: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

25

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 26: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

26

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 27: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

27

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 28: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

28

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 29: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

29

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 30: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

30

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 31: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

31

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 32: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

32

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 33: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

33

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 34: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

34

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 35: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

35

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 36: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

36

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 37: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

37

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 38: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

38

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 39: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

39

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 40: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

40

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 41: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

41

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 42: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

42

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 43: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

43

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 44: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

44

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 45: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

45

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 46: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

46

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 47: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

47

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 48: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

48

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 49: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

49

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 50: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

50

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 51: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

51

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 52: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

52

DFS

Mark current as visitedExplore all the unvisited nodes from this node

A B C D

FE

IHG

Page 53: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

53

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?

Page 54: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

54

DFS that finds pathdfs from v1 to v2:

mark v1 as visited, and add to path.perform a dfs from each of v1'sunvisited neighbors n to v2:

if dfs(n, v2) succeeds: a path is found! yay!if all neighbors fail: remove v1 from path.

• To retrieve the DFS path found, pass a collection parameter to each call and choose-explore-unchoose.

a

e

b c

hg

d f

i

Page 55: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

55

DFS observations• discovery: DFS is guaranteed to

find a path if one exists.

• retrieval: It is easy to retrieve exactlywhat the path is (the sequence of edges taken) if we find it– choose - explore - unchoose

• optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path– Example: dfs(a, i) returns {a, b, e, f, c, i} rather than {a, d, h, i}.

a

e

b c

hg

d f

i

Page 56: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

56

Plan For Today• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Page 57: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

57

Announcements• Assignment 7 will go out this Friday, is due Wed. after break

– Short graphs assignment (Google Maps!), implementing algorithms from this week

• Assignment 8 will go out the Wed. after break, is due the last day ofclass (Fri)– Graphs and inheritance assignment (Excel!)

Page 58: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

58

Plan For Today• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Page 59: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

59

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 60: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

60

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

Page 61: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

61

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 62: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

62

BFS

a b c d

fe

g h i

queue: a

Dequeue a nodeadd all its unseen neighbors to the queue

Page 63: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

63

BFS

a b c d

fe

g h i

queue: e, g

Dequeue a nodeadd all its unseen neighbors to the queue

Page 64: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

64

BFS

a b c d

fe

g h i

queue: e, g

Dequeue a nodeadd all its unseen neighbors to the queue

Page 65: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

65

BFS

a b c d

fe

g h i

queue: g, f

Dequeue a nodeadd all its unseen neighbors to the queue

Page 66: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

66

BFS

a b c d

fe

g h i

queue: g, f

Dequeue a nodeadd all its unseen neighbors to the queue

Page 67: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

67

BFS

a b c d

fe

g h i

queue: f, h

Dequeue a nodeadd all its unseen neighbors to the queue

Page 68: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

68

BFS

a b c d

fe

g h i

queue: f, h

Dequeue a nodeadd all its unseen neighbors to the queue

Page 69: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

69

BFS

a b c d

fe

g h i

queue: h

Dequeue a nodeadd all its unseen neighbors to the queue

Page 70: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

70

BFS

a b c d

fe

g h i

queue: h

Dequeue a nodeadd all its unseen neighbors to the queue

Page 71: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

71

BFS

a b c d

fe

g h i

queue: i

Dequeue a nodeadd all its unseen neighbors to the queue

Page 72: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

72

BFS

a b c d

fe

g h i

queue: i

Dequeue a nodeadd all its unseen neighbors to the queue

Page 73: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

73

BFS

a b c d

fe

g h i

queue: c

Dequeue a nodeadd all its unseen neighbors to the queue

Page 74: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

74

BFS

a b c d

fe

g h i

queue: c

Dequeue a nodeadd all its unseen neighbors to the queue

Page 75: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

75

BFS

Dequeue a nodeadd all its unseen neighbors to the queue

a b c d

fe

g h i

queue: c

Page 76: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

76

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

bfs from v1 to v2:create a queue of vertexes to visit,

initially storing just v1.mark v1 as visited.

while queue is not empty and v2 is not seen:dequeue a vertex v from it,mark that vertex v as visited,and add each unvisited neighbor n of v to the queue.

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

Page 77: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

77

BFS observations• optimality:

– always finds the shortest path (fewest edges).– in unweighted graphs, finds optimal cost path.– In weighted graphs, not always optimal cost.

• retrieval: harder to reconstruct the actual sequence of vertices or edges in the path once you find it– conceptually, BFS is exploring many possible paths in parallel, so it's not

easy to store a path array/list in progress– solution: We can keep track of the path by storing predecessors for

each vertex (each vertex can store a reference to a previous vertex).

• DFS uses less memory than BFS, easier to reconstruct the path once found; but DFS does not always find shortest path. BFS does.

a

e

b c

hg

d f

i

Page 78: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

78

Recap• Recap: Graphs• Practice: Twitter Influence• Depth-First Search (DFS)• Announcements• Breadth-First Search (BFS)

Next time: more graph searching algorithms

Page 79: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

79

Overflow

Page 80: CS 106X, Lecture 22 Graphs; BFS; DFS · 7 Graphs 2 1 2 2 1 1 A graph consists of a set of nodesconnected by edges. Nodes:degree(#connectededges) Nodes:in-degree(directed,#in- edges)

80

BFS that finds pathbfs from v1 to v2:

create a queue of vertexes to visit,initially storing just v1.

mark v1 as visited.

while queue is not empty and v2 is not seen:dequeue a vertex v from it,mark that vertex v as visited,and add each unvisited neighbor n of v to the queue, while setting n's previous to v.

preva

e

b c

hg

d f

i