Cecs 6010 Chapter 1 HW
Cecs 6010 Chapter 1 HW
ID #: 129074
CECS 6010 Winter 2022
Advanced Design and Analysis of
Algorithms
Chapter 1 Homework
Professor: Dr. Jeffrey Duffany
I. exercises 1.1 (page 7) exercises 3.b & 4
exercises 1.2 (page 18) exercises 9
exercises 1.3 (page 24) exercises 4
exercises 1.4 (page 37) exercises 4.a
3.b) Write down a recipe for cooking your favorite dish with the precision required by an
algorithm.
4) Design an algorithm for [√ n] for any positive integer n. Besides assignment and
comparison, your algorithm may only use the four basic arithmetical operations.
A user-friendly algorithm that does not rely in availability of an approximate value of √ n can
check the squares of consecutive positive integers until the first square exceeding n is
encountered. The answer to this algorithm will be the number’s immediate predecessor. A much
fasters algorithm for solving this problem is the Newton’s algorithm. If we apply the Newtons
algorithm to compute √ 2, we will have to round the numbers to six decimal places and use
standard numerical analysis notation to indicate the round-off.
Also we can express an algorithm in pseudocode that will solve this problem in the following
form:
Sqrt (int n) {
//Base case
If ( n = = 0 || n = = 1)
return n
The time complexity of the algorithm express above is O(log(n)) because Binary search is used.
9. Consider the following algorithm for finding the distance between the two closest
elements in an array of numbers.
ALGORITHM MinDistance(A[0..n − 1]) //Input: Array A[0..n − 1] of numbers
//Output: Minimum distance between two of its elements
dmin ← ∞
for i ← 0 to n − 1 do
forj ←0ton−1do
ifi̸=j and|A[i]−A[j]|<dmin
d mi n ← |A[i ] − A[j ]| return dmin
Make as many improvements as you can in this algorithmic solution to the problem. If you
need to, you may change the algorithm altogether; if not, improve the implementation
given.
The algorithm above can be further improved by only considering the same pair of elements just
once which in turn will avoid recomputing the same expression in the inner most loop (the
operation contributing to the most to the total running time-reference Chapter 2 page 44.).
The improved algorithm would be:
Algorithm MinDistance1(A[0..n-1])
//Input: An array A[0..n-1] of numbers
//Output: The minimum distance d between two of its elements
D min ∞
for i 0 to n-2 do
for j i + 1 to n-1 do
temp |A[i]-A[j]
if temp < dmin
dmin temp
return dmin.
III. Excercises 1.3 (page 24) exercise 4
4. Könisberg bridges The Könisberg bridge puzzle is universally accepted as the problem
that gave birth to graph theory. It was solved by the great Swiss-born mathematician
Leonhard Euler (1707–1783). The problem asked whether one could, in a single stroll, cross
all seven bridges of the city of Könisberg exactly once and return to a starting point.
Following is a sketch of the river with its two islands and seven bridges:
The graph above represents the problem of Könisberg, the problem asks us if there is a
path in this multigraph that goes to all the edges of a multigraph exactly once and reaches
to the starting vertex. Such a path is called a Eulian path named after Euler and its
characteristic is that it is a path that covers all the edges exactly once and does not reach
to the starting vertex.
b. Does this problem have a solution? If you believe it does, draw such a stroll; if you
believe it does not, explain why and indicate the smallest number of new bridges
that would be required to make such a stroll possible.
In the multigraph that represents the problem, there is no Eulerian circuit because all of the
vertices do not have even degrees. Meaning if we draw a circle around node a, we will see
that the circle interests three lines, thus it has odd degree of vertices, the same occurs with
node c and node d. The only node that has a even degree of vertices is b which has degree of
4. It is imperative to state that an Eulerian path exits in a multigraph if and only if it has
exactly two vertices of odd degrees and such path starts at one of those two vertices and end
at the other. We can see that are multigraph has three vertices with odd degrees.
If we wanted to satisfy this problem to form a Eulerian path, two out of the four vertices need
to be made even. In order to do so, adding an extra bridge that unites the same places as the
existing bridge. Thus, the new multigrapgh to satisfy the Eulerian path would be the
following:
We can see that we added a “loop” from node b to c in order to satisfy the Euler path. We can
see not that we have only two nodes of odd degrees which are a and d with degree 3.
IV. Exercise 1.4 (page 37) exercise 4.a
4a. Let A be the adjacency matrix of an undirected graph. Explain what property of the
matrix indicates that:
The property that makes a graph complete is that all its vertices are connected by an edge. This
can also be stated as if and only if all the elements of its adjacency matrix except those on the
main diagonal are equal to 1, A [I, j] = 1 for every 1 <= i, j <= n, i != j.
The second property exists on the adjacency matrix if and only if its adjacency matrix has an
element equal to 1 on its main diagonal, A [ i, i] = 1 for some 1<= i <= n.
For the final characteristic, an isolated graph (a graph that is undirected and does not contain any
loops) if and only if its adjacency matrix has an all-zero row.
We can see that the element that contains 1 in its main diagonal is c with c. Meaning c loops to
itself.
Part II.
II. Also please write and test an R language program to implement the Euclid’s algorithm
for finding the greatest common divisor of two numbers gcd (a,b).
Euclid’s algorithm for finding the greatest common divisor in R language: