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

cs 401
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 401

Midterm review

Xiaorui Sun

1

slide-2
SLIDE 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).
slide-3
SLIDE 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

slide-4
SLIDE 4

Topics

  • Analysis of running time
  • Graphs
  • Greedy algorithms
  • Divide and conquer

4

slide-5
SLIDE 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 N0 ³ 0 s.t., 0£ f(N) £ c⋅g(N) for all N ³ N0 f(N) is W(g(N)) iff there is a constant c>0 and N0 ³ 0 s.t., f(N) ³ c ⋅ g(N) ³ 0 for all N ³ N0 f(N) is Q(g(N)) iff there are c0>0, c1>0 and N0 ³ 0 s.t. c0 ⋅ g(N) £ f(N) £ c1 ⋅ g(N) for all N ³ N0

  • f(N) is Q(g(N)) iff f(N) is both O(g(N)) and W(g(N)).

5

slide-6
SLIDE 6

Properties

  • Reflexivity. f is O(f).
  • Constants. If f is O(g) and c > 0, then c⋅f is O(g).
  • Products. If f1 is O(g1) and f2 is O(g2), then f1⋅f2 is

O(g1⋅g2).

  • Sums. If f1 is O(g1) and f2 is O(g2), then f1 + f2 is

O(max {g1, g2}).

  • Transitivity. If f is O(g) and g is O(h), then f is O(h)

6

slide-7
SLIDE 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

slide-8
SLIDE 8

Exercise

Suppose ! " = "!, & " = 2( Is ! = ) & , ! = Ω & or ! = Θ(&)? Solution 1: Consider ! " & " = 1 2 2 2 … " 2 ≥ " 4 … " 2 ≥ " 4

(/3

Solution 2: Take log.

  • log ! " = log 1 + log 2 + ⋯ + log " = Θ(" log ")
  • log & " = " log 2 = Θ(")
  • So, log ! " = Ω(log &(")), hence ! " = Ω(& " )

8

" terms "/2 terms This method does not work when log ! " = Θ(log & " )

slide-9
SLIDE 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|

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 No self-loop, no multiedge

slide-10
SLIDE 10

Terminology

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

10

3 4 5 6 2 10 1 2 5 1 3 4 6

slide-11
SLIDE 11

Terminology

Degree of a vertex: # edges that touch that vertex deg(6)=3 Connected: Graph is connected if there is a path between every two vertices Connected component: Maximal set of connected vertices

11

3 4 5 6 7 2 10 1

slide-12
SLIDE 12

Graph representation

Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. Space proportional to n2. Checking if (u, v) is an edge takes Q(1) time. Identifying all edges takes Q(n2) time.

slide-13
SLIDE 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.

slide-14
SLIDE 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

slide-15
SLIDE 15

15

BFS

1 2 3 10 5 4 9 12 8 13 6 7 11

BFS Tree gives shortest paths from 1 to all vertices

1 2 3 4

All edges connect same

  • r adjacent levels
slide-16
SLIDE 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

slide-17
SLIDE 17

17

DFS

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Edge code: Tree edge Back edge

slide-18
SLIDE 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

slide-19
SLIDE 19

Directed Graphs

19

1 2 10 9 8 3 4 5 6 7 11 12 13

No self-loop, no multiedge (8, 10) and (10, 8) are different edges

slide-20
SLIDE 20

Directed Acyclic Graphs (DAG)

Def: A DAG is a directed acyclic graph, i.e.,

  • ne that contains no directed cycles.

Def: A topological order of a directed graph G = (V, E) is an

  • rdering of its nodes as !", !$, … , !& so that for every edge

(!(, !)) we have + < -.

20

a DAG

2 3 6 5 4 7 1

a topological ordering of that DAG– all edges left-to-right

1 2 3 4 5 6 7

slide-21
SLIDE 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 !

Dijkstra’s algorithm

Cost of path s-2-3-4-t = 9 + 23 + 6 + 6 = 44.

s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 length of path = sum of edge weights in path

slide-22
SLIDE 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

& !

slide-23
SLIDE 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

slide-24
SLIDE 24

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?

slide-25
SLIDE 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.

25

Time 1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d h e b

slide-26
SLIDE 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

slide-27
SLIDE 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 C1+C2+…+Cn is (n-i+1)tj

  • Because we want to minimize C1+C2+…+Cn, so we

should select the job with smallest running time in each step

27

slide-28
SLIDE 28

Divide and Conquer

Divide: We reduce a problem to several subproblems.

Typically, each sub-problem is at most a constant fraction of the size of the original problem

Conquer: Recursively solve each subproblem Combine: Merge the solutions Examples:

  • Mergesort, Binary Search, Closest Pair of Points,

Strassen’s Algorithm, FFT Log n levels

n n/2 n/2 n/4

slide-29
SLIDE 29

Master Theorem

Suppose ! " = $ !

% & + (") for all " > +. Then,

  • If $ < +) then ! " = Θ ")
  • If $ = +) then ! " = Θ ")log "
  • If $ > +) then ! " = Θ "23456

Example: For mergesort algorithm we have ! " = 2! " 2 + 8 " . So, 9 = 1, $ = +) and ! " = Θ(" log ")

slide-30
SLIDE 30

30

Naïve idea: Just sort all the elements (O(nk log (nk)) time) Another idea: Merge 1st array and 2nd array, then merge with 3rd array, then merge with 4th array, and so on…

  • Running time: O((k+k)+(2k+k)+(3k+k)+…+((n-1)k+k)) =

O(n2 k)

  • Even worse

Problem 4 of Homework 2

slide-31
SLIDE 31

31

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)

Problem 4 of Homework 2

slide-32
SLIDE 32

32

Divide and conquer: give an array, find the maximum subarray sum

  • Divide: partition the array into two halves
  • Conquer: Find the solution of each halves
  • Combine: What if the solution contains elements from

both halves? (for [6,-2, -3, 1,5], [6, -2] is from the first half, and [-3, 1, 5] is from the second half)

Problem 5 of Homework 2

slide-33
SLIDE 33

33

If the solution contains elements from both halves,

  • The elements in the solution from the first half
  • Form an interval contains the last element of the first half
  • Have largest sum among all the intervals of the first half containing the

last element of the first half ([6, -2] is the interval of [-2, -5, -6, -2] that (1) contains the last element and (2) has the largest sum)

  • The elements in the solution from the second half
  • Form an interval contains the first element of the second half
  • Have largest sum among all the intervals of the second half containing

the first element of the second half

Problem 5 of Homework 2

slide-34
SLIDE 34

34

Redefine problem: give an array, find (1)$ : the maximum subarray sum (2)& : the maximum of subarray sum among all subarrays containing the first element (3)( : the maximum of subarray sum among all subarrays containing the last element

Problem 5 of Homework 2

slide-35
SLIDE 35

35

Given !", $", %" for the first half, and !&, $&, %& for the second half, how to find the !, $, %?

  • ! = max{!", !&, %" + $&}
  • $ = max{$", !/0" + $&}
  • % = max{%&, !/0& + %"}

T(n) = 2 T(n/2) + O(n) ⟹ T(n) = O(n log n)

Problem 5 of Homework 2

!/0": sum of all the elements in the first half !/0&: sum of all the elements in the second half