20
GRAPH TRAVERSAL Lecture 18 CS 2110 — Spring 2019

GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

GRAPH TRAVERSALLecture 18CS 2110 — Spring 2019

Page 2: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

JavaHyperText Topics

“Graphs”, topic 8: DFS, BFS

2

Page 3: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Graph Representations3

0 1 2 3 4

0 F F F F F

1 F F T T F

2 F F F F T

3 F F T F T

4 F F F F F

1 2

3 4

1

2

3

4

3

4

42

2

Adjacency listAdjacency matrix

Page 4: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

(improved definitions in Lec 17)

Review: Sparse vs. Dense4

Page 5: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Graph Search5

Page 6: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Graph Traversal

Goal: visit each vertex that is reachable from some starting vertex

And: even if there are many paths to a node, visit only once

Two algorithms: DFS, BFS

6

1

7

2

5

3

4

68

1

7

2

5

3

4

68

Page 7: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Intuition: one person exploring a maze

Depth-First Search (DFS)7

Page 8: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Depth-First Search

/** Visit every node reachable along a path ofunvisited nodes from node v.Precondition: v is unvisited. */

void dfs(Vertex v) {mark v visited;for all edges (v,u):

if u is unmarked:dfs(u);

}

8

Idea: Recursively visit each unvisited neighbor

1

7

2

5

3

4

68

1 2 3

57

8

dfs(1) visits the nodes in this order: 1, 2, 3, 5, 7, 8

Page 9: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Poll #19

Page 10: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Depth-First Search10

/** Visit every node reachable along a path ofunvisited nodes from node v.Precondition: v is unvisited. */

void dfs(Vertex v) {mark v visited;for all edges (v,u):

if u is unmarked:dfs(u);

}

Demo

Page 11: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

DFS Space Efficiency

void dfs(Vertex v) {mark v visited;for all edges (v,u):

if u is unmarked:dfs(u);

}

11

Suppose graph has V vertices and E edges

Space required?• Mark for each vertex: O(V)• Frame for each recursive call: O(V)

Worst case: O(V)

1

7

2

5

3 4

68

Page 12: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

DFS Time Efficiency

void dfs(Vertex v) {mark v visited;for all edges (v,u):

if u is unmarked:dfs(u);

}

12

Suppose graph has V vertices and E edges

Time required?• Mark each vertex: O(V)• Recursive call for on each unvisited vertex: O(V)• Find each edge• in adj list: O(E): Worst case: O(V+E)• In adj matrix: O(V2): Worst case: O(V2)

1 2

34

Page 13: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

void dfs(Vertex u) {Stack s= new Stack();s.push(u);while (s is not empty) {

u= s.pop(); if (u not visited) {

visit u;for each edge (u, v):

s.push(v);}

}}

Variant: Iterative DFS13

1

7

2

5

3

4

68

1Stack:

1

257

3

8

2 3

57

8

Same algorithm; non-recursive implementation

u: 17

5

85255

3 55Visit order was 1, 7, 8, 5, 2, 3: differs from before because of order edges processed

Demo

Page 14: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Intuition: Search party fanning out in all directions

Breadth-First Search (BFS)14

Page 15: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Breadth-First Search15

Idea: Iteratively process the graph in "layers" moving further away from the source vertex.

1

7

2

5

3

4

6

8

1 2 3

57

8

Page 16: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Breadth-First Search16

1

7

2

5

3

4

6

8

1

1

752 53 5 8

2 3

57

8

/** Visit all vertices reachable on unvisited paths from u. */void dfs(int u) {

Stack s= new Stack()s.push(u);while ( ) {

u= s.pop(); if (u not visited) {

visit u;for each edge (u, v):

s.push(v);}

}}

/** Visit all vertices reachable unvisited paths from u. */void bfs(int u) {

Queue q= new Queue()q.add(u);while ( q is not empty ) {

u= q.remove(); if (u not visited) {

visit u;for each (u, v):

q.add(v); }

}}

Idea: Iteratively process the graph in "layers" moving further away from the source vertex.

Queue:Visit order was 1, 2, 5, 7, 3, 8 Demo

Page 17: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Poll #217

Page 18: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Improved BFS18

/** Visit all vertices reachable on unvisited paths from u. */void bfs(int u) {

Queue q= new Queueq.add(u);while ( q is not empty ) {

u= q.remove(); if (u not visited) {

visit u;for each (u, v):

if (v not encountered) {mark v as encountered;q.add(v);

} } } }

Idea: Don’t put vertex in queue if already encountered

Page 19: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Analyzing BFS19

/** Visit all vertices reachable on unvisited paths from u. */void bfs(int u) {

Queue q= new Queueq.add(u);while ( q is not empty ) {

u= q.remove(); if (u not visited) {

visit u;for each (u, v):

if (v not encountered) {mark v as encountered;q.add(v);

} } } }

Same as DFS.Space: O(V)Time:• Adj list: O(V+E)• Adj matrix: O(V2)

Page 20: GRAPH TRAVERSAL - Cornell University · Graph Traversal Goal: visiteach vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only

Comparing Traversal Algorithms

¨ Time: O(V+E) or O(V2)¨ Space: O(V)

¨ Time: O(V+E) or O(V2)¨ Space: O(V)

DFS (recursive) DFS & BFS (iterative, improved*)

20

*Without improvement, space becomes O(E)