Docsity

Graph Algorithms: Understanding Tree, Back, Forward, and Cross Edges in Depth-First Search, Slides of Computer Science

Partial preview of the text

Download Graph Algorithms: Understanding Tree, Back, Forward, and Cross Edges in Depth-First Search and more Slides Computer Science in PDF only on Docsity!

Algorithms

Graph Algorithms

Administrative โ— Test postponed to Friday โ— Homework: โ–  Turned in last night by midnight: full credit โ–  Turned in tonight by midnight: 1 day late, 10% off โ–  Turned in tomorrow night: 2 days late, 30% off โ–  Extra credit lateness measured separately

Review: Representing Graphs โ— Assume V = {1, 2, โ€ฆ, n } โ— An adjacency matrix represents the graph as a n x n matrix A: โ–  A[ i , j ] = 1 if edge ( i , j ) โˆˆ E (or weight of edge) = 0 if edge ( i , j ) โˆ‰ E โ–  Storage requirements: O(V 2 ) โ—‹ A dense representation โ–  But, can be very efficient for small graphs โ—‹ Especially if store just one bit/edge โ—‹ Undirected graph: only need one diagonal of matrix

Review: Graph Searching โ— Given: a graph G = (V, E), directed or undirected โ— Goal: methodically explore every vertex and every edge โ— Ultimately: build a tree on the graph โ–  Pick a vertex as the root โ–  Choose certain edges to produce a tree โ–  Note: might also build a forest if graph is not connected

Review: Breadth-First Search โ— Again will associate vertex โ€œcolorsโ€ to guide the algorithm โ–  White vertices have not been discovered โ—‹ All vertices start out white โ–  Grey vertices are discovered but not fully explored โ—‹ They may be adjacent to white vertices โ–  Black vertices are discovered and fully explored โ—‹ They are adjacent only to black and gray vertices โ— Explore vertices by scanning adjacency list of grey vertices

Review: Breadth-First Search BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v โˆˆ u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What does v->p represent? What does v->d represent?

Breadth-First Search: Example โˆž โˆž

r s t u v w x y Q:^ s

Breadth-First Search: Example 1 โˆž

r s t u v w x y Q: w r

Breadth-First Search: Example 1 2

r s t u v w x y Q: t^ x^ v

Breadth-First Search: Example 1 2

r s t u v w x y Q: x^ v^ u

Breadth-First Search: Example 1 2

r s t u v w x y Q: u^ y

Breadth-First Search: Example 1 2

r s t u v w x y Q: y

BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v โˆˆ u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the running time? Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vertโ€™s adjacency list Total running time: O(V+E)

BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v โˆˆ u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the storage cost in addition to storing the graph? Total space used: O(max(degree(v))) = O(E)