CS 401: Computer Algorithm I Complexity / Graphs Xiaorui Sun 1 - - PowerPoint PPT Presentation

cs 401 computer algorithm i
SMART_READER_LITE
LIVE PREVIEW

CS 401: Computer Algorithm I Complexity / Graphs Xiaorui Sun 1 - - PowerPoint PPT Presentation

CS 401: Computer Algorithm I Complexity / Graphs Xiaorui Sun 1 Complexity Given two positive functions f and g f(N) is O(g(N)) iff there is a constant c > 0 s.t., f(N) is eventually always c g(N) f(N) is W (g(N)) iff there is a


slide-1
SLIDE 1

CS 401: Computer Algorithm I

Complexity / Graphs

Xiaorui Sun

1

slide-2
SLIDE 2

Complexity

Given two positive functions f and g

  • f(N) is O(g(N)) iff there is a constant c>0 s.t.,

f(N) is eventually always £ c g(N)

  • f(N) is W(g(N)) iff there is a constant e>0 s.t.,

f(N) is eventually always ³ e g(N)

  • f(N) is Q(g(N)) iff there are constants c1, c2>0 so that

eventually always c1g(N) £ f(N) £ c2g(N)

2

slide-3
SLIDE 3

A Survey of Common Running Times

slide-4
SLIDE 4

Linear Time: O(n)

Linear time. Running time is at most a constant factor times the size of the input. Computing the maximum. Compute maximum of n numbers a1, …, an.

max ¬ a1 for i = 2 to n { if (ai > max) max ¬ ai }

4

slide-5
SLIDE 5

Linear Time: O(n)

  • Merge. Combine two sorted lists A = a1,a2,…,an with B =

b1,b2,…,bn into sorted whole.

  • Claim. Merging two lists of size n takes O(n) time.
  • Pf. After each comparison, the length of output list

increases by 1.

i = 1, j = 1 while (both lists are nonempty) { if (ai £ bj) append ai to output list and increment i else(ai > bj)append bj to output list and increment j } append remainder of nonempty list to output list

5

slide-6
SLIDE 6

O(n log n) Time

O(n log n) time. Arises in divide-and-conquer algorithms.

  • Sorting. Mergesort and heapsort are sorting algorithms

that perform O(n log n) comparisons. Largest empty interval. Given n time-stamps x1, …, xn on which copies of a file arrive at a server, what is largest interval of time when no copies of the file arrive? O(n log n) solution. Sort the time-stamps. Scan the sorted list in order, identifying the maximum gap between successive time-stamps.

also referred to as linearithmic time

6

slide-7
SLIDE 7

Quadratic Time: O(n2)

Quadratic time. Enumerate all pairs of elements. Closest pair of points. Given a list of n points in the plane (x1, y1), …, (xn, yn), find the pair that is closest. O(n2) solution. Try all pairs of points.

  • Remark. W(n2) seems inevitable, but this is just an illusion.

min ¬ (x1 - x2)2 + (y1 - y2)2 for i = 1 to n { for j = i+1 to n { d ¬ (xi - xj)2 + (yi - yj)2 if (d < min) min ¬ d } }

see chapter 5

slide-8
SLIDE 8

Polynomial Time: O(nk) Time

Independent set of size k. Given a graph, are there k nodes such that no two are joined by an edge? O(nk) solution. Enumerate all subsets of k nodes. Check whether S is an independent set = O(k2). Number of k element subsets = O(k2 nk / k!) = O(nk).

foreach subset S of k nodes { check whether S in an independent set if (S is an independent set) report S is an independent set } }

!! n k æ è ç ö ø ÷ = n (n-1) (n- 2) " (n- k +1) k (k -1) (k - 2) " (2) (1) £ nk k!

poly-time for k=17, but not practical k is a constant

8

slide-9
SLIDE 9

Exponential Time

Independent set. Given a graph, what is maximum size of an independent set? O(n2 2n) solution. Enumerate all subsets.

S* ¬ f foreach subset S of nodes { check whether S in an independent set if (S is largest independent set seen so far) update S* ¬ S } }

9

slide-10
SLIDE 10

Efficiency

10

An algorithm runs in polynomial time if ! " = "$(&). Equivalently, ! " = ((")) for some constant d.

Suppose we can do 1 million operations per second.

not only get very big, but do so abruptly, which likely yields erratic performance on small instances

Outdated: Nvidia announced a “computer” this Tue that do 2 quadrillion (2×10&.) operations/sec. It brings down the 31,710 years to 500 sec. However, 2&// operations still takes millions of years.

slide-11
SLIDE 11

Why “Polynomial”?

Point is not that n2000 is a practical bound, or that the differences among n and 2n and n2 are negligible. Rather, simple theoretical tools may not easily capture such differences, whereas exponentials are qualitatively different from polynomials, so more amenable to theoretical analysis.

  • “My problem is in P” is a starting point for a more

detailed analysis

  • “My problem is not in P” may suggest that you need to

shift to a more tractable variant

11

slide-12
SLIDE 12

Graph algorithms

slide-13
SLIDE 13

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

slide-14
SLIDE 14

Undirected Graphs G=(V,E)

14

A 2 10 9 8 3 4 B 6 7 11 12 13

Disconnected graph Isolated vertices Multi edges Self loop

slide-15
SLIDE 15

Graphs

15

slide-16
SLIDE 16

Graph applications

16

slide-17
SLIDE 17

Directed Graphs

17

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

Multi edge self loop

slide-18
SLIDE 18

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

18

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

slide-19
SLIDE 19

Terminology (cont’d)

  • 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

19

3 4 5 6 7 2 10 1

slide-20
SLIDE 20

Degree Sum

Claim: In any undirected graph, the number of edges is equal to ⁄ 1 2 ∑%&'(&) * deg(/) Pf: ∑%&'(&) * deg(/) counts every edge of the graph exactly twice; once from each end of the edge.

20

3 4 5 6 7 2 10 1

|E|=8 1

%&'(&) *

deg / = 2 + 2 + 1 + 1 + 3 + 2 + 3 + 2 = 16

slide-21
SLIDE 21

Odd Degree Vertices

Claim: In any undirected graph, the number of odd degree vertices is even Pf: In previous claim we showed sum of all vertex degrees is even. So there must be even number of odd degree vertices, because sum of odd number of odd numbers is

  • dd.

21

3 4 5 6 7 2 10 1

4 odd degree vertices 3, 4, 5, 6

slide-22
SLIDE 22

#edges

Let ! = ($, &) be a graph with ( = |$| vertices and * = & edges. Claim: 0 ≤ * ≤

  • . = - -/0

.

= 1((.) Pf: Since every edge connects two distinct vertices (i.e., G has no loops) and no two edges connect the same pair of vertices (i.e., G has no multi-edges) It has at most -

. edges.

22

slide-23
SLIDE 23

Degree 1 vertices

Claim: If G has no cycle, then it has a vertex of degree ≤ 1 (Every tree has a leaf) Proof: (By contradiction)

Suppose every vertex has degree ≥ 2. Start from a vertex &' and follow a path, &', … , &* when we are at &* we choose the next vertex to be different from &*+'. We can do so because deg &* ≥ 2. The first time that we see a repeated vertex (&/ = &*) we get a cycle. We always get a repeated vertex because 2 has finitely many vertices

23

&' &3 &4 &5 &6

slide-24
SLIDE 24

Trees and Induction

Claim: Show that every tree with ! vertices has ! − 1 edges. Proof: (Induction on !.) Base Case: ! = 1, the tree has no edge Inductive Step: Let % be a tree with ! vertices. So, % has a vertex & of degree 1. Remove & and the neighboring edge, and let %’ be the new graph. We claim %’ is a tree: It has no cycle, and it must be connected. So, %’ has ! − 2 edges and % has ! − 1 edges.

24