Lecture Notes on Graph Traversals | CS 245, Study notes of Data Structures and Algorithms

Material Type: Notes; Professor: Galles; Class: Data Struct & Algorithms; Subject: Computer Science; University: University of San Francisco (CA); Term: Intersession 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-urk
koofers-user-urk 🇺🇸

10 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS245-2009S-16 Graph Traversals
BFS & DFS 1
16-0: Graph Traversals
Visit every vertex, in an order defined by the topololgy of the graph.
Two major traversals:
Depth First Search
Breadth First Search
16-1: Depth First Search
Starting from a specific node (pseudo-code):
DFS(Edge G[], int vertex, boolean Visited[]) {
Visited[vertex] = true;
for each node w adajcent to vertex:
if (!Visited[w])
DFS(G, w, Visited);
}
16-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.next) {
if (!Visited[tmp.neighbor])
DFS(G, tmp.neighbor, Visited);
}
}
16-3: Depth First Search
Example
Visited nodes cicrled in red
0
1
2
3
4
5
6
16-4: Depth First Search
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Lecture Notes on Graph Traversals | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS245-2009S-

BFS & DFS 1

16-0: Graph Traversals

  • Visit every vertex, in an order defined by the topololgy of the graph.
  • Two major traversals:
    • Depth First Search
    • Breadth First Search

16-1: Depth First Search

  • Starting from a specific node (pseudo-code):

DFS(Edge G[], int vertex, boolean Visited[]) { Visited[vertex] = true; for each node w adajcent to vertex: if (!Visited[w]) DFS(G, w, Visited); }

16-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.next) { if (!Visited[tmp.neighbor]) DFS(G, tmp.neighbor, Visited); } }

16-3: Depth First Search

  • Example
    • Visited nodes cicrled in red

16-4: Depth First Search

CS245-2009S-

BFS & DFS 2

  • Example
    • Visited nodes cicrled in red

DFS(0)

16-5: Depth First Search

  • Example
    • Visited nodes cicrled in red

DFS(0)

DFS(1)

16-6: Depth First Search

  • Example
    • Visited nodes cicrled in red

DFS(0)

DFS(1)

DFS(3)

16-7: Depth First Search

CS245-2009S-

BFS & DFS 4

  • Example
    • Visited nodes cicrled in red

DFS(0)

DFS(1)

DFS(3)

DFS(4)

DFS(2)

DFS(5)

DFS(6)

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

16-12: Depth First Search

  • Examples

16-13: Depth First Search

  • Examples

CS245-2009S-

BFS & DFS 5

16-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

16-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.next) { if (!Visited[tmp.neighbor]) DFS(G, tmp.neighbor, Visited); } }

16-16: DFS & Stacks

  • DFS, using stack

void DFS(Edge G[], int vertex, boolean Visited[]) { Edge tmp; int nextV; Stack S = new Stack(); S.push(new Integer(vertex)); while (!S.empty()) { nextV = ((Integer) S.pop()).intValue(); if (!Visited[nextV]) { Visited[nextV] = true; for (tmp = G[nextV]; tmp != null; tmp = tmp.next) { S.push(new Integer(tmp.neighbor)); }} } }

16-17: Breadth First Search

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

CS245-2009S-

BFS & DFS 7

  • Visited nodes cicrled

Queue: 0

16-22: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 124

16-23: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 24034

16-24: Breadth First Search

  • Example

CS245-2009S-

BFS & DFS 8

  • Visited nodes cicrled

Queue: 4034056

16-25: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 034056013

16-26: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 34056013

16-27: Breadth First Search

  • Example

CS245-2009S-

BFS & DFS 10

  • Visited nodes cicrled

Queue: 60131426

16-31: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 013142625

16-32: Breadth First Search

  • Alternate version of BFS
    • Previous code marks nodes as VISITED as they are removed from the queue
    • We could also mark nodes as VISITED when they are placed on the queue

16-33: Breadth First Search

  • Coding BFS (Alternate version):

void BFS(Edge G[], int vertex, boolean Visited[]) { Edge tmp; int nextV; Queue Q = new Queue(); Viisited[vertex] = true; Q.enquque(new Integer(vertex)); while (!Q.empty()) { nextV = ((Integer) Q.dequeue()).intValue(); for (tmp = G[nextV]; tmp != null; tmp = tmp.next) { if (!Visited[tmp.neighbor]) { Visited[tmp.neighbor] = true; Q.enqueue(new Integer(tmp.neighbor)); } } } }

16-34: Breadth First Search

CS245-2009S-

BFS & DFS 11

  • Alternate version of BFS
    • Previous code marks nodes as VISITED as they are removed from the queue
    • We could also mark nodes as VISITED when they are placed on the queue
  • How does execution differ?

16-35: Breadth First Search

  • Alternate version of BFS
    • Previous code marks nodes as VISITED as they are removed from the queue
    • We could also mark nodes as VISITED when they are placed on the queue
  • How does execution differ?
  • How does execution differ?
    • Version I: A vertex is added to the queue for each edge in the graph (so the same vertex can be added to the queue more than once
    • Version II: Each vertex is added to the queue at most once

16-36: Breadth First Search

  • Example
    • Visited nodes cicrled

16-37: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 0

CS245-2009S-

BFS & DFS 13

  • Example
    • Visited nodes cicrled

Queue: 356

16-42: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 56

16-43: Breadth First Search

  • Example
    • Visited nodes cicrled

Queue: 6

16-44: Breadth First Search

CS245-2009S-

BFS & DFS 14

  • Example
    • Visited nodes cicrled

Queue:

16-45: Search Trees

  • Describes the order that nodes are examined in a traversal
  • Directed Tree
    • Directed edge from v 1 to v 2 if the edge (v 1 , v 2 ) was followed during the traversal

16-46: DFS Search Trees

  • Starting from node 0, adjacency list sorted by vertex number:

16-47: DFS Search Trees

  • Starting from node 0, adjacency list sorted by vertex number:

CS245-2009S-

BFS & DFS 16

16-51: DFS Search Trees

  • Starting from node 2, adjacency list sorted by vertex number:

16-52: DFS Search Trees

  • Starting from node 0, adjacency list sorted by vertex number:

16-53: DFS Search Trees

  • Starting from node 0, adjacency list sorted by vertex number:

CS245-2009S-

BFS & DFS 17

16-54: DFS Search Trees

  • Starting from node 2, adjacency list sorted by vertex number:

16-55: DFS Search Trees

  • Starting from node 2, adjacency list sorted by vertex number:

16-56: BFS Search Trees

  • Starting from node 0, adjacency list sorted by vertex number:

CS245-2009S-

BFS & DFS 19

16-60: DFS in Directed Graphs

  • Starting from node 0, adjacency list sorted by vertex number:

16-61: DFS in Directed Graphs

  • Starting from node 0, adjacency list sorted by vertex number: