Discrete Structure Lab Work
Discrete Structure Lab Work
Tribhuvan University
Faculty of Humanities and Social Sciences
Master in Computer Application
Submitted to
Bishnu Khadka
Department of Computer Application
Patan Multiple Campus
Patan Dhoka, Lalitpur
Submitted by
Yubraj Khatiwada
MCA 1st Semester
Symbol No: 2285030
Yubraj Khatiwada
MCA501 Discrete structure
Contents
Proposition Logic: ................................................................................................................................... 2
1. Find the propositional formula and truth table and check whether tautology exists. ....................... 2
Recursion ................................................................................................................................................. 4
3. Find the GCD of any two integers using the Euclidean theorem. .................................................... 5
4. Find the multiplicative inverse of b in zn using the extended Euclidean theorem. .......................... 6
7. Write a program to implement minimum cost-spanning tree algorithms using Kruskal .............. 10
9. Write a program for the implementation of DFA on the string that accepts, starting with
0 and ending with 1 over input alphabet {0,1}. ............................................................................. 14
10. Write a program to validate the E-mail, Mobile no and Identifiers using regular
expression....................................................................................................................................... 16
Yubraj Khatiwada
MCA501 Discrete structure
Proposition Logic:
1. Find the propositional formula and truth table and check whether tautology exists.
Import itertools
2 Yubraj Khatiwada
MCA501 Discrete structure
Output:-
Truth Table:
a|a
True | True
False | False
3 Yubraj Khatiwada
MCA501 Discrete structure
Recursion
2. Write a program to find the Fibonacci series.
def fibonacci_series(n):
fib_sequence = [0, 1]
while len(fib_sequence) < n:
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
return fib_sequence[:n]
Output :
4 Yubraj Khatiwada
MCA501 Discrete structure
Number Theory
3. Find the GCD of any two integers using the Euclidean theorem.
Output :
Enter the first number: 25
Enter the second number: 150
The GCD of 25 and 150 is:25
5 Yubraj Khatiwada
MCA501 Discrete structure
inverse = multiplicative_inverse(b, n)
if inverse is None:
print(f"The multiplicative inverse of {b} modulo {n} does not exist.")
else:
print(f"The multiplicative inverse of {b} modulo {n} is: {inverse}")
Output :
Enter the value of b: 4
Enter the value of n: 11
The multiplicative inverse of 4 modulo 11 is: 3
6 Yubraj Khatiwada
MCA501 Discrete structure
Recurrence Relation
5. Write a program to implement recurrence relation. Like Factorial.
def factorial(n):
if n == 0:
return 1 # Base case: 0! = 1
else:
return n * factorial(n - 1) # Recursive call
Output :
Enter a number: 5
The factorial of 5 is: 120
7 Yubraj Khatiwada
MCA501 Discrete structure
import sys
# Prim's algorithm to find the minimum spanning tree
def prims_mst(graph):
num_nodes = len(graph)
selected_node = [False] * num_nodes
mst_edges = []
total_cost = 0
# Find the minimum edge connecting the MST with a new node
for i in range(num_nodes):
if selected_node[i]:
# Node is part of the MST
for j in range(num_nodes):
if not selected_node[j] and graph[i][j] > 0:
# Not in MST and edge exists
if graph[i][j] < min_edge[2]:
min_edge = (i, j, graph[i][j])
8 Yubraj Khatiwada
MCA501 Discrete structure
graph = [
[0, 2, 0, 6, 0],
[2, 0, 3, 8, 5],
[0, 3, 0, 0, 7],
[6, 8, 0, 0, 9],
[0, 5, 7, 9, 0]]
prims_mst(graph)
Output :
Edges in the MST with their weights:
0 -- 1 == 2
1 -- 2 == 3
1 -- 4 == 5
0 -- 3 == 6
Total cost of MST: 16
9 Yubraj Khatiwada
MCA501 Discrete structure
10 Yubraj khatiwada
# If including this edge doesn't form a cycle, add it to the MST
if root_u != root_v:
mst_edges.append((u, v, weight))
total_cost += weight
union(parent, rank, root_u, root_v)
Output :
Edges in the MST with their weights:
0 -- 1 == 2
1 -- 2 == 3
1 -- 4 == 5
0 -- 3 == 6
Total cost of MST: 16
11 Yubraj Khatiwada
MCA501 Discrete structure
# If the current distance is greater than the recorded shortest distance, skip
if current_distance > distances[current_node]:
continue
12 Yubraj Khatiwada
MCA501 Discrete structure
graph = {
0: [(1, 4), (2, 1)],
1: [(3, 1)],
2: [(1, 2), (3, 5)],
3: [] }
# Output:
Shortest path from start node to all other nodes
shortest_distances = dijkstra(graph, start_node)
for node, distance in enumerate(shortest_distances):
print(f"Shortest distance from node {start_node} to node {node} is {distance}")
Output :
13 Yubraj Khatiwada
MCA501 Discrete structure
Finite Automata
9. Write a program for the implementation of DFA on the string that accepts,
starting with 0 and ending with 1 over input alphabet {0,1}.
Def dfa_accepts_string(s):
# Initial state
current_state = "q0"
# Process each character in the string for char in s:
if current_state == "q0":
if char == '0':
current_state = "q1"
elif char == '1':
current_state = "q0"
current_state = "q2"
# Accept if we end in the accepting state q2
return current_state == "q2"
# Input: Test strings
test_strings = [
"001", # Accepted
"01", # Accepted
"10", # Rejected
"0001", # Accepted
"0111", # Accepted
14 Yubraj Khatiwada
MCA501 Discrete structure
"110", # Rejected
"0", # Rejected
"1", # Rejected ]
Output :
String '001': Accepted
String '01': Accepted
String '10': Rejected
String '0001': Accepted
String '0111': Accepted
String '110': Rejected
String '0': Rejected
String '1': Rejected
15 Yubraj Khatiwada
MCA501 Discrete structure
10. Write a program to validate the E-mail, Mobile no and Identifiers using regular expression
import re
# Function to validate an email address
def validate_email(email):
# Email pattern: basic format with alphanumeric characters, underscores, periods, etc.
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
if re.match(pattern, email):
return "Valid email"
else:
return "Invalid email"
# Function to validate a mobile number that starts with 98 or 97 and has 10 digits
def validate_mobile(mobile):
# Mobile pattern: starts with 98 or 97, followed by 8 other digits
pattern = r'^(98|97)\d{8}$'
if re.match(pattern, mobile):
return "Valid mobile number"
else:
return "Invalid mobile number"
16 Yubraj Khatiwada
MCA501 Discrete structure
print("Email Validation:")
for email in emails:
print(f"{email}: {validate_email(email)}")
print("\nIdentifierValidation:")
for identifier in identifiers:
print(f"{identifier}: {validate_identifier(identifier)}")
Output:
Email Validation: [email protected]: Valid email
[email protected]:
Valid email invalid-email@:
Invalid email user@domain:
Invalid email
Mobile Number Validation:
905353171: Valid mobile
number 9811383812: Valid
mobile number 9612345678:
Invalid mobile number 98123:
Invalid mobile number
12345: Invalid mobile number
Identifier Validation:
_validIdentifier: Valid
identifier 2invalid:
Invalid identifier
valid_id: Valid
identifier
anotherValidIdentifier: Valid
identifier invalid-id: Invalid
identifier
17 Yubraj Khatiwada