Use R!
[1] 1350
[1] 4
[1] 0.675
#
0/ \1
# #
0/ \1 \1
# # #
0/ \1 0/ \1 0/ \1
a b c d e f
| letter | a | b | c | d | e | f |
| code word | 000 | 001 | 010 | 011 | 110 | 111 |
This code is not optimal, because the second bit of e
and f
is wasted. You can remove the second bit in the encoding of e
and f
without suffering any tradeoffs anywhere else.
More generally, an optimal binary tree is going to be one where there are no wasted bits, or in other words every parent node has exactly two child nodes.
Start with a node furthest away from the root, then travel upwards. If the length of the path is equal to \(k\), remove the highest node in this path and all the nodes below it. Add 1 to the count, then recurse to the next furthest node.
See Figure 4.6. Does this algorithm produce more paths?
Draw a path from the root of the tree to a node k levels down from the root whose subtree has minimal height. Recurse on any remaining subtrees whose heights are greater than or equal to k.
Let \(G\) be the set of all paths chosen by the greedy algorithm, and suppose \(T\) is a different set of paths chosen by a different algorithm. Each path in \(T\) that differs from \(G\) can be shifted down the tree until it reaches 1) the lowest available leaf in its subtree (available meaning there are no taken nodes between the leaf and the current path in question) or 2) another path. After this process has been completed with each path, the paths of \(T\) will all be replaced by greedy choices. So \(G\) must have at least as many paths as \(T\).
Section 5.2 of [E] defines the following terms:
If you ever encounter an unfamiliar term about graphs, you can probably find the definition in Section 5.2.
[[1]]
[1] 2 5 6
[[2]]
[1] 1 3 7
[[3]]
[1] 2 4 8
[[4]]
[1] 3 5 9
[[5]]
[1] 1 4 10
[[6]]
[1] 1 8 9
[[7]]
[1] 2 9 10
[[8]]
[1] 3 6 10
[[9]]
[1] 4 6 7
[[10]]
[1] 5 7 8
Abuse of notation: \(V\) is the number of vertices.
A[1..V]
is an array of lists.
A[i]
is a list of neighbors of vertex i
A[i]
is a list of out-neighbors of vertex i
See Figure 5.9 of [E].
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
James | Drake | Kevin | Kristen | Logan | Nathan | Talia |
Andrew | Isaac | Graham | Trevor | Jordan | Claire | Grace |
Blake | Ethan | Levi | Josiah | Jack | Bri | John |
Let A[1..V]
be the adjacency list for an undirected graph with \(V\) vertices and \(E\) edges.
A[i]
contains all the neighbors of vertex i
.A[i]
is implemented as a standard linked list.A[1..V]
require?i
is the length of the linked list A[i]
.)i
and vertex j
?An undirected graph is called bipartite if it can be vertex colored with two colors.
Given: a connected, bipartite, graph represented as an adjacency list A[1..V]
.
Assume that you have an iterator on the linked list A[i]
for each vertex i
. That is, you can go through the linked list A[i]
sequentially with a while
or for
loop.
Describe an algorithm for filling in the values of an array C[1..V]
with “colors” 0
and 1
that form a valid two-coloring of the graph.
What is the running time of your algorithm?