当前位置:文档之家› 实验3. 贪心算法

实验3. 贪心算法

实验3.贪心算法一、实验目的1.理解贪心算法的基本思想。

2.运用贪心算法解决实际问题。

二、实验环境与地点1.实验环境:Windows7,Eclipse2.实验地点:网络工程实验室三、实验内容与步骤编写程序完成下列题目,上机调试并运行成功。

1.活动安排问题。

问题:有n个活动的集合A={1,2,…,n},其中每个活动都要求使用同一资源,如演讲会场等,而在同一时间内只有一个活动能使用这一资源。

求解:安排尽量多项活动在该场地进行,即求A的最大相容子集。

设待安排的11个活动的开始时间和结束时间按结束时间的升序排列如下:将此表数据作为实现该算法的测试数据。

(1)给出算法基本思想;(2)给出用java语言实现程序的代码;算法:public static int greedySelector(int[] s, int[] f, boolean a[]) {int n = s.length - 1;a[1] = true;int j = 1;int count = 1;for (int i = 2; i <= n; i++) {if (s[i] >= f[j]) {a[i] = true;j = i;count++;} elsea[i] = false;}return count;}2.哈夫曼编码是广泛地用于数据文件压缩的十分有效的编码方法。

统计字符串中各个字符出现的频率,求各个字符的哈夫曼编码方案。

输入:good good study,day day up输出:各字符的哈夫曼编码。

算法:算法中用到的类Huffman定义为:private static class Huffman implements Comparable {Bintree tree;float weight;// 权值private Huffman(Bintree tt, float ww) {tree = tt;weight = ww;}public int compareTo(Object x) {float xw = ((Huffman) x).weight;if (weight < xw)return -1;if (weight == xw)return 0;return 1;}}算法huffmanTree描述如下:public static Bintree huffmanTree(float[] f) {// 生成单结点树int n = f.length;Huffman[] w = new Huffman[n + 1];Bintree zero = new Bintree();for (int i = 0; i < n; i++) {Bintree x = new Bintree();x.makeTree(new MyInteger(i), zero, zero);w[i + 1] = new Huffman(x, f[i]);}// 建优先队列MinHeap H = new MinHeap();H.initialize(w, n);// 反复合并最小频率树for (int i = 1; i < n; i++) {Huffman x = (Huffman) H.removeMin();Huffman y = (Huffman) H.removeMin();Bintree z = new Bintree();z.makeTree(null, x.tree, y.tree);Huffman t = new Huffman(z, x.weight + y.weight);H.put(t);}return ((Huffman) H.removeMin()).tree;}提示:统计一个字符串中每个字符出现的次数思路:1.创建一个map key:出现的字符value:出现的次数2.获取字符串中的每一个字符3.查看字符是否在Map中作为key存在否,若存在,说明已经统计过则value+1 不存在:value=1代码如下:public class CountString {public static void main(String[] args) {String str = "good good study,day day up";Map<Character, Integer> map = new HashMap<Character, Integer>();for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);// 用toCharArray()也可以if (map.containsKey(c)) {// 若统计过map.put(c, map.get(c) + 1);} else {map.put(c, 1);}}System.out.println(map);}}四、实验总结与分析参考代码:1.ActivityArrangementpublic class AvtivityArrangement {public static int[] s= { 0,1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12};public static int[] f= { 0,4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};public static boolean[] a=new boolean[s.length];public static int greedySelector(int[] s, int[] f, boolean[] a) {int n = s.length - 1;a[1] = true;int j = 1;int count = 1;for (int i = 2; i <= n; i++) {if (s[i] >= f[j]) {a[i] = true;j = i;count++;} elsea[i] = false;}return count;}public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.print("相容活动共有:"+greedySelector(s, f, a)+"个:");for (int i = 1; i < a.length; i++) {if (a[i]) {System.out.print(i+" ");}}}}2.Huffmanpublic class BtNode {Object data;BtNode lChild;BtNode rChild;public BtNode() {// 构造一个空节点this(null);}public BtNode(Object data) {// 构造左右孩子域为空的节点this(data,null,null);}public BtNode(Object data, BtNode lchild, BtNode rchild) {// TODO Auto-generated constructor stubthis.data=data;this.lChild=lchild;this.rChild=rchild;}}public class Bintree {BtNode root;public Bintree() {root=null;}public Bintree(BtNode root) {this.root=root;}public Bintree(Object data, BtNode lchild, BtNode rchild) { BtNode node=new BtNode(data, lchild, rchild);this.root=node;}}import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.PriorityQueue;import java.util.Scanner;public class Huffman implements Comparable {Bintree tree;float weight;// 权值public static Object[]c;private Huffman(Bintree tt, float ww) {tree = tt;weight = ww;}public int compareTo(Object x) {float xw = ((Huffman) x).weight;if (weight < xw)return -1;if (weight == xw)return 0;return 1;}public static Bintree huffmanTree(int[] f) {// 生成单结点树int n = f.length;Huffman[] w = new Huffman[n ];Bintree zero = new Bintree();for (int i = 0; i < n; i++) {Bintree x = new Bintree(new Integer(i),null, null );//null, nullw[i] = new Huffman(x, f[i]);}// 建优先队列List list=Arrays.asList(w);PriorityQueue H = new PriorityQueue(list);System.out.println(H);//* 反复合并最小频率树*/for (int i = 1; i < n; i++) {Huffman x = (Huffman) H.poll();Huffman y = (Huffman) H.poll();Bintree z = new Bintree(null, x.tree.root, y.tree.root);Huffman t = new Huffman(z, x.weight + y.weight);H.offer(t);}return ((Huffman) H.poll()).tree;}public static void CountString(String str ,Map<Character, Integer> map) { for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);// 用toCharArray()也可以if (map.containsKey(c)) {// 若统计过map.put(c, map.get(c) + 1);} else {map.put(c, 1);}}System.out.println(map);}public static void main(String[] args) {Map<Character, Integer> map = new HashMap<Character, Integer>();Scanner scanner=new Scanner(System.in);String string=scanner.nextLine();//输入字符串CountString(string ,map);//统计输入字符串的字符频率//得到字符数组和对应的频率数组c=map.keySet().toArray();int []f=new int[c.length];for (int i = 0; i < c.length; i++) {f[i]=map.get(c[i]);System.out.println(c[i]+":"+f[i]);}//创建huffman树Bintree huf=huffmanTree(f);//输出huffman编码String str="";traverseHuf(huf.root,str);}private static void traverseHuf(BtNode huf,String string) {String str1=""+string;if (huf.lChild==null) {System.out.println(c[(int) huf.data]+":"+str1);}else {traverseHuf( huf.lChild,str1+"0");traverseHuf( huf.rChild,str1+"1");}}}运行结果:good{d=1, g=1, o=2}o:0d:10g:11。

相关主题