











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Galles; Class: Data Struct & Algorithms; Subject: Computer Science; University: University of San Francisco (CA); Term: Intersession 2009;
Typology: Study notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!












16-0: Graph Traversals
16-1: Depth First Search
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
16-4: Depth First Search
16-5: Depth First Search
16-6: Depth First Search
16-7: Depth First Search
16-11: Depth First Search
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
16-13: Depth First Search
16-14: DFS & Stacks
16-15: DFS & Stacks
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
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
Queue: 0
16-22: Breadth First Search
Queue: 124
16-23: Breadth First Search
Queue: 24034
16-24: Breadth First Search
Queue: 4034056
16-25: Breadth First Search
Queue: 034056013
16-26: Breadth First Search
Queue: 34056013
16-27: Breadth First Search
Queue: 60131426
16-31: Breadth First Search
Queue: 013142625
16-32: Breadth First Search
16-33: Breadth First Search
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
16-35: Breadth First Search
16-36: Breadth First Search
16-37: Breadth First Search
Queue: 0
Queue: 356
16-42: Breadth First Search
Queue: 56
16-43: Breadth First Search
Queue: 6
16-44: Breadth First Search
Queue:
16-45: Search Trees
16-46: DFS Search Trees
16-47: DFS Search Trees
16-51: DFS Search Trees
16-52: DFS Search Trees
16-53: DFS Search Trees
16-54: DFS Search Trees
16-55: DFS Search Trees
16-56: BFS Search Trees
16-60: DFS in Directed Graphs
16-61: DFS in Directed Graphs