DSA ASSIGNMENT 1 and 2
DSA ASSIGNMENT 1 and 2
ASSIGNMENT NO # 01
Q1) Describe how the use of efficient data structures & algorithms can be helpful in the selected
field. The description should not be less than 150 words and more than 200 words.
ANSWER: In order to be able to store and retrieve information, a data structure is an
arrangement of data. A data structure is typically some sort of physical representation of an
ADT. Through data structure and algorithm we do everything like automation, modeling,
computer system etc. Programming is all about structures and algorithms for knowledge. Data
structures and algorithms go through comprehensive solutions to standard issues and provide you
with an insight into how easy it is to use each of them. It also teaches us the science of measuring
an algorithm's effectiveness. This helps you to select the best of several options. We sort a lot of
complex problem of real world and in modeling and simulation there is so much use of it. It is
possible to use various modelling algorithms in modelling and simulation. For example, in order
to analyze the data, the analyst may use unsupervised approaches. If an interesting group or
relationship is established, then it is possible to establish and use a supervised learning method to
find new cases. In fraud detection, an instance of this method can be seen.
Q2) Discuss, with concrete reasoning, at least two cases where concepts of data structures &
algorithms can be specifically applied in this field. Each case should not be less than 250 words
and not more than 300 words.
REFRENCES:
1. https://www.anylogic.com/use-of-simulation/
2. https://lib.dr.iastate.edu/cgi/viewcontent.cgi?article=1136&context=cs_techreports
3. http://vr.me.ncku.edu.tw/courses/cg95/download/CG7.pdf
4. https://link.springer.com/chapter/10.1007/978-3-642-83539-1_1
ASSIGNMENT NO # 02
Question 1 Following is an algorithm for Sorting an array:
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
Procedure: SORT(A, N)Where A is the array with N elements. This procedure sorts the array A having N
elements.
IMPLEMENTATION:
PASS 1:
12 23 56 38 19 45
12 23 56 38 19 45
12 23 38 56 19 45
12 23 38 19 56 45
12 23 38 19 45 56
PASS 2:
12 23 38 19 45 56
12 23 38 19 45 56
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
12 23 19 38 45 56
12 23 19 38 45 56
12 23 19 38 45 56
PASS 3:
12 23 19 38 45 56
12 19 23 38 45 56
12 19 23 38 45 56
PASS 4:
12 19 23 38 45 56
12 19 23 38 45 56
PASS 5:
12 19 23 38 45 56
Question 2) Let C denotes the number of times LOC is updated to find the largest element
in array A with N elements. Write a procedure COUNT(A, N, C)which finds C.
ANSWER:
ALGORITHM:
STEP 1: START [Initialize] Set LOC=1 LARGEST=A[1]
STEP 2: Repeat STEP 3 and 4 for C=1 to C<=N, then:
STEP 3: If LARGEST< A[C] then :
LOC = C , LARGEST = A[C]
[ end of if structure ]
STEP 4: Set C++
[ end of Step 2 ]
STEP 5: Write C , LARGEST
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
STEP 6: END
Question 3) Write a procedure INSERT (P, T, LOC, R, S)that inserts pattern P in string T
having lengths R and S respectively, at location LOC.
ANSWER:
ALGORITHM:
STEP 1: [Initialize] Set T: =text, LOC: =position and P: =pattern
STEP 2: Set R:= SUBSTRING (T, 1, LOC-1)
STEP 3: Set S:= SUBSTRING (T, K, LENGTH (T)-LOC+1)
STEP 4: [Concatenate] Set P2:=R||P||S
STEP 5: Write: P2
STEP 6: Exit
Question 4) Can stacks be used to implement queues? If yes, show how to implement a
queue using stacks and describe the working of your algorithms (enqueue and dequeue)
using appropriate diagrams.
ANSWER: Yes, stack can be used to implement queues. We uses two stacks to implement
queues first we added element in first stack (stack_1) then then pop (removing) the element and
pushed (added) in second stack (stack_2) thus in result it reverses the arrangement (order) of
element and forms a Queue. We will discuss and describe with the help of diagram to understand
enqueue and dequeue method.
ALGORITHM [ENQUEUE(ITEM)]:
STEP 1: START [initialize S1 and S2]
STEP 2: Repeat STEP 3 While S1 != empty
STEP 3: push S1 to S2.TOP and pop S1
[end of loop]
STEP 4: push ITEM to S1
STEP 5: Repeat STEP 6 While S2 != empty
STEP 6: push S2 to S1.TOP and pop S2
[end of loop]
STEP 7: EXIT
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
4
Rear Front
3
Stack_1 Stack_2
ENQUEUE
ALGORITHM [DEQUEUE]:
STEP 1: START [initialize S1 and S2]
STEP 2: [Check] if S1 = empty
Write “queue is empty” and return
STEP 3: Then;
ITEM= S1 TOP
STEP 4: pop S1 and Return ITEM
STEP 5: END
Push pop
pop
1
Rear Front
2
Stack_1 Stack_2
DEQUEUE
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
1)
2)
3)
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
4)
5)
6)
7)
8)
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
9)
10)
11)
12)
13)
ABDUL WASAY
DR.PROF.Ghazenfer khan 19B-009-TE
14)
15)
TREE DIAGRAM: