hw2 Questions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CST 370 – Summer A 2019

Homework 2
Due: 05/07/2019 (Tuesday) (11:55 PM)

Name: __________Eric Ybarra____________

ID: ________9243_________

1. (a) Based on our textbook’s definition, is this a graph? (True/False)

a e

b c f

(b) Based on our textbook’s definition, is this a graph? (True/False)

b c

d e f g

2. Assume that you should search a number in a list of n numbers. How can you take advantage of the fact
that the list is known to be sorted? Give separate answers for the following two cases.

(a) A list represented in an array.


Searching for a number while using an array is typically checking every value in the array which
takes time especially if the array is large. By knowing that the list is sorted, the time complexity can be
reduced by using the binary search algorithm.

(b) A list represented in a linked list.


When searching for a number while using a linked list and knowing that the list is sorted then if
the element is greater than or equal to search key the search can be stopped saving more time. Also
having an unsuccessful search for a number while having met the criteria of having a known sorted list
can result it being faster.
3. Represent the following graph in the adjacency list as you learned in the class. Note that there are five
vertices (= A, B, C, D, and E) in the graph.

[Note: If it is difficult to draw a diagram when doing homework, you can draw it on paper by hand. Then
take a picture of the paper and insert it.]

A B D

C E

4. Assume a binary tree with five vertices such as v1, v2, v3, v4, and v5. Determine the maximum
number of edges possible in the tree.
V = {v1, v2, v3, v4, v5}
|E|=|V|-1
E=5–1=4
Maximum number of edges = 4
5. (a) If your program takes n2 time and your classmate’s program takes n*log n time, whose program is
faster? Pick one between “You” and “Your Classmate”.
My program = n2
My classmate = n*log n

Fastest program = my classmate n*log n

(b) If your program takes constant time and your classmate’s program takes log n time, whose program is
faster? Pick one between “You” and “Your Classmate”.
My program = constant time
My classmate = log n

Fastest program = my program constant time

6. Consider the following algorithm.

Algorithm DoSomething (A[0.. n – 1])


1. num1  A[0];
2. num2  A[0]
3. i  1
4. while i < n do
5. if A[i] < num1
6. num1  A[i];
7. if A[i] > num2
8. num2  A[i];
9. i  i + 1
10. return (num2 – num1);

(a) Present the basic operation of the algorithm. When you present the basic operation, you should
indicate the line number of the basic operation clearly.
Line # Operation
4 <
5 <
7 >
9 +
10 return

Basic Operation = line 4 <

(b) Present the time complexity category of the algorithm among the eight most popular time
complexity categories we covered in the lecture.

Linear Time O(n)


7. Consider the following algorithm.
1. Algorithm Mystery(n)
2. // Input: A nonnegative integer n
3. S  0
4. for i  1 to n do
5. ki*i
6. SS+k
7. return S

(a) What does this algorithm compute?


Computes the sum of numbers being squared from 1 to n.

(b) Present the time complexity category of the algorithm among the eight most popular time complexity
categories we covered in the lecture.
Linear Time O(n)

You might also like