Understanding Computational Structures
Understanding Computational Structures
Chapter No. 4
COMPUTATIONAL STRUCTURES
STUDENT LEARNING OUTCOMES [C-11-B-01 to C-11-B-18]
Define and explain the purpose of primitive computational structures, including lists, stacks,
queues, tress, and graphs. (Understanding)
Identify and describe the characteristics and properties of different computational structures.
(Understanding)
Perform basic operations such as insertion, deletion traversal, and searching on various
computational structures. (Understanding)
Understand and implement LIFO (last in first out), FIFO (first in first out) principle in stack and
queues respectively. (Understanding)
Compare and contrast different types of trees and graph, and apply appropriate operations to
them. (Understanding)
Analyze and choose the most suitable computational structures based on problems requirements,
data organization, and performance consideration . (Understanding)
Apply computational structures in real world scenario, including data organization, task scheduling
and network modeling. (Understanding)
Combine different computational structures to solve complex problems and enhance functionality.
(Understanding)
1
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
2
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
3
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
4
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
party_list = ["Invite friends ","Buy decorations”, "Buy snacks”, "Buy cold drinks"] party_list.remove ("Buy
snacks”) # Removes ’Buy snacks ’ from the list
print(party_list)
Output: [’Invite friends’, ’Buy decorations’, ’Buy cold drinks ’]
party_list = ["Invite friends ", "Buy decorations", "Buy cold drinks " ] party_list.pop (0)
# Removes the item at index 0
print(party_list)
Output: [’Buy decorations’, ’Buy cold drinks’]
party_list = ["Invite friends", "Buy decorations", "Buy cold drinks"] if "Buy cold
drinks" in party_list:
print("Buy cold drinks is on the list.") # Prints if 'Buy cold drinks' is found
else:
print("Buy cold drinks is not on the list.") Output: Buy cold drinks is on the list.
inputs in programming.
1.1.1.1Applications of Lists
Data Storage and Manipulation:
Lists are commonly used to store
and manage collections of data,
such as records, entries, or values.
They allow for easy insertion,
deletion, and access to elements.
Stack and Queue
Implementations: Lists can be
used to implement stack (LIFO)
and queue (FIFO) data
structures, which are
fundamental for various 1.1.2 Stacks
algorithms and tasks in A stack is a simple data structure where you
computing. can only add or remove items from one end,
Sorting and Searching known as the “top”. Both insertion and
Algorithms: Lists are frequently
deletion of elements occur at this top end. A
used in algorithms for sorting and
stack operates on the Last-In, First-Out (LIFO)
5
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
principle, meaning that the most recently to remove the element from the top
added element is the first one to be removed. of the stack.
[Link] Properties of Stack
LIFO Principle: A stack works
by the Last-In, First-Out rule. The Description of above code:
last item added is the first one The code creates an empty stack to hold
removed. books
Single Access Point: You can It then adds three books (“Book A”, “Book B”,
only add or remove items from the and “Book C”) one by one to the top of the
top of the stack. stack.
Order Preservation: The order in Finally, it removes the top book “Book
which you add items is preserved, with C” from the stack, showing how the
the last item added being the first to last book added is the first one taken
leave. off.
Size Flexibility: A stack can either
1.1.2 Queues
have a fixed size or grow as needed.
Sequential Access: You can only access A queue is like a line in front of the a bank or
items in the order they were added, a ticket counter. The first person to get in line
starting from the top. is the first person to be served. In a computer,
a queue works the same way. It keeps track of
things so that the first item added is the first
one to be taken out. Just like in a bank line,
you add things to the back and remove them
from the front, following the FIFO (First-In,
First-Out) principle, as shown in Figure.
Example:
stack = []
6
Push operation - add item to the top of the stack
[Link]('A')
[Link]('B')
print("Stack after pushes:", stack) Output: ['A', 'B']
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
adding a person to the end of the data like file systems, organizational
line. In a queue, you add items to charts, and more.
the back.
Each node can have zero or more
Dequeue (Remove an Item): This is
children, but only one parent (except the
like serving the person at the front
of the line. In a queue, you take root, which has none).
Common types of trees include binary
trees, where each node has at most two
items out from the front. children.
operations Example:
Additional might include checking
if the queue is empty, retrieving the A family tree is a classic example
element at the front without of a tree structure in real life.
removing it, and determining the It shows relationships between
size of the queue. family members across
Example: generations.
The root node could be the oldest
Example of Enqueue and Dequeue operations in Pythonknown
using ancestor.
a list to simulate
a queue: Each person (node) can have
children (child nodes), representing
Queue using a Python list the next generation.
Each node (person) has one parent
queue = []
(except the root) and can have
# Enqueue operation - add element at the end
multiple children.
[Link]('A')
[Link]('B')
[Link]('C') Important! The concept of tree data structur
print("Queue after enqueues:", queue) scientist
Output: ['A', 'B', 'C'] John McCarthy in 1960.
1.1.3 Trees
A tree is a data structure that consists [Link] Properties of Trees
of nodes connected in a hierarchy. Root Node: The root is the very
It starts from a root node and branches first or top node in a tree, like the
out to child nodes, forming a parent- main folder in a computer where all
other folders and files are
child relationship.
contained.
Trees are used to represent hierarchical
7
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
8
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
Example: in a social network graph, each means they go from one vertex to another in a
user is represented by a vertex. specific way.
· Edges (Links): These are the connections Example: Consider the above graph: If you want
between vertices. to travel from city A to city B, you can only go in
Example, in a transport system graph, each the direction permitted by the city's sign. If
road connecting two cities is an edge. there's no one-way street going from city A to
[Link] Properties of Graphs city B, you cannot travel directly from city A to B.
Graphs also have specific details that describe Undirected Graphs:
their structure: In an undirected graph, edges do not have a
direction. This means that if there is a connection
Degree Weight Direction between two vertices, you can travel in both
This is the : In some Edges can directions.
number of graphs, be either
edges edges directed or
connected have undirected.
to a vertex. weights Directed
For that edges have
instance, if represent a one-way
a city is values like connection,
connected distances meaning a
to three or costs. road from
other For city A to
cities, the example, if city B does
degree of a road not
that city's between necessarily
vertex is 3. two cities have a
is 50 return road
kilometers from B to A.
long, its Undirected
edge edges
might represent a
have a two-way
weight of connection.
50.
[Link] Types of Graphs
Graphs can be classified into several types based
on their structure and properties. The main types
ofgraphs are directed, undirected, and
weighted. Each type has its own characteristics ,
which can be better understood through simple
examples.
Directed Graphs:
In a directed graph, edges have a direction, which
9
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
Example: Consider the below graph: if Person A d) Returns the length of the list.
is friends with Person B, then Person B is also 3. Which operation removes an item from
10
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
edges from the root to the Answer: When two variables reference
deepest node the same list in Python, they both point
b) The height is the number of nodes to the same object in memory. This
from the root to the deepest node means that changes made through one
c) The height is the number of variable will affect the other, which can
children of the root node lead to unexpected behavior or bugs,
d) The height is always equal to the especially if you're not intentionally
number of nodes in the tree sharing data.
9. Which graph traversal explores all
immediate neighbors of a vertex
before moving to the next level?
a) Depth-First Search (DFS)
b) Breadth-First Search (BFS)
Example: list1 = [1, 2, 3]
c) Depth-Limited Search (DLS)
list2 = list1 #Both variables refer to
d) Iterative Deepening Search (IDS)
10. For which scenario would a graph data the same list
structure be most appropriate? [Link](4)
a) Managing a to-do list print(list1) Output: [1, 2, 3, 4]
b) Modeling a line of customers in a print(list2) Output: [1, 2, 3, 4]
store 3. Define a stack and explain the Last-In,
c) Representing connections in a First-Out (LIFO) principle.
social network Answer: A stack is a simple data structure
d) None of these where you can only add or remove items
from one end, known as the “top”. Both
Short Questions: insertion and deletion of elements occur at
1. Explain how the ' extend() ' function this top end. A stack operates on the Last-In,
works in python lists. Provide an First-Out (LIFO) principle, meaning that the
example. most recently added element is the first one
Answer: In Python, extend() method is used to be removed.
to add items from one list to the end of 4. How does the stack help in
another list. This method modifies the balancing parentheses in an
original list by appending all items from the expression? Describe the process.
given iterable. Answer: The stack helps balance
Let’s look at a simple example of parentheses by keeping track of
the extend() method. opening parentheses encountered.
a = [1, 2, 3] When a closing parenthesis is met, it
b = [4, 5] checks if the top of the stack contains a
Using extend() to add elements of b to a
matching opening parenthesis. If it
[Link](b)
does, they cancel out (pop the stack). If
print(a)
Output: [1,2,3,4,5]
not, or if the stack is empty, the
2. Explain the potential issues which parentheses are unbalanced. If the stack
could arise when two variables is empty at the end, the parentheses are
reference the same list in a balanced.
program? Provide an example. 5. Differentiate between the Enqueue and
11
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
12
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
Why Is This Property Important? When you need a plate, you take the
topmost plate first (pop).
Flexibility in Usage: You don’t have to
know the number of elements beforehand. You cannot remove a plate from the
This makes lists very useful when working middle or bottom without taking off the
with data that can change size plates above it.
dynamically, such as user input, data
processing, or results of computations.
Efficient Memory Management: Python
optimizes memory allocation behind the Python code:
scenes, so the list grows and shrinks as
required without wasting too much stack = []
memory.
Ease of Programming: Programmers can # Push operation
focus more on the logic rather than [Link]('Plate 1')
managing memory or resizing arrays
[Link]('Plate 2')
manually, which reduces complexity and
print("Stack after pushes:", stack)
chances of errors.
Supports Various Operations:
Operations like insertion, deletion, and # Pop operation
appending become simpler and more top_item = [Link]()
natural, which is not always possible in print("Popped item:", top_item)
fixed-size data structures. print("Stack after pop:", stack)
2. Explain the operations on stack
with real life example and
3. Write, a simple program to
python code.
implement a queue (insertion and
Answer: A stack is a data structure that follows deletion).
the Last In, First Out (LIFO) principle. The two
main operations on a stack are: from collections import deque
14
CHAPTER # 4: COMPUTATIONAL STRUCTURES COMPUTER PART-I
anything, like cities, people, or even to travel from city A to city B, you can only go in
abstract concepts, and the edges the direction permitted by the city's sign. If
represent the relationships or there's no one-way street going from city A to
pathways between them. city B, you cannot travel directly from city A to B.
Example: : In a social network, each Undirected Graphs:
person can be connected to many In an undirected graph, edges do not have a
others, forming a graph. direction. This means that if there is a connection
There is no single starting point, between two vertices, you can travel in both
and people (vertices) can have directions.
multiple connections (edges) that do Example: Consider the below graph: if Person A
not follow a strict parent-child is friends with Person B, then Person B is also
relationship like in a tree. friends with Person A. There is no restriction on
the direction of the friendship, so you can move
freely between friends.
Undirected Graph
Directed Graphs:
In a directed graph, edges have a direction, which
Directed Graph
Example: Consider the above graph: If you want
15