/
Graph Traversal Algorithms Graph Traversal Algorithms

Graph Traversal Algorithms - PowerPoint Presentation

pasty-toler
pasty-toler . @pasty-toler
Follow
559 views
Uploaded On 2016-09-10

Graph Traversal Algorithms - PPT Presentation

DepthFirstSearchDFS BreadthfirstsearchBFS Cody Forrest Depth amp breadth Depth Breadth A E F G H A G F E I H B B C C D D B B C C ID: 463890

nodes dfs node bfs dfs nodes bfs node search depth amp topological path graphs stack https graph dfsstack visited

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Graph Traversal Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Graph Traversal Algorithms

*Depth-First-Search(DFS)

*Breadth-first-search(BFS)

Cody ForrestSlide2

Depth & breadth

Depth Breadth

| |

A

E

F

G

H

A

G

F

E

I

H

B

B

C

C

D

D

B

B

C

C

D

DSlide3

What can you do with(DFs||BFS)?

*

Searching trees

and graphs for a goal.*Calculating shortest paths from *s node to *g node (BFS).

*Finding all nodes reachable from *s node(BFS/DFS).

*Path-Finding(DFS).*Maze Generation(DFS).

*Simple Cycle Detections(DFS

).*Topological Sorting(DFS).*Checking for Bipartite Graphs(BFS).Slide4

Depth-First-Search(DFS)

*Searches depth of a given

path

until its exhausted.*Utilizes a stack data structure.

*Backtracks to next node on stack and checks nodes available.*Simple Recursive Implementation.

*Less overhead in comparison to BFS(In most cases).*High depth graphs or trees can be inefficient to search.Slide5

Breadth-First-Search(b

FS

)

*Searches all neighbors of a node before increasing search depth.*Utilizes a queue data structure.

*Searches node at head of the queue and checks for

nodes available.*Memory Intensive for trees or graphs with high branching factors.Slide6

Tri-Coloring

Node Types:

*White – Unvisited Nodes.

*Gray – Visited but n

ot finalized nodes.

*Black – Finalized Nodes.Generalizations:*All nodes are initialized as White nodes.*Nodes still on the search stack/queue must be gray nodes.

*Nodes in the search path but not in the search stack/queue must be black nodes.*Any white nodes left after DFS/BFS finishes are considered unreachable from the *S node.

Slide7

Simple DFS AND BFS Examples

G1

DFSVisited

[] = { A , B , F , C , D, E }

D

A

B

C

D

E

F

A

B

F

F

B

C

D

E

E

C

A

G2

BFSVisited

[] =

{ A , B , C , F , D, E }

A

B

C

D

E

F

A

B

C

F

A

F

B

D

E

C

F

D

ESlide8

To see who’s paying

attention,

t

he first person to raise their

hand gets removed from the name pool.Slide9

Less SIMPLE Examples

G1 G2

Search Path of DFS(G1, B) &

Search Path of BFS(G2, H)

A

B

C

D

F

G

H

E

B

A

D

F

G

E

C

HSlide10

Topological Sorting with

dfs

*Requires a

Directed-Acyclic-Graph(DAG).*Directed edges correspond to constraints.

*Vertices correspond to tasks.*Provided the graph is a DAG at least one topological order always exists.*Graphs with few constraints have many possible orderings.Slide11

TARJAN’S algorithm

*Randomly pick a starting node.

*DFS the node until you hit a dead end.

*While backtracking, add the finalized nodes you came from to a stack.

*Repeat steps until all nodes have been marked.*Stack represents the topological ordering.Slide12

MORE Examples

A Topological Order Starting With DFS(G3,A)

G3

D

B

A

F

G

I

E

J

C

H

A

C

E

I

I

E

F

J

J

F

C

A

B

D

H

H

D

B

G

G

TopStack

I

DFSStack

A

DFSStack

C

A

DFSStack

E

C

A

DFSStack

I

E

C

A

TopStack

E

I

DFSStack

I

E

C

A

DFSStack

I

E

C

A

DFSStack

F

I

E

C

A

DFSStack

J

F

I

E

C

A

TopStack

E

I

TopStack

E

I

TopStack

J

E

I

TopStack

F

J

E

I

DFSStack

J

F

I

E

C

A

DFSStack

J

F

I

E

C

A

TopStack

C

F

J

E

I

DFSStack

J

F

I

E

C

A

TopStack

A

C

F

J

E

I

DFSStack

J

F

I

E

C

A

TopStack

A

C

F

J

E

I

DFSStack

B

J

F

I

E

C

A

TopStack

A

C

F

J

E

I

DFSStack

D

B

J

F

I

E

C

A

TopStack

A

C

F

J

E

I

DFSStack

H

D

B

J

F

I

E

C

A

TopStack

H

A

C

F

J

E

I

DFSStack

H

D

B

J

F

I

E

C

A

TopStack

D

H

A

C

F

J

E

I

DFSStack

H

D

B

J

F

I

E

C

A

TopStack

B

D

H

A

C

F

JEI

DFSStackHDBJFIECA

DFSStackGHDBJFIECA

DFSStackGHDBJFIECA

TopStackGBDHACFJEI

TopStack

B

D

H

A

C

F

J

E

ISlide13

Bipartite Graph TESTING with BFS

Generalizations For Coloring:

*Graphs Nodes are colored Red or Blue.

*The *S Node is always red.

*All neighbors of red nodes must be blue nodes(Vice-versa).*An edge always connects a red node to a blue node(Vice-versa).Slide14

examples

G1

BFSCOL(G1,A

)

A

B

E

C

F

D

G

H

D

E

G

B

F

H

C

QUE

*A

*D

QUE

*A

*D

*E

QUE

*A

*D

*E

*G

QUE

*A

*D*E*G*B

QUE*A

*D*E*G*B*F

QUE*A

*D

*E

*G

*B

*F

*H

G2

BFSCOL(G2,G)

C

B

E

G

F

D

A

A

E

F

B

D

CSlide15

The good | the bad | the better(

Mostly

)

DFS Good

| *Low Memory Usage *Many Applications(Maze Generation, Topological

Sorting, Spanning Trees)

DFS Bad |

*Recursive Solution adds overhead.

*Can get stuck on one path.

BFS

Good

| *

Finds shortest solution available. *Guaranteed solution(if it exists). *

Simple implementation for state searching(AI).

BFS Bad |

*High Memory

Usage

The Better…->Slide16

Iterative deepening dfs

*DFS with increasing depths.

*Mimic’s BFS traversal pattern.

*Space requirements of DFS.

*Requires duplicated work.Slide17

Example

G1

Depth 0 Visited[] =

{A}

Depth 1 Visited[] = {A,B,F}Depth 2 Visited[] = {A,B,C,E,F,G,H}Depth 3 Visited[] = {A,B,C,D,E,F,G,H}

A

E

F

G

H

B

C

DSlide18

EXTRA: Matrix -> graph

&

Matrix - > grid - > graph

(J) 0 1 2 3 4 0 {{0,9,0,0,3} 1 {0,0,2,4,7}

E = (I -> J) (I) 2 {0,0,0,0,0} 3 {0,0,0,0,0} 4 {0,0,0,0,0}}

0

2

4

3

1

9

3

2

4

7

{{#,3,G}

{1,2,4} -> ->

{S,#,#}}

#

3 G

1 2 4

S # #

1

G

S

2

3

4Slide19

Sources

https://www.cs.usfca.edu/~

galles/visualization/java/download.html

https://www.ics.uci.edu/~eppstein/161/960215.htmlhttp://www.inf.ed.ac.uk/teaching/courses/cs2/LectureNotes/CS2Bh/ADS/ads10.pdf

https://www.cs.usfca.edu/~galles/visualization/TopoSortDFS.htmlhttps://www8.cs.umu.se/kurser/TDBAfl/VT06/algorithms/BOOK/BOOK4/NODE160.HTMhttps://

www.google.com/search?q=grid&biw=1760&bih=888&tbm=isch&source=lnms&sa=X&ved=0ahUKEwjBtqrcw8HJAhWM5iYKHYd8DdgQ_AUIBigB#imgrc=toBWsRVF1psi7M%3Ahttps://en.wikipedia.org/wiki/Topological_sorting#CITEREFCormenLeisersonRivestStein2001