Problems, Algorithms, and Running time analyses intro - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Problems, Algorithms, and Running time analyses intro

Description:

Divide A into entries below X and entries above X (smaller values first, then X, ... Divide-and-conquer. Dynamic programming. Recursion. The Rock Game. Two person game ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 26
Provided by: utcs8
Category:

less

Transcript and Presenter's Notes

Title: Problems, Algorithms, and Running time analyses intro


1
Problems, Algorithms, and Running time analyses
(intro)
  • Tandy Warnow, Prof.
  • January 27, 2009

2
Some computational problems
  • Given a list of numbers, put it into sorted order
  • Given a map and a collection of cities, find the
    shortest tour that visits every city
  • Given a collection of people, find the largest
    subset of them that all know each other
  • Given a collection of people, determine if they
    can be put into 2 groups so that no two people in
    the same group know each other.
  • Given a collection of people, determine if they
    can be put into 3 groups, so that no two people
    in the same group know each other.

3
Running time
  • You count the number of operations
  • Input/Output
  • Comparisons
  • Arithmetic
  • Assignments to variables
  • Etc.
  • Relate this number to the size of the input

4
Finding the maximum in an array
  • Input array A1,2,3,n
  • Output index containing the maximum entry from
    array A, and the maximum entry
  • Algorithm (in pseudocode)
  • Index1
  • MaxA1
  • For i2 up to n DO
  • If AigtMax, then set MaxAi and set
    Indexi
  • Return Index, Max

5
Running time analysis
  • Index1
  • MaxA1
  • For i2 up to n DO
  • If AigtMax, then set MaxAi and set
    Indexi
  • Return Index, Max
  • Running time n3 operations for the
    initialization of variables (array A, Index, max,
    and i), n-1 comparisons of array entries to Max,
    at most 3(n-1) changes of variables, and then one
    operation for returning Index and Max.
  • Total O(n).

6
Big-oh notation
  • A function f(n) is O(g(n)) if there is some pair
    of constants c and c such that
  • f(n) lt cg(n) whenever ngtc
  • Examples
  • 3n2 is O(n2)
  • 87n25000 is O(n2)
  • 5n2 is O(n3)
  • But n3 is not O(n2)

7
Sorting
  • Input array A1,2,n
  • Output array A1,2,n with the same entries as
    A, but in sorted order (from lowest to highest)
  • Algorithm use the previous algorithm as a
    subroutine!

8
Algorithm
  • Given array A1,2,,n
  • For jn down to 2 DO
  • Find index of max entry in A1,2,j
  • Swap Aindex with Aj
  • Running time O(n2)

9
Quick Sort
  • If array A has more than one entry, then pick
    random entry X from A, otherwise return A
  • Divide A into entries below X and entries above X
    (smaller values first, then X, then larger
    values)
  • Recursively sort each subarray of A.

10
Running time?
  • Remember
  • we are reporting the worst-case running time, not
    average.

11
Merge Sort
  • If array A has more than one entry, divide into
    two (roughly) equally sized subarrays otherwise,
    return A
  • Recursively sort each subarray (smallest to
    largest)
  • Merge the two subarrays into sorted order
  • Return A

12
Running time?
  • Need a recurrence relation to solve this!
  • Let T(n) be the running time of Merge Sort on
    arrays of size n.
  • Then T(n) satisfies
  • T(1)C
  • T(2n)2T(n)CnC
  • T(n) is O(n log n) (we will prove this later in
    the course)

13
Maximum clique problems
  • A clique in a graph is a subset of the vertices
    so that every pair of vertices in the subset are
    adjacent
  • A maximum clique is a clique of maximum size
    (number of vertices)

14
The decision problem for max clique
  • Input graph G and integer k
  • Output YES if G has a clique of k vertices,
    otherwise NO

15
The optimization problem for max clique
  • Input graph G
  • Output the largest k such that G has a clique of
    k vertices

16
The construction problem for max clique
  • Input graph G
  • Output a largest clique in G

17
Algorithms and Problems
  • Problems come in various forms
  • Yes/no
  • Optimization
  • Construction
  • Examples
  • Does the graph a k-clique? (Yes/No)
  • What is the size of the max clique in the graph?
    (Optimization)
  • Find a max clique in the graph. (Construction)

18
Solving an optimization problem using an oracle
for the decision problem
  • Using an oracle for the existence of a clique of
    size k, to find the maximum size clique
  • For kn ( vertices) down to 1 DO
  • If G has a k-clique, return k
  • Note if the Yes/No problem can be answered in
    polynomial time, then so can the optimization
    problem

19
Optimization and Construction
  • We can also find a max clique in a graph, using
    an oracle to answer the optimization problem!

20
Constructing a max clique, using an oracle
  • Let K be the size of the max clique in the graph
    G
  • List the vertices of G v1, v2, , vn
  • For i1 up to n, DO
  • If GG-vi has a max clique of size K, replace
    G by G
  • Return the vertices of G.
  • Note it takes only n calls to the oracle to
    construct the maximum clique. So if the
    optimization problem can be solved in polynomial
    time, so can the construction problem.

21
Problems and algorithms
  • Problems come in various forms, but solving the
    yes/no problem allows you to solve everything
    else (optimization and construction)
  • Algorithms are written in pseudocode
  • Running time analyses just count basic
    operations, and are given in big-oh notation

22
How do we solve the decision problem for max
clique?
  • Input graph G and integer k
  • Output YES if G has a clique of size k, NO
    otherwise

23
Algorithm design
  • Brute force (will work)
  • Greedy (sometimes works)
  • Divide-and-conquer
  • Dynamic programming
  • Recursion

24
The Rock Game
  • Two person game
  • Starting condition two piles of rocks
  • Each person can do one of the following remove
    one rock from each pile, or remove one rock from
    exactly one pile
  • The person to remove the last rock wins

25
The Rock Game
  • Yes/No problem Given the number of rocks in each
    pile, determine if the first player wins
  • Construction problem For the same input, devise
    a winning strategy for the player who wins.
Write a Comment
User Comments (0)
About PowerShow.com