Answers to Selected Exercises Chapter 1



Download 0.49 Mb.
Page6/7
Date18.07.2017
Size0.49 Mb.
#23634
1   2   3   4   5   6   7

Chapter 9


2. The relational operator <= should be replaced with >=.

5. (a) The member functions would not change, but the private data members would change. The only data member would be a pointer to a linked lists.

(b) The code for SortedType::InsertItem (linked implementation) can be used directly with the relational operator reversed.

(c) The code is identical to StackType::Pop.

(d) Dequeue for a priority queue implemented as a linked list (ordered from highest to lowest priority) is very simple and efficient; we have direct access to the largest element. There is less work involved in fixing the structure after the largest element is removed, because the next-largest element immediately follows it. Thus the operation is 0(1). In the heap implementation we have immediate access to the largest element, but we have to perform a reheap operation to fix the structure, resulting in an O(log2N) operation. The linked list implementation is more efficient, in terms of Big O.

When the priority queue is implemented as a linked list, the efficiency of Enqueue varies according to the position that the new element occupies in the list. If the new element belongs in the last position, the whole list is searched before the insert place is found. Thus the operation is O(N). Using heaps, the insertion operation is O(log2N). The linked list implementation might be better if the elements were inserted in largely sorted order from smallest to largest value.

8. (a) The highest-priority element is the one with the largest time stamp. (This assumes that the time stamp never reaches INT_MAX.

(b)


Push

Assign the next largest time stamp to the new element

Put new element in the stack (position is unimportant)

Pop

Find the element with the largest time stamp

Assign this element to item (to be returned)

Remove the element

(c) The Push operation has O(1) because it doesn't matter where the item is stored in the structure. The Pop operation has O(N), because the item with the largest time stamp must be searched for.

Therefore, Push is the same in both implementations, but Pop is not. If the priority queue is implemented using a heap with largest value the highest priority, Pop and Push have O(log2N).



12.

EmployeeGraph



..vertices







.numVertices







.vertexList







[0]

Brent

[1]

Darlene

[2]

Fran

[3]

Fred

[4]

Jean

[5]

John

[6]

Lance

[7]

Mike

[8]

Sander

[9]

Susan

.Edges

[0]

F

F

F

T

F

F

T

F

F

F

[1]

F

F

F

F

F

F

F

T

F

T

[2]

F

F

F

F

F

T

T

F

T

F

[3]

T

F

F

F

F

F

F

F

F

F

[4]

F

F

F

F

F

F

T

F

F

T

[5]

F

F

T

F

F

F

F

F

F

T

[6]

T

F

T

F

T

F

F

F

F

F

[7]

F

T

F

F

F

F

F

F

F

F

[8]

F

F

T

F

F

F

F

F

F

T

[9]

F

T

F

F

T

T

F

F

T

F




[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

[9]

14. "works with" is the best description of the relationship represented by the edges between vertices in EmployeeGraph, because it is an undirected graph. The other relationships listed have an order implicit in them.

17. The correct answer is (b). For example, a dalmatian is an example of a dog.

20. (a)

StateGraph



..vertices







.numVertices

7




.vertexList







[0]

Alaska

[1]

California

[2]

Hawaii

[3]

New York

[4]

Oregon

[5]

Texas

[6]

Vermont

.Edges

[0]

F

F

F

F

T

F

F

[1]

F

F

F

F

F

F

F

[2]

T

T

F

T

F

T

F

[3]

F

F

F

F

F

F

F

[4]

F

F

F

F

F

F

F

[5]

F

F

T

F

F

F

T

[6]

T

T

F

T

F

F

F




[0]

[1]

[2]

[3]

[4]

[5]

[6]

(b)


..vertices







.numVertices

7




.vertexList







[0]

Alaska

->

4

/




























[1]

California





































[2]

Hawaii

->

1

---

->

3

---

->

5

---

->

0

/

[3]

New York





































[4]

Oregon





































[5]

Texas

->

2

---

->

6

/



















[6]

Vermont

->

0

---

->

1

---

->

3

/



23. Deleting a vertex is more complicated than deleting an edge for two reasons. First, in addition to removing the vertex from the set of vertices, we must also remove the edges to all its adjacent vertices in the set of edges. Second, we must dedecide what to do about the now unused vertex number. The best solution is to keep a list of returned vertex numbers and assign new numbers from there first.

25. Implicit set representations represent those items that are present in an instance of a set explicitly; those items that are not represented are not in the set. Explicit set representations associate a place (bit or Boolean flag) for each item in the base type in the instance of each set . The place for an item is true if the item is in the set and false if the item is not in the set.



Download 0.49 Mb.

Share with your friends:
1   2   3   4   5   6   7




The database is protected by copyright ©ininet.org 2024
send message

    Main page