7 vistas

Cargado por Tiatemsu

Student Sample

- tujrr4
- TCC102-Unit1 - Part_2_-_Searching_Sorting_Recursion
- wht
- Chapter Wise
- Nesting Loops, Factorial
- Python Program to Merge Two Lists and Sort It - Sanfoundry
- j15recursion
- Optimal Solutions for the Temporal Precedence Problem
- ExtentReport Review
- quiz02a
- Data Structures
- bsit 41 tb
- Algorithm Analysis Design Lecture4 PowerPoint Presentation
- hw3-soln.pdf
- CAP_2013
- 6 - 4 - Comparators (643)
- Simula PDF File
- The Average Distance in a Random Graph
- Arrays Homework
- DAA FILE

Está en la página 1de 7

1. ___________ is the process of executing a correct program on data sets and measuring the time and space it takes to

compute the results?

Ans. Profiling

2. Define algorithm? What are its properties?

Ans.

An algorithm is a set of instructions that provide step-by-step specifications to perform a task.

The properties of an algorithm are:

Input: Specifies the data set that is applied to the algorithm to check its validity.

Output: Specifies the data set that is produced as a result of the algorithm execution.

Definiteness: Specifies that the instructions described in the algorithm should be well

defined and should not create any ambiguity.

Termination: Specifies that the instructions described in the algorithm must contain a

proper termination condition.

Effectiveness: Specifies that the algorithm take less time and less memory space during

its execution.

3. What is debugging and what is profiling?

Ans.

Debugging is the process of identifying and fixing the errors in a program. Errors in a program can be identified by

executing the program with a sample dataset.

Profiling is the process of measuring the performance of the program by executing it on different data sets. Performance

of a program is measured by recording the time and memory space that the program takes during its execution.

4. One of the properties of an algorithm is beauty (true/false)

Ans. False

Solutions to Chapter Two Questions

1. Give at least 5 real life examples where we use stack operations.

Ans.

The real life examples of stacks are:

Bangles in a hand: The bangles wore in a hand follow last-in-first-out (LIFO) strategy of

stack. The bangle that you wear first is the last one to be taken out while removing all the

bangles from the hand. The bangle that is worn last is the first one to be taken out.

Same circumference circular rings in a pole: The rings having same circumference

placed into a pole also follow LIFO strategy. The topmost ring, which was the last to be

placed in the pole, is the first one to be taken out.

Sacks full of wheat placed one over other: The sack at the top is removed first and the

sack at the bottom is removed last.

The bolts screwed to a single nut: When the bolts are screwed to a single nut, the last

screwed bolt is unscrewed first and the bolt that was screwed first is unscrewed in the last.

Battery cells in a torch: The battery cells in a torch also follow the same LIFO strategy of

stack.

2. Give at least 5 real life examples where queue is used.

Ans.

Real life examples of queue are:

A queue of people at ticket-window: The person who comes first gets the ticket first. The

person who is coming last is getting the tickets in last. Therefore, it follows first-in-first-out

(FIFO) strategy of queue.

Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the

booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out

(FIFO) strategy of queue.

Phone answering system: The person who calls first gets a response first from the phone

answering system. The person who calls last gets the response last. Therefore, it follows

first-in-first-out (FIFO) strategy of queue.

Luggage checking machine: Luggage checking machine checks the luggage first that

comes first. Therefore, it follows FIFO principle of queue.

Patients waiting outside the doctor's clinic: The patient who comes first visits the doctor

first, and the patient who comes last visits the doctor last. Therefore, it follows the first-infirst-

out (FIFO) strategy of queue.

3. Name 10 situations that can be represented by means of graphs. Explain what each vertex and edge represents.

Ans.

The situations that can be represented by means of graphs are:

Example Vertex represents Edge represents

Displacement Position or state Distance between the two vertices

connected through the edge

Shortest Path City Distance between the two vertices

connected through the edge

City Map Places of the city Distance between the two vertices

connected through the edge

Air route Different airports Distance between the two vertices

connected through the edge

Population Growth per

year

Population for an year Variation in the population between the

two vertices connected through the

edge

In mathematical

expressions

Value Conditions and calculations

Daily temperature

report

Temperature for a

specific time

Variation in the temperature between

the vertices connected through the

edge

Child-parent

relationship

Name of the person Relationship

Organization chart Designation Order or flow

Chemical structure Chemical elements Bonding between the elements

4. Draw a connected graph that becomes disconnected when any edge is removed from it.

Ans.

The following figure shows a graph that becomes disconnected when any edge is removed from it:

Disconnected Graph

5. Draw all trees of n labeled vertices for n=1,2,3,4 and 5.

Ans.

The following figures show the trees for various values of n, where n represents the number of vertices in a tree.

For n=1

For n=2

For n =3

There can be a number of trees with 3 labeled vertices. Some of the examples of the trees with 3 labeled vertices are:

For n=4

Again, there can be a number of trees with 4 labeled vertices. Some of the examples of the trees with 4 labeled vertices

are:

For n=5

Again, there can be a number of trees with 5 labeled vertices. Some of the examples of the trees with 5 labeled vertices

are:

6. Sketch all binary trees with six pendent edges.

Ans.

The following figures show binary trees with six pendent edges:

Binary Tree with Six Pendent Edges

The preceding figures, A, B, and C, show three examples of binary trees having six pendent edges. All the binary trees

having six leaf nodes come under this category.

7. Write adjacency and incidence matrix for all the graphs developed.

Ans.

The following tables show adjacency and incidence matrices for the graph of question no. 4:

Solutions to Chapter Three Questions

1. Design and develop algorithms for multiplying n integers.

Hint: Follow the algorithm to add n numbers given in the text

Ans.

Algorithm: Multiply_n_Integers

Input: integers, number of integers to be multiplied (n), loop variable (i)

Output: mul, updated

Method: Display 'Enter the number of elements'

Accept n

Display 'Enter elements one by one'

for (i = 1 to n in steps of 1 do)

Accept a (i)

end_for

mul = 1

for (i = 1 to n in steps of 1 do)

mul = mul*a(i)

end_for

Display 'multiplication of n integers is = ', mul

2. Design and develop an algorithm for finding the middle element in three numbers.

Ans.

To find the middle element one has to first sort the numbers in ascending order. The smallest number becomes the first

element in the sorted list and the largest number becomes the last element in the sorted list.

Consider three numbers 3, 1, and 7.

The smallest number amongst these three is 1; therefore, 1 becomes the first element in the sorted list. Amongst 3 and 7,

3 is the second element. The larger number 7 is left out and becomes the last element. Hence the middle number 3 is

displayed.

Algorithm : Middle_3_Elements

Input: a, b, c the three numbers to be sorted, two temporary variables k1, and K2

Output: Middle element in the three numbers.

Method:

If (a<b)

If (a<c)

If (b<c)

m = b

else

m = c

end if

else

m = a

end if

else

if (b<c)

if (a<c)

m = a

else

m = c

end if

else

m = b

end if

end if

3. Develop an algorithm to find the number of Permutations and Combinations for a given n and r.

Ans.

Permutation of a given number is given by n*(n-1)*(n-2)...up to r factors.

This is a generalized algorithm for n>2

Algorithm: Permutation of a number for a given r

Input: n and r

Output: Permutation of n

Method:

a) per = 1

for (j = n to n - r + 1 in steps of -1 do) //where j is a loop variable

per = per*j

end_for

Display ' Permutation = ', per

b) Combination of a number n for a given r is calculated by

nCr = nPr / r!

Calculate the permutation nPr using the above algorithm

Calculate the factorial for r using the algorithm

fact = 1

for (j =1 to r in steps of 1 do) //where j is a loop variable

fact = fact*j

end_for

comb = per / fact

Display ' Combination =', comb

4. Design an algorithm to generate all prime numbers within the limits l1 and l2.

Ans.

Algorithm: to generate all prime numbers between the limits l1 and l2.

Input: l1 and l2

Output: Prime numbers between l1 and l2

Method:

for (n=l1 to l2 in steps of 1 do)

prime=true

for (i=2 to n/2 in steps of 1 do)

if (n % i =0)

prime = false

break

end_if

end_for

if (prime = true)

Display 'Prime number is =', n

end_for

5. Design an algorithm to find the reverse of a number.

Ans.

Algorithm: Reverse of a number

Input: number

Output: Reverse of a number

Method:

new_number = 0

while (number > 0)

n = number % 10 //n denotes a digit extracted from the number

number = number / 10

new_number = new_number +n

new_number= new_number*10

end while

new_number = new_number/10

Display 'Reverse number is =', new_number

6. A number is said to be a palindrome if the reverse of a number is same as the original. Design an algorithm to check

whether a number is a palindrome or not.

Ans.

Algorithm: check whether the number is a palindrome or not

Input: number, flag

Output: number is a palindrome

Method:

count = 0

while (number > 0)

n = number%10

a(count)=n

count = count+1

end while

half = count/2

palin = true

for (j=1 to half in steps of 1 and k=count to half in steps of 1 do)

if (a (j)! =a(k))

palin = false

break

end if

if (palin = true)

Display 'Number is a palindrome'

else

Display 'Number is not a palindrome'

end if

end for

7. Design an algorithm to check whether a given string is a palindrome or not.

Ans.

Algorithm: check whether the string is a palindrome or not

Input: string, flag

Output: string is a palindrome

Method:

count = 0

while (the next character ch in the string is not empty)

a(count) = ch

count = count+1

end while

half = count/2palin = true

for (i=1 to half in steps of 1 and j=count to half in steps of 1 do)

if (a (i)! =a (j))

palin = false

break

end if

if (palin = true)

Display 'String is a palindrome'

else

Display 'String is not a palindrome' end if

end for

8. Implement all the devised algorithms and also the algorithms discussed in the chapter.

Ans.

You can implement algorithms discussed in this chapter in various languages, such as C++ and Java. However, you need

to know the syntax of the respective language before implementing it.

Solutions to Chapter Four Questions

1. What are the serious shortcomings of the binary search method and sequential search method?

Ans.

A serious shortcoming of the sequential search method is that even if the element that you are trying to search is not

present in the given file, the entire file is searched at least once.

A serious shortcoming of the binary search method is that it can be applied only to a list in which the elements are

arranged in ascending order.

2. Consider a data set of nine elements {10, 30, 45, 54, 56, 78, 213, 415, 500} and trace the linear search algorithm to find

whether the keys 30, 150, 700 are present in the data set or not.

Ans.

In linear search algorithm, each element in the list is compared with the given key element. If any of the elements in the

list is equal to the given key element, the linear search algorithm returns TRUE, else it returns FALSE.

Let us apply linear search to find the key element 30 in the given list.

Take the first element 10 from the list and compare it with the key element 30. Clearly the

two elements are not equal.

Take the next element 30 from the list and compare it with the key element 30. Clearly, the

two elements are equal. The algorithm returns the TRUE value, and the algorithm is

terminated.

Similarly, search for the key elements 150 and 700. At the end of the search, you will find that the key elements 150 and

700 are not found in the list.

3. Trace the binary search algorithm on the same data set and same key elements of problem 2.

Ans.

Binary search algorithm can be applied only to the sorted list of elements. Lets first apply the binary search algorithm to

find the key element 30 in the following list taken from problem 2:

Take the middle element from the list and compare it with the key element. Clearly, the middle element 56 > key element

30. As the key element is smaller than the middle element, the key element can only be present in the left sub list that is

as follows:

Again, take the middle element from this list and compare it with the key element. Clearly, the middle element 45 > key

element 30. As the key element is smaller than the middle element, the key element can only be present in the left sub

list that is as follows:

Again, take the middle element from this list and compare it with the key element. Clearly middle element 30= key

element 30, therefore, the binary search algorithm returns a TRUE value and the algorithm is terminated.

Similarly, search for the key elements 150 and 700. At the end of the search, you will find that the key elements 150 and

700 are not found in the list.

4. Try to know more sorting techniques and make a comparative study of them.

Ans.

There are various sorting techniques, such as bubble sort, quick sort, and shell sort. Each of these sorting techniques is

defined as follows:

Bubble sort: In the bubble sort technique two elements are compared at a time and if the

two elements are not in ascending order these elements are interchanged. This process is

repeatedly performed throughout the given list until the list is completely sorted. To sort a

list of n elements using bubble sort you need to make a total of (n-1)2 comparisons.

Quick sort: The basic idea underlying quick sort is to allow a specific element 'a' within the

list 'x' to find its proper position 'j'. The proper position 'j' is found such that it satisfies the

following two conditions:

The elements on the left hand side of position 'j' are all smaller than or equal to 'a'

The elements on the right hand side of position 'j' are all greater than or equal to 'a'

If 'a' satisfies these two conditions, then 'a' is the jth smallest element in the list and 'a' is placed at jth position in the finally

sorted list. This process is then repeated for sub arrays x [0..j-1] and x [j+1..n-1].

Shell sort: In shell sort, the given list x is divided into sub lists containing every kth element

of the given list. For example, if k=5 then one sub list contains x [0], x [5], x [10]..., another

sub list contains x [1], x [6], x [11]..., and so on. The elements of these sub lists are then

compared two at a time and if the two elements are not in ascending order, these elements

are interchanged.

Now, a new value of k is chosen which is smaller than the previous value of k and the process is repeated again. This

process is repeated until the value of k is set to 1 so that the sub list consisting of the entire list is sorted.

5. Hand simulate Insertion Sort on the data set {13, 45, 12, 9, 1, 10, 40}

Ans.

Let us apply Insertion Sort algorithm on the given list:

Note

The figure 4.1 given on page 41 of the book is incorrect. It shows the steps for

straight selection sort.

6. Implement all the algorithms designed in the chapter.

You can implement algorithms discussed in this chapter in various languages, such as C++ and Java. However, you need

to know the syntax of the respective language before implementing it.

Solutions to Chapter Five Questions

1. Trace out the algorithm Merge Sort on the data set {1,5,2,19,4,17,45,12,6}

Ans.

Steps to perform Merge Sort on the data set {1,5,2,19,4,17,45,12,6} are:

(1,5,2,19,4}{17,45,12,6)

((1,5,2) (19,4)) ((17,45)(12,6))

(((1,5) (2)) ((19)(4))) (((17)(45)) ((12) (6)))

((((1) (5)) (2)) ((19)(4))) (((17)(45)) ((12) (6)))

(1,5) (2) (4,19) (17,45) (6,12)

(1,2,5) (4,19) (17,45) (6,12)

(1,2,4,5,19) (6,12,17,45)

(1,2,4,5,6,12,17,19,45)

2. Trace out the algorithm Quick Sort on the data set {12,1,5,7,19,15,8,9,10}.

Ans.

Steps to perform Quick Sort on the data set{12,1,5,7,19,15,8,9,10} are:

{(12),1,5,7,19,15,8,9,10}

{(12),1,5,7,10,15,8,9,19}

{(12),1,5,7,10,9,8,15,19}

{8,1,5,7,10,9,(12),15,19}

{(7),1,5,8,10,9}{12}{(15),19}

{5,1,(7),8,10,9}{12}{15,19}

{(5),1}{7}{(8),10,9}{12}{15}{19}

{1,(5)}{7}{(8),9,10}{12}{15}{19}

{1,(5)}{7}{(8),9,10}{12}{15}{19}

{1}{5}{7}{8}{(9),10}{12}{15}{19}

{1}{5}{7}{8}{9}{10}{12}{15}{19}

3. Implement all the algorithms designed in this chapter.

Ans.

You can implement algorithms discussed in this chapter in various languages, such as C++ and Java. However, you need

to know the syntax of the respective language before implementing it.

4. Trace out the algorithm MaxMin on a data set consisting of at least 8 elements.

Ans.

Steps to perform MaxMin on a data set (2,4,6,3,8,1,9,7) are:

(2,4,6,3) (8,1,9,7)

((2,4)(6,3)) ((8,1)(9,7))

In sublist (4,6), max is 6 and min is 4. In sublist (8,9), max is 9 and min is 8.

Comparing max and min values of sublist (2,4) and sublist (6,3), value of max is 6 and min is 2.

Therefore, for sublist (2,4,6,3) max is 6 and min is 2.

Similarly, comparing max and min values of sublist (8,1) and sublist (9,7), value of max is 9 and min is 1.

Therefore, for sublist (8,1,9,7) max is 9 and min is 1.

Finally, comparing max and min values of sublist (2,4,6,3) and sublist (8,1,9,7), value of max is 9 and min is 1.

5. List out the merits and the demerits of recursion.

Ans.

Merits of recursion are:

Mathematical functions, such as fibonacci series generation can be easily implemented using recursion as compared to

iteration technique.

Demerits of recursion are:

Many programming languages do not support recursion; hence, recursive mathematical function is implemented using

iterative methods.

Even though mathematical functions can be easily implemented using recursion, it is always at the cost of execution time

and memory space.

The recursive programs take considerably more storage and take more time during processing.

6. The data structure used by recursive algorithms is ______.

Ans.

Stack

7. When is it appropriate to use recursion?

Ans.

Recursion is used for repetitive computations in which each action is stated in terms of previous calculation results.

Solutions to Chapter Six Questions

1. What is a binary tree?

Ans.

A binary tree is made up of nodes where each node consists of three elements, left node, right node and a data element.

The root node is the topmost node of the binary tree.

2. Differentiate between complete and full binary trees?

Ans.

The following table lists the differences between complete binary trees and full binary trees:

Complete binary trees Full binary trees

All the nodes at the previous level are

fully accommodated before the next level

is accommodated.

All levels are maximally accommodated.

Number of nodes at the last (n) level may

or may not equal to 2n.

Number of nodes at the last (n) level is exactly

equal to 2n.

Leaf nodes may or may not be at the

same level.

All leaf nodes are at the same level.

A complete binary tree may or may not

be full binary tree.

A full binary tree is always a complete binary

tree.

3. What is the maximum number of nodes in the binary tree of level 7, 8, and 9?

- tujrr4Cargado porKatraj Nawaz
- TCC102-Unit1 - Part_2_-_Searching_Sorting_RecursionCargado porapi-26781128
- whtCargado porSandeep Arora
- Chapter WiseCargado porhbgfjghjj hhfh
- Nesting Loops, FactorialCargado porWilla Marie
- Python Program to Merge Two Lists and Sort It - SanfoundryCargado porRavinderRavi
- j15recursionCargado porKule Anthony
- Optimal Solutions for the Temporal Precedence ProblemCargado porJamesHowson
- ExtentReport ReviewCargado porshaikameermalik
- quiz02aCargado poraucyaucy123
- Data StructuresCargado porsumanta_76394
- bsit 41 tbCargado porsudip dahal
- Algorithm Analysis Design Lecture4 PowerPoint PresentationCargado porPedro Ped
- hw3-soln.pdfCargado porPhilip Mak
- CAP_2013Cargado porrakesh_200003
- 6 - 4 - Comparators (643)Cargado poreshwaar1
- Simula PDF FileCargado porqwertykavi
- The Average Distance in a Random GraphCargado porprem
- Arrays HomeworkCargado porahmed
- DAA FILECargado porJishnu Asok
- Paths with Jumps: Definition, Topology-preserving Dynamics, and ApplicationsCargado porapjeas
- TreePlan-204-Example-Windows.xlsxCargado porRyu Kurosaki
- Big-O Algorithm Complexity Cheat SheetCargado porMìrägé Σ∞ Ãçê
- Make Tree From Infix ExpressionCargado porCosma Aurelian
- Lec05 Uninformed SearchCargado pornayanisateesh2805
- 5 Algorithms GraphCargado porsaimanobhiram
- 10.1.1.464.3408_voronoiCargado porsoumya mondal
- Multi-Agent Influence Diagrams for Representing and Solving GamesCargado porMichael Rogers
- 1329389726_A1Cargado porNorliyana Idris
- C,C++Cargado porapi-3760815

- Java SampleCargado porTiatemsu
- Software Engineering SampleCargado porTiatemsu
- Unix and Shell Programming SampleCargado porTiatemsu
- bsiit 41Cargado porKajal Sharma
- Solutions to Chapter One Questions AlgorithmCargado porTiatemsu
- aCargado porSteve John
- bsiit 41Cargado porKajal Sharma
- Kuvempu Univeristy BSc(IT) 4th semester exercise answer (Unix & Shell Programming 43)Cargado porswobhagya
- AlgorithmCargado porTiatemsu

- Existence of MatchingsCargado porRoozbeh Bassirian
- 12 Beautiful Mathematical Theorems With Short ProofsCargado porhumejias
- Lowell W. Beineke & Robin J. Wilson & Peter J. Cameron - Topics in Algebraic Graph TheoryCargado porÖzkan Yılmaz
- MG SEM 1Cargado porSuresh Kumar
- Combinatorial NotesCargado porPerry01
- 59766045-II-b-tech-Cse-r10-Jntuk-Syllabus.docCargado porChristina Robinson
- qb2018_EEM303Cargado porShivam Gautam
- Nr 211502 Applied Systems EngineeringCargado porSrinivasa Rao G
- Acm Cheat SheetCargado porIsrael Marino
- 0898719909 GraphCargado porNirmanaFernando
- Trees NetworksCargado porAugusto Cabrera
- Armiet AOA Lab ManualCargado porAmit Dubey
- Total Resolving Set - ProfCargado porKhusnulKhotimahImnida
- operativa 2 lect1Cargado porBoris Polanco
- Competitive Programming 1Cargado porVipul Verma
- Discrete Mathematics MCQ'SCargado porGuruKPO
- chapter 3Cargado porSalman Ilyas
- CS 1151 Data StructuresCargado poranon-845033
- Chapter 8Cargado portbijle
- The Algorithms of Kruskal and PrimCargado porGabriela Matei
- Lecture1_2ppCargado porMrZaggy
- The Hitchhiker’s Guide to the Programming ContestsCargado pordpiklu
- CSC263 Spring 2011 Assignment 4Cargado poryellowmoog
- Hausdorff-DvoretzkyCargado porEduardo Carey
- Unit7-CMCargado porPrateekMandi
- Dynamics 1Cargado porRima Ghosh
- Competitive Programming 2Cargado porMatias Reyes Mora
- Computer Science SylabusCargado porJagannath Jaggu
- Kruskal algorithmCargado porGowdham Prabhakar
- Absolutely Harmonious Labeling of GraphsCargado porDon Hass