graph traversal
Download
Skip this Video
Download Presentation
Graph Traversal

Loading in 2 Seconds...

play fullscreen
1 / 41

Graph Traversal - PowerPoint PPT Presentation


  • 183 Views
  • Uploaded on

Graph Traversal. Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue. Overview. Goal To systematically visit the nodes of a graph A tree is a directed, acyclic, graph (DAG) If the graph is a tree,

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Graph Traversal' - erasto


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
graph traversal
Graph Traversal

Text

  • Weiss, § 9.6

Depth-First Search

  • Think Stack

Breadth-First Search

  • Think Queue
overview
Overview
  • Goal
    • To systematically visit the nodes of a graph
  • A tree is a directed, acyclic, graph (DAG)
  • If the graph is a tree,
    • DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals
    • BFS is exhibited by level-order traversal
depth first search
Depth-First Search

// recursive, preorder, depth-first search

void dfs (Node v) {

if (v == null)

return;

if (v not yet visited)

visit&mark(v); // visit node before adjacent nodes

for (each w adjacent to v)

if (w has not yet been visited)

dfs(w);

} // dfs

slide4
Depth-First Search

// recursive, postorder, depth-first search

void dfs (Node v) {

if (v == null)

return;

tag(v); // mark v as having been considered

for (each w adjacent to v)

if (w has not yet been tagged)

dfs(w);

visit(v); // postorder traversal: visit node after

// adjacent nodes

} // dfs

slide5
Depth-First Search

// non-recursive, preorder, depth-first search

void dfs (Node v) {

if (v == null)

return;

push(v);

while (stack is not empty) {

pop(v);

if (v has not yet been visited)

mark&visit(v);

for (each w adjacent to v)

if (w has not yet been visited)

push(w);

} // while

} // dfs

slide6
Depth-First Search

// non-recursive, postorder, depth-first search

void dfs (Node v) {

// Lex 20 (Do not need to submit)

} // dfs

example
Example

5

2

0

1

3

7

4

6

Policy: Visit adjacent nodes in increasing index order

slide8
Preorder DFS: Start with Node 5

5

2

0

1

3

7

4

6

5 1 0 3 2 7 6 4

slide10
Preorder DFS: Start with Node 5

5

2

0

1

3

7

4

6

Pop/Visit/Mark 5

5

slide11
Preorder DFS: Start with Node 5

1

2

5

2

0

1

3

7

4

6

Push 2, Push 1

5

slide12
Preorder DFS: Start with Node 5

2

5

2

0

1

3

7

4

6

Pop/Visit/Mark 1

5 1

slide13
Preorder DFS: Start with Node 5

0

2

4

2

5

2

0

1

3

7

4

6

Push 4, Push 2, Push 0

5 1

slide14
Preorder DFS: Start with Node 5

2

4

2

5

2

0

1

3

7

4

6

Pop/Visit/Mark 0

5 1 0

slide15
Preorder DFS: Start with Node 5

3

7

2

4

2

5

2

0

1

3

7

4

6

Push 7, Push 3

5 1 0

slide16
Preorder DFS: Start with Node 5

7

2

4

2

5

2

0

1

3

7

4

6

Pop/Visit/Mark 3

5 1 0 3

slide17
Preorder DFS: Start with Node 5

2

7

2

4

2

5

2

0

1

3

7

4

6

Push 2

5 1 0 3

slide18
Preorder DFS: Start with Node 5

7

2

4

2

5

2

0

1

3

7

4

6

Pop/Mark/Visit 2

5 1 0 3 2

slide19
Preorder DFS: Start with Node 5

2

4

2

5

2

0

1

3

7

4

6

Pop/Mark/Visit 7

5 1 0 3 2 7

slide20
Preorder DFS: Start with Node 5

6

2

4

2

5

2

0

1

3

7

4

6

Push 6

5 1 0 3 2 7

slide21
Preorder DFS: Start with Node 5

2

4

2

5

2

0

1

3

7

4

6

Pop/Mark/Visit 6

5 1 0 3 2 7 6

slide22
Preorder DFS: Start with Node 5

4

2

5

2

0

1

3

7

4

6

Pop (don’t visit) 2

5 1 0 3 2 7 6

slide23
Preorder DFS: Start with Node 5

2

5

2

0

1

3

7

4

6

Pop/Mark/Visit 4

5 1 0 3 2 7 6 4

slide24
Preorder DFS: Start with Node 5

5

2

0

1

3

7

4

6

Pop (don’t visit) 2

5 1 0 3 2 7 6 4

slide25
Preorder DFS: Start with Node 5

5

2

0

1

3

7

4

6

Done

5 1 0 3 2 7 6 4

slide27
Depth-First SearchPolicy: Don’t push nodes twice

// non-recursive, preorder, depth-first search

void dfs (Node v) {

if (v == null)

return;

push(v);

while (stack is not empty) {

pop(v);

if (v has not yet been visited)

mark&visit(v);

for (each w adjacent to v)

if (w has not yet been visited && not yet stacked)

push(w);

} // while

} // dfs

slide29
Postorder DFS: Start with Node 5

5

2

0

1

3

7

4

6

2 3 6 7 0 4 1 5

breadth first search
Breadth-first Search
  • Ripples in a pond
  • Visit designated node
  • Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc.
  • For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order)
slide31
Breadth-First Search

// non-recursive, preorder, breadth-first search

void bfs (Node v) {

if (v == null)

return;

enqueue(v);

while (queue is not empty) {

dequeue(v);

if (v has not yet been visited)

mark&visit(v);

for (each w adjacent to v)

if (w has not yet been visited && has not been queued)

enqueue(w);

} // while

} // bfs

slide32
BFS: Start with Node 5

5

2

0

1

3

7

4

6

5 1 2 0 4 3 7 6

slide35
BFS: Visit 1 and 2

5

2

0

1

3

7

4

6

5 1 2

slide36
BFS: Nodes two-away

5

2

0

1

3

7

4

6

5 1 2

slide37
BFS: Visit 0 and 4

5

2

0

1

3

7

4

6

5 1 2 0 4

slide38
BFS: Nodes three-away

5

2

0

1

3

7

4

6

5 1 2 0 4

slide39
BFS: Visit nodes 3 and 7

5

2

0

1

3

7

4

6

5 1 2 0 4 3 7

slide40
BFS: Node four-away

5

2

0

1

3

7

4

6

5 1 2 0 4 3 7

slide41
BFS: Visit 6

5

2

0

1

3

7

4

6

5 1 2 0 4 3 7 6

ad