algorithms - princeton university computer science · 9 graph applications graph vertex edge...

85
ROBERT S EDGEWICK | KEVIN WAYNE FOURTH EDITION Algorithms Algorithms R OBERT S EDGEWICK | K EVIN W AYNE Last updated on 3/27/19 9:15 PM 4.1 U NDIRECTED G RAPHS introduction graph API depth-first search breadth-first search applications of DFS and BFS https://algs4.cs.princeton.edu

Upload: others

Post on 01-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

Last updated on 3/27/19 9:15 PM

4.1 UNDIRECTED GRAPHS

‣ introduction

‣ graph API

‣ depth-first search

‣ breadth-first search

‣ applications of DFS and BFShttps://algs4.cs.princeton.edu

Page 2: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ graph API

‣ depth-first search

‣ breadth-first search

‣ applications of DFS and BFS

4.1 UNDIRECTED GRAPHS

https://algs4.cs.princeton.edu

Page 3: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Graph. Set of vertices connected pairwise by edges.

Why study graph algorithms?

・Thousands of practical applications.

・Hundreds of graph algorithms known.

・Interesting and broadly useful abstraction.

・Challenging branch of computer science and discrete math.

3

Undirected graphs

Page 4: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Vertex = station; edge = route

4

Rail network

Page 5: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Social networks

Vertex = person; edge = social relationship.

5

“Visualizing Friendships” by Paul Butler

Page 6: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Protein-protein interaction network

Vertex = protein; edge = interaction.

6Reference: Jeong et al, Nature Review | Genetics

Page 7: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

7

The Internet as mapped by the Opte Project

http://en.wikipedia.org/wiki/Internet

Vertex = IP address.

Edge = connection.

Page 8: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

8

Romantic and sexual relationships in a high school

Relationship graph at "Jefferson High"

Peter Bearman, James Moody, and Katherine Stovel. Chains of affection: The structure of adolescent romantic and sexual networks. American Journal of Sociology, 110(1): 44–99, 2004.

Page 9: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

9

Graph applications

graph vertex edge

communication telephone, computer fiber optic cable

circuit gate, register, processor wire

mechanical joint rod, beam, spring

financial stock, currency transactions

transportation intersection street

internet class C network connection

game board position legal move

social relationship person friendship

neural network neuron synapse

protein network protein protein–protein interaction

molecule atom bond

Page 10: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Graph: set of vertices connected pairwise by edges.

Path: sequence of vertices connected by edges, with no repeated edges.

Two vertices are connected if there is a path between them.

Cycle: Path (with at least 1 edge) whose first and last vertices are the same.

10

Graph terminology

1

4

9

2

5

3

0

1211

10

2

3

0

7

vertex

path between 0 to 2 (of length 3)

5

edge (incident to vertices 6 and 8)

6 8

(of degree 3)

6 8

1

4

2

0

5

4

9

1211

10

cycle (of length 4)

9

1211

10

Page 11: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Challenge. Which graph problems are easy? Difficult? Intractable?

problem description

s-t path Is there a path between s and t ?

shortest s-t path What is the shortest path between s and t ?

cycle Is there a cycle in the graph ?

Euler cycle Is there a cycle that uses each edge exactly once ?

Hamilton cycle Is there a cycle that uses each vertex exactly once ?

connectivity Is there a path between every pair of vertices ?

biconnectivity Is there a vertex whose removal disconnects the graph ?

planarity Can the graph be drawn in the plane with no crossing edges ?

graph isomorphism Are two graphs isomorphic?

11

Some graph-processing problems

Page 12: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ graph API

‣ depth-first search

‣ breadth-first search

‣ applications of DFS and BFS

4.1 UNDIRECTED GRAPHS

https://algs4.cs.princeton.edu

Page 13: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Graph drawing. Provides intuition about the structure of the graph.

Caveat. Intuition can be misleading. 13

Graph representation

different drawings of the same graph

Page 14: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Vertex representation.

・This lecture: integers between 0 and V – 1.

・Applications: use symbol table to convert between names and integers.

Anomalies.

A

G

E

CB

F

D

14

Graph representation

symbol table

0

6

4

21

5

3

Anomalies

paralleledges

self-loop

Page 15: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

15

Graph API

public class Graph

Graph(int V) create an empty graph with V vertices

void addEdge(int v, int w) add an edge v–w

Iterable<Integer> adj(int v) vertices adjacent to v

int V() number of vertices

int E() number of edges

// degree of vertex v in graph G public static int degree(Graph G, int v) { int count = 0; for (int w : G.adj(v)) count++; return count; }

All graph processing can be done using above API. Example: Arvind’s view:

This API is oversimplified.

Any competent graph API

must provide degree() and

other methods.

Page 16: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Maintain a V-by-V boolean array; for each edge v–w in graph: adj[v][w] = adj[w][v] = true.

0 1 2 3 4 5 6 7 8 9 10 11 12

0 0 1 1 0 0 1 1 0 0 0 0 0 0

1 1 0 0 0 0 0 0 0 0 0 0 0 02 1 0 0 0 0 0 0 0 0 0 0 0 03 0 0 0 0 1 1 0 0 0 0 0 0 04 0 0 0 1 0 1 1 0 0 0 0 0 05 1 0 0 1 1 0 0 0 0 0 0 0 0

6 1 0 0 0 1 0 0 0 0 0 0 0 07 0 0 0 0 0 0 0 0 1 0 0 0 08 0 0 0 0 0 0 0 1 0 0 0 0 09 0 0 0 0 0 0 0 0 0 0 1 1 1

10 0 0 0 0 0 0 0 0 0 1 0 0 011 0 0 0 0 0 0 0 0 0 1 0 0 112 0 0 0 0 0 0 0 0 0 1 0 1 0

16

Graph representation: adjacency matrix

87

109

1211

0

6

4

21

5

3

Page 17: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Maintain a V-by-V boolean array; for each edge v–w in graph: adj[v][w] = adj[w][v] = true.

0 1 2 3 4 5 6 7 8 9 10 11 12

0 0 1 1 0 0 1 1 0 0 0 0 0 0

1 1 0 0 0 0 0 0 0 0 0 0 0 02 1 0 0 0 0 0 0 0 0 0 0 0 03 0 0 0 0 1 1 0 0 0 0 0 0 04 0 0 0 1 0 1 1 0 0 0 0 0 05 1 0 0 1 1 0 0 0 0 0 0 0 0

6 1 0 0 0 1 0 0 0 0 0 0 0 07 0 0 0 0 0 0 0 0 1 0 0 0 08 0 0 0 0 0 0 0 1 0 0 0 0 09 0 0 0 0 0 0 0 0 0 0 1 1 1

10 0 0 0 0 0 0 0 0 0 1 0 0 011 0 0 0 0 0 0 0 0 0 1 0 0 112 0 0 0 0 0 0 0 0 0 1 0 1 0

17

Graph representation: adjacency matrix

two entries per edge

87

109

1211

0

6

4

21

5

3

Page 18: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Which is the order of growth of running time of the following code fragment if the graph uses the adjacency-matrix representation, where V is the number of vertices and E is the number of edges?

A. V

B. E + V

C. V 2

D. V E

18

Undirected graphs: quiz 1

for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "-" + w);

print each edge twice

0 1 2 3 4 5 6 7

0 0 1 1 0 0 1 1 0

1 1 0 0 0 0 0 0 0

2 1 0 0 0 0 0 0 0

3 0 0 0 0 1 1 0 0

4 0 0 0 1 0 1 1 0

5 1 0 0 1 1 0 0 0

6 1 0 0 0 1 0 0 0

7 0 0 0 0 0 0 0 0

adjacency-matrix representation

Page 19: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Maintain vertex-indexed array of lists.

19

Graph representation: adjacency lists

87

109

1211

0

6

4

21

5

3

adj[]0

1

2

3

4

5

6

7

8

9

10

11

12

5 4

0 4

9 12

11 9

0

0

8

7

9

5 6 3

3 4 0

11 10 12

6 2 1 5

Adjacency-lists representation (undirected graph)

Bag objects

representationsof the same edge

Page 20: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Which is the order of growth of running time of the following code fragment if the graph uses the adjacency-lists representation, where V is the number of vertices and E is the number of edges?

A. V

B. E + V

C. V 2

D. V E

20

Undirected graphs: quiz 2

for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "-" + w);

degree(v0) + degree(v1) + degree(v2) + … = 2 E

print each edge twice

adj[]0

1

2

3

4

5

6

7

8

9

10

11

12

5 4

0 4

9 12

11 9

0

0

8

7

9

5 6 3

3 4 0

11 10 12

6 2 1 5

Adjacency-lists representation (undirected graph)

Bag objects

representationsof the same edge

Page 21: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

In practice. Use adjacency-lists representation.

・Algorithms based on iterating over vertices adjacent to v.

・Real-world graphs tend to be sparse (not dense).

21

Graph representations

proportional to V edges

sparse (E = 200) dense (E = 1000)

Two graphs (V = 50)

proportional to V 2 edges

Page 22: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

In practice. Use adjacency-lists representation.

・Algorithms based on iterating over vertices adjacent to v.

・Real-world graphs tend to be sparse (not dense).

22

Graph representations

representation space add edge edge betweenv and w?

iterate over verticesadjacent to v?

list of edges E 1 E E

adjacency matrix V 2 1 † 1 V

adjacency lists E + V 1 degree(v) degree(v)

† disallows parallel edges

Page 23: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

23

Adjacency-list graph representation: Java implementation

public class Graph {

}

private final int V; private Bag<Integer>[] adj;

public Iterable<Integer> adj(int v){ return adj[v]; }

public Graph(int V) { this.V = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<Integer>();}

public void addEdge(int v, int w){ adj[v].add(w); adj[w].add(v); }

adjacency lists (using Bag data type)

create empty graphwith V vertices

add edge v-w(parallel edges andself-loops allowed)

iterator for vertices adjacent to v

https://algs4.cs.princeton.edu/41undirected/Graph.java.html

Page 24: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ graph API

‣ depth-first search

‣ breadth-first search

‣ applications of DFS and BFS

4.1 UNDIRECTED GRAPHS

https://algs4.cs.princeton.edu

Page 25: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

25

Warmup: maze exploration

Maze graph.

・Vertex = intersection.

・Edge = passage.

Goal. Explore every intersection in the maze.

intersection passage

Page 26: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

How Theseus escaped from the labyrinth after killing the Minotaur:

・Unroll a ball of string behind you.

・Mark each newly discovered intersection.

・Retrace steps when no unmarked options.

26

Maze exploration algorithm in Greek myth

Tremaux explorationTremaux explorationTremaux explorationTremaux explorationTremaux explorationTremaux exploration

Page 27: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Goal. Systematically traverse a graph.

Typical applications.

・Find all vertices connected to a given vertex.

・Find a path between two vertices.

Depth-first search

Mark vertex v.Recursively visit all unmarked vertices w adjacent to v.

DFS (to visit a vertex v)

Page 28: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

To visit a vertex v :

・Mark vertex v.

・Recursively visit all unmarked vertices adjacent to v.

87

109

1211

0

6

4

21

5

3

Depth-first search demo

28

graph G

87

109

1211

0

6

4

21

5

3

87

109

1211

0

6

4

21

5

3

Page 29: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

To visit a vertex v :

・Mark vertex v.

・Recursively visit all unmarked vertices adjacent to v.

0

4

5

621

3

Depth-first search demo

29

87

109

1211

87

109

1211

0 1 2 3 4 5 6 7 8 9

10 11 12

v marked[]

T T T T T T T F F F F F F

edgeTo[]

– 0 0 5 6 4 0 – – – – – –

vertices connected to 0(and associated paths)

Page 30: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Run DFS using the following adjacency-lists representation of graph G, starting at vertex 0. In which order is dfs(G, v) called?

A. 0 1 2 4 5 3 6

B. 0 1 2 4 5 6 3

C. 0 1 4 2 5 3 6

D. 0 1 2 6 4 5 3

30

Undirected graphs: quiz 3

0 1

4

2

5

3 6

adj[]0

1

2

3

4

5

6

1 4

0 2

1

0 5

4

1 6 4

2

2

3

DFS preorder

Page 31: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

31

Depth-first search: Java implementation

public class DepthFirstPaths {

}

private boolean[] marked; private int[] edgeTo; private int s;

public DepthFirstPaths(Graph G, int s){ ... dfs(G, s);}

private void dfs(Graph G, int v){ marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) { edgeTo[w] = v; dfs(G, w); }}

recursive DFS does the work

marked[v] = true if v connected to s

edgeTo[v] = previous vertex on path from s to v

find vertices connected to s

initialize data structures

https://algs4.cs.princeton.edu/41undirected/DepthFirstPaths.java.html

Page 32: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Depth-first search: properties

Proposition. DFS marks all vertices connected to s (and no others).

Proof.

・If w marked, then w connected to s (why?)

・If w connected to s, then w marked.(if w unmarked, then consider the last edgeon a path from s to w that goes from amarked vertex to an unmarked one).

32

set ofunmarked

vertices

no such edgecan exist

source

v

s

set of markedvertices

w

x

Skipped

in class

Page 33: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Depth-first search: properties

Proposition. DFS marks all vertices connected to s in time proportional to V + E in the worst case.

Proof.

・Initialize two arrays of length V.

・Each vertex is visited at most once.(visiting a vertex takes time proportional to its degree)

33

degree(v0) + degree(v1) + degree(v2) + … = 2 E

Page 34: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Proposition. After DFS, can check if vertex v is connected to s in constant

time; can find v–s path (if one exists) in time proportional to its length.

Proof. edgeTo[] is parent-link representation of a tree rooted at vertex s.

34

Depth-first search: properties

Trace of pathTo() computation

edgeTo[] 0 1 2 2 0 3 2 4 3 5 3

5 53 3 52 2 3 50 0 2 3 5

x path

Page 35: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ graph API

‣ depth-first search

‣ breadth-first search

‣ applications of DFS and BFS

4.1 UNDIRECTED GRAPHS

https://algs4.cs.princeton.edu

Page 36: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Tree traversal. Many ways to explore a binary tree.

・Inorder: A C E H M R S X

・Preorder: S E A C R H M X

・Postorder: C A M H R E X S

・Level-order: S E X A R C H M

Graph search. Many ways to explore a graph.

・Preorder: vertices in order of calls to dfs(G, v).

・Postorder: vertices in order of returns from dfs(G, v).

・Level-order: vertices in increasing order of distance from s.

Graph search

36

AC

E

HM

R

SX

stack/recursion

queue

queue

stack/recursion

Page 37: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Breadth-First Search (BFS)

Intuition. BFS traverses vertices in order of distance from s.

37

0

4

2

1

53

graph G

4

3

dist = 2dist = 1

2

1

5

0

dist = 0

6

s

6

Put s on a queue, and mark s as visited.Repeat until the queue is empty: - dequeue vertex v - enqueue each of v’s unmarked neighbors, and mark them.

BFS (from source vertex s)

Page 38: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

38

graph G

0

4

2

1

5

3

0

4

2

1

5

3

6

Page 39: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

39

enqueue 0

0

4

2

1

5

3

4

2

1

5

3

00

0 1 2 3 4 5 6

v edgeTo[]

– – – – – – –

queue marked[]

F F F F F F F

T

Page 40: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

Breadth-first search demo

40

0

4

2

1

5

3

0

4

2

1

5

3

dequeue 0

0

0 1 2 3 4 5 6

v edgeTo[] marked[]

– – – – – – –

T F F F F F F

queue

Page 41: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

– – – – – – –

T F F F F F F

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

0 1 2 3 4 5 6

6

Breadth-first search demo

41

0

4

2

1

5

3

0

4

2

1

5

3

22v edgeTo[] marked[]

0 T

queue

: check 2, check 1, check 5dequeue 0

Page 42: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

42

0

4

2

1

5

3

0

4

2

1

5

3

2

2

11

0 1 2 3 4 5 6

v edgeTo[]

– – 0 – – – –

marked[]

T F T F F F F

0 T

queue

dequeue 0 : check 2, check 1, check 5

Page 43: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

43

0

4

2

1

5

3

0

4

2

1

5

3

2

1

2

1

55

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 – – – –

marked[]

T T T F F F F

0 T

queue

dequeue 0 : check 2, check 1, check 5

Page 44: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

44

0 done

0

4

2

1

5

3

4

2

1

5

3

2

1

2

1

5

5

0

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 – – 0 –

marked[]

T T T F F T F

queue

Page 45: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

– 0 0 – – 0 –

T T T F F T F

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

45

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

2

1

5

1

5

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

Page 46: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

– 0 0 – – 0 –

T T T F F T F

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

46

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

1

5

1

5

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

: check 0, check 1, check 3, check 4

Page 47: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

47

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

1

5

1

5

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 – – 0 –

marked[]

T T T F F T F

queue

: check 0, check 1, check 3, check 4

Page 48: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

48

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

1

5

1

5

33

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 – – 0 –

marked[]

T T T F F T F

2 T

queue

: check 0, check 1, check 3, check 4

Page 49: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

49

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

1

5

1

5

3

3

44

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 – 0 –

marked[]

T T T T F T F

2 T

queue

: check 0, check 1, check 3, check 4

Page 50: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

50

2 done

0

4

2

1

5

3

4

2

1

5

3

1

5

1

5

3

3

4

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

Page 51: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

51

dequeue 1

0

4

2

1

5

3

4

1

5

3

1

55

3

3

4

4

0 1 2 3 4 5 6

v edgeTo[] marked[]

– 0 0 2 2 0 –

T T T T T T F

queue

Page 52: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

52

dequeue 1

0

4

2

1

5

3

4

1

5

3

5

3

4

5

3

4

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

– 0 0 2 2 0 –

T T T T T T F

: check 0, check 2

Page 53: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

53

dequeue 1

0

4

2

1

5

3

4

1

5

3

5

5

33

4

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

: check 0, check 2

Page 54: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

54

1 done

0

4

2

1

5

3

4

1

5

3

5

5

33

4

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

Page 55: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

55

dequeue 5

0

4

2

1

5

3

45

3

5

33

4

4

0 1 2 3 4 5 6

v edgeTo[] marked[]

– 0 0 2 2 0 –

T T T T T T F

queue

Page 56: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

56

dequeue 5

0

4

2

1

5

3

45

33

3

44

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

– 0 0 2 2 0 –

T T T T T T F

: check 3, check 0

Page 57: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

57

dequeue 5

0

4

2

1

5

3

45

33

3

44

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

: check 3, check 0

Page 58: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

58

5 done

0

4

2

1

5

3

45

33

3

44

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

Page 59: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

59

dequeue 3

0

4

2

1

5

3

4

3

3

44

0 1 2 3 4 5 6

v edgeTo[] marked[]

– 0 0 2 2 0 –

T T T T T T F

queue

Page 60: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

60

dequeue 3

0

4

2

1

5

3

4

3

4

4

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

– 0 0 2 2 0 –

T T T T T T F

: check 5, check 4, check 2, check 6

Page 61: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

61

dequeue 3

0

4

2

1

5

3

4

3

4

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

: check 5, check 4, check 2, check 6

Page 62: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

62

dequeue 3

0

4

2

1

5

3

4

3

4

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

: check 5, check 4, check 2, check 6

Page 63: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

3

Breadth-first search demo

63

0

4

2

1

5 44

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 –

marked[]

T T T T T T F

queue

3

3 T

6

dequeue 3 : check 5, check 4, check 2, check 6

Page 64: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

33

Breadth-first search demo

64

3 done

0

4

2

1

5 44

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 3

marked[]

T T T T T T T

queue

6

6

Page 65: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

Breadth-first search demo

65

dequeue 4

0

4

2

1

5

3

4

4

0 1 2 3 4 5 6

v edgeTo[] marked[]

– 0 0 2 2 0 3

T T T T T T T

queue

6

6

Page 66: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

Breadth-first search demo

66

dequeue 4

0

4

2

1

5

3

4

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

6

6

– 0 0 2 2 0 3

T T T T T T T

: check 3, check 2

Page 67: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

Breadth-first search demo

67

dequeue 4

0

4

2

1

5

3

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 3

marked[]

T T T T T T T

queue

6

6

: check 3, check 2

Page 68: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

Breadth-first search demo

68

4 done

0

4

2

1

5

3

4

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 3

marked[]

T T T T T T T

queue

6

6

Page 69: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

69

dequeue 6

6

0

4

2

1

5

3

0 1 2 3 4 5 6

v edgeTo[] marked[]

– 0 0 2 2 0 3

T T T T T T T

queue

6

Page 70: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

70

dequeue 6

0

4

2

1

5

0 1 2 3 4 5 6

v edgeTo[] marked[]queue

3

6

– 0 0 2 2 0 3

T T T T T T T

: check 3

Page 71: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

6

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

71

6 done

0

4

2

1

5

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 3

marked[]

T T T T T T T

queue

3

6

Page 72: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

6

Breadth-first search demo

72

all done

0

4

2

1

5

3

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 3

marked[]

T T T T T T T

Page 73: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Repeat until queue is empty:

・Remove vertex v from queue.

・Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search demo

73

done

0

4

2

1

5

6

3

0 1 2 3 4 5 6

v edgeTo[]

– 0 0 2 2 0 3

distTo[]

0 1 1 2 2 1 3

Page 74: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

74

Breadth-first search: Java implementation

public class BreadthFirstPaths { private boolean[] marked; private int[] edgeTo; private int[] distTo;

}

initialize FIFO queue of vertices to explore

found new vertex w via edge v–w

while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { q.enqueue(w); marked[w] = ?; edgeTo[w] = ?; distTo[w] = ?; } } }}

private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; distTo[s] = 0;

https://algs4.cs.princeton.edu/41undirected/BreadthFirstPaths.java.html

Page 75: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

75

Breadth-first search: Java implementation

public class BreadthFirstPaths { private boolean[] marked; private int[] edgeTo; private int[] distTo;

}

initialize FIFO queue of vertices to explore

found new vertex w via edge v–w

while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeTo[w] = v; distTo[w] = distTo[v] + 1; } } }}

private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; distTo[s] = 0;

https://algs4.cs.princeton.edu/41undirected/BreadthFirstPaths.java.html

Page 76: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

BFS examines vertices in order of increasing distance (# of edges) from s. Proposition. In any connected graph G, BFS computes shortest pathsfrom s to all other vertices in time proportional to E + V.

Breadth-first search properties

76

0

4

2

1

53

graph G

4

3

dist = 2dist = 1

2

1

5

0

dist = 0

6

s

queue always consists of ≥ 0 vertices of distance k from s, followed by ≥ 0 vertices of distance k+1

6

Page 77: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ graph API

‣ depth-first search

‣ breadth-first search

‣ applications of DFS and BFS

4.1 UNDIRECTED GRAPHS

https://algs4.cs.princeton.edu

Page 78: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

78

Breadth-first search application: routing

Fewest number of hops in a communication network.

ARPANET, July 1977

Page 79: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

79

Breadth-first search application: Kevin Bacon numbers

http://oracleofbacon.org

Page 80: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

80

Kevin Bacon graph

・Include one vertex for each performer and one for each movie.

・Connect a movie to all performers that appear in that movie.

・Compute shortest path from s = Kevin Bacon.

KevinBacon

KathleenQuinlan

MerylStreep

NicoleKidman

JohnGielgud

KateWinslet

BillPaxton

DonaldSutherland

The StepfordWives

Portraitof a Lady

Dial Mfor Murder

Apollo 13

To Catcha Thief

The EagleHas Landed

ColdMountain

Murder on theOrient Express

VernonDobtcheff

An AmericanHaunting

Jude

Enigma

Eternal Sunshineof the Spotless

Mind

TheWoodsman

WildThings

Hamlet

Titanic

AnimalHouse

GraceKellyCaligola

The RiverWild

LloydBridges

HighNoon

The DaVinci Code

Joe Versusthe Volcano

PatrickAllen

TomHanks

SerrettaWilson

GlennClose

JohnBelushi

YvesAubert Shane

Zaza

PaulHerbert

performervertex

movievertex

Symbol graph example (adjacency lists)

...Tin Men (1987)/DeBoy, David/Blumenfeld, Alan/... /Geppi, Cindy/Hershey, Barbara...Tirez sur le pianiste (1960)/Heymann, Claude/.../Berger, Nicole (I)...Titanic (1997)/Mazin, Stan/...DiCaprio, Leonardo/.../Winslet, Kate/...Titus (1999)/Weisskopf, Hermann/Rhys, Matthew/.../McEwan, GeraldineTo Be or Not to Be (1942)/Verebes, Ernö (I)/.../Lombard, Carole (I)...To Be or Not to Be (1983)/.../Brooks, Mel (I)/.../Bancroft, Anne/...To Catch a Thief (1955)/París, Manuel/.../Grant, Cary/.../Kelly, Grace/...To Die For (1995)/Smith, Kurtwood/.../Kidman, Nicole/.../ Tucci, Maria......

movies.txt

V and E not explicitly

specified

"/"delimiter

Page 81: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Exercise: applications of DFS and BFS

Recall: a connected component is a maximal set of connected vertices.

Given a graph, partition vertices into connected components using DFS or BFS.

i.e. create an id[] array such that id[u] == id[v] iff u & v are in same CC.

Euler cycle: given a graph, find a general cycle that traverses each edge exactly

once, or determine that none exists.

81

0

6

4

21

5

3

0-1-2-3-4-2-0-6-4-5-0

0

6

4

21

5

3

0

6

4

21

5

3

Same property as quick-find

May traverse a node more than once

Challenge

Page 82: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

Goal. Partition vertices into connected components.

82

Connected components

Initialize all vertices v as unmarked.

For each unmarked vertex v, run DFS to identify all vertices discovered as part of the same component.

Connected components

13130 54 30 19 126 45 40 211 129 100 67 89 115 3

tinyG.txt

Input format for Graph constructor (two examples)

2501273244 246239 240238 245235 238233 240232 248231 248229 249228 241226 231...(1261 additional lines)

mediumG.txtV

EV

E

Page 83: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

83

Finding connected components with DFS

public class CC { private boolean[] marked; private int[] id; private int count;

public CC(Graph G) { marked = new boolean[G.V()]; id = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { dfs(G, v); count++; } } }

public int count() public int id(int v) public boolean connected(int v, int w) private void dfs(Graph G, int v) }

run DFS from one vertex in each component

id[v] = id of component containing v

number of components

see next slide

Page 84: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

84

Finding connected components with DFS (continued)

public int count() { return count; }

public int id(int v) { return id[v]; }

public boolean connected(int v, int w) { return id[v] == id[w]; }

private void dfs(Graph G, int v) { marked[v] = true; id[v] = count; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); }

all vertices discovered in same call of dfs have same id

number of components

id of component containing v

v and w connected iff same id

Page 85: Algorithms - Princeton University Computer Science · 9 Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor

85

Graph traversal summary

BFS and DFS enables efficient solution of many (but not all) graph problems.

graph problem BFS DFS time

s-t path ✔ ✔ E + V

shortest s-t path ✔ E + V

cycle ✔ ✔ V

Euler cycle ✔ E + V

Hamilton cycle

bipartiteness (odd cycle) ✔ ✔ E + V

connected components ✔ ✔ E + V

biconnected components ✔ E + V

planarity ✔ E + V

graph isomorphism

2 1.657 V

2 c ln3 V