0% found this document useful (0 votes)
5 views6 pages

DS Lab It

Uploaded by

12e27.vimalraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

DS Lab It

Uploaded by

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

1. To implement stack operations using array.

Ans. class Stack:

def __init__(self):

self.stack = []

def push(self, item):

self.stack.append(item)

def pop(self):

if self.stack:

return self.stack.pop()

raise IndexError("pop from empty stack")

def peek(self):

if self.stack:

return self.stack[-1]

raise IndexError("peek from empty stack")

def is_empty(self):

return not self.stack

def size(self):

return len(self.stack)

# Example usage

stack = Stack()

stack.push(10)

stack.push(20)

stack.push(30)
print(stack.peek()) # 30

print(stack.pop()) # 30

print(stack.pop()) # 20

print(stack.size()) # 1

print(stack.is_empty()) # False

output:

10

10

20

False

2. To implement queue operations using array.

Ans. class Queue:

def __init__(self):

self.queue = []

def enqueue(self, item):

self.queue.append(item)

def dequeue(self):

if self.queue:

return self.queue.pop(0)

def front(self):

if self.queue:

return self.queue[0]
def is_empty(self):

return not self.queue

def size(self):

return len(self.queue)

# Example usage

queue = Queue()

queue.enqueue(10)

queue.enqueue(20)

queue.enqueue(30)

print(queue.front()) # 10

print(queue.dequeue()) # 10

print(queue.dequeue()) # 20

print(queue.size()) # 1

print(queue.is_empty()) # False

output:

10

10

20

False

2. To create adjacency matrix of the given graph and to perform


breadth first search traversal.
Ans. # Number of vertices
vertices = 5
# Edges of the graph (undirected)
edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 4)]

# Step 1: Create an adjacency matrix


adj_matrix = [[0] * vertices for _ in range(vertices)] # Initialize with
zeros

# Populate the adjacency matrix with the edges


for u, v in edges:
adj_matrix[u][v] = 1 # Directed edge u -> v
adj_matrix[v][u] = 1 # For undirected graph, also mark v -> u

# Print the adjacency matrix


print("Adjacency Matrix:")
for row in adj_matrix:
print(row)

# Step 2: BFS traversal starting from node 0


visited = [False] * vertices # To track visited nodes
queue = [0] # Queue initialized with the start node (0)
visited[0] = True # Mark the start node as visited
bfs_order = [] # List to store the BFS order

while queue:
node = queue.pop(0) # Dequeue the front element
bfs_order.append(node)

# Visit all the neighbors of the current node


for neighbor in range(vertices):
if adj_matrix[node][neighbor] == 1 and not visited[neighbor]:
queue.append(neighbor) # Enqueue unvisited neighbor
visited[neighbor] = True # Mark as visited

# Print the BFS traversal order


print("\nBFS Traversal Order starting from node 0:")
print(bfs_order)

outuput:

Example Graph:
 Vertices: 5 (0, 1, 2, 3, 4)
 Edges: (0-1), (0-2), (1-3), (1-4), (2-4) (undirected)
Adjacency Matrix:
[0, 1, 1, 0, 0]
[1, 0, 0, 1, 1]
[1, 0, 0, 0, 1]
[0, 1, 0, 0, 0]
[0, 1, 1, 0, 0]

BFS Traversal Order starting from node 0:


[0, 1, 2, 3, 4]

3. To create adjacency matrix of the given graph and to perform depth


first search traversal.
Ans. # Number of vertices
vertices = 5

# Edges of the graph (undirected)


edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 4)]

# Step 1: Create an adjacency matrix


adj_matrix = [[0] * vertices for _ in range(vertices)] # Initialize with
zeros

# Populate the adjacency matrix with the edges


for u, v in edges:
adj_matrix[u][v] = 1 # Directed edge u -> v
adj_matrix[v][u] = 1 # For undirected graph, also mark v -> u

# Print the adjacency matrix


print("Adjacency Matrix:")
for row in adj_matrix:
print(row)

# Step 2: DFS traversal starting from node 0


visited = [False] * vertices # To track visited nodes
stack = [0] # Stack initialized with the start node (0)
visited[0] = True # Mark the start node as visited
dfs_order = [] # List to store the DFS order

while stack:
node = stack.pop() # Pop the top element from the stack
dfs_order.append(node)

# Visit all the neighbors of the current node


for neighbor in range(vertices-1, -1, -1): # Loop from last to first
(to maintain order)
if adj_matrix[node][neighbor] == 1 and not visited[neighbor]:
stack.append(neighbor) # Push unvisited neighbor to the
stack
visited[neighbor] = True # Mark as visited

# Print the DFS traversal order


print("\nDFS Traversal Order starting from node 0:")
print(dfs_order)

output:

Example Graph:
 Vertices: 5 (0, 1, 2, 3, 4)
 Edges: (0-1), (0-2), (1-3), (1-4), (2-4) (undirected)

Adjacency Matrix:
[0, 1, 1, 0, 0]
[1, 0, 0, 1, 1]
[1, 0, 0, 0, 1]
[0, 1, 0, 0, 0]
[0, 1, 1, 0, 0]

DFS Traversal Order starting from node 0:


[0, 2, 4, 1, 3]

You might also like