Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following algorithm that takes two sorted lists of integers ( A [ 1 ] , dots, A [ n ] ) and (

Consider the following algorithm that takes two sorted lists of
integers (A[1],dots,A[n]) and (B[1],..B[m]), and tests whether there
are elements A[i] and B[j] so that |A[i]-B[j]|3.
For example if the input is: A[1..6]=(40,72,75,101,123,184) and
B[1..6]=(13,17,33,38,94,142), the algorithm returns True
because |40-38|3.
procedure CloseMatch (A[1],.., A[n] ; B[1],.., B[m]) :
J=1;I=1;
. While Jm and In do:
IF|A[I]-B[J]|3 Return True.
ELSEJ=J+11in1jm|A[i]-B[j]|3IiJjIJt+1(It,Jt,It+1,Jt+1)|A[It]-B[Jt]|3A[It]>B[Jt]+3A[It]>B[Jt]+3It+1Jt+1A[It]>B[Jt]+3vnmJt.
Q1.5
Induction step: For the case A[It]>B[Jt]+3, what are
the values ofIt+1 and Jt+1?
Q1.6
Induction step: Show that the invariant holds for the
case A[It]>B[Jt]+3. You can use what you proved in
iv. and v.
Q1.7
Use the invariant to prove that the algorithm is correct.
(Youdo not need tore-prove the invariant here, just use
it.)
Q1.8
Give a time analysis for this algorithm upto order in
terms ofn and m. Explain your answer referring to the
algorithm.IFA[I]
ELSEJ=J+1
6. Return False.
This algorithm has the following loop invariant: Assume
there are 1in and 1jmso that |A[i]-B[j]|3.
Then if CloseMatch hasn't terminated, Ii and Jj.
Below, you'll give most of the proof of this loop invariant.
We've omitted one case in the interest of time.
Q1.1
Base case: What are the initial values of the relevant
variables and why do these satisfy the invariant?
Q1.2
State the induction hypothesis and goal of the
induction step in terms of the values of the variables I
and J before and after the t+1'st iteration
(It,Jt,It+1,Jt+1)
Q1.3
Induction step: For the case |A[It]-B[Jt]|3, show
that the invariant remains true by explaining what
happens in the next iteration.
Q1.4
Induction step: For the case A[It]>B[Jt]+3, show
that Jt.
Q1.5
Induction step: For the case A[It]>B[Jt]+3, what are
the values ofIt+1 and Jt+1?
Q1.6
Induction step: Show that the invariant holds for the
case A[It]>B[Jt]+3. You can use what you proved in
iv. and v.
Q1.7
Use the invariant to prove that the algorithm is correct.
(Youdo not need tore-prove the invariant here, just use
it.)
Q1.8
Give a time analysis for this algorithm upto order in
terms ofn and m. Explain your answer referring to the
algorithm.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions