CS252 Algorithms Wednesday, 10 April 2024 + Questions + Studying for the exam - My philosophy of in-class exams - Read through Layla's review sheet (posted on our website) - Topics - O, Ω, ϴ - definitions - simple O, Ω, ϴ proofs (e.g., polynomials) - arrange common functions in complexity order (e.g. n = O(n^2) but n^2 != O(n)) - Stable matching and Gale-Shapley - inputs and outputs - stepping through a small example - arguments in the correctness proof - arguments in the runtime analysis - variants we have discussed - Graphs - basic vocabulary - BFS, DFS - [not topological sort, Dijkstra, etc. -- all of which are coming soon] - Question types - Very simple proofs - Definitions of various terms - Short answer - Here's a specific graph or GS preference list set or whatever walk through algorithm X using this example - Here's a specific graph or ... does it have such and such a property? why or why not? - Questions about problem set problems - Questions about slight variations of problem set problems + BFS and DFS BFS on graph g = (V, E) for v in g: v.visited = false q = queue() q.add(start_node) visit(start_node) while not q.empty() v = q.remove() for w in v.neighbors if not w.visited visit(w) q.add(w) B ---- C --- D / ____/| / \ / / | / \ A -- | / E \ |/ / F ----- G ---- H THIS WAS DUMB - Grab a partner. Each of you get a piece of paper. One of you is "queue" and one of you is "stack". - Start at A - We're gonna walk through BFS/DFS together. + Thinking about BFS & DFS - Why do you want to traverse a graph? - compute shortest paths from start node (BFS) - look for cycles - traveling salesperson problem (lowest cost visit every node and come home) - is this graph connected? if not, which nodes are in A's connected component - find whether a particular piece of data / node is here at all - accumulate some statistics about the nodes - bfs could collect attributes of the 1-level, 2-level,... ... - From Slack - "Both BFS and DFS feel like very brute force algorithms, it feels like there should be a clever, faster solution..." - "In real-world comp-sci applications of graph traversal, are 'pure' BFS or 'pure' DFS ever the best option?" + Graph algorithm categories - Traversal - Topological sort - [Greedy Algorithms, starting with interval scheduling] - Shortest paths - Spanning trees + Topological sort - What is it? - What's it for?