Dsa Ta 3-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Theoretical Assignment 3

Anitej Jain (220153)

Question 1: All or None


Part (a)
Solution:
Using DFS algorithm we may or may not find the required path.Not All DFS
traversal gives the path but there exist a dfs traversal which gives the required
path (if such path exists).

Part (b)
Solution:
Breadth-First Search (BFS) traversal also cannot guarantee finding a path that
visits every city once, using each road exactly once, and returning to the starting
point. Because BFS works on a distance based approach from starting point.

Part (c)
Solution:
Firstly the graph should be connected and secondly As Starting and Ending
position of graph is same so the degree of vertices in the graph should be all
even i.e there shouldn’t be any odd degree vertex in the graph

Part (d)
Solution:
We can use these steps for getting vertices in the path:
• If there is a valid adjacent vertex to the current one, we add it to the path
and proceed to the next step.

1
Name: Anitej Jain Roll Number: 220153

• If a valid path is not found, we repeat step 1 for other adjacent vertices
of the starting node.
To establish the validity of the next vertex (v), one of the following conditions
must be met:

• If v is the sole adjacent vertex to the current vertex, we proceed to it.


• If there are multiple adjacent vertices, we select the one that is not a
bridge. Determining whether a vertex is a bridge or not involves these
steps:

• Initially, we calculate the number of vertices that can be reached through


a Depth-First Search (DFS) traversal starting from the current vertex.
• If the count of reachable vertices from the current vertex is greater than
the count of vertices reachable after removing the edge between u and v,
then we identify that edge as a bridge.

2
Name: Anitej Jain Roll Number: 220153

Question 2: Chaotic Dino


Part (a)
Solution Description
The provided algorithm, ”Can Send Signal Solution,” is a description of a
method for determining whether there is sufficient power to send a signal from
a source city ’S’ to a destination city ’D’ in a graph of cities connected by roads.
Here’s a detailed description of the algorithm:

Initialization: Create two arrays, ’distances’ and ’processed,’ each of size ’num-
Cities,’ to keep track of the minimum distances and processed cities. Initially,
set all values in the ’distances’ array to infinity and all values in the ’processed’
array to ’false.’ Set the Source Distance to Zero: Set the distance at the source
city ’S’ to 0. This means that the distance from ’S’ to itself is zero.

For Loop (Repeat for ’numCities - 1’ times): In each iteration of this loop,
you perform the following steps for each city: a. Find the unprocessed city ’u’
with the minimum distance in the ’distances’ array. b. Mark city ’u’ as pro-
cessed by setting ’processed[u]’ to ’true.’ For each neighboring city ’v’ of ’u’:
c. Check if ’v’ is unprocessed and if the power at ’u’ is sufficient to reach ’v.’
d. Calculate a potential new distance as the sum of the current distance at ’u’
and the distance between ’u’ and ’v.’ e. If the potential distance is less than
the current distance at ’v,’ update ’distances[v]’ with the potential distance.
This is done to find the shortest distance from ’S’ to all other cities. Check the
Distance to the Destination:

After the loop, check if the distance at the destination city ’D’ is less than
infinity. If it is less than infinity, it means that there exists a path from ’S’
to ’D’ with the available power, so return ’true’ (Power is sufficient to send a
signal from ’S’ to ’D’). If it is still infinity, it means there is no path from ’S’ to
’D’ with the available power, so return ’false’ (Power is not sufficient to send a
signal from ’S’ to ’D’).

Pseudocode

3
Name: Anitej Jain Roll Number: 220153

Can Send Signal Solution


1: Initialize an array distances of size numCities, all set to infinity.
2: Initialize an array processed of size numCities, all set to false.
3: Set distances[S] to 0.
4: for i ← 0 to numCities − 1 do
5: Find the unprocessed city with the minimum distance, let’s call it ’u’.
6: Mark city ’u’ as processed.
7: for each neighboring city ’v’ of ’u’ do
8: if ’v’ is unprocessed and the power at ’u’ is sufficient to reach ’v’
then
9: Calculate a potential new distance as distances[u] + distance be-
tween ’u’ and ’v’.
10: if the potential distance is less than the current distance at ’v’
then
11: Update distances[v] with the potential distance.
12: end if
13: end if
14: end for
15: end for
16: if distances[D] is less than infinity then
17: return true (Power is sufficient to send a signal from S to D).
18: else
19: return false (Power is not sufficient to send a signal from S to D).
20: end if

4
Name: Anitej Jain Roll Number: 220153

Part (b)
Solution Description
The algorithm uses binary search to efficiently determine the minimum power
needed. It repeatedly calls the ”Check If Signal Can Be Sent for All Powers”
algorithm with different power values, narrowing down the range of possible
power values required to establish a signal path.

Pseudocode

Find Minimum Power Required


1: function FindMinPower(S, D, towerPowers)
2: low ← 0
3: high ← MAX POWER ▷ Initialize high with maximum possible power.
4: result ← −1
5: while low ≤ high do
6: mid ← (low + high)/2
7: if Can Send Signal Solution(graph, S, D, towerPowers
with all towers having power mid) then
8: result ← mid
9: high ← mid − 1
10: else
11: low ← mid + 1
12: end if
13: end while
14: return result
15: end function

5
Name: Anitej Jain Roll Number: 220153

Question 3: Room Colors


Solution Description
The algorithm below can be breakdown and understood by following points

Initialization: The first step of the procedure is to initialize two arrays. The
first colors of every room are kept in the roomColors array, while modifications
to the room colors are monitored in the second array.

BuildTree: This function creates a tree structure to stand in for the rooms.
It employs a recursive method to give the tree’s nodes colors depending on the
specified room colors. A range of rooms are represented by each node in the
tree due to its construction(segment tree).

UpdateTree Function: When color bombs happen, this function updates the
tree structure. It modifies the range of several rooms in the tree. Before making
any more modifications, it applies any withstanding updates for that node. It
determines if the range that the current node represents and the range of rooms
that need to be updated overlap. It immediately assigns the new color and
marks the children for a pending update if the range is entirely enclosed. If not,
it updates the left and right children recursively.

Now using all these functions we perform all updates continously and stores
and output the roomColors array.

Pseudocode

6
Name: Anitej Jain Roll Number: 220153

Room Color Solution


1: Initialize an array roomColors to store initial room colors
2: Initialize an array to track room color updates
3: function BuildTree(start, end, node)
4: if start == end then
5: Assign roomColors[start] to the node
6: else
7: mid ← (start + end) / 2
8: BuildTree(start, mid, 2 * node + 1)
9: BuildTree(mid + 1, end, 2 * node + 2)
10: Assign the value of the left child to the node
11: end if
12: end function
13: function UpdateTree(start, end, l, r, color, node)
14: if there are pending updates for the node then
15: Apply the pending update to the node and its children
16: end if
17: if [start, end] does not intersect with [l, r] then
18: return
19: end if
20: if [start, end] is fully contained within [l, r] then
21: Assign ’color’ to the node
22: if the node is not a leaf then
23: Mark the children for a pending update with ’color’
24: end if
25: return
26: end if
27: mid ← (start + end) / 2
28: UpdateTree(start, mid, l, r, color, 2 * node + 1)
29: UpdateTree(mid + 1, end, l, r, color, 2 * node + 2)
30: Assign the value of the left child to the node
31: end function
32: function ResolveUpdates(start, end, node)
33: if there are pending updates for the node then
34: Apply the pending update to the node and its children
35: end if
36: if the node is not a leaf then
37: mid ← (start + end) / 2
38: ResolveUpdates(start, mid, 2 * node + 1)
39: ResolveUpdates(mid + 1, end, 2 * node + 2)
40: end if
41: end function
42: Initialize the tree and assign initial room colors
43: for each bombing (l, r, color) do
44: UpdateTree(0, n - 1, l - 1, r - 1, color, 0)
45: end for
46: ResolveUpdates(0, n - 1, 0) to resolve any pending updates
47: The final room colors are now in the roomColors array
7
Name: Anitej Jain Roll Number: 220153

Question 4: Fest Fever


Solution Description
The algorithm given below manages sweet prices during a festival by processing
”update” and ”request” queries. It maintains a tree structure to store prices,
and for each ”request,” it calculates the total cost of the lowest-priced sweets.
If Shantanu has enough money, it returns ”YES”; otherwise, it returns ”NO.”
The algorithm ensures Shantanu can fulfill his brother’s sweet requests within
his budget.

Pseudocode

Fest Fever Solution


1: Initialize an empty tree structure called ”pricesTree” for managing prices
2: for each query do
3: Read the query type: ”update” or ”request”
4: if the query type is ”update” then
5: Read the index i and the new price x
6: Insert (i, x) into ”pricesTree”
7: end if
8: if the query type is ”request” then
9: Read the number of sweets to request: r
10: Initialize a variable ”totalCost” and set it to 0
11: Initialize a counter variable ”count” and set it to 0
12: for each (i, price) in ”pricesTree” do
13: if count < r then
14: Increment ”totalCost” by price
15: Increment ”count” by 1
16: else
17: Break
18: end if
19: end for
20: if ”totalCost” is less than or equal to Shantanu’s available money M
then
21: Output ”YES.”
22: Set M to 0 (Shantanu gets all his money back)
23: else
24: Output ”NO.”
25: end if
26: end if
27: end for
28: Step 3: Complete the process
29: Repeat for each query in chronological order.

8
Name: Anitej Jain Roll Number: 220153

Question 5: No Sugar in this Coat


Solution Description
The below algorithm determines whether a given sequence is edible in a Breadth-
First Search (BFS) order within a graph. It begins by initializing a queue and
an array to track visited nodes, starting from the root node, and marking it as
visited. It then performs a BFS traversal of the graph, comparing the values of
visited nodes with the corresponding elements in the given ”sequence.” If the
sequence is not compatible with the BFS order, it returns false. If the algorithm
successfully processes the entire sequence, it returns true, indicating that the
sequence is edible in BFS order.

Pseudocode

No Sugar in this Coat Solution


1: Create a queue
2: Initialize an array to keep track of visited nodes
3: Enqueue the root node (starting point)
4: visited[root] ← true
5: for i ← 0 to n − 1 do
6: if queue is empty then
7: return false
8: end if
9: currentNode ← Dequeue from the queue
10: if currentNode.data != sequence[i] then
11: return false
12: end if
13: for each adjacentNode in adjacencyList[currentNode] do
14: if not visited[adjacentNode] then
15: Enqueue adjacentNode
16: visited[adjacentNode] ← true
17: end if
18: end for
19: end for
20: if isEdibleBFS(adjacencyList, sequence, n) is true then
21: Print ”The sequence is edible in BFS order.”
22: else
23: Print ”The sequence is not edible in BFS order.”
24: end if

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy