Midterm Review - PowerPoint PPT Presentation

About This Presentation
Title:

Midterm Review

Description:

Midterm Review 22C:21 Computer Science II – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 21
Provided by: Saur60
Category:

less

Transcript and Presenter's Notes

Title: Midterm Review


1
Midterm Review
  • 22C21 Computer Science II

2
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.

3
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.

4
Problem 1(What to do?)
  • To implement the Set ADT as a boolean array 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,

5
Solution 1
/ Implementation of the Set ADT as a boolean
array _at_authors Michael Edwards and Sriram
Pemmaraju / import java.util. public class
Set private int n private boolean
set contd.

6
Solution 1 (contd.)
// This is the constructor for the Set. public
Set(int size) n size set new
booleann for(int i 0 i lt n
i) seti false // Copy constructor
for duplicating a set. public Set(Set s) n
s.n set new booleann for(int i 0 i
lt n i) seti s.seti contd.

7
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.

8
Solution 1 (contd.)
// This inserts an element into the set.
public void insert(int x) if(x gt 0 x
lt n) setx-1 true //
This removes an element from the set.
public void delete(int x) if(x gt 0 x lt
n) setx-1 false contd.

9
Solution 1 (contd.)
// Test an element for membership public
boolean isMember(int x) if(x gt 0 x lt
n) return setx-1 else return
false // Test the set to see if it
is empty. public boolean isEmpty() boolean
empty true for(int i 0 i lt n
i) empty empty !seti return
empty contd.

10
Solution 1 (contd.)
// Combine another set with this one using the
union public void union(Set s) for(int i
0 i lt n i) seti seti
s.seti 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

11
Solution 1 (contd.)
// Combine another set with this one using the
intersection public void intersection(Set s)
for(int i 0 i lt n i) seti seti
s.seti contd. 1,3,4 n 3,4,5
3,4 T,F,T,T,F F,F,T,T,T F,F,T,T,F

12
Solution 1 (contd.)
// Return the number of elements in the set
public int size() int size 0 for(int i
0 i lt n i) if(seti) size return
size // Print out all the elements in this
set public void print() System.out.print("")
for(int i 0 i lt n i) if(seti) S
ystem.out.print((i1)", ") System.out.println(
"")

13
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.

14
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

15
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).

16
Solution 7 (using myGraph)
  • public boolean haveCommonNeighbor(String
    vertex1, String vertex2)
  • int id1 getIndex(vertex1)
  • int id2 getIndex(vertex2)
  • for(int i 0 i lt numVertices i)
  • // Check if i is a neighbor of both id1 and
    id2
  • boolean check1 false
  • boolean check2 false
  • if(i lt id1)
  • check1 Edgesid1i
  • else
  • check1 Edgesiid1
  • if(i lt id2)
  • check2 Edgesid2i
  • else
  • check2 Edgesiid2
  • if (check1 check2)
  • return true

17
Problem 8
  • Consider the STRANGE_QUEUE ADT that is similar
    to the QUEUE ADT, 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 QUEUE ADT, 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.

18
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.

19
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.

20
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 delete operation 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.
Write a Comment
User Comments (0)
About PowerShow.com