0% found this document useful (0 votes)
65 views

Assignment 1

The document contains answers to 4 questions on algorithms. Question 1 involves implementing quicksort in Python to sort a sample array. Question 2 discusses different graph representation methods and implements adjacency lists for a 4 vertex graph. Question 3 implements coin change problem for Pakistani currency. Question 4 provides pseudo-code for Huffman coding compression and decompression routines.

Uploaded by

Umer ShreEf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Assignment 1

The document contains answers to 4 questions on algorithms. Question 1 involves implementing quicksort in Python to sort a sample array. Question 2 discusses different graph representation methods and implements adjacency lists for a 4 vertex graph. Question 3 implements coin change problem for Pakistani currency. Question 4 provides pseudo-code for Huffman coding compression and decompression routines.

Uploaded by

Umer ShreEf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 1 – Design and Analysis of Algorithms

M. Umar Sharif – 1612213

Q1: Implement the code for inline quick sort algorithm and sort the following data: [2 8 7 1 3 5 6 4]

Ans1) Python Code

def quick_sort(A):
quick_sort2(A,0,len(A)-1)

def quick_sort2(A, low, hi):


if low < hi:
p = partition(A, low ,hi)
quick_sort2(A, low, p-1)
quick_sort2(A, p+1,hi)

def get_pivot(A,low,hi):
mid = (hi + low) // 2
pivot = hi
if A[low] < A[mid]:
if A[mid] < A[hi]:
pivot = mid
elif A[low] < A[hi]:
pivot = low
return pivot

def partition(A,low,hi):
pivotIndex = get_pivot(A,low,hi)
pivotValue = A[pivotIndex]
A[pivotIndex],A[low] = A[low], A[pivotIndex]
border=low
for i in range(low,hi+1):
if A[i] < pivotValue:
border += 1
A[i],A[border]=A[border],A[i]
A[low],A[border]=A[border],A[low]
return (border)

A = [2,8,7,1,3,5,6,4]
quick_sort(A)
print A
Execution

Q2: Identify all formats for graph representation and storage (at least one other than the two taught
in course). Implement Adjacency list method. Show output for a 4 vertex graph in which each vertex is
connected to other (use linked list already provided by the language).

Ans2) Types of representations :

1. Adjacency Matrix
2. Adjacency List
3. Incidence Matrix
4. Incidence List
class AdjNode:
def __init__(self, data):
self.vertex = data
self.next = None

class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [None] * self.V

def add_edge(self, src, dest):

node = AdjNode(dest)
node.next = self.graph[src]
self.graph[src] = node

node = AdjNode(src)
node.next = self.graph[dest]
self.graph[dest] = node

def print_graph(self):
for i in range(self.V):
print("Adjacency list vertex {}\n head".format(i), end="")
temp = self.graph[i]
while temp:
print(" - {}".format(temp.vertex), end="")
temp = temp.next
print(" \n")

if __name__ == "__main__":
V=4
graph = Graph(V)
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(0, 3)
graph.add_edge(1, 2)
graph.add_edge(1, 3)
graph.add_edge(2, 3)
graph.print_graph()
Q3: Implement the coin-changing problem using your favorite programming language for Pakistani
currency (Use single rupee as minimum denomination). Run the program for Rs. 1988 and display
each denomination currency required.

Ans3)
a = [5000,1000,500,100,50,20,10,5,2,1]
z = 1988
c=0
for q in a:
c=z/q
z=z-(c*q)
print str(q) + " notes needed = "+ str(c)
Q4: Write the pseudo-code for compression and decompression routines of Huffman Coding. Show
the encoding and decoding of the following sample text: “Time complexity of Huffman Coding is O(n
log n)”

Ans4)
Compression
Procedure Huffman(C): // C is the set of n characters and related information
n = C.size
Q = priority_queue()
for i = 1 to n
n = node(C[i])
Q.push(n)
end for
while Q.size() is not equal to 1
Z = new node()
Z.left = x = Q.pop
Z.right = y = Q.pop
Z.frequency = x.frequency + y.frequency
Q.push(Z)
end while
Return Q

Decompression
Procedure HuffmanDecompression(root, S): // root represents the root of Huffman Tree
n := S.length // S refers to bit-stream to be decompressed
for i := 1 to n
current = root
while current.left != NULL and current.right != NULL
if S[i] is equal to '0'
current := current.left
else
current := current.right
endif
i := i+1
endwhile
print current.symbol
endfor

You might also like