activity select problem(greedy algorithms)

many activities will use the same place, every activity ai has its‘  start time si and finish time fi.let the number of activities to be as many as possible.

1. dynamic programming

use ak be a knife to cut the set activities into two parts and recursive to find the max subset

c[i,j](star after ai finish and finish before aj star) = max {1+c[i,k] + c[k,j]} or 0(haven‘t ak);

2.greedy programming

let ai ranked by their finish time. earlier finish time ranked front than the later.

then choose the activities by its finish time, keep they are not contradictory.

 1 public class activity_select {
 2      int[] s = {1,3,0,5,3,5,6,8,8,2,12};
 3     int[] f = {4,5,6,7,9,9,10,11,12,14,16};
 4     private static class activity{
 5         private int sta ;
 6         private int fin ;
 7         public activity(){
 8             sta = 0;
 9             fin = 0;
10         }
11     }
12
13     public   activity[] select(){
14     activity[]  act = new activity[s.length];
15     for(int i = 0;i<s.length;i++){   //initial
16         act[i] = new activity();
17         act[i].sta = s[i];
18         act[i].fin = f[i];
19     }
20     for(int i = 0;i<s.length;i++){   //insert sort from early fin to later fin
21         for(int j = i;j < s.length;j++){
22             if(act[i].fin > act[j].fin){
23                 int testa = act[j].sta;
24                 int tefin = act[j].fin;
25                 act[j].sta = act[i].sta;
26                 act[j].fin = act[i].fin;
27                 act[i].fin = tefin;
28                 act[i].sta = testa;
29             }
30         }
31     }
32     activity[] res = new activity[s.length];
33     res[0] = act[0];
34     int j = 0;
35     for(int i = 0;i < s.length -1;i++){
36         if(act[i+1].sta > res[j].fin){
37             res[++j] = act[i + 1];
38         }
39     }
40     activity[] res1 = new activity[j+1];
41     for(int i = 0;i <=j;i++){
42         res1[i] = res[i];
43     }
44     return res1;
45     }
46
47
48
49     public static void main(String[] args){
50         activity_select  ac = new activity_select();
51         activity[] a = ac.select();
52         int n = a.length;
53         for(int i = 0;i < n;i++){
54             System.out.println(a[i].sta + " " +a[i].fin);
55         }
56     }
57
58
59 }
时间: 2024-11-05 15:52:01

activity select problem(greedy algorithms)的相关文章

an optimal solution to the problem

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Greedy/greedyIntro.htm Greedy Introduction Greedy algorithms are simple and straightforward. They are shortsighted in their approach in the sense that they take decisions on the basis of

Complexity and Tractability (3.44) - The Traveling Salesman Problem

转自:http://csfieldguide.org.nz/en/curriculum-guides/ncea/level-3/complexity-tractability-TSP.html This is a guide for students attempting Complexity and Tractability in digital technologies achievement standard 3.44. This guide is not official, althou

[Stanford Algorithms: Design and Analysis, Part 2]

Specific topics in Part 2 include: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes), dynamic programming (knapsack, sequence alignment, optimal search trees, shortest paths), NP-completeness and what it means for the

Data Structures and Algorithms with JavaScript

Book Description As an experienced JavaScript developer moving to server-side programming, you need to implement classic data structures and algorithms associated with conventional object-oriented languages like C# and Java. This practical guide show

(转)Awesome Courses

Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect

Dynamic Programming

We began our study of algorithmic techniques with greedy algorithms, which in some sense form the most natural approach to algorithm design. Faced with a new computational problem, we've seen that it's not hard to propose multiple possible greedy alg

机器学习初学者的10个课程推荐

转自:https://hackerlists.com/beginner-ml-courses/ 10 Machine Learning Online Courses For Beginners 10 Machine Learning Online Courses For Beginners The following is a list of, mostly free, machine learning online courses for beginners. If video lecture

https那些事儿

(一)SSL/TLS协议运行机制的概述 一.作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. (1) 窃听风险(eavesdropping):第三方可以获知通信内容. (2) 篡改风险(tampering):第三方可以修改通信内容. (3) 冒充风险(pretending):第三方可以冒充他人身份参与通信. SSL/TLS协议是为了解决这三大风险而设计的,希望达到: (1) 所有信息都是加密传播,第三方无法窃听. (2) 具有校验机制,一旦被篡改,通

斯坦福CS课程列表

http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principles. 3-5 Units. Introduces the essential ideas of computing: data representation, algorithms, programming "code", computer hardware, networking, s