
























































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
A comprehensive review of graph algorithms, focusing on depth-first search (dfs) and the distinction among tree, back, forward, and cross edges. It includes explanations, diagrams, and code examples to help students understand the concepts.
Typology: Slides
1 / 64
This page cannot be seen from the preview
Don't miss anything!

























































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)