200 likes | 212 Views
Midterm Review . 22C:21 Computer Science II. Problem 1. A Set ADT represents some subset of {1,2, ..., n } for some natural number n . It supports the operations insert , delete , isMember , isEmpty , union , and intersection , which are described below.
E N D
Midterm Review 22C:21 Computer Science II
Problem 1 A Set ADT represents some subset of {1,2, ..., n} for some natural number n. It supports the operations insert, delete, isMember, isEmpty, union, and intersection, which are described below.
Problem 1(methods) • S.insert(x) takes a number x in {1, 2, ..., n} and inserts x it into S. • S.delete(x) takes a number x in {1, 2, ..., n} and deletes it from S. • S.isMember(x) takes a number x in {1, 2, ..., n} and returns true if x is in S and returns false, otherwise. • S.isEmpty() returns true if S is empty and returns false otherwise. • S.union(A) takes a subset A of {1, 2, ..., n} and changes S to S union A. • S.intersection(A) takes a subset A of {1, 2, ..., n} and changes S to S intersection A.
Problem 1(What to do?) To implement the Set ADT as a booleanarray of size n. If an element x in {1, 2, ..., n} belongs to the set, then slot x-1 in the boolean array should be true; otherwise it should be false. e.g. {1,3,4,7,…} → {T,F,T,T,F,F,T,…}
Solution 1 /** * Implementation of the Set ADT as a boolean array * * @authors: Michael Edwards and Sriram Pemmaraju */ import java.util.*; public class Set { private int n; private boolean[] set; … contd.
Solution 1 (contd.) // This is the constructor for the Set. public Set(int size) { n = size; set = new boolean[n]; for(int i = 0; i < n; i++) set[i] = false; } // Copy constructor for duplicating a set. public Set(Set s) { n = s.n; set = new boolean[n]; for(int i = 0; i < n; i++) set[i] = s.set[i]; } … contd.
Solution 1 (contd.) /** * This is the default constructor for the Set. * If no argument is provided, it will construct * an empty set that has the potential to hold 100 elements. */ public Set() { this(100); } … contd.
Solution 1 (contd.) // This inserts an element into the set. public void insert(int x) { if(x > 0 && x <= n) set[x-1] = true; } // This removes an element from the set. public void delete(int x) { if(x > 0 && x <= n) set[x-1] = false; } … contd.
Solution 1 (contd.) // Test an element for membership public boolean isMember(int x) { if(x > 0 && x <= n) return set[x-1]; else return false; } // Test the set to see if it is empty. public boolean isEmpty() { boolean empty = true; for(int i = 0; i < n; i++) empty = empty && !set[i]; return empty; } … contd.
Solution 1 (contd.) // Combine another set with this one using the union public void union(Set s) { for(int i = 0; i < n; i++) set[i] = set[i] || s.set[i]; } … contd. {1,3,4} ∪ {2,4} = {1,2,3,4} {T,F,T,T,F} || {F,T,F,T,F} = {T,T,T,T,F}
Solution 1 (contd.) // Combine another set with this one using the intersection public void intersection(Set s) { for(int i = 0; i < n; i++) set[i] = set[i] && s.set[i]; } … contd. {1,3,4} ∩ {3,4,5} = {3,4} {T,F,T,T,F} && {F,F,T,T,T} = {F,F,T,T,F}
Solution 1 (contd.) // Return the number of elements in the set public int size() { int size = 0; for(int i = 0; i < n; i++) if(set[i]) size++; return size; } // Print out all the elements in this set public void print() { System.out.print("{"); for(int i = 0; i < n; i++) if(set[i]) System.out.print((i+1)+", "); System.out.println("}"); } }
Problem 6 Write a method of the LinkList class that deletes and returns the last Link of the linked list. Use the following function header: public Link deleteLast() If the linked list is empty, then the function just returns null.
Solution 6 public Link deleteLast() { // if list is empty if(first == null) return null; else{ Link current = first; Link previous = null; while(current.next != null) { previous = current; current = current.next; } // if list has one element, first needs to be updated if(previous == null) first = null; else previous.next = null; return current; } }
Problem 7 Write a method of the myGraph class that determines if a given pair of vertices have a common neighbor. The function returns true if the given vertices have a common neighbor; otherwise it returns false. Two vertices A and B have a common neighbor if for some vertex C, the graph contains the edge between A and C and the edge between B and C. For simplicity, you may assume that the two given vertices are present in the graph. Solve this problem using both the adjacency matrix implemenation (myGraph class) and the adjacency list representation (myListGraph class).
Solution 7 (using myGraph) public boolean haveCommonNeighbor(String vertex1, String vertex2) { int id1 = getIndex(vertex1); int id2 = getIndex(vertex2); for(int i = 0; i < numVertices; i++) { // Check if i is a neighbor of both id1 and id2 boolean check1 = false; boolean check2 = false; if(i <= id1) check1 = Edges[id1][i]; else check1 = Edges[i][id1]; if(i <= id2) check2 = Edges[id2][i]; else check2 = Edges[i][id2]; if (check1 && check2) return true; } return false; }
Problem 8 Consider the STRANGE_QUEUEADT that is similar to the QUEUEADT, except that each item has an associated priority that can be 0 or 1. Items with priority 0 are considered very important and are "served" before items of priority 1. Like the QUEUEADT, the STRANGE_QUEUE ADT also supports the operations insert and delete, but these operations for the STRANGE_QUEUE ADT pay attention to the priority of the items.
Problem 8 (contd.) Here is a description of how insert and delete work for the STRANGE_QUEUE ADT. • Insert: Add the given item, with the specified priority, to the collection. • Delete: if there is an item with priority 0 in the collection, delete the oldest item in the collection with priority 0 and return it. Otherwise, if there is an item with priority 1 in the collection, delete the oldest item in the collection with priority 1 and return it. Otherwise, the collection is empty and there is nothing to return.
Problem 8 (contd.) Write a brief description (5-6 lines) of how you would implement the STRANGE_QUEUE ADT so that the insert and delete functions, both run in O(1) time. Your description would have two parts: (1) describing the data structures you will use for your implementation and, (2) how the two operations are implemented, using these data structures.
Solution 8 We should keep two separate regular queues, one exclusively for items with priority 0 and another for items with priority 1. So the data members could just be: private Queue q0; private Queue q1; Any implementation of the Queue class that performs insert, delete, and isEmpty in O(1) time will suffice for our purposes. The insert operation inserts the given item into q0 if it has priority 0 and into q1 if it has priority 1. The deleteoperation checks if q0 is empty first. If it is non-empty, then it deletes an item from q0 and returns it. If q0 is empty, it deletes an item from q1 and returns it, assuming that q1 is non-empty.