CS252 Algorithms Friday, 12 April 2024 + Whatever you want to talk about + DFS and BFS - Let's do an example of each - Storage of G - n = |V| - how big can m = |E| be? how small? - adjacency lists: space = O(m + n), i.e. O(|E| + |V|) - adjacency matrix: space = O(n^2) - Runtime complexity of BFS (w/ adjacency lists) BFS(s): visited[s] = true O(1) visited[v] = false for all other v O(n) L[0] = [s] # using python list notation [] O(1) i = 0 # layer counter O(1) while L[i] is not empty L[i+1] = [] for u in L[i] for v in neighbors(u) <-- how many iterations of this loop, total? 2m, because we do this twice per edge if not visited[v] O(1) visited[v] = true O(1) L[i+1].add(v) O(1) i++ Runtime = O(n + m) = O(|V| + |E|) + What would you change about this algorithm to - compute the spanning tree? - determine whether G is connected? - compute G's diameter? + Worksheet for more practice