Skip to content

Commit

Permalink
2023-12-10 22:31:31
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/counting-money-algorithm.md
src/content/blog/huffan-encoding-algorithm.md
  • Loading branch information
gyunseo committed Dec 10, 2023
1 parent dd7b0ca commit a04be77
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 8 deletions.
26 changes: 19 additions & 7 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@
"source": false
}
}
},
{
"id": "566a4e8637f06512",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/huffan-encoding-algorithm.md",
"mode": "source",
"source": false
}
}
}
],
"currentTab": 4
"currentTab": 5
}
],
"direction": "vertical"
Expand Down Expand Up @@ -134,7 +146,7 @@
"state": {
"type": "backlink",
"state": {
"file": "src/content/blog/counting-money-algorithm.md",
"file": "src/content/blog/huffan-encoding-algorithm.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -151,7 +163,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "src/content/blog/counting-money-algorithm.md",
"file": "src/content/blog/huffan-encoding-algorithm.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -174,7 +186,7 @@
"state": {
"type": "outline",
"state": {
"file": "src/content/blog/counting-money-algorithm.md"
"file": "src/content/blog/huffan-encoding-algorithm.md"
}
}
}
Expand All @@ -197,10 +209,11 @@
"table-editor-obsidian:Advanced Tables Toolbar": false
}
},
"active": "765260b1666451fb",
"active": "566a4e8637f06512",
"lastOpenFiles": [
"src/content/blog/floyd-warshall-algorithm.md",
"src/content/blog/counting-money-algorithm.md",
"src/content/blog/huffan-encoding-algorithm.md",
"src/content/blog/floyd-warshall-algorithm.md",
"src/content/blog/0-1-knapsack-dp.md",
"src/content/blog/bellmanford-algorithm.md",
"src/content/blog/interpolation-search-algorithm.md",
Expand All @@ -224,7 +237,6 @@
"src/content/blog/karastuba-al.md",
"src/content/blog/sequential-search-algorithm.md",
"src/content/blog/brute-force-string-matching-algorithm.md",
"src/content/blog/brute-force-traveling-salesman-problem.md",
"data-communications-and-networking-pdfs/Behrouz A. Forouzan - Data Communications and Networking with TCP_IP Protocol Suite-McGraw-Hill (2022).pdf",
"data-communications-and-networking-pdfs/Behrouz A. Forouzan - Data Communications and Networking-McGraw Hill (2012).pdf",
"ostep-pdfs/26_threads-intro.pdf",
Expand Down
2 changes: 1 addition & 1 deletion src/content/blog/counting-money-algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tags:
- Greedy
- Algorithms
- Computer-Science
description: this is template
description: 거스름돈 계산기
---

## Table of contents
Expand Down
124 changes: 124 additions & 0 deletions src/content/blog/huffan-encoding-algorithm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: Huffman Encoding Algorithm
pubDatetime: 2023-12-10T22:30:00Z
featured: false
draft: false
tags:
- Greedy
- Algorithms
- Computer-Science
description: 허프만 알고리즘
---

## Table of contents

## `python` 코드

```python
from __future__ import annotations

import sys


class Letter:
def __init__(self, letter: str, freq: int):
self.letter: str = letter
self.freq: int = freq
self.bitstring: dict[str, str] = {}

def __repr__(self) -> str:
return f"{self.letter}:{self.freq}"


class TreeNode:
def __init__(self, freq: int, left: Letter | TreeNode, right: Letter | TreeNode):
self.freq: int = freq
self.left: Letter | TreeNode = left
self.right: Letter | TreeNode = right


def parse_file(file_path: str) -> list[Letter]:
chars: dict[str, int] = {}
with open(file_path) as f:
while True:
c = f.read(1)
if not c:
break
chars[c] = chars[c] + 1 if c in chars else 1
return sorted((Letter(c, f) for c, f in chars.items()), key=lambda x: x.freq)


def build_tree(letters: list[Letter]) -> Letter | TreeNode:
response: list[Letter | TreeNode] = letters # type: ignore
while len(response) > 1:
left = response.pop(0)
right = response.pop(0)
total_freq = left.freq + right.freq
node = TreeNode(total_freq, left, right)
response.append(node)
response.sort(key=lambda x: x.freq)
return response[0]


def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
if isinstance(root, Letter):
root.bitstring[root.letter] = bitstring
return [root]
treenode: TreeNode = root
letters = []
letters += traverse_tree(treenode.left, bitstring + "0")
letters += traverse_tree(treenode.right, bitstring + "1")
return letters


def huffman(file_path: str) -> None:
letters_list = parse_file(file_path)
root = build_tree(letters_list)
letters = {
k: v for letter in traverse_tree(root, "") for k, v in letter.bitstring.items()
}
print(f"Huffman Coding of {file_path}: ")
with open(file_path) as f:
while True:
c = f.read(1)
if not c:
break
print(letters[c], end=" ")
print()


if __name__ == "__main__":
huffman(sys.argv[1])

```

## How to Run

python version: `3.11.6`

### Run `main.py`

```
pip install pipenv
pipenv --python 3.11.6
pipenv run python3 main.py input.txt
```

### Input

`input.txt`:

```
She Sells Seashells by the Seashore
```

### Output

```zsh
Huffman Coding of input.txt:
001 010 111 110 001 111 011 011 100 110 001 111 0001 100 010 111 011 011 100 110 10100 10101 110 10110 010 111 110 001 111 0001 100 010 10111 0000 111
```

## Execution Image

![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1702215088/image_xts0fs.png)

0 comments on commit a04be77

Please sign in to comment.