Professional Documents
Culture Documents
Gdfer 3
Gdfer 3
Gdfer 3
• Arrays are a common choice for implementing ADTs because they can
hold contiguous elements in the same order.
• In the context of multi-arrays, we’ll consider arrays of arrays (i.e.,
matrices or multi-dimensional arrays).
• Here are some key points for implementing multi-arrays using arrays:
• Static Implementation:
• Dynamic Implementation:
3. Example in Python:
4. Python
class MultiArray:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.data = [[0] * cols for _ in range(rows)]
def get(self, row, col):
return self.data[row][col]
def set(self, row, col, value):
self.data[row][col] = value
# Example usage
matrix = MultiArray(3, 4)
matrix.set(0, 1, 42)
print(matrix.get(0, 1)) # Output: 42
In this example, MultiArray represents a 3x4 matrix, and we can set/get values at specific
positions.
Q1
b.What is a python set? Demonstrate union, intersection and
addition operations on set with example.
1. Python Sets:
2. Union Operation:
• The union of two sets contains all unique elements from both sets.
• You can use the union() method or the | operator to perform the
union.
• Example:
3. Python
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
4.
5. AI-generated code. Review and use carefully. More info on FAQ.
6. Intersection Operation:
• The intersection of two sets contains only the elements that exist in
both sets.
• You can use the intersection() method or the & operator for
intersection.
• Example:
7. Python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {2, 3}
8.
9. AI-generated code. Review and use carefully. More info on FAQ.
10. Addition Operation:
11. Python
my_set = set()
my_set.add('apple')
my_set.add('banana')
my_set.add('apple') # Adding 'apple' again
2. Best-Case Analysis:
3. Average-Case Analysis:
Asymptotic Notations
Asymptotic notations help express the efficiency of algorithms in terms of input size. Some
commonly used notations include:
Key Points:
• Given two functions f(n) and g(n), we say that f(n) is O(g(n)) if there
exist constants c > 0 and n₀ ≥ 0 such that f(n) ≤ c * g(n) for all n ≥ n₀.
• In simpler terms, f(n) is O(g(n)) if f(n) grows no faster than c * g(n) for
sufficiently large n.
3. Importance:
• Big O notation helps analyze the efficiency of algorithms.
• It allows programmers to compare different algorithms and choose
the most efficient one for a specific problem.
• Understanding scalability and predicting performance as input size
grows are crucial aspects.
• You can use a regular Python list and manually insert elements while
ensuring that the list remains sorted.
• Whenever you add a new element, insert it at the correct position to
maintain the sorted order.
• Here’s an example:
2. Python
• Note that this approach requires linear search for insertion, which
may not be efficient for large lists.
• To improve efficiency, you can use binary search to find the correct
position for insertion.
• Binary search reduces the search time to logarithmic complexity.
• Here’s an example using the bisect module:
4. Python
import bisect
def insert_sorted_bisect(my_list, new_element):
bisect.insort(my_list, new_element)
# Example usage
sorted_list = [10, 20, 30]
insert_sorted_bisect(sorted_list, 15)
print(sorted_list) # Output: [10, 15, 20, 30]
6. Python
class SortedList:
def __init__(self):
self.data = []
def add(self, new_element):
index = bisect.bisect_left(self.data, new_element)
self.data.insert(index, new_element)
# Example usage
my_sorted_list = SortedList()
my_sorted_list.add(30)
my_sorted_list.add(10)
my_sorted_list.add(20)
2. Access Functions:
• These operations are not strictly part of the core ADT but provide
useful functionality.
• Examples:
Example ADTs:
1. List ADT:
2. Stack ADT:
Q.2
a.What is linked list? Explain types of linked lists
Linked Lists
A linked list is a fundamental data structure where elements (nodes) are connected using
pointers. Unlike arrays, linked list elements are not stored in contiguous memory locations.
Instead, each node contains data and a reference (link) to the next node in the list. Linked
lists allow dynamic memory allocation and efficient insertions and deletions.
• In a singly linked list, each node contains data and a pointer to the
next node.
• Traversal is possible only in one direction (from head to tail).
• Example:
• Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Creating a singly linked list: 1 -> 2 -> 3
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
• A doubly linked list has nodes with pointers to both the next and
previous nodes.
• Allows traversal in both forward and backward directions.
• Example:
• Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Creating a doubly linked list: 1 <-> 2 <-> 3
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
• In a circular linked list, the last node points back to the first node
(forming a loop).
• Useful for applications like round-robin scheduling.
Example:
1 -> 2 -> 3 -> 1 (loop back)
• If the list is empty, create a new node and set both the head and tail
pointers to this new node.
• If the list is not empty, follow these steps:
• The idea is to put all the opening brackets (such as (, {, [) in the stack.
• Whenever you encounter a closing bracket (such as ), }, ]), check if the
top of the stack contains the corresponding opening bracket.
• If it does, pop the stack and continue the iteration.
• In the end, if the stack is empty, it means all brackets are balanced or
well-formed. Otherwise, they are not balanced1
2. Illustration: