贪心策略基本思想

换硬币问题

image-20220428141906045

贪心算法思想

image-20220428141912047

适用问题

image-20220428142002288

活动安排问题

image-20220428142122919

image-20220428142138411

算法实现

  • 这里是代码是不完整的,执行前,编写并执行先一个排序

image-20220428142226169

image-20220428142236752

贪心的基本要素

image-20220428142247768

image-20220428142253625

image-20220428142303686

image-20220428142950753

image-20220428143656897

image-20220428143741469

image-20220428143807207

image-20220428143022565

image-20220428143857796

image-20220428143927173

背包问题

  • 注意:这里是普通背包问题,不是01背包问题

image-20220428144055285

贪心依据的选择

image-20220428143031558

价值优先——次优解

image-20220428144533176

image-20220428144552526

image-20220428143042989

容量优先——次优解

image-20220428143049189

image-20220428144637354

单位利益值——最优解

image-20220428144645373

image-20220428144655386

算法实现

image-20220428144703601

image-20220428144734498

Dijkstra算法

image-20220428145801910

image-20220428145815503

image-20220428145825775

image-20220428145835232

image-20220428145843145

最小生成树

image-20220428150131023

image-20220428150143056

Prim算法

image-20220428150406015

image-20220428150414423

image-20220428150421524

image-20220428150458584

image-20220428150510438

Kruskal算法

image-20220428151125822

解决环的问题

image-20220428151438809

image-20220428151421528

image-20220428152142712

image-20220428152213472

image-20220428152239553

image-20220428152317648

image-20220428152325430

哈夫曼编码

  • Data Compression via Huffman Coding
    • Motivation
      • Limited network bandwidth.
      • Limited disk space.
    • Human codes are used for data compression.
      • Reducing time to transmit large files, and
      • Reducing the space required to store them on disk or tape.
    • Huffman codes are a widely used and very effective technique for compressing data, savings of 20% to 90% are typical, depending on the characteristics of the data.

问题提出

  • Problem
    • Suppose that you have a file of 100K characters. To keep the example simple, suppose that each character is one of the 6 letters from a through f. Since we have just 6 characters, we need just 3 bits to represent a character, so the file requires 300K bits to store. Can we do better?
    • Suppose that we have more information about the file:
      • the frequency which each character appears
  • Solution
    • The idea is that we will use a variable length code instead of a fixed length code (3 bits for each character), with fewer bits to store the common characters, and more bits to store the rare characters

Example of Data Compression数据压缩示例

  • For example, suppose that the characters appear with the following frequencies, and following codes:
  • image-20220428173555160
  • Then the variable-length coded version will take not 300K bits but 451 + 133 + 123 + 163 + 94 + 54 = 224K bits to store, a 25% saving. In fact this is the optimal way to encode the 6 characters present, as we shall see

Characteristic of Optimal Code

  • Represented as a binary tree whose leaves are the given characters

  • In an optimal code each non-leaf node has two children

image-20220428173714577

image-20220428173728693

Prefix-free Code((非)前缀编码)

  • In a Prefix code no codeword is a prefix of another code word没有一个编码是另一个编码的前缀

    • Easy encoding and decoding
  • To encode, we need only concatenate the codes of consecutive characters in the message

    • The string 110001001101 parses uniquely as 1100-0-100-1101, which decodes to FACE
  • To decode, we have to decide where each code begins and ends

    • Easy, since, no codes share a prefix

      “prefix-free codes” would be a better name, but the term “prefix codes” is standard in the literature

Algorithm of Huffman Coding

  • The greedy algorithm for computing the optimal Human coding tree T is as follows
    • It starts with a forest of one-node trees representing each c ∈*C, and merges them in a greedy style, using a priority queue Q*, sorted by the smallest frequency:

image-20220428174130651

配套作业