0% found this document useful (0 votes)
6 views

4P03Week4Slides

The document discusses the task scheduling problem where unit-time tasks must be optimally scheduled on a single processor, considering deadlines and penalties for late completion. It introduces the concept of canonical form for scheduling, independent sets of tasks, and matroid properties, providing an algorithm to find an optimal subset of tasks. Additionally, it covers approximation algorithms for NP-complete problems, including the Traveling Salesman Problem and Set Cover, and outlines combinatorial algorithms and structures.

Uploaded by

weiming0390
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

4P03Week4Slides

The document discusses the task scheduling problem where unit-time tasks must be optimally scheduled on a single processor, considering deadlines and penalties for late completion. It introduces the concept of canonical form for scheduling, independent sets of tasks, and matroid properties, providing an algorithm to find an optimal subset of tasks. Additionally, it covers approximation algorithms for NP-complete problems, including the Traveling Salesman Problem and Set Cover, and outlines combinatorial algorithms and structures.

Uploaded by

weiming0390
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

TASK SCHEDULING PROBLEM DESCRIPTION

(SECTION 16.5, CLRS 3RD ED.)

 We wish to optimally schedule unit-time tasks on a single processor.


 Each task has (i) a deadline, and (ii) a penalty if the deadline is missed.
Given:
 Finite set S = {a1, …, an} of unit-time tasks
 Deadlines {d1, …, dn} (1 ≤ di ≤ n)
 Non-negative penalties {w1, …, wn}
 Penalty wi incurred if task ai is late (not finished by time di)
 No penalty for tasks that are early (not late).

Find:
 A schedule (ordering of elements of S) that minimizes the total penalty.

COSC 4P03 WEEK 4 1


TASK SCHEDULING – CANONICAL FORM

Canonical form:
 All early tasks precede all late tasks (“early-first”), and
 Early tasks are scheduled in order of non-decreasing deadlines.
We can always put a schedule into canonical form:
 Early-first: if an early task x follows a late task y, then switch x and y (this won’t
affect x being early or y being late)
 Swap any 2 early tasks ai (finishing at time t) and aj (finishing at time t+1) with
deadlines dj<di.
 Since aj was early before the swap, this means that t+1≤dj. Therefore since dj<di we have
t+1<di and so ai is still early after the swap.
 Task aj is moved earlier in the schedule, so it’s still early.
COSC 4P03 WEEK 4 2
TASK SCHEDULING – CANONICAL FORM (2)

 The problem of finding an optimal schedule is equivalent to


finding a set A of tasks that are early in that optimal
schedule.
 Once found, put into canonical form:
 List early tasks (i.e. those in A) in order of non-decreasing deadline,
then
 List late tasks (i.e. those in S-A) in any order.

COSC 4P03 WEEK 4 3


TASK SCHEDULING – INDEPENDENT SET OF
TASKS

 A set of tasks is independent if there is a schedule such that no tasks are


late (i.e. all are “early”).
 The set of early tasks for a schedule forms an independent set of tasks.
 Let I = {sets of early tasks} (i.e. a set of independent sets)
 Let Nt(A) = number of tasks in A whose deadline is t or earlier.
 For any set A of n tasks:
 A is independent if and only if Nt(A) ≤ t for every t = 1, …, n.
 We will use this property to compute whether we have an independent set
of tasks. Clearly we want to maximize the size of A, so that as many tasks as
possible are early.
COSC 4P03 WEEK 4 4
MATROID PROPERTIES (REMINDER)

A matroid M = (S,I) satisfies the following:


 S is a finite nonempty set
 I is a nonempty family of subsets of S (“independent subsets”) with 2
properties:
1. I is hereditary: if B is in I, and A is a subset of B, then A is also in I
2. Exchange property: If A and B are both in I, and |A| < |B| then there
must be an element x in B-A such that AU{x} is in I.

COSC 4P03 WEEK 4 5


TASK SCHEDULING – MATROID
S = {a1, …, an} is a finite set of unit-time tasks; I = {sets of early tasks}
(S,I) is a matroid:
 I is hereditary: obvious, since if a set consists only of early tasks then all its subsets do too.
 The exchange property is satisfied. See full proof on Page 445 of CLRS. In brief:
 Consider two independent sets A and B with |A| < |B|. Let k be the largest t such that
Nt(B) ≤ Nt(A).
 Nn(B) = |B| and Nn(A) = |A|. Since |B| > |A| we must have k < n and Nj(B) > Nj(A) for
all j such that k+1 ≤ j ≤ n. Therefore B contains more tasks with deadline k+1 than A.
 Create A’ by adding a task from B-A with deadline k+1 to A.
 Since A is independent, when 0 ≤ t ≤ k we have Nt(A’) = Nt(A) ≤ t.
 Since B is independent, when k < t ≤ n we have Nt(A’) ≤ Nt(B) ≤ t.
 Therefore A’ must also be independent.
COSC 4P03 WEEK 4 6
GENERAL GREEDY ALGORITHM FOR
MATROIDS (REMINDER)

Finds an optimal subset A of the matroid M = (S,I) with weight function w.

A = ;
Sort S into non-increasing order by weight w;
For each element x in S (in non-increasing order)
{
if A U {x} is in I
then A = A U {x};
}
return A;

COSC 4P03 WEEK 4 7


TASK SCHEDULING – ALGORITHM AND
EXAMPLE

A = ;
n = 0;
sort S into non-increasing order by penalty wi;
for each x in S (taken in non-increasing order)
{
if Nt(A U {x}) ≤ t for all 1≤t≤n+1
{
A = A U {x}; ai 1 2 3 4 5 6 7
n = n+1;
} di 4 2 4 3 1 4 6
}
wi 70 60 50 40 30 20 10
return A;
COSC 4P03 WEEK 4 8
GREEDY ALGORITHMS FOR OTHER PROBLEMS
Some other problems for which greedy algorithms are guaranteed to give an optimal solution:
 MST algorithms by Kruskal and Prim
 Single-source shortest path by Dijkstra
Some problems for which greedy algorithms “come close”:
 Travelling salesman problem, in which we put restrictions on weights such that they
represent distances on a plane – guaranteed to be at most 2 times worse than optimal
 0-1 Knapsack problem – guaranteed to be at most 1+1/k worse, where k = # of items
considered at a time
Some problems for which greedy algorithms cannot be guaranteed to come within a constant
factor of being optimal:
 Travelling salesman problem (general)
 Graph colouring
 People have shown that if there were a “guarantee” then P=NP.
COSC 4P03 WEEK 4 9
APPROXIMATION ALGORITHMS
(CHAPTER 35, CLRS)

 NP-complete problems: unless P=NP, we won’t be able to


find polynomial time solutions
 Possible ways to deal with this:
1. If inputs are very small, maybe an exponential-time algorithm is
acceptable
2. There may be some special cases that can be solved in polynomial
time
3. Use an approximation algorithm: an algorithm that returns near-optimal
solutions
COSC 4P03 WEEK 4 10
APPROXIMATION ALGORITHMS – CONCEPTS

 Approximation ratio of an algorithm: the value returned C of the algorithm is within


a factor ρ(n) of the value C* of an optimal solution:
 Maximization problem: C < C* and ρ(n) = C*/C
 Minimization problem: C* < C and ρ(n) = C/C*
 ρ(n)-approximation algorithm: an algorithm that achieves an approximation ratio ρ(n)
 Sometimes we can get a smaller approximation ratio by using more time – tradeoff.
 Approximation scheme: given a fixed ε, the scheme is a (1+ ε)-approximation
algorithm
 Polynomial-time approximation scheme: for any fixed ε, the scheme runs in
polynomial time relative to n, the size of the input
 Fully polynomial-time approximation scheme: for any fixed ε, the scheme runs in
polynomial time relative to both 1/ε and n

COSC 4P03 WEEK 4 11


APPROXIMATION ALGORITHM FOR TSP
(SECTION 35.2, CLRS)
 Given complete undirected graph G = (V,E) in which all edge
weights are non-negative, find a tour A of G with minimum
cost.
 Let c(A) = total cost of edges chosen.
 Triangle inequality: c(u,w) ≤ c(u,v) + c(v,w)
 A simple 2-approximation algorithm assuming triangle
inequality is satisfied:
Select a vertex r as “root”
Compute an MST T for G from root r
Visit the vertices of T in preorder
Visit r
 Running time: O(|V|2)
COSC 4P03 WEEK 4 12
APPROXIMATION ALGORITHM FOR TSP

General idea of proof that this is a 2-approximation algorithm:


 We obtain a spanning tree by deleting any edge from a tour, so cost of MST c(T) ≤ cost
of optimal tour
 Full walk W: lists vertices when first visited and also after visiting subtree.
 This traverses every edge twice, so cost of walk c(W) = 2*c(T) ≤ 2*cost of optimal tour
→ 2-approximation
 Note W is not a tour, since we’re visiting vertices more than once.
 But by triangle inequality, we can delete the 2nd visit to a vertex and the cost does not
increase.
→ tour with cost no worse than c(W)
 If triangle inequality is not satisfied: no polynomial-time approximation algorithms for TSP
unless P=NP.
COSC 4P03 WEEK 4 13
APPROXIMATION ALGORITHM FOR SET COVER
(SECTION 35.3, CLRS AND SECTION 12.5, SKIENA)
 A family of subsets F of set X covers X if every element in X belongs to at least
one subset in F, i.e. 𝑋 = ‫𝑆 𝐹 ∈ 𝑆ڂ‬
 Set Cover Problem:
 Given a family of subsets F of the set X, what is the smallest sub-family C of F such
that C covers X?
 Greedy heuristic: repeatedly choose the subset that covers the largest collection
of elements of X that are still uncovered
 Note that using this heuristic, the number of freshly-covered elements at each step
must be non-increasing.
 Example (Skiena):
Milestone class 6 5 4 3 2 1 0
Uncovered elements 64 51 40 30 25 22 19 16 13 10 7 4 2 1
Selected subset size 13 11 10 5 3 3 3 3 3 3 3 2 1 114
COSC 4P03 WEEK 4
APPROXIMATION ALGORITHM FOR SET COVER
(2)
 Consider the number of remaining uncovered elements at each step.
 “Milestone”: a step at which the number of remaining uncovered elements reduces
past a power of 2.
 There can be at most ‫ ۀ𝑛 𝑔𝑙ڿ‬such milestones if 𝑋 = 𝑛.
 Let wi be the number of subsets that cover the 2i elements between milestones 2i+1-1 and 2i,
and let w be the maximum such wi. The greedy algorithm contains at most w * lg n subsets.
 The average number of new elements covered by each of the subsets between milestones
2i+1-1 and 2i is ui = 2i / wi
 There are 2i elements remaining to be covered, and all remaining subsets cannot cover more
than ui elements (or they would have been chosen sooner)
 As a result, we need at least wi more subsets to cover the remaining elements
 Therefore the optimal solution must contain at least w subsets, so the greedy solution is no
worse than log n times optimal.
COSC 4P03 WEEK 4 15
COMBINATORIAL ALGORITHMS

Kreher&Stinson (KS) classify Combinatorial Algorithms as follows:


 Generation: Construct all combinatorial structures of a given type
 Example: construct all graphs with a given set of parameters (given # of
vertices, edges, etc.).
 Enumeration: Count the number of “different” structures of a given
type
 Usually “different” means non-isomorphic
 Note: generation includes enumeration but enumeration is often easier.
 Search: Find at least one example of a structure of a given type
 It is usually easier to find one example than to generate or enumerate all
structures.
 Optimization: find (an) optimal structure of a given type.

COSC 4P03 WEEK 4 16


COMBINATORIAL STRUCTURES

 In general, these can be described as collections of subsets of a given


size.
 There are many applications, especially in Computer Science.
 There are lots of permutations involved in their analysis.
 The term “Combinatorial explosion” is sometimes used to describe the
growth rate of a particularly difficult search.
 It certainly implies a great deal of difficulty in narrowing down the search to
something manageable.
 Let’s look at examples of some of them before looking at the algorithms
used to generate/enumerate/search for them.

COSC 4P03 WEEK 4 17


COMBINATORIAL STRUCTURES –
SETS AND LISTS
• Basic building blocks
• Set: Unordered collection of distinct objects
• List: ordered collection of objects (not necessarily
distinct)
• Cardinality: |A| = number of elements in set A
• k-set: set of cardinality k
• Permutation: an ordering of a set
– a list of the elements of the set, such that every element
occurs exactly once
COSC 4P03 WEEK 4 18
COMBINATORIAL STRUCTURES – GRAPHS

G = (V, E) where V = {vertices}, E = {edges}


• Edge: 2-element subset of vertices
• Incidence: edge e = (v1,v2) is incident with v1 and v2
• Degree: deg(v) = number of edges incident with v.

Some types of graphs:


• Complete graph: E = all 2-element subsets of V.
– Kn = complete graph on n vertices.
• Regular graph of degree d: all vertices have degree d.
• Weighted graph: all edges have an associated weight.
COSC 4P03 WEEK 4 19
COMBINATORIAL STRUCTURES –
GRAPH REPRESENTATION
Adjacency Matrix
5
0 1 1 0 0 1 0
1 0 1 1 0 1 1
6 7 1 1 0 1 1 1 1
3
0 1 1 0 0 0 1
0 0 1 0 0 1 1
1 1 1 0 1 0 1
1 2 4
0 1 1 1 1 1 0

List of Vertices: {1,2,3,4,5,6,7}


List of Edges: {{1,2}, {1,3}, {1,6}, {2,3}, {2,4}, {2,6}, {2,7}, {3,4}, {3,5}, {3,6}, {3,7},
{4,7}, {5,6}, {5,7}, {6,7}}
COSC 4P03 Week 4 20
COMBINATORIAL STRUCTURES –
INCIDENCE MATRIX OF GRAPH
5 Incidence matrix: |V|x|E| matrix in which
A[i][j] = 1 iff vertex i is incident with edge j
6 7
3 List of Edges: {{1,2}, {1,3}, {1,6}, {2,3},
{2,4}, {2,6}, {2,7}, {3,4}, {3,5}, {3,6}, {3,7},
1 2 4 {4,7}, {5,6}, {5,7}, 6,7}}
1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 1 1
1 1 1
1 1 1 1 1
1 1 1 1 1
COSC 4P03 WEEK 4 21
COMBINATORIAL STRUCTURES – SET
SYSTEMS
General description: (X, B) where X = {points} and B = {blocks}
 Every block is a subset of X.

Some types of set systems:


 Graphs: X = V, B = E, every block (edge) has cardinality 2.
 Projective plane of order n: n2+n+1 lines (B) and n2+n+1 points (X) such that:
 Every line contains n+1 points, every point is on n+1 lines, any 2 distinct lines intersect
at exactly 1 point, and any 2 distinct points lie on exactly 1 line.
 t-(v, k, λ) Design: v points and b blocks such that:
 Every block contains k points, every point is in exactly r=λ*(v-1)/(k-1) blocks, and every
set of t points is contained in λblocks
Common Representation: Incidence matrix
COSC 4P03 WEEK 4 22
COMBINATORIAL STRUCTURES –
PROJECTIVE PLANE OF ORDER 2
Incidence Matrix:
5 1 1 1
1 1 1
1 1 1
6 7 1 1 1
3 1 1 1
1 1 1
1 1 1
1 2 4

List of Lines: {{1,2,4},{2,3,5},{3,4,6},{4,5,7},{5,6,1},{6,7,2},{7,1,3}}


This is also a 2-(7,3,1) design.
COSC 4P03 WEEK 4 23
COMBINATORIAL STRUCTURES – ERROR-
CORRECTING CODES
Binary (n,M,d) Error-correcting Code:
 M binary vectors (codewords) of length n with minimum distance d
 Distance: 2 codewords are distance t apart if they differ in exactly t bits
 Weight: the number of 1-bits in a codeword
 Minimum distance: Smallest distance between any pair of codewords
Linear Binary (n, k, d) Error-Correcting Code:
 A code is linear if for any codewords u and v, u+v is also a codeword. Addition
is performed bitwise, mod 2.
 A linear (n,k,d) code has k linearly independent codewords.
Representation:
 If not linear: list of M codewords
 If linear: k x n generator matrix containing k linearly independent vectors. The
full set of codewords is the set of all 2k linear combinations of these.
COSC 4P03 WEEK 4 24
(7,4,3) HAMMING CODE
0000000
1101000
Generator Matrix: 0110100
1 1 0 1 0 0 0 0011010
0001101
0 1 1 0 1 0 0 1000110
0 0 1 1 0 1 0 0100011
0 0 0 1 1 0 1 1010001
0010111
1001011
List of Codewords: 1100101
1110010
0111001
1011100
COSC 4P03 WEEK 4
0101110 25
1111111
ERROR CORRECTION

The (7,4,3) Hamming Code can correct up to 1 error in a string of 7 bits. How
it works:
 All data must be stored as codewords, i.e. we can’t use just any one of the 27
possible strings of 7 bits, but only the chosen codewords.
 When the data is read:
 If it’s the same as one of the codewords, then we assume there was no error and
accept it as is.
 Otherwise, we pick the closest codeword, guessing that was what the string was
meant to be (because this will be as a result of the smallest number of errors).
 For this code (not all codes), there will always be one codeword at distance ≤ 1
from any string of 7 bits.
 Example: if we read 1111000 then we guess it should be 1101000 since it’s
the only codeword that differs in just 1 bit from 1111000.
COSC 4P03 WEEK 4 26

You might also like