Data Structures Algorithms Part IIIb
Data Structures Algorithms Part IIIb
Encoded Message:
000000010001100000101000001101000100100000100011100001010110001001110100
ASCII ENCODING
65 0000 How to Decode the Message?
76 0001
73 0010 ✓ Use the table from the fixed-width encoding.
83 0011
69 0100
87 0101
72 0110
84 0111
- 1000
Huffman coding for lossless data
compression
Variable-width Encoding
Message : ALL I SEE IS WHITE CHARACTERS f
A 1
L 2
1. Count the frequency of each character I 3
S 2
E 3
W 1
H 1
T 1
- (space) 4
TOTAL 18
Huffman coding for lossless data
compression
Variable-width Encoding
Message : ALL I SEE IS WHITE
2. Arrange the characters from A:1 W:1 H:1 T:1 L:2 S:2 I:3 E:3 -: 4
least to greatest of their
frequency.
3. Merge the nodes with the least
frequencies until there is only
one node. AW:2 HT:2
AWHTLS:8
3. Merge the nodes with the
least frequencies until there
is only one node. AWHT:4
AWHTLS:8
3. Merge the nodes with the
least frequencies until there
is only one node. AWHT:4
IE-:10
AWHTLS:8
3. Merge the nodes with the
least frequencies until there
is only one node. AWHT:4
IE-:10
1 IE-:10
0
0
AW:2 HT:2 LS:4 IE:6 1
0 1 0 1 0 1 0 1
W 0001 0
L 010 2 6 AWHTLS:8
1
I 100 3 9 0
S 011 2 6 AWHT:4 1
E 101 3 3 1 IE-:10
0
W 0001 1 4 0
No Compression : No table/Tree
Fixed-width: 9 chars • 4 bits = 36 bits
Variable-width (Huffman Coding): SUM(LEN(Huffman Coding) = 30 bits
Huffman coding for lossless data
compression
Cost of Message: No Compression vs. Fixed-width vs. Variable-width
(Huffman Coding)
Message : ALL I SEE IS WHITE
3! = 3•2•1 = 6 leaves
Exhaustive Search: Backtrack search
trees
▪ Two different search trees for generating all combinations
C(3,0)
C(3,1)
C(3,2)
C(3,3)
Exhaustive Search: Backtrack search
trees
n non-attacking queens on an nxn
chessboard.
Exhaustive Search: Backtrack search
trees
Mazes, knight’s tour on an mxn chessboard, Sudoku, and a lot more recreational
puzzles, and combinatorial optimization/ enumeration and constraint-satisfaction
problems
Depth-First Search (DFS): Graph Traversal
▪ Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible
along each branch before backtracking.
▪ It uses a stack (either explicitly or via recursion) to keep track of nodes to visit. DFS is
useful for solving problems like maze exploration, topological sorting, and connected
components detection.
Depth-First Search (DFS): Graph Traversal
▪ Example:
Choose any node. Choose any adjacent vertex No more adjacent vertex from 3. We move
Suppose we choose 2. from 2 (e.g., 3, 4, 0) . back/backtrack to 2. From 2, choices are 4
Suppose We choose 3. and 0. suppose we choose 4.
Adjacent to 4 are 0, 5, 1 Choose any adjacent vertex No more unexplored adjacent vertex from 0.
and 2. We visited 2, so except for visited vertex. We backtrack to 5. From 5, we choose the
ignore 2. Suppose we Suppose We choose 0. unexplored vertex 1.
choose 5.
Visited = {2, 3, 4} Visited = {2, 3, 4, 5} Visited = {2, 3, 4, 5, 0}
Depth-First Search (DFS): Graph Traversal
▪ Example:
Visited = {2, 3, 4, 5, 0, 1}
Breadth-First Search (DFS): Graph
Traversal
▪ a graph traversal algorithm that explores all nodes level by level, starting from a source
node.
▪ It uses a queue to keep track of nodes to visit next.
▪ BFS is commonly used to find the shortest path in an unweighted graph.
Breadth-First Search (BFS): Graph
Traversal
▪ Example:
Choose any node. Choose all unexplored If we choose 3, there are no unexplored adjacent
Suppose we choose 2. adjacent vertex from 2. vertex from 3. Suppose we choose 4. The unexplored
adjacent vertices are 5 and 1.
Visited = [2, 3, 4, 0, 5, 1]
Graph Traversal: DFS vs. BFS
DFS BFS
Data Structure Stack Queue