cs 401
play

CS 401 Midterm review Xiaorui Sun 1 Midterm Exam Midterm exam via - PowerPoint PPT Presentation

CS 401 Midterm review Xiaorui Sun 1 Midterm Exam Midterm exam via gradescope : October 16 (Friday) The exam will be available on Oct 16 0am You must submit no later than Oct 16 11:59pm Usually can be finished in 2 hours


  1. CS 401 Midterm review Xiaorui Sun 1

  2. Midterm Exam Midterm exam via gradescope : October 16 (Friday) • The exam will be available on Oct 16 0am • You must submit no later than Oct 16 11:59pm • Usually can be finished in 2 hours • Open textbook, but no copy from internet and between each other • No class on October 16, I will answer exam questions on blackboard from 1pm to 4pm. • The last problem is for graduate student only. • Graduate students must finish it • Undergraduate students DO NOT work on it (no extra credit).

  3. Midterm Exam True or false • Only answer true or false, no justification • Choose true/false via gradescope Short answer • Answer questions, no justification • Either type your solution via gradescope or upload the pic of your handwriting to gradescop Algorithm design • Either type your solution via gradescope or upload the pic of your handwriting to gradescope • Graph algorithm • Greedy algorithm • Davide and conquer • Each problem have several questions, understand and answer each question, no justification/correctness proof 3

  4. Topics • Analysis of running time • Graphs • Greedy algorithms • Divide and conquer 4

  5. Analysis of running time Given two positive functions f and g f(N) is O(g(N)) iff there is a constant c > 0 and N 0 ³ 0 s.t., 0 £ f(N) £ c ⋅ g(N) for all N ³ N 0 f(N) is W (g(N)) iff there is a constant c > 0 and N 0 ³ 0 s.t., f(N) ³ c ⋅ g(N) ³ 0 for all N ³ N 0 f(N) is Q (g(N)) iff there are c 0 > 0, c 1 >0 and N 0 ³ 0 s.t. c 0 ⋅ g(N) £ f(N) £ c 1 ⋅ g(N) for all N ³ N 0 • f(N) is Q (g(N)) iff f(N) is both O(g(N)) and W (g(N)). 5

  6. Properties Reflexivity. f is O(f) . Constants. If f is O(g) and c > 0 , then c ⋅ f is O(g) . Products. If f 1 is O(g 1 ) and f 2 is O(g 2 ) , then f 1 ⋅ f 2 is O(g1 ⋅ g2). Sums. If f 1 is O(g 1 ) and f 2 is O(g 2 ) , then f 1 + f 2 is O(max {g 1 , g 2 }) . Transitivity. If f is O(g) and g is O(h) , then f is O(h) 6

  7. Asymptotic Bounds for common fns Polynomials: ! " + ! $ % + ⋯ + ! ' % ' is ( % ' Logarithms: log , % = ((log / %) for all constants !, 2 > 0 Logarithms: log grows slower than every polynomial For all 5 > 0 , log % = ((% 6 ) % log % = ( % $."$ 7

  8. Exercise Suppose ! " = "!, & " = 2 ( Is ! = ) & , ! = Ω & or ! = Θ(&) ? Solution 1: Consider ! " & " = 1 2 2 … " 2 ≥ " 4 … " 2 ≥ " (/3 2 4 " terms "/2 terms Solution 2: Take log. • log ! " = log 1 + log 2 + ⋯ + log " = Θ(" log ") This method does not work • log & " = " log 2 = Θ(") when log ! " = Θ(log & " ) • So, log ! " = Ω(log &(")) , hence ! " = Ω(& " ) 8

  9. Undirected Graphs G=(V,E) Notation. G = (V, E) • V = nodes (or vertices) • E = edges between pairs of nodes • Captures pairwise relationship between objects • Graph size parameters: n = |V|, m = |E| No self-loop, no multiedge V = {1, 2, 3, 4, 5 ,6, 7, 8} E = {(1,2), (1,3), (2,3), (2,4), (2,5), (3,5), (3,7), aaaa(3,8), (4,5), (5,6), (7,8)} m=11, n=8

  10. Terminology 3 Path: A sequence of vertices 5 s.t. each vertex is connected 6 to the next vertex with an edge 4 1 Cycle: Path of length > 2 that has 2 the same start and end 10 1 Tree: A connected graph with no cycles 2 5 3 4 6 10

  11. Terminology Degree of a vertex: # edges that touch that vertex 3 5 1 4 6 deg(6)=3 2 7 10 Connected: Graph is connected if there is a path between every two vertices Connected component: Maximal set of connected vertices 11

  12. Graph representation Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. Space proportional to n 2 . Checking if (u, v) is an edge takes Q (1) time. Identifying all edges takes Q (n 2 ) time.

  13. Graph representation Adjacency list. Node indexed array of lists. Space proportional to m+n. Checking if (u, v) is an edge takes O (deg(u)) time. Identifying all edges takes Q (m+n) time.

  14. Graph Traversal Walk (via edges) from a fixed starting vertex ! to all vertices reachable from ! . Breadth First Search (BFS): Order nodes in successive layers based on distance from ! Depth First Search (DFS): More natural approach for exploring a maze; 14

  15. BFS 0 BFS Tree gives shortest 1 paths from 1 to all vertices 2 3 1 5 6 7 4 2 10 11 9 8 3 4 All edges connect same 12 13 or adjacent levels 15

  16. BFS Properties: • Edges into then-undiscovered vertices define a tree – the “Breadth First spanning tree” of ! • Level " in the tree are exactly all vertices # s.t., the shortest path (in ! ) from the root $ to # is of length " • All nontree edges join vertices on the same or adjacent levels of the tree Applications: • Find connected components • Single source shortest part on unweighted undirected graph • Testing bipartiteness 16

  17. DFS A,1 Edge code: Tree edge Back edge B,2 C,3 H,8 D,4 J,10 E,5 I,9 K,11 F,6 L,12 G,7 M,13 17

  18. DFS Properties: • Edges into then-undiscovered vertices define a “DFS tree” of ! • All nontree edges {#, %} , one of # or % is an ancestor of the other in the DFS tree. 18

  19. Directed Graphs 1 2 10 3 11 12 4 8 13 5 9 6 No self-loop, no multiedge 7 (8, 10) and (10, 8) are different edges 19

  20. Directed Acyclic Graphs (DAG) Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles. Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as ! " , ! $ , … , ! & so that for every edge (! ( , ! ) ) we have + < - . 2 3 6 5 4 1 2 3 4 5 6 7 7 1 a topological ordering of that DAG– a DAG all edges left-to-right 20

  21. Single Source Shortest Path Given an (un)directed connected graph ! = ($, &) with non- negative edge weights ( ) ≥ 0 and a start vertex , . Find length of shortest paths from , to each vertex in ! length of path = sum of edge weights in path Dijkstra’s algorithm 23 2 3 9 s Cost of path s-2-3-4-t 18 14 6 = 9 + 23 + 6 + 6 2 6 = 44. 30 4 19 11 5 15 5 6 20 16 t 7 44

  22. Spanning Tree Given a connected undirected graph ! = #, % . We call & is a spanning tree of ! if All edges in & are from % . & includes all of the vertices of ! . & ! Kruskal’s algorithm 22

  23. Exercise True or false: Given a graph G with n vertices and n-1 edges, G is a tree. No. G can be a disconnected graph. So G can be a cycle of n-1 vertices + an isolated vertex 23

  24. Greedy Algorithms ‘Best’ current partial solution at each step • Solution is built in small steps • Decisions on how to build the solution are made to maximize some criterion without looking to the future • Want the ‘best’ current partial solution as if the current step were the last step How to define each step? What is the strategy of each step? 24

  25. Interval Scheduling Interval Scheduling •Job j starts at !(#) and finishes at %(#) . •Two jobs compatible if they don’t overlap. •Goal: find maximum subset of mutually compatible jobs. a b b c d e e f g h h Time 0 1 2 3 4 5 6 7 8 9 10 11 25

  26. Interval Scheduling Every step we consider a single job In each step, we decide if the job will be in the solution set • Strategy: if the job is compatible with the current solution set, put it into the solution set How do we order jobs? • Sort in the ascending order of the finish times 26

  27. Problem 2 of Homework 2 Each step we select a job to execute Which job to execute? • In the i-th step, if we select job j, then the contribution of job j to C 1 +C 2 +…+C n is (n-i+1)t j • Because we want to minimize C 1 +C 2 +…+C n , so we should select the job with smallest running time in each step 27

  28. Divide and Conquer Divide: We reduce a problem to several subproblems. Typically, each sub-problem is n at most a constant fraction of Log n levels the size of the original problem n/2 n/2 Conquer: Recursively solve each subproblem n/4 Combine: Merge the solutions Examples: • Mergesort, Binary Search, Closest Pair of Points, Strassen’s Algorithm, FFT

  29. Master Theorem % & + (" ) for all " > +. Then, Suppose ! " = $ ! • If $ < + ) then ! " = Θ " ) • If $ = + ) then ! " = Θ " ) log " • If $ > + ) then ! " = Θ " 234 5 6 Example: For mergesort algorithm we have ! " = 2! " 2 + 8 " . So, 9 = 1, $ = + ) and ! " = Θ(" log ")

  30. Problem 4 of Homework 2 Naïve idea: Just sort all the elements (O(nk log (nk)) time) Another idea: Merge 1 st array and 2 nd array, then merge with 3 rd array, then merge with 4 th array, and so on… • Running time: O((k+k)+(2k+k)+(3k+k)+…+((n-1)k+k)) = O(n 2 k) • Even worse 30

  31. Problem 4 of Homework 2 Divide and conquer: given n sorted arrays, sort them into a single array • Divide: partition n arrays into two groups of size n/2 arrays • Conquer: sort each group • Combine: merge sort the solution of each group Let T(n) denote the running time to sort n arrays T(n) = 2 T(n/2) + O(nk) ⟹ T(n) = O(nk log n) 31

Recommend


More recommend