# assignment4 ```Name : Ismael Al-safadi
Student Number : 120160618
Data Structure and Algorithms
Assignment #4
Islamic University OF Gaza
Give man n*n array like n*n matrix , so
1) first of all check if the matrix has just an element Using if statement , (if r ==0 and c ==0) r is
rows and c is columns now return the first index in matrix using this expression (matrix)
2) Then, check if columns == 0 , then return the elements in row 0 + call function with rows – 1
3) Now after checking , return elements with specific index (matrix[r][c]) , + the call of function
with c -1 .
4) Note that , c is the number of columns , r is the number of rows , if we say the matrix n*n is 3*3
size so r = 3 and c = 3 .
Here is an implementation using python .
Execution output :
Iterative:
Execution output :
Recursive:
Execution output :
I was tried maximum value of long number in java it will calculate it by less than minute in iterative , but it
recursive I get an error ( stackoverflow error )
Algorithm Brute force , ( complexity O(n^2))
-
Calculate the number of substrings starts with M and ends with T
Input , array of chars of all main string Ch is the name of char array
Output number of substring in main string
Count = 0
For I  0 to n-2 do
If (Ch[i] == M)do
For j  I to n-1 do
If (Ch[j] == T)do
Count = count +1
End if
End for
End if
End for
Return count
Another Way , more effective way … O(n)
Algorithm string matching
-
Declare two counters , one for number of M’s and another for substring start with M and end with T
Input , array of chars , name of array is Ch
Output number of substring
Algorithm in the next page …
Count_M = 0 , count_total = 0 ;
For I  0 to n-1 do
If (Ch[i] == M ) do
Count_M = count_M+1
End if
If (Ch[i] == T ) do
Count_total = count+total + count_A
End if
End for
Return count_total
A
Selection of one element in each row of the matrix
…
B
Consider the knapsack of capacity W=90 and three items i1,i2 and i3 with items weight w={50,80,88} and
items value v={200,250,260}
The value by weight metric m={200/50, 250/80,260/88} = {4,3.125,2.954}
If we use the algorithm of knapcask then we will get 200 , 250 ,260 by order and this is the worest
case of solution
Here is test using python :
8
-
-
If S is odd, that’s mean there is no solution.
-
If S is even, generate all subsets until either a subset whose elements sum is S/2 is encountered or no more
subsets are left.
-
Algorithm Binarystring,
Input n is any positive int number
Output , all bit string of n length into array let name (A)
Peodocode :
If n == 0 do
Print array or binary_data(A)
Else do
A[n-1]  0 , Binarystring(n-1)
A[n-1] , Binarystring(n-1)
End of pesodocode
Here is an implementation using python
8:
Algorithm generatePrintBinary
-
Input , n int positive number the last number in binary sequence that we will create
Output : all binary numbers from 0 to n
Algorithm
Print 0 is the first number in every case whatever n
Then enqueue 1 into Queue
Make loop , while n &gt; 0 do
Dequeue and store into var S1
Another var S2 = S1( assign the value of s1 to s2 )
Now enqueue s1+&quot;0&quot; and enqueue s2+&quot;1&quot;
Then n = n-1
Here is an implementation using python and execution output .
8
9:
N= 2
Check value in if statement , if it’s false go to else block
HeapPermute(2-1) = HeapPermute(1) . so only one following loop execute either if block or else block
If n = 2 not odd then swap A[i] and A[n] 1 2 will be 2 1
Print the result
N= 3
Note , use the previous steps to follow the n = 3 and n = 4
123 after swapping will be 213
312 after swapping will be 132
231 after swapping will be 321
N =4
All result after swapping will be like this
As in (N =2 ) it will check every time in loop then if odd swap A and A[n]
If even swap A[i] and A[n] …
10:
-
if n = 1 then the problem solved
if n &gt; 1 do
scan the array of n-1 teams
insert the team who is not added in the group right before the first team on array which lost to it in the
contest
-
if team is not found (if it is not yet on the array lost every game to the teams on the array then add that
team at the last or the array
time complexity n log n .
Here another idea to do that , I found it online .. 7
```