question
stringlengths
34
5.67k
answer
stringlengths
20
20.1k
support_files
listlengths
0
4
metadata
dict
Write a program that takes a string on standard input and an integer k as command-line argument and puts on standard output a sorted list of the k-grams found in the string, each followed by its index in the string.
3.1.15 Searches Percentage of total time spent on insertions 1000 0.00% 1000000 6.10% 1000000000 96.26% The average insertion cost is N, so the total insertion cost for N keys is ...
[]
{ "number": "3.5.15", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 3, "chapter_title": "Searching", "section": 3.5, "section_title": "Applications", "type": "Exercise" }
Multisets. After referring to Exercises 3.5.2 and 3.5.3 and the previous exercise, develop APIs MultiHashSET and MultiSET for multisets (sets that can have equal keys) and implementations SeparateChainingMultiSET and BinarySearchMultiSET for multisets and ordered multisets, respectively.
3.1.18 The rank() method in BinarySearchST always starts the search in the middle of the array if it has an odd number of elements. If the array has an even number of elements, the rank() method starts the search on the left of the two middle elements. After comparing the middle element with the search key, if it is s...
[]
{ "number": "3.5.18", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 3, "chapter_title": "Searching", "section": 3.5, "section_title": "Applications", "type": "Creative Problem" }
Concordance. Write an ST client Concordance that puts on standard output a concordance of the strings in the standard input stream (see page 498).
3.1.20 Proposition B. Binary search in an ordered array with N keys uses no more than lg N + 1 compares for a search (successful or unsuccessful). Proof: Let C(N) be the number of compares to search for a key in a symbol table of size N. We have C(0) = 0, C(1) = 1, and for N > 0 we can write a recurrence relationshi...
[]
{ "number": "3.5.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 3, "chapter_title": "Searching", "section": 3.5, "section_title": "Applications", "type": "Creative Problem" }
Inverted concordance. Write a program InvertedConcordance that takes a concordance on standard input and puts the original string on standard output stream. Note: This computation is associated with a famous story having to do with the Dead Sea Scrolls. The team that discovered the original tablets enforced a secrecy r...
3.1.21 - Memory usage * BinarySearchST object overhead -> 16 bytes Key[] reference (keys) -> 8 bytes Value[] reference (values) -> 8 bytes int value (size) -> 4 bytes padding -> 4 bytes Key[] object overhead -> 16 bytes int value (length) -> 4 bytes padding -> 4 bytes N Key re...
[]
{ "number": "3.5.21", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 3, "chapter_title": "Searching", "section": 3.5, "section_title": "Applications", "type": "Creative Problem" }
Sparse matrices. Develop an API and an implementation for sparse 2D matrices. Support matrix addition and matrix multiplication. Include constructors for row and column vectors.
3.1.23 - Analysis of binary search As the book and exercise 3.1.20 have proven, the maximum number of compares used for a binary search in a table of size N is lg N + 1. A number N has exactly lg N + 1 bits. This is because shifting 1 bit to the right reduces the number by half (rounded down). For example: N Bit ...
[]
{ "number": "3.5.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 3, "chapter_title": "Searching", "section": 3.5, "section_title": "Applications", "type": "Creative Problem" }
List. Develop an implementation of the following API: public class List<Item> implements Iterable<Item> List() create a list void addFront(Item item) add item to the front void addBack(Item item) add item to the back Item deleteFront() remove from the front Item deleteBack() remove from the back void delete(Item ...
3.1.27 - Small tables Building the binary search symbol table requires N calls to put(). Every put() operation makes a call to rank() and does a search, an operation with order of growth O(lg N). Assuming that we can choose the order of the keys to insert, we can create the table in a sorted order, starting with the s...
[]
{ "number": "3.5.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 3, "chapter_title": "Searching", "section": 3.5, "section_title": "Applications", "type": "Creative Problem" }
What is the maximum number of edges in a graph with V vertices and no parallel edges? What is the minimum number of edges in a graph with V vertices, none of which are isolated?
4.1.1 The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2. Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide th...
[]
{ "number": "4.1.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Draw, in the style of the figure in the text (page 524), the adjacency lists built by Graph’s input stream constructor for the file tinyGex2.txt depicted at left.
4.1.2 adj[] 0 -> 5 -> 2 -> 6 1 -> 4 -> 8 -> 11 2 -> 5 -> 6 -> 0 -> 3 3 -> 10 -> 6 -> 2 4 -> 1 -> 8 5 -> 0 -> 10 -> 2 6 -> 2 -> 3 -> 0 7 -> 8 -> 11 8 -> 1 -> 11 -> 7 -> 4 9 -> 10 -> 5 -> 3 11 -> 8 -> 7 -> 1
[]
{ "number": "4.1.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Consider the four-vertex graph with edges 0-1, 1-2, 2-3, and 3-0. Draw an array of adjacency-lists that could not have been built calling addEdge() for these edges no matter what order.
4.1.6 The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections. adj[] 0 -> 1 -> 3 (the original was 0 -> 3 -> 1) 1 -> 2 -> 0 2 -> 3 -> 1 3 -> 0 -> 2
[]
{ "number": "4.1.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Show, in the style of the figure on page 533, a detailed trace of the call dfs(0) for the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see Exercise 4.1.2). Also, draw the tree represented by edgeTo[].
4.1.9 marked[] adj[] dfs (0) 0 T 0 5 2 6 1 1 4 8 11 2 2 5 6 0 3 3 3 10 6 2 4 4 1 8 5 5 0 10 2 6 6 2 3 0 7 ...
[]
{ "number": "4.1.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Prove that every connected graph has a vertex whose removal (including all adjacent edges) will not disconnect the graph, and write a DFS method that finds such a vertex. Hint: Consider a vertex whose adjacent vertices are all marked.
4.1.10 Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph. Proof by contradiction: If the graph has a node of degree one, removing it gives a connected graph. Example: o - o Otherwise, every path in the graph belongs to a cycle. To see this, start with any p...
[]
{ "number": "4.1.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Draw the tree represented by edgeTo[] after the call bfs(G, 0) in ALGORITHM 4.2 for the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see Exercise 4.1.2).
4.1.11 Tree represented by edgeTo[] after call to bfs(G, 0): 0 5 2 6 10 3
[]
{ "number": "4.1.11", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
What does the BFS tree tell us about the distance from v to w when neither is at the root?
4.1.12 When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch. If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance f...
[]
{ "number": "4.1.12", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Suppose you use a stack instead of a queue when running breadth-first search. Does it still compute shortest paths?
4.1.14 If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths. This can be seen in the following graph: 0 (source) / \ 1 - 2 | | 4 - 3 If the edge 0 - 2 is inserted before the edge 0 - 1: Using a stack, the distance from 0 to 4 will be 3. Using a queue, the dis...
[]
{ "number": "4.1.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Show, in the style of the figure on page 545, a detailed trace of CC for finding the connected components in the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see EXERCISE 4.1.2).
4.1.19 count marked[] id[] 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) 0 T 0 dfs(5) 0 T T 0 0 check 0 dfs(10) 0 ...
[]
{ "number": "4.1.19", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Show, in the style of the figures in this section, a detailed trace of Cycle for finding a cycle in the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see EXERCISE 4.1.2). What is the order of growth of the running time of the Cycle constructor, in the worst case?
4.1.20 Has Cycle? marked[] F 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T dfs(5) T T check 0 F dfs(10) T T T check 5 F ...
[]
{ "number": "4.1.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Show, in the style of the figures in this section, a detailed trace of TwoColor for finding a two-coloring of the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see EXERCISE 4.1.2). What is the order of growth of the running time of the TwoColor constructor, in the worst case?
4.1.21 Is 2-colorable? marked[] color[] T 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T F F F F F F F F F F F F dfs(5) T ...
[]
{ "number": "4.1.21", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Compute the number of connected components in movies.txt, the size of the largest component, and the number of components of size less than 10. Find the eccentricity, diameter, radius, a center, and the girth of the largest component in the graph. Does it contain Kevin Bacon?
4.1.24 Number of connected components: 33 Size of the largest component: 118774 Number of components of size less than 10: 5 Does the largest component contain Kevin Bacon: Yes Eccentricity, diameter, radius, center and girth: The strategy for computing the eccentricity, diameter, radius, center and girth of the lar...
[]
{ "number": "4.1.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Determine the amount of memory used by Graph to represent a graph with V vertices and E edges, using the memory-cost model of SECTION 1.4.
4.1.27 Integer * object overhead -> 16 bytes * int value -> 4 bytes * padding -> 4 bytes Amount of memory needed: 16 + 4 + 4 = 24 bytes Node * object overhead -> 16 bytes * extra overhead for reference to the enclosing instance -> 8 bytes * Item reference (item) -> 8 bytes * Node reference (next) -> 8 bytes Amount of...
[]
{ "number": "4.1.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Two graphs are isomorphic if there is a way to rename the vertices of one to make it identical to the other. Draw all the nonisomorphic graphs with two, three, four, and five vertices.
4.1.28 Non-isomorphic graphs: There are 2 non-isomorphic graphs with 2 vertices: o o o-o There are 4 non-isomorphic graphs with 3 vertices: o o o o o-o o-o-o o / \ o-o There are 11 non-isomorphic graphs with 4 vertices: o o o o o-o o o o-o-o o o-o-o-o o-o o-o o | o / \ o o o / \ o---o o o-...
[]
{ "number": "4.1.28", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Exercise" }
Eulerian and Hamiltonian cycles. Consider the graphs defined by the following four sets of edges: 0-1 0-2 0-3 1-3 1-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8 0-1 0-2 0-3 1-3 0-3 2-5 5-6 3-6 4-7 4-8 5-8 5-9 6-7 6-9 8-8 0-1 1-2 1-3 0-3 0-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8 4-1 7-9 6-2 7-3 5-0 0-2 0-8 1-6 3-9 6-3 2-...
4.1.30 - Eulerian and Hamiltonian cycles An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once. A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visite...
[]
{ "number": "4.1.30", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Creative Problem" }
Graph enumeration. How many different undirected graphs are there with V vertices and E edges (and no parallel edges)?
4.1.31 - Graph enumeration If we were considering graphs with no self-loops: There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect. (2) So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)! ((2)) ...
[]
{ "number": "4.1.31", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Creative Problem" }
Odd cycles. Prove that a graph is two-colorable (bipartite) if and only if it contains no odd-length cycle.
4.1.33 - Odd cycles A graph is two-colorable (bipartite) if and only if it contains no odd-length cycle. Proof: 1- Proving that a graph with an odd-length cycle cannot be bipartite: If a graph G is bipartite with vertex sets V1 and V2, every step along a walk takes you either from V1 to V2 or from V2 to V1. To end up...
[]
{ "number": "4.1.33", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Creative Problem" }
Biconnectedness. A graph is biconnected if every pair of vertices is connected by two disjoint paths. An articulation point in a connected graph is a vertex that would disconnect the graph if it (and its adjacent edges) were removed. Prove that any graph with no articulation points is biconnected. Hint: Given a pair of...
4.1.35 - Biconnectedness Any graph with no articulation points is biconnected. Proof: Consider two vertices, s, t, and a path P1 connecting s to t. We know that no vertex in P1 is an articulation point, so for each vertex v in the path, there is always another path P2 connecting s to t that does not include it. Also,...
[]
{ "number": "4.1.35", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.1, "section_title": "Undirected Graphs", "type": "Creative Problem" }
What is the maximum number of edges in a digraph with V vertices and no parallel edges? What is the minimum number of edges in a digraph with V vertices, none of which are isolated?
4.1.1 The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2. Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide th...
[]
{ "number": "4.2.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Draw, in the style of the figure in the text (page 524), the adjacency lists built by Digraph’s input stream constructor for the file tinyDGex2.txt depicted at left.
4.1.2 adj[] 0 -> 5 -> 2 -> 6 1 -> 4 -> 8 -> 11 2 -> 5 -> 6 -> 0 -> 3 3 -> 10 -> 6 -> 2 4 -> 1 -> 8 5 -> 0 -> 10 -> 2 6 -> 2 -> 3 -> 0 7 -> 8 -> 11 8 -> 1 -> 11 -> 7 -> 4 9 -> 10 -> 5 -> 3 11 -> 8 -> 7 -> 1
[]
{ "number": "4.2.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Develop a test client for Digraph.
4.1.6 The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections. adj[] 0 -> 1 -> 3 (the original was 0 -> 3 -> 1) 1 -> 2 -> 0 2 -> 3 -> 1 3 -> 0 -> 2
[]
{ "number": "4.2.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Write a method that checks whether or not a given permutation of a DAG’s vertices is a topological order of that DAG.
4.1.9 marked[] adj[] dfs (0) 0 T 0 5 2 6 1 1 4 8 11 2 2 5 6 0 3 3 3 10 6 2 4 4 1 8 5 5 0 10 2 6 6 2 3 0 7 ...
[]
{ "number": "4.2.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Given a DAG, does there exist a topological order that cannot result from applying a DFS-based algorithm, no matter in what order the vertices adjacent to each vertex are chosen? Prove your answer.
4.1.10 Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph. Proof by contradiction: If the graph has a node of degree one, removing it gives a connected graph. Example: o - o Otherwise, every path in the graph belongs to a cycle. To see this, start with any p...
[]
{ "number": "4.2.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Describe a family of sparse digraphs whose number of directed cycles grows exponentially in the number of vertices.
4.1.11 Tree represented by edgeTo[] after call to bfs(G, 0): 0 5 2 6 10 3
[]
{ "number": "4.2.11", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
How many edges are there in the transitive closure of a digraph that is a simple directed path with V vertices and V–1 edges?
4.1.12 When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch. If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance f...
[]
{ "number": "4.2.12", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Prove that the strong components in G^R are the same as in G.
4.1.14 If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths. This can be seen in the following graph: 0 (source) / \ 1 - 2 | | 4 - 3 If the edge 0 - 2 is inserted before the edge 0 - 1: Using a stack, the distance from 0 to 4 will be 3. Using a queue, the dis...
[]
{ "number": "4.2.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Exercise" }
Topological sort and BFS. Explain why the following algorithm does not necessarily produce a topological order: Run BFS, and label the vertices by increasing distance to their respective source.
4.1.19 count marked[] id[] 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) 0 T 0 dfs(5) 0 T T 0 0 check 0 dfs(10) 0 ...
[]
{ "number": "4.2.19", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
Directed Eulerian cycle. An Eulerian cycle is a directed cycle that contains each edge exactly once. Write a graph client Euler that finds an Eulerian cycle or reports that no such tour exists. Hint: Prove that a digraph G has a directed Eulerian cycle if and only if G is connected and each vertex has its indegree equa...
4.1.20 Has Cycle? marked[] F 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T dfs(5) T T check 0 F dfs(10) T T T check 5 F ...
[]
{ "number": "4.2.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
LCA of a DAG. Given a DAG and two vertices v and w, find the lowest common ancestor (LCA) of v and w. The LCA of v and w is an ancestor of v and w that has no descendants that are also ancestors of v and w. Computing the LCA is useful in multiple inheritance in programming languages, analysis of genealogical data (find...
4.1.21 Is 2-colorable? marked[] color[] T 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T F F F F F F F F F F F F dfs(5) T ...
[]
{ "number": "4.2.21", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
Hamiltonian path in DAGs. Given a DAG, design a linear-time algorithm to determine whether there is a directed path that visits each vertex exactly once. Answer: Compute a topological sort and check if there is an edge between each consecutive pair of vertices in the topological order.
4.1.24 Number of connected components: 33 Size of the largest component: 118774 Number of components of size less than 10: 5 Does the largest component contain Kevin Bacon: Yes Eccentricity, diameter, radius, center and girth: The strategy for computing the eccentricity, diameter, radius, center and girth of the lar...
[]
{ "number": "4.2.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
Digraph enumeration. Show that the number of different V-vertex digraphs with no parallel edges is 2^(V^2). (How many digraphs are there that contain V vertices and E edges?) Then compute an upper bound on the percentage of 20-vertex digraphs that could ever be examined by any computer, under the assumptions that every...
4.1.27 Integer * object overhead -> 16 bytes * int value -> 4 bytes * padding -> 4 bytes Amount of memory needed: 16 + 4 + 4 = 24 bytes Node * object overhead -> 16 bytes * extra overhead for reference to the enclosing instance -> 8 bytes * Item reference (item) -> 8 bytes * Node reference (next) -> 8 bytes Amount of...
[]
{ "number": "4.2.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
DAG enumeration. Give a formula for the number of V-vertex DAGs with E edges.
4.1.28 Non-isomorphic graphs: There are 2 non-isomorphic graphs with 2 vertices: o o o-o There are 4 non-isomorphic graphs with 3 vertices: o o o o o-o o-o-o o / \ o-o There are 11 non-isomorphic graphs with 4 vertices: o o o o o-o o o o-o-o o o-o-o-o o-o o-o o | o / \ o o o / \ o---o o o-...
[]
{ "number": "4.2.28", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
Queue-based topological sort. Develop a topological sort implementation that maintains a vertex-indexed array that keeps track of the indegree of each vertex. Initialize the array and a queue of sources in a single pass through all the edges, as in EXERCISE 4.2.7. Then, perform the following operations until the source...
4.1.30 - Eulerian and Hamiltonian cycles An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once. A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visite...
[]
{ "number": "4.2.30", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
Euclidean digraphs. Modify your solution to EXERCISE 4.1.37 to create an API EuclideanDigraph for graphs whose vertices are points in the plane, so that you can work with graphical representations.
4.1.31 - Graph enumeration If we were considering graphs with no self-loops: There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect. (2) So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)! ((2)) ...
[]
{ "number": "4.2.31", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.2, "section_title": "Directed Graphs", "type": "Creative Problem" }
Prove that you can rescale the weights by adding a positive constant to all of them or by multiplying them all by a positive constant without affecting the MST.
4.1.1 The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2. Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide th...
[]
{ "number": "4.3.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Draw all of the MSTs of the graph depicted at right (all edge weights are equal).
4.1.2 adj[] 0 -> 5 -> 2 -> 6 1 -> 4 -> 8 -> 11 2 -> 5 -> 6 -> 0 -> 3 3 -> 10 -> 6 -> 2 4 -> 1 -> 8 5 -> 0 -> 10 -> 2 6 -> 2 -> 3 -> 0 7 -> 8 -> 11 8 -> 1 -> 11 -> 7 -> 4 9 -> 10 -> 5 -> 3 11 -> 8 -> 7 -> 1
[]
{ "number": "4.3.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Give the MST of the weighted graph obtained by deleting vertex 7 from tinyEWG.txt (see page 604).
4.1.6 The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections. adj[] 0 -> 1 -> 3 (the original was 0 -> 3 -> 1) 1 -> 2 -> 0 2 -> 3 -> 1 3 -> 0 -> 2
[]
{ "number": "4.3.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Implement the constructor for EdgeWeightedGraph that reads a graph from the input stream, by suitably modifying the constructor from Graph (see page 526).
4.1.9 marked[] adj[] dfs (0) 0 T 0 5 2 6 1 1 4 8 11 2 2 5 6 0 3 3 3 10 6 2 4 4 1 8 5 5 0 10 2 6 6 2 3 0 7 ...
[]
{ "number": "4.3.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Develop an EdgeWeightedGraph implementation for dense graphs that uses an adjacency-matrix (two-dimensional array of weights) representation. Disallow parallel edges.
4.1.10 Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph. Proof by contradiction: If the graph has a node of degree one, removing it gives a connected graph. Example: o - o Otherwise, every path in the graph belongs to a cycle. To see this, start with any p...
[]
{ "number": "4.3.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Determine the amount of memory used by EdgeWeightedGraph to represent a graph with V vertices and E edges, using the memory-cost model of SECTION 1.4.
4.1.11 Tree represented by edgeTo[] after call to bfs(G, 0): 0 5 2 6 10 3
[]
{ "number": "4.3.11", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Suppose that a graph has distinct edge weights. Does its shortest edge have to belong to the MST? Can its longest edge belong to the MST? Does a min-weight edge on every cycle have to belong to the MST? Prove your answer to each question or give a counterexample.
4.1.12 When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch. If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance f...
[]
{ "number": "4.3.12", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Given an MST for an edge-weighted graph G, suppose that an edge in G that does not disconnect G is deleted. Describe how to find an MST of the new graph in time proportional to E.
4.1.14 If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths. This can be seen in the following graph: 0 (source) / \ 1 - 2 | | 4 - 3 If the edge 0 - 2 is inserted before the edge 0 - 1: Using a stack, the distance from 0 to 4 will be 3. Using a queue, the dis...
[]
{ "number": "4.3.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Suppose that you use a priority-queue implementation that maintains a sorted list. What would be the order of growth of the worst-case running time for Prim’s algorithm and for Kruskal’s algorithm for graphs with V vertices and E edges? When would this method be appropriate, if ever? Defend your answer.
4.1.19 count marked[] id[] 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) 0 T 0 dfs(5) 0 T T 0 0 check 0 dfs(10) 0 ...
[]
{ "number": "4.3.19", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
True or false: At any point during the execution of Kruskal’s algorithm, each vertex is closer to some vertex in its subtree than to any vertex not in its subtree. Prove your answer.
4.1.20 Has Cycle? marked[] F 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T dfs(5) T T check 0 F dfs(10) T T T check 5 F ...
[]
{ "number": "4.3.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Provide an implementation of edges() for PrimMST (page 622). Solution: public Iterable<Edge> edges() { Bag<Edge> mst = new Bag<Edge>(); for (int v = 1; v < edgeTo.length; v++) mst.add(edgeTo[v]); return mst; }
4.1.21 Is 2-colorable? marked[] color[] T 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T F F F F F F F F F F F F dfs(5) T ...
[]
{ "number": "4.3.21", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Exercise" }
Reverse-delete algorithm. Develop an implementation that computes the MST as follows: Start with a graph containing all of the edges. Then repeatedly go through the edges in decreasing order of weight. For each edge, check if deleting that edge will disconnect the graph; if not, delete it. Prove that this algorithm com...
4.1.24 Number of connected components: 33 Size of the largest component: 118774 Number of components of size less than 10: 5 Does the largest component contain Kevin Bacon: Yes Eccentricity, diameter, radius, center and girth: The strategy for computing the eccentricity, diameter, radius, center and girth of the lar...
[]
{ "number": "4.3.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Creative Problem" }
Animations. Write a client program that does dynamic graphical animations of MST algorithms. Run your program for mediumEWG.txt to produce images like the figures on page 621 and page 624.
4.1.27 Integer * object overhead -> 16 bytes * int value -> 4 bytes * padding -> 4 bytes Amount of memory needed: 16 + 4 + 4 = 24 bytes Node * object overhead -> 16 bytes * extra overhead for reference to the enclosing instance -> 8 bytes * Item reference (item) -> 8 bytes * Node reference (next) -> 8 bytes Amount of...
[]
{ "number": "4.3.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Creative Problem" }
Space-efficient data structures. Develop an implementation of the lazy version of Prim’s algorithm that saves space by using lower-level data structures for EdgeWeightedGraph and for MinPQ instead of Bag and Edge. Estimate the amount of memory saved as a function of V and E, using the memory-cost model of SECTION 1.4 (...
4.1.28 Non-isomorphic graphs: There are 2 non-isomorphic graphs with 2 vertices: o o o-o There are 4 non-isomorphic graphs with 3 vertices: o o o o o-o o-o-o o / \ o-o There are 11 non-isomorphic graphs with 4 vertices: o o o o o-o o o o-o-o o o-o-o-o o-o o-o o | o / \ o o o / \ o---o o o-...
[]
{ "number": "4.3.28", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Creative Problem" }
Euclidean weighted graphs. Modify your solution to EXERCISE 4.1.37 to create an API EuclideanEdgeWeightedGraph for graphs whose vertices are points in the plane, so that you can work with graphical representations.
4.1.30 - Eulerian and Hamiltonian cycles An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once. A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visite...
[]
{ "number": "4.3.30", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Creative Problem" }
MST weights. Develop implementations of weight() for LazyPrimMST, PrimMST, and KruskalMST, using a lazy strategy that iterates through the MST edges when the client calls weight().Then develop alternate implementations that use an eager strategy that maintains a running total as the MST is computed.
4.1.31 - Graph enumeration If we were considering graphs with no self-loops: There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect. (2) So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)! ((2)) ...
[]
{ "number": "4.3.31", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Creative Problem" }
Certification. Write an MST and EdgeWeightedGraph client check() that uses the following cut optimality conditions implied by PROPOSITION J to verify that a proposed set of edges is in fact an MST: A set of edges is an MST if it is a spanning tree and every edge is a minimum-weight edge in the cut defined by removing t...
4.1.33 - Odd cycles A graph is two-colorable (bipartite) if and only if it contains no odd-length cycle. Proof: 1- Proving that a graph with an odd-length cycle cannot be bipartite: If a graph G is bipartite with vertex sets V1 and V2, every step along a walk takes you either from V1 to V2 or from V2 to V1. To end up...
[]
{ "number": "4.3.33", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.3, "section_title": "Minimum Spanning Trees", "type": "Creative Problem" }
True or false: Adding a constant to every edge weight does not change the solution to the single-source shortest-paths problem.
4.1.1 The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2. Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide th...
[]
{ "number": "4.4.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Provide an implementation of toString() for EdgeWeightedDigraph.
4.1.2 adj[] 0 -> 5 -> 2 -> 6 1 -> 4 -> 8 -> 11 2 -> 5 -> 6 -> 0 -> 3 3 -> 10 -> 6 -> 2 4 -> 1 -> 8 5 -> 0 -> 10 -> 2 6 -> 2 -> 3 -> 0 7 -> 8 -> 11 8 -> 1 -> 11 -> 7 -> 4 9 -> 10 -> 5 -> 3 11 -> 8 -> 7 -> 1
[]
{ "number": "4.4.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Give a trace that shows the process of computing the SPT of the digraph defined in EXERCISE 4.4.5 with the eager version of Dijkstra’s algorithm.
4.1.6 The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections. adj[] 0 -> 1 -> 3 (the original was 0 -> 3 -> 1) 1 -> 2 -> 0 2 -> 3 -> 1 3 -> 0 -> 2
[]
{ "number": "4.4.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
The table below, from an old published road map, purports to give the length of the shortest routes connecting the cities. It contains an error. Correct the table. Also, add a table that shows how to achieve the shortest routes.
4.1.9 marked[] adj[] dfs (0) 0 T 0 5 2 6 1 1 4 8 11 2 2 5 6 0 3 3 3 10 6 2 4 4 1 8 5 5 0 10 2 6 6 2 3 0 7 ...
[]
{ "number": "4.4.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Consider the edges in the digraph defined in EXERCISE 4.4.4 to be undirected edges such that each edge corresponds to equal-weight edges in both directions in the edge-weighted digraph. Answer EXERCISE 4.4.6 for this corresponding edge-weighted digraph.
4.1.10 Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph. Proof by contradiction: If the graph has a node of degree one, removing it gives a connected graph. Example: o - o Otherwise, every path in the graph belongs to a cycle. To see this, start with any p...
[]
{ "number": "4.4.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Use the memory-cost model of SECTION 1.4 to determine the amount of memory used by EdgeWeightedDigraph to represent a graph with V vertices and E edges.
4.1.11 Tree represented by edgeTo[] after call to bfs(G, 0): 0 5 2 6 10 3
[]
{ "number": "4.4.11", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Adapt the DirectedCycle and Topological classes from SECTION 4.2 to use the EdgeWeightedDigraph and DirectedEdge APIs of this section, thus implementing EdgeWeightedCycleFinder and EdgeWeightedTopological classes.
4.1.12 When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch. If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance f...
[]
{ "number": "4.4.12", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Show the paths that would be discovered by the two strawman approaches described on page 668 for the example tinyEWDn.txt shown on that page.
4.1.14 If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths. This can be seen in the following graph: 0 (source) / \ 1 - 2 | | 4 - 3 If the edge 0 - 2 is inserted before the edge 0 - 1: Using a stack, the distance from 0 to 4 will be 3. Using a queue, the dis...
[]
{ "number": "4.4.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Find the lowest-weight cycle (best arbitrage opportunity) in the example shown in the text.
4.1.19 count marked[] id[] 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) 0 T 0 dfs(5) 0 T T 0 0 check 0 dfs(10) 0 ...
[]
{ "number": "4.4.19", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Find a currency-conversion table online or in a newspaper. Use it to build an arbitrage table. Note: Avoid tables that are derived (calculated) from a few values and that therefore do not give sufficiently accurate conversion information to be interesting. Extra credit: Make a killing in the money-exchange market!
4.1.20 Has Cycle? marked[] F 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T dfs(5) T T check 0 F dfs(10) T T T check 5 F ...
[]
{ "number": "4.4.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Show, in the style of the trace in the text, the process of computing the SPT with the Bellman-Ford algorithm for the edge-weighted digraph of EXERCISE 4.4.5.
4.1.21 Is 2-colorable? marked[] color[] T 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 dfs(0) T F F F F F F F F F F F F dfs(5) T ...
[]
{ "number": "4.4.21", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Exercise" }
Multisource shortest paths. Develop an API and implementation that uses Dijkstra’s algorithm to solve the multisource shortest-paths problem on edge-weighted digraphs with positive edge weights: given a set of sources, find a shortest-paths forest that enables implementation of a method that returns to clients the shor...
4.1.24 Number of connected components: 33 Size of the largest component: 118774 Number of components of size less than 10: 5 Does the largest component contain Kevin Bacon: Yes Eccentricity, diameter, radius, center and girth: The strategy for computing the eccentricity, diameter, radius, center and girth of the lar...
[]
{ "number": "4.4.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
Shortest paths in Euclidean graphs. Adapt our APIs to speed up Dijkstra’s algorithm in the case where it is known that vertices are points in the plane.
4.1.27 Integer * object overhead -> 16 bytes * int value -> 4 bytes * padding -> 4 bytes Amount of memory needed: 16 + 4 + 4 = 24 bytes Node * object overhead -> 16 bytes * extra overhead for reference to the enclosing instance -> 8 bytes * Item reference (item) -> 8 bytes * Node reference (next) -> 8 bytes Amount of...
[]
{ "number": "4.4.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
Longest paths in DAGs. Develop an implementation AcyclicLP that can solve the longest-paths problem in edge-weighted DAGs, as described in PROPOSITION T.
4.1.28 Non-isomorphic graphs: There are 2 non-isomorphic graphs with 2 vertices: o o o-o There are 4 non-isomorphic graphs with 3 vertices: o o o o o-o o-o-o o / \ o-o There are 11 non-isomorphic graphs with 4 vertices: o o o o o-o o o o-o-o o o-o-o-o o-o o-o o | o / \ o o o / \ o---o o o-...
[]
{ "number": "4.4.28", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
All-pairs shortest path in graphs with negative cycles. Articulate an API like the one implemented on page 656 for the all-pairs shortest-paths problem in graphs with no negative cycles. Develop an implementation that runs a version of Bellman-Ford to identify weights pi[v] such that for any edge v->w, the edge weight ...
4.1.30 - Eulerian and Hamiltonian cycles An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once. A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visite...
[]
{ "number": "4.4.30", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
All-pairs shortest path on a line. Given a weighted line graph (undirected connected graph, all vertices of degree 2, except two endpoints which have degree 1), devise an algorithm that preprocesses the graph in linear time and can return the distance of the shortest path between any two vertices in constant time.
4.1.31 - Graph enumeration If we were considering graphs with no self-loops: There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect. (2) So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)! ((2)) ...
[]
{ "number": "4.4.31", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
Shortest path in a grid. Given an N-by-N matrix of positive integers, find the shortest path from the (0, 0) entry to the (N-1, N-1) entry, where the length of the path is the sum of the integers in the path. Repeat the problem but assume you can only move right and down.
4.1.33 - Odd cycles A graph is two-colorable (bipartite) if and only if it contains no odd-length cycle. Proof: 1- Proving that a graph with an odd-length cycle cannot be bipartite: If a graph G is bipartite with vertex sets V1 and V2, every step along a walk takes you either from V1 to V2 or from V2 to V1. To end up...
[]
{ "number": "4.4.33", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
Bitonic shortest path. Given a digraph, find a bitonic shortest path from s to every other vertex (if one exists). A path is bitonic if there is an intermediate vertex v such that the edges on the path from s to v are strictly increasing and the edges on the path from v to t are strictly decreasing. The path should be ...
4.1.35 - Biconnectedness Any graph with no articulation points is biconnected. Proof: Consider two vertices, s, t, and a path P1 connecting s to t. We know that no vertex in P1 is an articulation point, so for each vertex v in the path, there is always another path P2 connecting s to t that does not include it. Also,...
[]
{ "number": "4.4.35", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 4, "chapter_title": "Graphs", "section": 4.4, "section_title": "Shortest Paths", "type": "Creative Problem" }
Give a trace for LSD string sort for the keys no is th ti fo al go pe to co to th ai of th pa
5.1.2 Trace for LSD string sort (same model as used in the book): input d=1 d=0 output no pa ai ai is pe al al th of co co ti th fo fo fo th go go al th is is go ti no no pe ai of of to al pa pa co no pe ...
[]
{ "number": "5.1.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
Give a trace for MSD string sort for the keys no is th ti fo al go pe to co to th ai of th pa
5.1.3 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 15, 0): input 0 no 1 is 2 th 3 ti 4 fo 5 al 6 go 7 pe 8 to 9 co 10...
[]
{ "number": "5.1.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
Give a trace for 3-way string quicksort for the keys no is th ti fo al go pe to co to th ai of th pa
5.1.4 Trace for 3-way string quicksort (same model as used in the book): -- -- -- 0 no is ai ai ai ai 1 is ai co al -- al 2 th co fo -- al co 3 ti fo al fo -- fo 4 fo al go ...
[]
{ "number": "5.1.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
Give a trace for MSD string sort for the keys now is the time for all good people to come to the aid of
5.1.5 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 13, 0): input 0 now 1 is 2 the 3 time 4 for 5 all 6 good 7 people 8 ...
[]
{ "number": "5.1.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
Give a trace for 3-way string quicksort for the keys now is the time for all good people to come to the aid of
5.1.6 Trace for 3-way string quicksort (same model as used in the book): ---- --- --- --- 0 now is aid aid aid aid aid 1 is aid come all --- --- all 2 the come for --- all all come 3 time f...
[]
{ "number": "5.1.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
Give the number of characters examined by MSD string sort and 3-way string quicksort for a file of N keys a, aa, aaa, aaaa, aaaaa, . . .
5.1.8 Both MSD string sort and 3-way string quicksort examine all characters in the N keys. That number is equal to 1 + 2 + ... + N = (N^2 + N) / 2 characters. MSD string sort, however, generates (R - 1) * N empty subarrays (an empty subarray for all digits in R other than 'a', in every pass) while 3-way string quicks...
[]
{ "number": "5.1.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
What is the total number of characters examined by 3-way string quicksort when sorting N fixed-length strings (all of length W), in the worst case?
5.1.10 The total number of characters examined by 3-way string quicksort when sorting N fixed-length strings (all of length W) in the worst case is O(N * W * R). This can be seen with a recurrence relation T(W). The base case T(1) is when all the strings have length 1. An example with R = 3 is { "a", "b", "c" }. In ...
[]
{ "number": "5.1.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Exercise" }
Hybrid sort. Investigate the idea of using standard MSD string sort for large arrays, in order to get the advantage of multiway partitioning, and 3-way string quicksort for smaller arrays, in order to avoid the negative effects of large numbers of empty bins.
5.1.13 - Hybrid sort Idea: using standard MSD string sort for large arrays, in order to get the advantage of multiway partitioning, and 3-way string quicksort for smaller arrays, in order to avoid the negative effects of large numbers of empty bins. This idea will work well for random strings because, in general, the...
[]
{ "number": "5.1.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Creative Problem" }
In-place key-indexed counting. Develop a version of key-indexed counting that uses only a constant amount of extra space. Prove that your version is stable or provide a counterexample.
5.1.17 - In-place key-indexed counting LSD and MSD sorts that use only a constant amount of extra space are not stable. Counterexample for LSD sort: The array ["4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720", "1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723"] after being so...
[]
{ "number": "5.1.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Creative Problem" }
Timings. Compare the running times of MSD string sort and 3-way string quicksort, using various key generators. For fixed-length keys, include LSD string sort.
5.1.22 - Timings Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays used in Mo...
[]
{ "number": "5.1.22", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Experiment" }
Array accesses. Compare the number of array accesses used by MSD string sort and 3-way string sort, using various key generators. For fixed-length keys, include LSD string sort.
5.1.23 - Array accesses Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays use...
[]
{ "number": "5.1.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Experiment" }
Rightmost character accessed. Compare the position of the rightmost character accessed for MSD string sort and 3-way string quicksort, using various key generators.
5.1.24 - Rightmost character accessed Running 1 experiment with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small s...
[]
{ "number": "5.1.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.1, "section_title": "String Sorts", "type": "Experiment" }
Draw the TST that results when the keys no is th ti fo al go pe to co to th ai of th pa are inserted in that order into an initially empty TST.
5.1.2 Trace for LSD string sort (same model as used in the book): input d=1 d=0 output no pa ai ai is pe al al th of co co ti th fo fo fo th go go al th is is go ti no no pe ai of of to al pa pa co no pe ...
[]
{ "number": "5.2.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Exercise" }
Draw the R-way trie that results when the keys now is the time for all good people to come to the aid of are inserted in that order into an initially empty trie (do not draw null links).
5.1.3 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 15, 0): input 0 no 1 is 2 th 3 ti 4 fo 5 al 6 go 7 pe 8 to 9 co 10...
[]
{ "number": "5.2.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Exercise" }
Draw the TST that results when the keys now is the time for all good people to come to the aid of are inserted in that order into an initially empty TST.
5.1.4 Trace for 3-way string quicksort (same model as used in the book): -- -- -- 0 no is ai ai ai ai 1 is ai co al -- al 2 th co fo -- al co 3 ti fo al fo -- fo 4 fo al go ...
[]
{ "number": "5.2.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Exercise" }
Develop nonrecursive versions of TrieST and TST.
5.1.5 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 13, 0): input 0 now 1 is 2 the 3 time 4 for 5 all 6 good 7 people 8 ...
[]
{ "number": "5.2.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Exercise" }
Implement the following API, for a StringSET data type: public class StringSET StringSET() create a string set void add(String key) put key into the set void delete(String key) remove key from the set boolean contains(String key) is key in the set? boolean isEmpty() is the set empty? int size() number of keys in the se...
5.1.6 Trace for 3-way string quicksort (same model as used in the book): ---- --- --- --- 0 now is aid aid aid aid aid 1 is aid come all --- --- all 2 the come for --- all all come 3 time f...
[]
{ "number": "5.2.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Exercise" }
Ordered operations for tries. Implement the floor(), ceil(), rank(), and select() (from our standard ordered ST API from Chapter 3) for TrieST.
5.1.8 Both MSD string sort and 3-way string quicksort examine all characters in the N keys. That number is equal to 1 + 2 + ... + N = (N^2 + N) / 2 characters. MSD string sort, however, generates (R - 1) * N empty subarrays (an empty subarray for all digits in R other than 'a', in every pass) while 3-way string quicks...
[]
{ "number": "5.2.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Creative Problem" }
Size. Implement very eager size() (that keeps in each node the number of keys in its subtree) for TrieST and TST.
5.1.10 The total number of characters examined by 3-way string quicksort when sorting N fixed-length strings (all of length W) in the worst case is O(N * W * R). This can be seen with a recurrence relation T(W). The base case T(1) is when all the strings have length 1. An example with R = 3 is { "a", "b", "c" }. In ...
[]
{ "number": "5.2.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Creative Problem" }
Hybrid TST with R2-way branching at the root. Add code to TST to do multiway branching at the first two levels, as described in the text.
5.1.13 - Hybrid sort Idea: using standard MSD string sort for large arrays, in order to get the advantage of multiway partitioning, and 3-way string quicksort for smaller arrays, in order to avoid the negative effects of large numbers of empty bins. This idea will work well for random strings because, in general, the...
[]
{ "number": "5.2.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Creative Problem" }
Spell checking. Write a TST client SpellChecker that takes as command-line argument the name of a file containing a dictionary of words in the English language, and then reads a string from standard input and prints out any word that is not in the dictionary. Use a string set.
5.1.17 - In-place key-indexed counting LSD and MSD sorts that use only a constant amount of extra space are not stable. Counterexample for LSD sort: The array ["4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720", "1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723"] after being so...
[]
{ "number": "5.2.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Creative Problem" }
Typing monkeys. Suppose that a typing monkey creates random words by appending each of 26 possible letter with probability p to the current word and finishes the word with probability 1 - 26p. Write a program to estimate the frequency distribution of the lengths of words produced. If "abc" is produced more than once, c...
5.1.22 - Timings Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays used in Mo...
[]
{ "number": "5.2.22", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Creative Problem" }
Duplicates (revisited again). Redo Exercise 3.5.30 using StringSET (see Exercise 5.2.6) instead of HashSET. Compare the running times of the two approaches. Then use Dedup to run the experiments for N = 10^7, 10^8, and10^9, repeat the experiments for random long values and discuss the results.
5.1.23 - Array accesses Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays use...
[]
{ "number": "5.2.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Experiment" }
Spell checker. Redo Exercise 3.5.31, which uses the file dictionary.txt from the booksite and the BlackFilter client on page 491 to print all misspelled words in a text file. Compare the performance of TrieST and TST for the file war.txt with this client and discuss the results.
5.1.24 - Rightmost character accessed Running 1 experiment with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small s...
[]
{ "number": "5.2.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.2, "section_title": "Tries", "type": "Experiment" }
Give the dfa[][] array for the Knuth-Morris-Pratt algorithm for the pattern A A A A A A A A A, and draw the DFA, in the style of the figures in the text.
5.1.2 Trace for LSD string sort (same model as used in the book): input d=1 d=0 output no pa ai ai is pe al al th of co co ti th fo fo fo th go go al th is is go ti no no pe ai of of to al pa pa co no pe ...
[]
{ "number": "5.3.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }