Docsity

Two Major Graph Traversal Algorithms: Depth First Search and Breadth First Search, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Two Major Graph Traversal Algorithms: Depth First Search and Breadth First Search and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ Graph Traversals^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p.1/

19-0:^ Graph Traversals

Visit every vertex, in an order defined by the topology ofthe graph. Two major traversals:^ Depth First Search^ Breadth First Search

Department of Computer Science — University of San Francisco – p.2/

19-2:^ Depth First Search

class^ Edge {public int^ neighbor;public int^ next;} void^ DFS(Edge^ G[], int

vertex,^ boolean^ Visited[]) Edge tmp;Visited[vertex]^ = true;for (tmp =^ G[vertex];

tmp !=^ null; tmp^ = tmp.nex if (!Visited[tmp.neighbor])DFS(G,^ tmp.neighbor,

Visited); } }

Department of Computer Science — University of San Francisco – p.4/

19-3:^ Depth First Search

Example^ Visited nodes cicrled in red^1340265

Department of Computer Science — University of San Francisco – p.5/

19-5:^ Depth First Search

Example^ Visited nodes cicrled in red^1340265

DFS(0)^ DFS(1)^ Department of Computer Science — University of San Francisco – p.7/

19-6:^ Depth First Search

Example^ Visited nodes cicrled in red^1340265

DFS(0)^ DFS(1)^ DFS(3)^ Department of Computer Science — University of San Francisco – p.8/

19-8:^ Depth First Search

Example^ Visited nodes cicrled in red^1340265

DFS(0)^ DFS(1)^ DFS(3)^ DFS(4)^ DFS(2)^ Department of Computer Science — University of San Francisco – p.10/

19-9:^ Depth First Search

Example^ Visited nodes cicrled in red^1340265

DFS(0)^ DFS(1)^ DFS(3)^ DFS(4)^ DFS(2)^ DFS(5)^ Department of Computer Science — University of San Francisco – p.11/

19-11:^ Depth First Search

To visit every node in the graph: TraverseDFS(Edge^ G[])^

int i;boolean Visited^ = new

Edge[G.length]; for (i=0;^ i<G.length;

i++) Visited[i]^ = false;for (i=0;^ i<G.length;

i++) if (!Visited[i])DFS(G,^ i, Visited);}

Department of Computer Science — University of San Francisco – p.13/

19-12:^ Depth First Search

Examples 02 13

4 5 6 Department of Computer Science — University of San Francisco – p.14/

19-14:^ DFS & Stacks

Keep track of what nodes we have left using a stack Recursive version implicitly uses the system stack Can write DFS non-recursively, using our own stack

Department of Computer Science — University of San Francisco – p.16/

19-15:^ DFS & Stacks

DFS, using recursion void^ DFS(Edge^ G[], int

vertex,^ boolean^ Visited[]) Edge tmp;Visited[vertex]^ = true;for (tmp =^ G[vertex];

tmp !=^ null; tmp^ = tmp.nex if (!Visited[tmp.neighbor])DFS(G,^ tmp.neighbor,

Visited); } }

Department of Computer Science — University of San Francisco – p.17/

19-17:^ Breadth First Search

DFS: Look as^ Deep^ as possible, before looking wide^ Examine all descendants of a node, before looking atsiblings BFS: Look as^ Wide^ as possible, before looking deep^ Visit all nodes 1 away, then 2 away, then three away,and so on

Department of Computer Science — University of San Francisco – p.19/

19-18:^ Breadth First Search

Examples 02 13

4 5 6 Department of Computer Science — University of San Francisco – p.20/