graphs - stanford university · 2013-08-06 · final final: monday, august 12th, 7-10pm location:...

186
Graphs

Upload: others

Post on 09-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Graphs

Page 2: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Announcements() {

Page 3: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Change in Schedule● I'm changing the schedule around a bit...

● Today: Graphs● Tuesday: Shortest Path Algorithms● Wednesday: Minimum Spanning Trees● Tursday: Review Session for Midterm II

Page 4: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Final

● Final: Monday, August 12th, 7-10pm

● Location: Cubberly Auditorium● Cumulative (but weighted towards post midterm

material)● Covers material up through Tuesday

– SCPD students and students who require special arrangements should email me in the next couple days

● We do the final “early” so we have time to grade it, get it back to you and resolve any grading issues.

Page 5: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

} //Announcements

Page 6: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Data Structures Cheat Sheet

● Vector, Stack: dynamic array

● Queue: linked list or dynamic array

● Set, Map: Binary Search Tree

● HashSet, HashMap: Hash Table

● Lexicon: Trie

Page 7: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Graphs

Page 8: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A Social Network

Page 9: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

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

Page 10: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

http://www.toothpastefordinner.com/

Page 11: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A graph is a mathematical structurefor representing relationships.

Page 12: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A graph is a mathematical structurefor representing relationships.

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

Page 13: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A graph is a mathematical structurefor representing relationships.

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

Page 14: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A graph is a mathematical structurefor representing relationships.

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

Page 15: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A graph is a mathematical structurefor representing relationships.

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

Nodes

Page 16: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

A graph is a mathematical structurefor representing relationships.

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

Edges

Page 17: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Some graphs are directed.

Page 18: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Some graphs are undirected.

Page 19: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Some graphs are undirected.

You can think of them as directedgraphs with edges both ways.

Page 20: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Graphs● “Yo Teach, why are we studying graphs?”

● We study graphs because a lot of problems can be modeled in terms of graphs

● Also, there are many off-the-shelf graph algorithms that we apply if we're able to formulate a problem as a graph problem.

Page 21: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Pathfinding

● Each intersection is a node

● Each street connecting intersections is an edge

● Find paths between intersections that minimize distance or travel time

Page 22: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Content-Aware Resizing

Page 23: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Content-Aware Resizing

● Each pixel is a node in a graph

● Each pixel is connected to adjacent pixels

● Find paths from the top of the image to the bottom that minimize the “energy function”

Page 24: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

The Wikipedia Graph

● Wikipedia (and the web in general) is a graph!

● Each page is a node.

● There is an edge from one page to another if the first page links to the second.

Page 25: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Social Networks and Epidemics

http://3278as3udzze1hdk0f2th5nf18c1.wpengine.netdna-cdn.com/wp-content/uploads/2010/09/social-networks-new-science1.jpg

https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&docid=MPB6JuYe9sdbDM&tbnid=VWbN0BtTG7-ZWM:&ved=0CAUQjRw&url=http%3A%2F%2Fblogs.cornell.edu%2Finfo2040%2F2011%2F09%2F17%2Fhow-network-structure-can-provide-advanced-warning-about-epidemics%2F&ei=rrP9Ufa8G6n0iwK9q4CoBQ&bvm=bv.50165853,d.cGE&psig=AFQjCNHljXC1ZggH6hQDrKxPM1CV7ocVXQ&ust=1375667391653650

Page 26: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

How can we represent graphs in C++?

Page 27: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Representing Graphs

Node Connected To

Vector<Node*> Node*

Map<Node*, Vector<Node*>> We can represent a graph as a map from nodes to the list

of nodes each node is connected to.

We can represent a graph as a map from nodes to the list

of nodes each node is connected to.

Page 28: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

What interesting things can we do with graphs?

Page 29: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Connected Components

● A connected component is a subset of the nodes in a graph such that:

● For every pair of nodes in the subset there exists a path between them

● No node in the subset is not connected any node not in the subset

Page 30: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Connected Components

1 Connected Component

Page 31: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Connected Components

2 Connected Components

Page 32: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Connected Components

3 Connected Components

Page 33: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Connected Components

6 Connected Components

Page 34: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Connected Components

● Detecting connected components in a graph is important because it can provide useful insights into the structure of graph

● e.g. How do people in a community separate themselves into separate groups.

● In order to detect connected components we first need to be able to iterate over nodes in a graph.

● We'll come back to connected components later.

Page 35: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

● Given a linked list, there was just one way to traverse the list.● Keep going forward.

● In a binary search tree, there are many traversal strategies:

● An inorder traversal that produces all the elements in sorted order.

● A postorder traversal used to delete all the nodes in the BST.

● There are many ways to iterate over a graph.

Page 36: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

● All methods of iterating over a graph involve keeping track of 3 sets of nodes:

Set of Nodes already visited

Set of Nodes to look at next

Everything else

Page 37: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 38: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 39: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 40: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 41: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 42: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 43: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 44: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 45: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 46: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 47: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 48: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 49: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 50: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 51: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 52: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 53: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 54: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 55: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 56: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 57: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 58: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 59: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 60: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 61: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 62: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 63: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 64: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 65: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 66: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 67: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 68: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 69: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 70: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

Page 71: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterating over a Graph

● All methods of iterating over a graph involve keeping track of 3 sets of nodes:

Set of Nodes already visited

Set of Nodes to look at next

Everything else

● Methods of iterating over nodes differ in how they choose which node to look at next

Page 72: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth First Search● To detect connected components we just

want to see whether or not some path exists between them (we don't care about finding the “shortest” path).

● One way to detect connectivity would be to just pick an arbitrary direction and keep following it.● When you run out of room to go in one

direction, just go back and look in a different direction

Page 73: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 74: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 75: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 76: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 77: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 78: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 79: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 80: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 81: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 82: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 83: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 84: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 85: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 86: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 87: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 88: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 89: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 90: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 91: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 92: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 93: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 94: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 95: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 96: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 97: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 98: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 99: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 100: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 101: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 102: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 103: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 104: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 105: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 106: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 107: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 108: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 109: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 110: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 111: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 112: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 113: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 114: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 115: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

Page 116: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Recursive Depth-First Search

● To do a depth-first search (DFS) from a node u, do the following:● If u is already marked, stop.● Mark u.● For each neighbor v of u:

– Recursively run DFS from v.

● The backtracking here is similar to the backtracking done in standard recursion.

Page 117: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Iterative Depth-First Search

● DFS is most commonly implemented iteratively using a Stack

Page 118: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Page 119: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

StackSet

Page 120: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

A

Set

Page 121: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

StackSet

Page 122: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

StackSet

A

Page 123: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

E

Set

A

Page 124: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

E

Set

A

Page 125: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

Set

A

Page 126: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

Set

A

E

Page 127: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

C

Set

A

E

Page 128: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

C

Set

A

E

Page 129: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A

E

Page 130: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A

E

C

Page 131: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A C

E

B

Page 132: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A C

E

B

Page 133: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A C

E

B

Page 134: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A C

E

Page 135: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A C

E

B

Page 136: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

F

Set

A C

E

B

Page 137: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

Set

A C

E

B

Page 138: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

Set

A C

E

B

F

Page 139: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

D

Set

A C

E

B

F

Page 140: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

Set

A C

E

B

F

Page 141: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

Set

A C

E

B

FD

Page 142: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

Stack

B

Set

A C

E

B

FD

Page 143: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Depth-first search

A B

D E

C

F

StackSet

A C

E

B

FD

Page 144: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Coding Depth-First Search

Page 145: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

DFS and Connected Components

● Detecting connected components becomes relatively straightforward once we have Depth First Search.

● Not going to code it up, but I encourage you to!

Page 146: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Problems with DFS

● Useful when trying to explore everything.● Not good at finding shortest paths.

Page 147: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Problems with DFS

● Useful when trying to explore everything.● Not good at finding shortest paths.

Stack

Page 148: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Problems with DFS

● Useful when trying to explore everything.● Not good at finding shortest paths.

A B

Stack

Page 149: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Problems with DFS

● Useful when trying to explore everything.● Not good at finding shortest paths.

A B

Stack

B

A

Page 150: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-First Search

Page 151: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers
Page 152: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers
Page 153: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers
Page 154: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers
Page 155: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

1

1

11

Page 156: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

2

1

1

2

2

12

1

2

2

Page 157: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

2

1

1

2

2

12

1

2

2

Page 158: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers
Page 159: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-First Search

● Specialization of the general search algorithm where nodes to visit are put into a queue.

● Explores nodes one hop away, then two hops away, etc.

● Finds path with fewest edges from start node to all other nodes.

Page 160: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Note: The following animation has been simplified for pedagogic purposes. In

reality there would be a Set keeping track of visited nodes and redundant adds to the

Queue

Page 161: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Page 162: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue

Page 163: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue A

Page 164: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue

Page 165: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue B E

Page 166: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue B E

Page 167: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue E

Page 168: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue E C

Page 169: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue E C

Page 170: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue C

B

Page 171: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue C D F

B

Page 172: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue C D F

B

Page 173: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue D F

B

E

Page 174: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue D F

B

E

Page 175: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue F

B

E

C

Page 176: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue F

B

E

C

Page 177: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue

B

E

C

D

Page 178: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Breadth-first search

A B

D E

C

F

Queue

B

E

C

D

Page 179: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Coding Breadth-First Search

Page 180: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Page 181: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Page 182: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Page 183: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Page 184: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT SAT RAT

RANMAN

MAT

CAN

Page 185: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

CAT

RANCAN

Page 186: Graphs - Stanford University · 2013-08-06 · Final Final: Monday, August 12th, 7-10pm Location: Cubberly Auditorium Cumulative (but weighted towards post midterm material) Covers

Next Time

● Shortest Paths● Dijkstra's Algorithm.● A* Search.