Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
bf4f5beb17
|
|||
|
a39a53ea90
|
|||
|
2d45b17899
|
@@ -0,0 +1,95 @@
|
||||
# Code-A2 (Huffman Coding)
|
||||
|
||||
import heapq
|
||||
|
||||
# Node class for Huffman Tree
|
||||
class Node:
|
||||
def __init__(self, char, freq):
|
||||
self.char = char
|
||||
self.freq = freq
|
||||
self.left = None
|
||||
self.right = None
|
||||
|
||||
# Comparison function for priority queue
|
||||
def __lt__(self, other):
|
||||
return self.freq < other.freq
|
||||
|
||||
|
||||
# Function to build Huffman Tree
|
||||
def build_huffman_tree(char_freq):
|
||||
heap = [Node(ch, freq) for ch, freq in char_freq.items()]
|
||||
heapq.heapify(heap)
|
||||
|
||||
while len(heap) > 1:
|
||||
# Pick two smallest nodes (greedy choice)
|
||||
left = heapq.heappop(heap)
|
||||
right = heapq.heappop(heap)
|
||||
|
||||
# Merge them into a new node
|
||||
merged = Node(None, left.freq + right.freq)
|
||||
merged.left = left
|
||||
merged.right = right
|
||||
|
||||
heapq.heappush(heap, merged)
|
||||
|
||||
return heap[0]
|
||||
|
||||
|
||||
# Function to generate Huffman codes
|
||||
def generate_codes(root, current_code="", codes={}):
|
||||
if root is None:
|
||||
return
|
||||
|
||||
if root.char is not None:
|
||||
codes[root.char] = current_code
|
||||
|
||||
generate_codes(root.left, current_code + "0", codes)
|
||||
generate_codes(root.right, current_code + "1", codes)
|
||||
return codes
|
||||
|
||||
|
||||
# Main program
|
||||
text = input("Enter text to encode: ")
|
||||
|
||||
# Step 1: Calculate frequency of each character
|
||||
freq = {}
|
||||
for ch in text:
|
||||
freq[ch] = freq.get(ch, 0) + 1
|
||||
|
||||
# Step 2: Build Huffman Tree using greedy approach
|
||||
root = build_huffman_tree(freq)
|
||||
|
||||
# Step 3: Generate Huffman Codes
|
||||
codes = generate_codes(root)
|
||||
|
||||
# Step 4: Encode the text
|
||||
encoded_text = "".join(codes[ch] for ch in text)
|
||||
|
||||
# Step 5: Display results
|
||||
print("\nCharacter | Frequency | Huffman Code")
|
||||
print("------------------------------------")
|
||||
for ch in freq:
|
||||
print(f" {ch!r} | {freq[ch]} | {codes[ch]}")
|
||||
|
||||
print("\nEncoded Text:", encoded_text)
|
||||
|
||||
# SAMPLE OUTPUT
|
||||
"""
|
||||
Enter text to encode: lord kska git
|
||||
|
||||
Character | Frequency | Huffman Code
|
||||
------------------------------------
|
||||
'l' | 1 | 1100
|
||||
'o' | 1 | 1101
|
||||
'r' | 1 | 001
|
||||
'd' | 1 | 1010
|
||||
' ' | 2 | 011
|
||||
'k' | 2 | 100
|
||||
's' | 1 | 000
|
||||
'a' | 1 | 010
|
||||
'g' | 1 | 1011
|
||||
'i' | 1 | 1111
|
||||
't' | 1 | 1110
|
||||
|
||||
Encoded Text: 110011010011010011100000100010011101111111110
|
||||
"""
|
||||
@@ -0,0 +1,74 @@
|
||||
# Code-A5 (N-Queen)
|
||||
|
||||
def print_board(board, n):
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
print(board[i][j], end=" ")
|
||||
print()
|
||||
print() # blank line between solutions
|
||||
|
||||
|
||||
def is_safe(board, row, col, n):
|
||||
# Check column
|
||||
for i in range(row):
|
||||
if board[i][col] == 1:
|
||||
return False
|
||||
|
||||
# Check upper-left diagonal
|
||||
i, j = row, col
|
||||
while i >= 0 and j >= 0:
|
||||
if board[i][j] == 1:
|
||||
return False
|
||||
i -= 1
|
||||
j -= 1
|
||||
|
||||
# Check upper-right diagonal
|
||||
i, j = row, col
|
||||
while i >= 0 and j < n:
|
||||
if board[i][j] == 1:
|
||||
return False
|
||||
i -= 1
|
||||
j += 1
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def solve_n_queens(board, row, n):
|
||||
if row == n:
|
||||
print_board(board, n)
|
||||
return True
|
||||
|
||||
res = False
|
||||
for col in range(n):
|
||||
if is_safe(board, row, col, n):
|
||||
board[row][col] = 1
|
||||
res = solve_n_queens(board, row + 1, n) or res
|
||||
board[row][col] = 0 # backtrack
|
||||
|
||||
return res
|
||||
|
||||
|
||||
# Main program
|
||||
n = int(input("Enter number of queens: "))
|
||||
board = [[0 for _ in range(n)] for _ in range(n)]
|
||||
|
||||
print(f"\nSolutions for {n}-Queens Problem:\n")
|
||||
if not solve_n_queens(board, 0, n):
|
||||
print("No solution exists!")
|
||||
|
||||
# SAMPLE OUTPUT
|
||||
"""
|
||||
Enter number of queens: 4
|
||||
|
||||
Solutions for 4-Queens Problem:
|
||||
|
||||
0 1 0 0
|
||||
0 0 0 1
|
||||
1 0 0 0
|
||||
0 0 1 0
|
||||
|
||||
0 0 1 0
|
||||
1 0 0 0
|
||||
0 0 0 1
|
||||
0 1 0 0
|
||||
"""
|
||||
@@ -25,6 +25,7 @@ This repository contains valuable resources for the Design and Analysis of Algor
|
||||
|
||||
> [!NOTE]
|
||||
> C++ versions of all codes are available in the [./Codes/C++](./Codes/C++) directory.
|
||||
> Python version of some codes are available in [./Codes/Python](./Codes/Python) directory.
|
||||
|
||||
### Practical
|
||||
|
||||
|
||||
Reference in New Issue
Block a user