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

Lab07 DSA

Uploaded by

Aman Khan
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)
18 views

Lab07 DSA

Uploaded by

Aman Khan
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/ 4

Computer Science, Usman Institute of Technology

CS211 - Data Structures and Algorithms

Lab 07

Instructor: Dr. Sharaf Hussain


E-mail: [email protected]
Semester: Fall, 2021

1 Objective
The purpose of this lab session is to implement Linked List data structure and its applications.

2 Instructions
You have to perform the following tasks yourselves. Raise your hand if you face any difficulty in
understanding and solving these tasks. Plagiarism is an abhorrent practice and you should not
engage in it.

3 How to Submit
• Submit lab work in a single .py file on Google Classroom. (No other format will be accepted)
• Lab work file name should be saved with your roll number (e.g. 19a-001-SE_LW04.py)

• Submit home work in a single .py file on Google Classroom. (No other format will be accepted)
• Lab work file name should be saved with your roll number (e.g. 19a-001-SE_HW04.py)

Task 1 - Sparse Matrix


Implement following Sparse Matrix using linked list data structure. Implement subtract, multiply,
and transpose methods in the code.
# I m p l e m e n t a t i o n of the S p a r s e M a t r i x ADT u s i n g an a r r a y of l i n k e d l i s t s
.
from C h a p t e r 2 . m y a r r a y i m p o r t A r r a y

class SparseMatrix :
# C r e a t e s a s p a r s e m a t r i x of size n u m R o w s x n u m C o l s i n i t i a l i z e d to 0.
def _ _ i n i t _ _ ( self , numRows , n u m C o l s ) :
self . _ n u m C o l s = n u m C o l s
self . _ l i s t O f R o w s = A r r a y ( n u m R o w s )

# R e t u r n s the n u m b e r of rows in the m a t r i x .


def n u m R o w s ( self ) :
r e t u r n len ( self . _ l i s t O f R o w s )

# R e t u r n s the n u m b e r of c o l u m n s in the m a t r i x .
def n u m C o l s ( self ) :
r e t u r n self . _ n u m C o l s

# R e t u r n s the v a l u e of e l e m e n t ( i , j ) : x [ i , j ]
def _ _ g e t i t e m _ _ ( self , n d x T u p l e ) :
row = n d x T u p l e [0]
col = n d x T u p l e [1]
Computer Science, Usman Institute of Technology

p r e d N o d e = None
c u r N o d e = self . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None and c u r N o d e . col != col :
predNode = curNode
c u r N o d e = c u r N o d e . next
if c u r N o d e is not None and c u r N o d e . col == col :
return curNode . value

# Sets the v a l u e of e l e m e n t ( i , j ) to the v a l u e s : x [ i , j ] = s


def _ _ s e t i t e m _ _ ( self , ndxTuple , v a l u e ) :
row = n d x T u p l e [0]
col = n d x T u p l e [1]
p r e d N o d e = None
c u r N o d e = self . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None and c u r N o d e . col != col :
predNode = curNode
c u r N o d e = c u r N o d e . next

# See if the e l e m e n t is in the list .


if c u r N o d e is not None and c u r N o d e . col == col :
if v a l u e == 0.0 : # r e m o v e the node .
if c u r N o d e == self . _ l i s t O f R o w s [ row ] :
self . _ l i s t O f R o w s [ row ] = c u r N o d e . next
else :
p r e d N o d e . next = c u r N o d e . next
else : # c h a n g e the node ’ s v a l u e .
curNode . value = value

# Otherwise , the e l e m e n t is not in the list .


elif v a l u e != 0.0 :
n e w N o d e = _ M a t r i x E l e m e n t N o d e ( col , v a l u e )
n e w N o d e . next == c u r N o d e
if c u r N o d e == self . _ l i s t O f R o w s [ row ] :
self . _ l i s t O f R o w s [ row ] = n e w N o d e
else :
p r e d N o d e . next = n e w N o d e

# S c a l e s the m a t r i x by the g i v e n s c a l a r .
def s c a l e B y ( self , s c a l a r ) :
for row in r a n g e ( self . n u m R o w s () ) :
c u r N o d e = self . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None :
c u r N o d e . v a l u e *= s c a l a r
c u r N o d e = c u r N o d e . next

# C r e a t e s and r e t u r n s a new m a t r i x that is the t r a n s p o s e of this


matrix .
def t r a n s p o s e ( self ) :
...

# M a t r i x a d d i t i o n : n e w M a t r i x = self + r h s M a t r i x .
def _ _ a d d _ _ ( self , r h s M a t r i x ) :
# Make sure the two m a t r i c e s have the c o r r e c t size .
a s s e r t r h s M a t r i x . n u m R o w s () == self . n u m R o w s () and \
r h s M a t r i x . n u m C o l s () == self . n u m C o l s () , \
" M a t r i x s i z e s not c o m p a t a b l e for a d d i n g . "

# C r e a t e a new s p a r s e m a t r i x of the same size .


n e w M a t r i x = S p a r s e M a t r i x ( self . n u m R o w s () , self . n u m C o l s () )

# Add the e l e m e n t s of this m a t r i x to the new m a t r i x .


for row in r a n g e ( self . n u m R o w s () ) :
c u r N o d e = self . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None :
n e w M a t r i x [ row , c u r N o d e . col ] = c u r N o d e . v a l u e
c u r N o d e = c u r N o d e . next

# Add the e l e m e n t s of the r h s M a t r i x to the new m a t r i x .


for row in r a n g e ( r h s M a t r i x . n u m R o w s () ) :
c u r N o d e = r h s M a t r i x . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None :
v a l u e = n e w M a t r i x [ row , c u r N o d e . col ]
v a l u e += c u r N o d e . v a l u e
Computer Science, Usman Institute of Technology

n e w M a t r i x [ row , c u r N o d e . col ] = v a l u e
c u r N o d e = c u r N o d e . next

# R e t u r n the new m a t r i x .
return newMatrix

# - - - M a t r i x s u b t r a c t i o n and m u l t i p l i c a t i o n - - -
def _ _ s u b _ _ ( self , r h s M a t r i x ) :
...
# def _ _ m u l _ _ ( self , r h s M a t r i x ) :
...

# S t o r a g e c l a s s for c r e a t i n g m a t r i x e l e m e n t n o d e s .
class _MatrixElementNode :
def _ _ i n i t _ _ ( self , col , v a l u e ) :
self . col = col
self . v a l u e = v a l u e
self . next = None

Task 2 - Polynomial Equation


Implement following Polynomial Equation using linked list data structure. Implement subtract, mul-
tiply, and evaluation methods in the code.
# I m p l e m e n t a t i o n of the P o l y n o m i a l ADT u s i n g a s o r t e d l i n k e d list
class Polynomial :
def _ _ i n i t _ _ ( self , d e g r e e = None , c o e f f i c i e n t = None ) :
if d e g r e e is None :
self . _ p o l y H e a d = None
else :
self . _ p o l y H e a d = _ P o l y T e r m N o d e ( degree , c o e f f i c i e n t )
self . _ p o l y T a i l = self . _ p o l y H e a d

def d e g r e e ( self ) :
if self . _ p o l y H e a d is None :
r e t u r n -1
else :
r e t u r n self . _ p o l y H e a d . d e g r e e

def _ _ g e t i t e m _ _ ( self , d e g r e e ) :
a s s e r t self . d e g r e e () >= 0 , \
" O p e r a t i o n not p e r m i t t e d in e m p t y p o l y n o m i a l . "
c u r N o d e = self . _ p o l y H e a d
w h i l e c u r N o e is not None and c u r N o d e . d e g r e e >= d e g r e e :
c u r N o d e = c u r N o d e . next
if c u r N o d e is None or c u r N o d e . d e g r e e != d e g r e e :
r e t u r n 0.0
else :
return curNode . degree

def e v a l u a t e ( self , s c a l a r ) :
pass

def _ _ a d d _ _ ( self , r h s P o l y ) :
a s s e r t self . d e g r e e () >= 0 and r h s P o l y . d e g r e e () >= 0 , \
" A d d i t i o n only a l l o w e d in non - e m p t y P o l y n o m i a l s "
n e w P o l y = P o l y n o m i a l ()
n o d e A = self . _ p o l y H e a d
n o d e B = self . _ p o l y H e a d

w h i l e n o d e A is not None and n o d e B is not None :


if n o d e A . d e g r e e > n o d e B . d e g r e e :
degree = nodeA . degree
value = nodeA . coefficient
n o d e A = n o d e A . next
elif n o d e A . d e g r e e < n o d e B . d e g r e e :
degree = nodeB . degree
value = nodeB . coefficient
n o d e B = n o d e B . next
Computer Science, Usman Institute of Technology

else :
degree = nodeA . degree
value = nodeA . coefficient + nodeB . coefficient
# adds when d e g r e e is same
n o d e A = n o d e A . next
n o d e B = n o d e B . next
n e w P o l y . _ a p p e n d T e r m ( degree , v a l u e )

w h i l e n o d e A is not None :
n e w P o l y . _ a p p e n d T e r m ( n o d e A . degree , n o d e A . c o e f f i c i e n t )
n o d e A = n o d e A . next
# if the list i t s e l f is l o n g e r
w h i l e n o d e B is not None :
n e w P o l y . _ a p p e n d T e r m ( n o d e B . degree , n o d e B . c o e f f i c i e n t )
n o d e B = n o d e B . next
return newPoly

def _ _ s u b _ _ ( self , r h s P o l y ) :
pass

def _ _ m u l _ _ ( self , r h s P o l y ) :
pass

# H e l p e r m e t h o d for a p p e n d i n g t e r m s in the p o l y n o m i a l
def _ a p p e n d T e r m ( self , degree , c o e f f i c i e n t ) :
if c o e f f i c i e n t != 0.0:
n e w T e r m = _ P o l y T e r m N o d e ( degree , c o e f f i c i e n t )
if self . _ p o l y H e a d is None :
self . _ p o l y H e a d = n e w T e r m
else :
self . _ p o l y T a i l . next = n e w T e r m
self . _ p o l y T a i l = n e w T e r m

class _PolyTermNode ( object ):


def _ _ i n i t _ _ ( self , degree , c o e f f i c i e n t ) :
self . d e g r e e = d e g r e e
self . c o e f f i c i e n t = c o e f f i c i e n t
self . next = None

You might also like