DATA STRUCTURE
Programming and Technical
Programming
Technical
You are planning the group seating arrangement for a open book test given a list of students, V from different schools to participate. Assuming the fact that students who are known to each other directly or indirectly will probably cheat more as compared to unknown people sitting together.
Suppose you are also given a lookup table T where T[u] for u ? V is a list of students that u knows. If u knows v, then v knows u. You are required to arrange the seating such that any student at a table doesn't knows any other student sitting at the same table either directly or through some other student sitting at the same table. For example, if x knows y, and y knows z, then x, y, z can sit at the same table. Describe an efficient algorithm that, given V and T, returns the minimum number of tables needed to achieve this requirement. Analyze the running time of your algorithm.
Read Solution (Total 1)
-
- We can construct an undirected graph G = (V, E) with guests as vertices,
and an edge between two vertices means the two guests know each other. Table T
represents the adjacency lists for the vertices. Two guests can sit at the same table if
there is a path between them. If we start from one vertex s and search the graph using
breadth-first search (BFS) or depth-first search (DFS), all the guests that are reachable
from s can sit at the same table, and additional tables are needed for vertices that are
unreachable from s.
Hence, to find the minimum number of tables, we can iterate through s ∈ V . If
s is not visited, increment the number of tables needed and call DFS-VISIT(s, T)
or BFS(s, T), marking vertices as visited during the traversal. Return the number
of tables needed after iterating through all the vertices. This problem is equivalent to
finding the number of connected components in the graph. The running time is Θ(V +
E) because every vertex or edge is visted exactly once. Below is the pseudocode.
NUM-TABLES(V, T)
1 visited = {}
2 n = 0
3 for s ∈ V
4 if s ∈/ visited
5 n = n + 1
6 add s to visited
7 DFS-VISIT(s, T, visitied)
8 return n
DFS-VISIT(u, T, visitied)
1 for v ∈ T[u]
2 if v ∈/ visited
3 add v to visited
4 DFS-VISIT(v, T, visited) - 9 years agoHelpfull: Yes(0) No(0)
DATA STRUCTURE Other Question
Your job is to build a data structure to maintain a set of photographs. Your photograph database should allow you to insert and search for photographs, as well as to designate some of the photographs as favourites by marking them. In more detail, your data structure should support the following operations:
• Insert(x, t, m): inserts photograph x that was taken at time t. If m = 1, then the photograph is marked as a favourite; if m = 0, then it is not a favourite.
• Search(t): find the next photograph taken after time t.
• NextFavorite(t): find the next photograph taken after time t that is a favourite.
Give a data structure for solving this problem, and explain how it works. For more efficiency, your data structure should be both time and space efficient: more points will be given for more efficient solutions. (Remember that the photographs themselves may be quite large.) Give the performance (i.e., running time) of each operation and the space usage of the data structure. You may assume that at any given time t, your camera has taken at most one photograph x. Describe your solution in words, or very high-level pseudocode. You do not need to provide complete details. You must provide enough detail, however, that your solution is clear.
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. In the question here, we are concerned with the following, related problem: First n straws are dumped on the table. Next, for every pair of straws, we want to determine if the straws are connected by a path of touching straws. In other words, given a list of the endpoints for n > 1 straws (as if they were dumped on a large piece of graph paper), determine all the pairs of straws that are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws. Give an algorithm to compute all the pairs of straws that are connected.
Here is some additional information that you can use:
i. As a basic step you need to determine whether two straws directly touch (intersect), i.e., whether straw ab with end points a and b intersects with straw cd (with end points c and d).
ii. To get full credit also explain how the elementary step i) can be done. If you cannot find an algorithm for this step, assume an O(n 2 ) algorithm and explain how to find all pairs of straws that are connected.
Analyse and explain the worst-case computational complexity of your algorithm in terms of n.