167
Mar. 22, 2016 BBM 202 - ALGORITHMS UNDIRECTED GRAPHS DEPT. OF COMPUTER ENGINEERING Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.

BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Mar. 22, 2016

BBM 202 - ALGORITHMS

UNDIRECTED GRAPHS

DEPT. OF COMPUTER ENGINEERING

Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.

Page 2: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

TODAY

‣ Undirected Graphs‣ Graph API‣ Depth-first search‣ Breadth-first search‣ Connected components‣ Challenges

Page 3: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

4

Protein-protein interaction network

Reference: Jeong et al, Nature Review | Genetics

Page 5: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

5

The Internet as mapped by the Opte Project

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

Page 6: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

6

Map of science clickstreams

http://www.plosone.org/article/info:doi/10.1371/journal.pone.0004803

Page 7: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

7

10 million Facebook friends

"Visualizing Friendships" by Paul Butler

Page 8: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

8

Framingham heart study

“The Spread of Obesity in a Large Social Network over 32 Years” by Christakis and Fowler in New England Journal of Medicine, 2007

The Spread of Obesity in a Large Social Network Over 32 Years

n engl j med 357;4 www.nejm.org july 26, 2007 373

educational level; the ego’s obesity status at the previous time point (t); and most pertinent, the alter’s obesity status at times t and t + 1.25 We used generalized estimating equations to account for multiple observations of the same ego across examinations and across ego–alter pairs.26 We assumed an independent working correlation structure for the clusters.26,27

The use of a time-lagged dependent variable (lagged to the previous examination) eliminated serial correlation in the errors (evaluated with a Lagrange multiplier test28) and also substantial-ly controlled for the ego’s genetic endowment and any intrinsic, stable predisposition to obesity. The use of a lagged independent variable for an alter’s weight status controlled for homophily.25 The key variable of interest was an alter’s obesity at time t + 1. A significant coefficient for this vari-able would suggest either that an alter’s weight affected an ego’s weight or that an ego and an alter experienced contemporaneous events affect-

ing both their weights. We estimated these mod-els in varied ego–alter pair types.

To evaluate the possibility that omitted vari-ables or unobserved events might explain the as-sociations, we examined how the type or direc-tion of the social relationship between the ego and the alter affected the association between the ego’s obesity and the alter’s obesity. For example, if unobserved factors drove the association be-tween the ego’s obesity and the alter’s obesity, then the directionality of friendship should not have been relevant.

We evaluated the role of a possible spread in smoking-cessation behavior as a contributor to the spread of obesity by adding variables for the smoking status of egos and alters at times t and t + 1 to the foregoing models. We also analyzed the role of geographic distance between egos and alters by adding such a variable.

We calculated 95% confidence intervals by sim-ulating the first difference in the alter’s contem-

Figure 1. Largest Connected Subcomponent of the Social Network in the Framingham Heart Study in the Year 2000.

Each circle (node) represents one person in the data set. There are 2200 persons in this subcomponent of the social network. Circles with red borders denote women, and circles with blue borders denote men. The size of each circle is proportional to the person’s body-mass index. The interior color of the circles indicates the person’s obesity status: yellow denotes an obese person (body-mass index, ≥30) and green denotes a nonobese person. The colors of the ties between the nodes indicate the relationship between them: purple denotes a friendship or marital tie and orange denotes a familial tie.

Page 9: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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 street intersection, airport highway, airway route

internet class C network connection

game board position legal move

social relationship person, actor friendship, movie cast

neural network neuron synapse

protein network protein protein-protein interaction

chemical compound molecule bond

Page 10: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

10

Graph terminology

Path. Sequence of vertices connected by edges.Cycle. Path whose first and last vertices are the same.

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

Anatomy of a graph

cycle oflength 5

vertex

vertex ofdegree 3

edge

path oflength 4

connectedcomponents

Page 11: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

11

Some graph-processing problems

Path. Is there a path between s and t ?Shortest path. What is the shortest path between s and t ?Cycle. Is there a cycle in the graph? Euler tour. Is there a cycle that uses each edge exactly once?Hamilton tour. Is there a cycle that uses each vertex exactly once? Connectivity. Is there a way to connect all of the vertices? MST. What is the best way to connect all of the vertices? Biconnectivity. Is there a vertex whose removal disconnects the graph?Planarity. Can you draw the graph in the plane with no crossing edges? Graph isomorphism. Do two adjacency lists represent the same graph?Challenge. Which of these problems are easy? difficult? intractable?

Page 12: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

UNDIRECTED GRAPHS

‣ Graph API‣ Depth-first search‣ Breadth-first search‣ Connected components‣ Challenges

Page 13: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

Caveat. Intuition can be misleading.

13

Graph representation

Two drawings of the same graph

Two drawings of the same graphtwo drawings of the same graph

Page 14: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Vertex representation.• This lecture: use integers between 0 and V – 1.

• Applications: convert between names and integers with symbol table.

Anomalies.

A

G

E

CB

F

D

14

Graph representation

symbol table

0

6

4

21

5

3

Anomalies

paralleledges

self-loop

Page 15: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

15

Graph API

public class Graph

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

Graph(In in) create a graph from input stream

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

String toString() string representation

In in = new In(args[0]); Graph G = new Graph(in);

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

read graph from input stream

print out each

edge (twice)

Page 16: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

16

Graph input format.

Graph API: sample client

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% java Test tinyG.txt 0-6 0-2 0-1 0-5 1-0 2-0 3-5 3-4 … 12-11 12-9

In in = new In(args[0]); Graph G = new Graph(in);

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

read graph from input stream

print out each

edge (twice)

Page 17: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

17

Typical graph-processing codetask implementation

compute the degree of v

public static int degree(Graph G, int v) { int degree = 0; for (int w : G.adj(v)) degree++; return degree; }

compute maximum degree

public static int maxDegree(Graph G) { int max = 0; for (int v = 0; v < G.V(); v++) if (degree(G, v) > max) max = degree(G, v); return max;}

compute average degree public static double averageDegree(Graph G) { return 2.0 * G.E() / G.V(); }

count self-loops

public static int numberOfSelfLoops(Graph G) { int count = 0; for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) if (v == w) count++; return count/2; // each edge counted twice }

string representation of the graph’s adjacency lists

(instance method in Graph)

public String toString(){ String s = V + " vertices, " + E + " edges\n"; for (int v = 0; v < V; v++) { s += v + ": "; for (int w : this.adj(v)) s += w + " "; s += "\n"; } return s;}

Typical graph-processing code

5234.1 n Undirected Graphs

Page 18: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Maintain a list of the edges (linked list or array).

18

Set-of-edges graph representation

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

87

109

1211

0

6

4

21

5

3

Page 19: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Maintain a two-dimensional 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 0

2 1 0 0 0 0 0 0 0 0 0 0 0 0

3 0 0 0 0 1 1 0 0 0 0 0 0 0

4 0 0 0 1 0 1 1 0 0 0 0 0 0

5 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 0

7 0 0 0 0 0 0 0 0 1 0 0 0 0

8 0 0 0 0 0 0 0 1 0 0 0 0 0

9 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 0

11 0 0 0 0 0 0 0 0 0 1 0 0 1

12 0 0 0 0 0 0 0 0 0 1 0 1 0

19

Adjacency-matrix graph representation

two entries

for each edge

109

1211

0

6

4

21

5

3

87

Page 20: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Maintain vertex-indexed array of lists.

20

Adjacency-list graph representation

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

87

Page 21: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

21

Adjacency-list graph representation: Java implementation

public class Graph { private final int V; private Bag<Integer>[] adj;

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); }

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

adjacency lists

( using Bag data type )

create empty graph

with V vertices

add edge v-w (parallel edges allowed)

iterator for vertices adjacent to v

Page 22: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

In practice. Use adjacency-lists representation.• Algorithms based on iterating over vertices adjacent to v.

• Real-world graphs tend to be sparse.

22

Graph representations

huge number of vertices,

small average vertex degree

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

Two graphs (V = 50)

Page 23: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

In practice. Use adjacency-lists representation.• Algorithms based on iterating over vertices adjacent to v.

• Real-world graphs tend to be sparse.

23

Graph representations

representation space add edgeedge between

v and w?iterate over vertices

adjacent 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)

huge number of vertices,

small average vertex degree

* disallows parallel edges

Page 24: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

UNDIRECTED GRAPHS

‣ Graph API‣ Depth-first search‣ Breadth-first search‣ Connected components‣ Challenges

Page 25: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

25

Maze exploration

Maze graphs.• Vertex = intersection.

• Edge = passage.

Goal. Explore every intersection in the maze.

intersection passage

Page 26: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Algorithm.• Unroll a ball of string behind you.

• Mark each visited intersection and each visited passage.

• Retrace steps when no unvisited options.

26

Trémaux maze exploration

Tremaux exploration

Page 27: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

27

Maze exploration

Page 28: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

28

Maze exploration

Page 29: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Goal. Systematically search through a graph.Idea. Mimic maze exploration.Typical applications.• Find all vertices connected to a given source vertex.

• Find a path between two vertices.

Design challenge. How to implement?

Depth-first search

Mark v as visited.Recursively visit all unmarked vertices w adjacent to v.

DFS (to visit a vertex v)

Page 30: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

30

Design pattern. Decouple graph data type from graph processing. • Create a Graph object.

• Pass the Graph to a graph-processing routine, e.g., Paths.

• Query the graph-processing routine for information.

Design pattern for graph processing

Paths paths = new Paths(G, s); for (int v = 0; v < G.V(); v++) if (paths.hasPathTo(v)) StdOut.println(v);

print all vertices

connected to s

public class Paths

Paths(Graph G, int s) find paths in G from source s

boolean hasPathTo(int v) is there a path from s to v?

Iterable<Integer> pathTo(int v) path from s to v; null if no such path

Page 31: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

87

109

1211

0

6

4

21

5

3

Depth-first search

31

graph G

87

109

1211

0

6

4

21

5

3

87

109

1211

0

6

4

21

5

3

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 32: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

87

109

1211

0

6

4

21

5

3

Depth-first search

32

graph G

87

109

1211

0

6

4

21

5

3

87

109

1211

0

6

4

21

5

3

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

F

F

F

F

F

F

F

F

F

F

F

F

F

edgeTo[v]

Page 33: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

00 87

109

1211

6

4

21

5

3

Depth-first search

33

visit 0

87

109

1211

6

4

21

5

3

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

F

F

F

F

F

F

F

F

F

F

edgeTo[v]

Page 34: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

66

00

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

21

5

3

Depth-first search

34

visit 6

4

21

5

3

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

F

F

F

T

F

F

F

F

F

F

edgeTo[v]

0

Page 35: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

00

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

6

4

21

5

3

Depth-first search

35

visit 6

6

4

21

5

3

6

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

F

F

F

T

F

F

F

F

F

F

edgeTo[v]

0

Page 36: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

00

66

4

21

5

3

Depth-first search

36

visit 4

4

21

5

3 4

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

F

T

F

T

F

F

F

F

F

F

edgeTo[v]

6

0

Page 37: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

00

44

66

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

21

5

3

Depth-first search

37

visit 5

21

5

3

5

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

F

T

T

T

F

F

F

F

F

F

edgeTo[v]

6

4

0

Page 38: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

55

00

44

66

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

21

3

Depth-first search

38

visit 3

21

3

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 39: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

55

00

44

66

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

21

3

Depth-first search

39

visit 3

21

3

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 40: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

55

00

44

6621

3

Depth-first search

40

3 done

21

3

87

109

1211

87

109

1211

5

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 41: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

55

00

44

6621

3

Depth-first search

41

visit 5

21

87

109

1211

87

109

1211

5

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 42: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

55

00

44

6621

3

Depth-first search

42

visit 5

21

87

109

1211

87

109

1211

5

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 43: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

5

00

44

6621

3

Depth-first search

43

5 done

21

87

109

1211

87

109

1211

5

4

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 44: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

5

00

44

6621

3

Depth-first search

44

visit 4

21

87

109

1211

87

109

12114

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 45: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

5

00

44

6621

3

Depth-first search

45

visit 4

21

87

109

1211

87

109

12114

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 46: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

6621

3

Depth-first search

46

4 done

21

87

109

1211

87

109

12114

6

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 47: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

6

4

5

00

21

3

Depth-first search

47

6 done

21

87

109

1211

87

109

1211

0

6

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 48: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Depth-first search

48

visit 0

21

87

109

1211

87

109

1211

00

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

F

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

5

6

4

0

Page 49: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Depth-first search

49

visit 2

21

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

T

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

0

5

6

4

0

Page 50: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Depth-first search

50

2 done

21

87

109

1211

87

109

1211

00

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

T

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

0

5

6

4

0

Page 51: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Depth-first search

51

visit 0

1

87

109

1211

87

109

1211

00

1

2

3

4

5

6

7

8

9

10

11

12

v marked[]

T

F

T

T

T

T

T

F

F

F

F

F

F

edgeTo[v]

0

5

6

4

0

Page 52: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Depth-first search

52

visit 1

1

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[v]

0

0

5

6

4

0

Page 53: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Depth-first search

53

1 done

1

87

109

1211

87

109

1211

00

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[v]

0

0

5

6

4

0

Page 54: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

00

4

5

621

3

Depth-first search

54

0 done

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[v]

0

0

5

6

4

0

Page 55: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

0

4

5

621

3

Depth-first search

55

vertices reachable from 0

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[v]

0

0

5

6

4

0

Page 56: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Goal. Find all vertices connected to s (and a path).Idea. Mimic maze exploration.Algorithm.• Use recursion (ball of string).

• Mark each visited vertex (and keep track of edge taken to visit it).

• Return (retrace steps) when no unvisited options.

Data structures.• boolean[] marked to mark visited vertices.

• int[] edgeTo to keep tree of paths. (edgeTo[w] == v) means that edge v-w taken to visit w for first time

Depth-first search

Page 57: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

57

Depth-first search

public class DepthFirstPaths { private boolean[] marked; private int[] edgeTo; private int s;

public DepthFirstSearch(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]) { dfs(G, w); edgeTo[w] = v; } } }

marked[v] = trueif v connected to s

find vertices connected to s

recursive DFS does the work

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

initialize data structures

Page 58: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Depth-first search properties

Proposition. DFS marks all vertices connected to s in time proportional tothe sum of their degrees.

Pf.• Correctness:

- if w marked, then w connected to s (why?)

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

• Running time: Each vertex connected to s is visited once.

58

set ofunmarked

vertices

no such edgecan exist

source

v

s

set of markedvertices

w

x

Page 59: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Proposition. After DFS, can find vertices connected to s in constant time and can find a path to s (if one exists) in time proportional to its length.

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

59

Depth-first search properties

public boolean hasPathTo(int v) { return marked[v]; }

public Iterable<Integer> pathTo(int v) { if (!hasPathTo(v)) return null; Stack<Integer> path = new Stack<Integer>(); for (int x = v; x != s; x = edgeTo[x]) path.push(x); path.push(s); return path; }

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 60: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Depth-first search application: preparing for a date

60

http://xkcd.com/761/

Page 61: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Challenge. Flood fill (Photoshop magic wand).Assumptions. Picture has millions to billions of pixels.

Solution. Build a grid graph.• Vertex: pixel.

• Edge: between two adjacent gray pixels.

• Blob: all pixels connected to given pixel.

Depth-first search application: flood fill

61

Page 62: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

UNDIRECTED GRAPHS

‣ Graph API‣ Depth-first search‣ Breadth-first search‣ Connected components‣ Challenges

Page 63: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

63

graph G

0

4

2

1

5

3

adj[]0

1

2

3

4

5

2 1 5

0 2

5 4 2

3 2

3 0

0 1 3 4

6 80 52 42 31 20 13 43 50 2

tinyCG.txt standard drawing

drawing with both edges

adjacency lists

A connected undirected graph

VE

0

4

2

1

5

3

Page 64: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

64

add 0 to queue

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

queue00

Page 65: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

65

0

4

2

1

5

3

0

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

queue

dequeue 0

0

Page 66: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

66

0

4

2

1

5

3

0

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

queue

dequeue 0

22

0

Page 67: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

67

0

4

2

1

5

3

0

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

queue

2

dequeue 0

2

110

Page 68: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

68

dequeue 0

0

4

2

1

5

3

0

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

queue

2

1

2

1

55 0

Page 69: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

69

0 done

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

0

queue

2

1

2

1

5

5

0

Page 70: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

70

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

0

queue

2

1

5

1

5

Page 71: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

71

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

0

queue

1

5

1

5

Page 72: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

72

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

0

queue

1

5

1

5

Page 73: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

73

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

0

queue

1

5

1

5

33

2

Page 74: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

74

dequeue 2

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

0

queue

1

5

1

5

3

3

44

2

Page 75: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

75

2 done

0

4

2

1

5

3

4

2

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

1

5

1

5

3

3

4

4

Page 76: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

76

dequeue 1

0

4

2

1

5

3

4

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

1

55

3

3

4

4

Page 77: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

77

dequeue 1

0

4

2

1

5

3

4

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

5

3

45

3

4

Page 78: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

78

dequeue 1

0

4

2

1

5

3

4

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

55

33

4

4

Page 79: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

79

1 done

0

4

2

1

5

3

4

1

5

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

55

33

4

4

Page 80: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

80

dequeue 5

0

4

2

1

5

3

45

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

5

33

4

4

Page 81: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

81

dequeue 5

0

4

2

1

5

3

45

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

34

4

Page 82: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

82

dequeue 5

0

4

2

1

5

3

45

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

34

4

Page 83: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

83

5 done

0

4

2

1

5

3

45

3

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

34

4

Page 84: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

84

dequeue 3

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

34

4

Page 85: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

85

dequeue 3

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

44

Page 86: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

86

dequeue 3

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

44

Page 87: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

87

dequeue 3

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

3

44

Page 88: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Repeat until queue is empty:• Remove vertex v from queue.

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

33

Breadth-first search

88

3 done

0

4

2

1

5 4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

44

Page 89: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

89

dequeue 4

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

4

Page 90: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

90

dequeue 4

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

Page 91: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

91

dequeue 4

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

Page 92: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

92

4 done

0

4

2

1

5

3

4

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

queue

Page 93: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

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

93

done

0

1

2

3

4

5

v edgeTo[v]

0

0

2

2

0

0

4

2

1

5

3

Page 94: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Depth-first search. Put unvisited vertices on a stack.Breadth-first search. Put unvisited vertices on a queue.

Shortest path. Find path from s to t that uses fewest number of edges.

Intuition. BFS examines vertices in increasing distance from s.

94

Breadth-first search

Put s onto a FIFO queue, and mark s as visited.Repeat until the queue is empty: - remove the least recently added vertex v - add each of v's unvisited neighbors to the queue, and mark them as visited.

BFS (from source vertex s)

Breadth-firstmaze exploration

Page 95: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Proposition. BFS computes shortest path (number of edges) from s in a connected graph in time proportional to E + V.

Pf. [correctness] Queue always consists of zero or more vertices of distance k from s, followed by zero or more vertices of distance k + 1.Pf. [running time] Each vertex connected to s is visited once.

Breadth-first search properties

95

0

4

2

1

53

standard drawing

0

4

2

1

5

3

dist = 0 dist = 1 dist = 2

Page 96: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

96

Breadth-first search

public class BreadthFirstPaths { private boolean[] marked; private boolean[] edgeTo[]; private final int s; …

private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; 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; } } } } }

Page 97: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

97

Breadth-first search application: routing

Fewest number of hops in a communication network.

ARPANET, July 1977

Page 98: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

98

Breadth-first search application: Kevin Bacon numbers

Kevin Bacon numbers.

http://oracleofbacon.org SixDegrees iPhone App

Endless Games board game

Page 99: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

99

Kevin Bacon graph

• Include a vertex for each performer and 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 100: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

100

Breadth-first search application: Erdös numbers

hand-drawing of part of the Erdös graph by Ron Graham

Page 101: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

UNDIRECTED GRAPHS

‣ Graph API‣ Depth-first search‣ Breadth-first search‣ Connected components‣ Challenges

Page 102: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Def. Vertices v and w are connected if there is a path between them.Goal. Preprocess graph to answer queries: is v connected to w ? in constant time.Depth-first search. [next few slides]

102

Connectivity queries

public class CC

CC(Graph G) find connected components in G

boolean connected(int v, int w) are v and w connected?

int count() number of connected components

int id(int v) component identifier for v

Page 103: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

The relation "is connected to" is an equivalence relation:• Reflexive: v is connected to v.

• Symmetric: if v is connected to w, then w is connected to v.

• Transitive: if v connected to w and w connected to x, then v connected to x.

Def. A connected component is a maximal set of connected vertices.Remark. Given connected components, can answer queries in constant time.

103

Connected components

v id[v] 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 1 8 1 9 2 10 2 11 2 12 2

87

109

1211

0

6

4

21

5

3

3 connected components

Page 104: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Def. A connected component is a maximal set of connected vertices.

104

Connected components

63 connected components

Page 105: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Goal. Partition vertices into connected components.

105

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

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 106: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

87

109

1211

0

6

4

21

5

3

Connected components

106

graph G

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

F

F

F

F

F

F

F

F

F

F

F

F

F

87

109

1211

0

6

4

21

5

3

87

109

1211

0

6

4

21

5

3

cc[]

Page 107: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

00 87

109

1211

6

4

21

5

3

Connected components

107

visit 0

87

109

1211

6

4

21

5

3

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

F

F

F

F

F

F

F

F

F

F

cc[]

0

Page 108: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

0

0

66

00

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

21

5

3

Connected components

108

visit 6

4

21

5

3

87

109

1211

87

109

1211

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

F

F

F

T

F

F

F

F

F

F

cc[]

Page 109: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

00

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

6

4

21

5

3

Connected components

109

visit 6

6

4

21

5

3

6

87

109

1211

87

109

1211

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

F

F

F

T

F

F

F

F

F

F

cc[]

Page 110: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

00

66

4

21

5

3

Connected components

110

visit 4

4

21

5

3 4

87

109

1211

87

109

1211

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

F

T

F

T

F

F

F

F

F

F

cc[]

Page 111: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

00

44

66

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

21

5

3

Connected components

111

visit 5

21

5

3

5

87

109

1211

87

109

1211

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

F

T

T

T

F

F

F

F

F

F

cc[]

Page 112: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

55

00

44

66

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

21

3

Connected components

112

visit 3

21

3

87

109

1211

87

109

1211

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 113: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

55

00

44

66

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

21

3

Connected components

113

visit 3

21

3

87

109

1211

87

109

1211

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 114: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

55

00

44

6621

3

Connected components

114

3 done

21

3

87

109

1211

87

109

1211

5

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 115: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

55

00

44

6621

3

Connected components

115

visit 5

21

87

109

1211

87

109

1211

5

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 116: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

55

00

44

6621

3

Connected components

116

visit 5

21

87

109

1211

87

109

1211

5

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 117: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

5

00

44

6621

3

Connected components

117

5 done

21

87

109

1211

87

109

1211

5

4

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 118: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

5

00

44

6621

3

Connected components

118

visit 4

21

87

109

1211

87

109

12114

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 119: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

5

00

44

6621

3

Connected components

119

visit 4

21

87

109

1211

87

109

12114

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 120: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

6621

3

Connected components

120

4 done

21

87

109

1211

87

109

12114

6

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 121: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

6

4

5

00

21

3

Connected components

121

6 done

21

87

109

1211

87

109

1211

0

6

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 122: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Connected components

122

visit 0

21

87

109

1211

87

109

1211

00

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

F

T

T

T

T

F

F

F

F

F

F

cc[]

Page 123: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Connected components

123

visit 2

21

87

109

1211

87

109

1211

0

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 124: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Connected components

124

2 done

21

87

109

1211

87

109

1211

00

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 125: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Connected components

125

visit 0

1

87

109

1211

87

109

1211

00

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

F

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 126: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Connected components

126

visit 1

1

87

109

1211

87

109

1211

0

0

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 127: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

4

5

00

621

3

Connected components

127

1 done

1

87

109

1211

87

109

1211

00

0

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 128: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

00

4

5

621

3

Connected components

128

0 done

87

109

1211

87

109

1211

0

0

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 129: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

T

T

T

T

T

T

T

F

F

F

F

F

F

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

Connected components

129

connected component: 0 1 2 3 4 5 6

87

109

1211

87

109

1211

connectedcomponent

0

4

5

621

3

0

0

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v cc[]

Page 130: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

0

4

5

621

3

Connected components

130

check 1 2 3 4 5 6

87

109

1211

87

109

1211

0

0

0

0

0

0

0

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

F

F

F

F

F

F

cc[]

Page 131: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

0

4

5

621

3

Connected components

131

visit 7

87

109

1211

87

109

1211

0

0

0

0

0

0

0

1

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

F

F

F

F

F

cc[]

Page 132: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

0

4

5

621

3

Connected components

132

visit 8

87

109

1211

87

109

1211

0

0

0

0

0

0

0

1

1

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

F

F

F

F

cc[]

Page 133: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

0

4

5

621

3

Connected components

133

8 done

87

109

1211

7

109

1211

870

0

0

0

0

0

0

1

1

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

F

F

F

F

cc[]

Page 134: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

134

7 done

8

109

1211

109

1211

70

0

0

0

0

0

0

1

1

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

F

F

F

F

cc[]

Page 135: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

135

connected component: 7 8

8

109

1211

109

1211

7 80

0

0

0

0

0

0

1

1

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

F

F

F

F

cc[]

Page 136: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

136

check 8

8

109

1211

109

1211

0

0

0

0

0

0

0

1

1

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

F

F

F

F

cc[]

Page 137: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

137

visit 9

8

109

1211

109

1211

0

0

0

0

0

0

0

1

1

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

F

F

cc[]

Page 138: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

138

visit 11

8

109

1211

109

1211

0

0

0

0

0

0

0

1

1

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

F

cc[]

Page 139: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

139

visit 11

8

109

1211

109

1211

0

0

0

0

0

0

0

1

1

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

F

cc[]

Page 140: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

140

visit 12

8

109

1211

109

1211

0

0

0

0

0

0

0

1

1

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

T

cc[]

Page 141: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

141

visit 12

8

109

1211

109

1211

0

0

0

0

0

0

0

1

1

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

T

cc[]

Page 142: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

142

12 done

8

109

1211

109

11 1211

0

0

0

0

0

0

0

1

1

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

T

cc[]

Page 143: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

11

70

4

5

621

3

Connected components

143

11 done

8

109

12

109

11

9

0

0

0

0

0

0

0

1

1

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

T

cc[]

Page 144: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

11

70

4

5

621

3

Connected components

144

visit 9

8

109

12

1099

0

0

0

0

0

0

0

1

1

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

F

T

T

cc[]

Page 145: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

11

70

4

5

621

3

Connected components

145

visit 10

8

109

12

109

0

0

0

0

0

0

0

1

1

2

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

T

T

T

cc[]

Page 146: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

11

70

4

5

621

3

Connected components

146

10 done

8

109

12

9 109

0

0

0

0

0

0

0

1

1

2

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

T

T

T

cc[]

Page 147: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

9

11

70

4

5

621

3

Connected components

147

9 done

8

10

12

9

0

0

0

0

0

0

0

1

1

2

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

T

T

T

cc[]

Page 148: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

148

connected component: 9 10 11 12

8

9

11

10

12

0

0

0

0

0

0

0

1

1

2

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

T

T

T

cc[]

Page 149: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

149

check 10 11 12

8

11 12

109

0

0

0

0

0

0

0

1

1

2

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

T

T

T

cc[]

Page 150: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

To visit a vertex v :• Mark vertex v as visited.

• Recursively visit all unmarked vertices adjacent to v.

70

4

5

621

3

Connected components

150

done

8

11 12

109

0

0

0

0

0

0

0

1

1

2

2

2

2

0

1

2

3

4

5

6

7

8

9

10

11

12

marked[]v

T

T

T

T

T

T

T

T

T

T

T

T

T

cc[]

Page 151: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

151

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) private void dfs(Graph G, int v)

}

run DFS from one vertex in

each component

id[v] = id of component containing vnumber of components

see next slide

Page 152: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

152

Finding connected components with DFS (continued)

public int count() { return count; }

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

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

Page 153: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

UNDIRECTED GRAPHS

‣ Graph API‣ Depth-first search‣ Breadth-first search‣ Connected components‣ Challenges

Page 154: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 1

Problem. Is a graph bipartite?How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

154

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

0

6

4

21

5

3

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

0

6

4

21

5

3

Page 155: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 1

Problem. Is a graph bipartite?How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

155

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

0

6

4

21

5

3

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

0

6

4

21

5

3

simple DFS-based solution

(see textbook)

Page 156: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

High-school dating graph

Problem. Is a graph bipartite?

156

Image created by Mark Newman.Data drawn from Peter S. Bearman, James Moody, and Katherine Stove,Chains of affection: The structure of adolescent romantic and sexual networks,American Journal of Sociology 110, 44-91 (2004)

Page 157: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 2

Problem. Find a cycle.How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

157

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

0

6

4

21

5

3

0

6

4

21

5

3

Page 158: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 2

Problem. Find a cycle.

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

158

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

0

6

4

21

5

3

simple DFS-based solution

(see textbook)0

6

4

21

5

3

Page 159: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

The Seven Bridges of Königsberg. [Leonhard Euler 1736]

Euler tour. Is there a (general) cycle that uses each edge exactly once?Answer. Yes iff connected and all vertices have even degree.To find path. DFS-based algorithm (see textbook).

159

Bridges of Königsberg

“ … in Königsberg in Prussia, there is an island A, called the Kneiphof; the river which surrounds it is divided into two branches ... and these branches are crossed by seven bridges. Concerning these bridges, it was asked whether anyone could arrange a route in such a way that he could cross each bridge once and only once. ”

Page 160: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 3

Problem. Find a cycle that uses every edge.Assumption. Need to use each edge exactly once.

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

160

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

0

6

4

21

5

3

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

Page 161: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 3

Problem. Find a cycle that uses every edge.Assumption. Need to use each edge exactly once.

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

161

Eulerian tour

(classic graph-processing problem)

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

0

6

4

21

5

3

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

Page 162: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 4

Problem. Find a cycle that visits every vertex exactly once.

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

162

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

0

6

4

21

5

3

Page 163: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 4

Problem. Find a cycle that visits every vertex.Assumption. Need to visit each vertex exactly once.

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

163

Hamiltonian cycle

(classical NP-complete problem)

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

0

6

4

21

5

3

Page 164: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 5

Problem. Are two graphs identical except for vertex names?How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

164

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

0

6

4

21

5

3

3

1

5

2

4

0

6

0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1

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

Page 165: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 5

Problem. Are two graphs identical except for vertex names?

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

165

graph isomorphism is

longstanding open problem

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

0

6

4

21

5

3

✓3

1

5

2

4

0

6

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

0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1

Page 166: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 6

Problem. Lay out a graph in the plane without crossing edges?How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

166

1

6

4

2

0

5

3

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

0

6

4

21

5

3

Page 167: BBM 202 - ALGORITHMSbbm202/Spring2016/... · Mar. 22, 2016 BBM 202 - ALGORITHMS ... We estimated these mod-els in varied ego–alter pair types. To evaluate the possibility that omitted

Graph-processing challenge 6

Problem. Lay out a graph in the plane without crossing edges?

How difficult?• Any programmer could do it.

• Typical diligent algorithms student could do it.

• Hire an expert.

• Intractable.

• No one knows.

• Impossible.

167

linear-time DFS-based planarity algorithm

discovered by Tarjan in 1970s(too complicated for practitioners)

1

6

4

2

0

5

3

0

6

4

21

5

3

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