You are on page 1of 2

Andrew Chesney

09/3/2014
CPS340

1.)

ALGORITHM IntSqrt(n)
//Computes the integer square root of a positive number
//Input: Any positive integer, n
//Output: The integer square root of n
if n < 0
return 0
numA <- (n / 2) + 1
numB <- (numA + (n / numA)) / 2
while numB < numA do
numA <- numB
numB <- (numA + (n / numA)) / 2
return numA

2.)

a.) You could take advantage of the sorted list represented as an array by
using a binary search. The binary search algorithm is extremely efficient
on sorted arrays.
b.)You could take advantage of the sorted linked list by stopping as soon
as a key greater than or equal to the search key is found. This reduces the
time used for unsuccessful searches.

3.)

a)
push(a) -> a
push(b) -> b
a
pop

>a

push(c) -> c
a
push(d) -> d
c
a

pop

-> c
a

b)
enqueue(a) -> a
enqueue(b) -> ab
dequeue

-> b

enqueue(c) -> bc
enqueue(d) -> bcd
dequeue
4.)

-> cd

a)
i) If all elements in the adjacency matrix, not including the main
diagonal, are equal to 1, then the graph is considered complete.
ii) If there is an element on the main diagonal equal to 1, then the
graph will have a loop.
iii) If the adjacency matrix has a row of all zeros, then the graph will
have an isolated vertex.
b)
i) If the linked lists contain all of the vertices of the graph then the
graph is considered complete.
ii) If the vertex that defines the list is contained in one of the
adjacency lists, then the graph will
contain a loop.
iii) If one of the adjacency lists are empty then the graph will have
an isolated vertex.

You might also like