0% found this document useful (0 votes)
3 views18 pages

Important QN

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)
3 views18 pages

Important QN

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/ 18

1) Consider the following directed graph G.

Using Dijkstra’s algorithm, find the sequence


of vertices to be visited if the starting vertex is G. Show the step by step selection of each
vertex in the graph with the updated distance. At the end show the sequence of vertices
visited by following Dijkstra's algorithm.

Applying Dijkstra's Algorithm to the Graph


Initialization:
 Set G as the starting vertex.
 Initialize distances to all other vertices as infinity:
o A: ∞
o B: ∞
o C: ∞
o D: ∞
o E: ∞
o F: ∞
Step-by-Step Selection of Vertices:
1. G:
o Visited: G
o Update distances:
 F: 4
 E: 5
2. F:
o Visited: G, F
o Update distances:
 C: 10 (4 + 6)
 E: 8 (4 + 4)
3. E:
o Visited: G, F, E
o Update distances:
 D: 14 (8 + 6)
4. C:
o Visited: G, F, E, C
o Update distances:
 A: 7 (10 + 3)
 B: 11 (10 + 1)
5. A:
o Visited: G, F, E, C, A
o Update distances:
 B: 10 (7 + 3)
6. B:
o Visited: G, F, E, C, A, B
o No more vertices to update.
Final Sequence of Vertices:
 G, F, E, C, A, B, D
Explanation:
 Dijkstra's algorithm starts at the source vertex (G) and iteratively selects the unvisited
vertex with the smallest distance from the source.
 It updates the distances of neighboring vertices if a shorter path is found.
 The process continues until all vertices are visited.
The final sequence of vertices represents the shortest path from G to each other vertex in the
graph.
2) You are working as a Network Engineer in one the projects at Hadoop Data Center. The
nodes represent various Cat-6 copper connector, and the edges represent the length of a
copper cable wiring between them (in meters). Find the way to connect these Cat-6
Connectors using the least amount of cable wiring.
[ 5Marks]

Analyzing the Network Topology


Understanding the Problem:
 We have a network represented by a graph.
 Nodes are Cat-6 connectors.
 Edges represent the length of copper cable wiring between them.
 The goal is to find the minimum total length of cable wiring required to connect all
nodes.
Solution Approach: This problem can be solved using the Minimum Spanning Tree
(MST) algorithm. An MST is a subset of the edges in a graph that connects all vertices
without forming any cycles and has the minimum possible total edge weight.
Applying Kruskal's Algorithm (a popular MST algorithm):
1. Sort all edges in ascending order of their weights.
2. Initialize an empty set to store the edges of the MST.
3. Iterate through the sorted edges:
o If adding an edge to the MST does not create a cycle, add it to the MST.
Steps:
1. Sort edges by weight:
o (A, C): 1
o (A, B): 2
o (B, D): 3
o (B, E): 4
o (D, F): 4
o (E, F): 4
2. Build the MST:
o (A, C): Add to MST.
o (A, B): Add to MST.
o (B, D): Add to MST.
o (B, E): Add to MST.
o (D, F): Add to MST.
o (E, F): Skip (would create a cycle).
Minimum Spanning Tree: The MST consists of the following edges: (A, C), (A, B), (B, D),
(B, E), and (D, F).
Total Cable Length:
 1 + 2 + 3 + 4 + 4 = 14 meters
Conclusion: The minimum amount of cable wiring required to connect all Cat-6 connectors
is 14 meters. The optimal connection is represented by the MST found using Kruskal's
algorithm.

3) Construct the AVL tree with the following numbers (by mentioning the rotations used)
12, 15, 36, 17, 34, 85, 64, 19, 3 and find the balance factor of the nodes: 64 and 34.
Constructing the AVL Tree
AVL Tree Properties:
 Self-balancing binary search tree.
 The height difference between the left and right subtrees of any node is at most one.
 This balance is maintained using rotations (left and right rotations).
Insertion and Rotations:
1. Insert 12:
o Root node.
2. Insert 15:
o Right child of 12.
3. Insert 36:
o Right child of 15.
4. Insert 17:
o Left child of 36.
5. Insert 34:
o Left child of 36.
o Right rotation at 15 to balance the subtree.
6. 12
7. / \
15 36 /
17 34

6. **Insert 85:**
* Right child of 36.

7. **Insert 64:**
* Left child of 85.

8. **Insert 19:**
* Left child of 17.

9. **Insert 3:**
* Left child of 12.

**Final AVL Tree:**

17
/ \
12 36
/\ /\
3 15 34 85 /
19 64

**Balance Factors:**

* **Balance factor of 64:** 0 (left subtree height = right subtree height)


* **Balance factor of 34:** 1 (left subtree height is 1 less than right subtree height)

**Rotations Used:**
* One right rotation was used during the insertion of 34 to maintain the balance of the AVL
tree.

4) Find the DFS traversal for the graph given below start node at 1. show the stack trace at
each step of the DFS traversal provide the DFS tree.
DFS Traversal for the Given Graph
DFS (Depth-First Search):
 A graph traversal algorithm that explores as deeply as possible along each branch
before backtracking.
Given Graph:
DFS Traversal:
1. Stack: [1]
o Visit: 1
2. Stack: [1, 2]
o Visit: 2
3. Stack: [1, 2, 3]
o Visit: 3
4. Stack: [1, 2, 3, 4]
o Visit: 4
5. Stack: [1, 2, 3, 4, 5]
o Visit: 5
6. Stack: [1, 2, 3, 4, 5, 6]
o Visit: 6
7. Stack: [1, 2, 3, 4, 5, 6, 7]
o Visit: 7
8. Stack: [1, 2, 3, 4, 5, 6]
o Backtrack: 7
9. Stack: [1, 2, 3, 4, 5]
o Backtrack: 6
10. Stack: [1, 2, 3, 4]
 Backtrack: 5
11. Stack: [1, 2, 3]
 Backtrack: 4
12. Stack: [1, 2]
 Backtrack: 3
13. Stack: [1]
 Backtrack: 2
14. Stack: Empty
 DFS traversal complete.
DFS Tree:
DFS Traversal Sequence: 1, 2, 3, 4, 5, 6, 7

5) Given an undirected graph with V vertices labeled from 0 to V-1 and E edges, check
whether it contains any cycle or not. Graph is in the form of adjacency list where adj[i]

contains all the nodes ith node is having edge with.


Show each step of the sorting process and the final sorted result. [ 5Marks]

Analyzing the Graph and Applying Depth-First Search (DFS)


Understanding the Problem:
 We have an undirected graph represented as an adjacency list.
 The goal is to determine if the graph contains any cycles.
DFS Approach: DFS can be used to detect cycles in an undirected graph. The key idea is to
mark nodes as visited during the traversal. If we encounter a visited node that is not the
parent of the current node, it indicates a cycle.
Implementation:
Python
def dfs(graph, visited, node, parent):
visited[node] = True
for neighbor in graph[node]:
if not visited[neighbor]:
if not dfs(graph, visited, neighbor, node):
return False
elif neighbor != parent:
return False
return True

def is_cyclic(graph):
visited = [False] * len(graph)
for node in range(len(graph)):
if not visited[node]:
if not dfs(graph, visited, node, -1):
return True
return False
Explanation:
1. dfs function:
o Marks the current node as visited.
o Recursively calls dfs for unvisited neighbors.
o If a visited neighbor is encountered and it's not the parent, a cycle is detected.
2. is_cyclic function:
o Iterates through all nodes.
o If an unvisited node is found, starts a DFS traversal from it.
o If a cycle is detected during the traversal, returns True.
Applying to the Given Graph:
The given graph can be represented as an adjacency list:
Python
graph = [
[2, 5],
[1, 3, 5, 8],
[2, 6],
[2],
[1, 2],
[3],
[]
]
DFS Traversal and Cycle Detection:
1. Start DFS from node 1:
o Visit 1
o Visit 2
o Visit 3
o Visit 6
o Backtrack to 3
o Backtrack to 2
o Visit 5
o Backtrack to 2
o Visit 8
o Backtrack to 1
2. Since no cycles were detected during the DFS traversal, the graph is acyclic.
Conclusion: The given graph does not contain any cycles. The DFS traversal effectively
detected this by marking nodes as visited and checking for back edges that would indicate a
cycle.
7) Let G be a graph whose vertices are the integers 1 through 8, and let the adjacent vertices
of each vertex be given by the table below:

Vertex Adjacent Vertices


1 (2,3,4)
2 (1,3,4)
3 (1,2,4)
4 (1,2,3,6)
5 (6,7,8)
6 (4,5,7)
7 (5,6,8)
8 (5,7)

Assume that, in a traversal of G, the adjacent vertices of a given vertex are returned in
the same order as they are listed in the above table.
a. Draw G.
To draw the graph \( G \) based on the provided adjacency information for the vertices 1
through 8, we can list the connections:

1. **Vertex 1**: Connected to 2, 3, 4


2. **Vertex 2**: Connected to 1, 3, 4
3. **Vertex 3**: Connected to 1, 2, 4
4. **Vertex 4**: Connected to 1, 2, 3, 6
5. **Vertex 5**: Connected to 6, 7, 8
6. **Vertex 6**: Connected to 4, 5, 7
7. **Vertex 7**: Connected to 5, 6, 8
8. **Vertex 8**: Connected to 5, 7

### Graph Representation


Here’s how you can visualize the graph \( G \):

```
1
/|\
2 | 4 -- 6
\|/ /|\
3 5 7
|
8
```

### Description of the Drawing:


- Vertices 1, 2, and 3 are connected to each other, and they all connect to vertex 4.
- Vertex 4 also connects to vertex 6.
- Vertices 5, 6, 7, and 8 form a separate component.
- Vertex 5 connects to 6, 7, and 8, while vertex 7 connects back to 5 and also to 8.

This representation captures the adjacency relationships provided in the table.


[ 3Marks]
8) Suppose you have analysed a file and determined that the frequency of occurrence of
certain characters is as follows:

Character a b c d e f
Occurences 15 7 5 8 30 10

a) Construct the Huffman tree for the characters


b) List the codes for each character
c) Use the tree to compress the following strings:
i) ‘faded’ ii) ‘feed’ [ 5Marks]
a) Determine the percentage of vowels in the word. (0.5M) [ 2 Marks]

c)Explain which letter you would expect to find closest to the top of the Huffman Tree
when created for the expected word. (0.5M)

d)Determine the percentage of vowels in the word. (0.5M)

### a) Constructing the Huffman Tree

To construct the Huffman tree, we start by listing the characters and their frequencies:

| Character | Frequency |
|-----------|-----------|
|a | 15 |
|b |7 |
|c |5 |
|d |8 |
|e | 30 |
|f | 10 |

1. **Initial List**:
- (c, 5), (b, 7), (d, 8), (f, 10), (a, 15), (e, 30)

2. **Combine the Two Smallest Frequencies**:


- Combine (c, 5) and (b, 7) → New Node (cb, 12)
- List: (d, 8), (f, 10), (cb, 12), (a, 15), (e, 30)

3. **Combine Again**:
- Combine (d, 8) and (f, 10) → New Node (df, 18)
- List: (cb, 12), (df, 18), (a, 15), (e, 30)

4. **Combine Again**:
- Combine (cb, 12) and (a, 15) → New Node (cba, 27)
- List: (df, 18), (cba, 27), (e, 30)

5. **Combine Again**:
- Combine (df, 18) and (cba, 27) → New Node (dfcba, 45)
- List: (e, 30), (dfcba, 45)

6. **Final Combine**:
- Combine (e, 30) and (dfcba, 45) → Root Node (root, 75)

### Huffman Tree Structure


```
(75)
/ \
(30) (45)
(e) / \
(18) (27)
/ \ / \
(8) (10)(15)(cb)
(d) (f) (a) / \
(5)(7)
(c)(b)
```

### b) List the Codes for Each Character


- **e**: 0
- **d**: 100
- **f**: 101
- **a**: 11
- **c**: 010
- **b**: 011
### c) Compressing the Strings
i) **String: ‘faded’**
- f → 101
- a → 11
- d → 100
-e→0
- d → 100

**Compressed:** `101111000100`

ii) **String: ‘feed’**


- f → 101
-e→0
-e→0
- d → 100

**Compressed:** `101000100`

### d) Percentage of Vowels in the Word 'faded'


**Vowels:** a, e
**Total letters:** 5 (f, a, d, e, d)
**Vowel count:** 2 (a, e)

\[
\text{Percentage of vowels} = \left(\frac{2}{5}\right) \times 100 = 40\%
\]

### e) Explanation for Expected Letter at the Top of the Huffman Tree
In Huffman coding, characters with higher frequencies are assigned shorter codes and are placed
closer to the root of the tree. Therefore, since 'e' has the highest frequency (30 occurrences), we
would expect it to be closest to the top of the Huffman tree.

### Final Percentage of Vowels in the Word


The percentage of vowels calculated earlier for the word "faded" is **40%**.

10) Compress ‘BILL BEATS BEN.’ using the Huffman approach. [ 5 Marks]
To compress the string "BILL BEATS BEN." using the Huffman coding approach, we first
need to determine the frequency of each character in the string.

### Step 1: Count Character Frequencies

Here's the breakdown of the characters in "BILL BEATS BEN.":

| Character | Frequency |
|-----------|-----------|
|B |3 |
|I |2 |
|L |2 |
|E |2 |
|A |1 |
|T |1 |
|S |1 |
|N |1 |
|. |1 |
| (space) | 2 |

### Step 2: Create the Huffman Tree

1. **Initial List of Frequencies**:


- (B, 3), (I, 2), (L, 2), (E, 2), (A, 1), (T, 1), (S, 1), (N, 1), (., 1), (space, 2)

2. **Combine the Two Smallest Frequencies**:


- Combine (A, 1) and (T, 1) → New Node (AT, 2)
- List: (B, 3), (I, 2), (L, 2), (E, 2), (space, 2), (S, 1), (N, 1), (., 1), (AT, 2)

3. **Combine Again**:
- Combine (S, 1) and (N, 1) → New Node (SN, 2)
- List: (B, 3), (I, 2), (L, 2), (E, 2), (space, 2), (SN, 2), (., 1), (AT, 2)

4. **Combine Again**:
- Combine (., 1) and (AT, 2) → New Node (AT., 3)
- List: (B, 3), (I, 2), (L, 2), (E, 2), (space, 2), (SN, 2), (AT., 3)

5. **Combine Again**:
- Combine (I, 2) and (L, 2) → New Node (IL, 4)
- List: (B, 3), (E, 2), (space, 2), (SN, 2), (AT., 3), (IL, 4)

6. **Combine Again**:
- Combine (E, 2) and (space, 2) → New Node (Espace, 4)
- List: (B, 3), (SN, 2), (AT., 3), (IL, 4), (Espace, 4)

7. **Combine Again**:
- Combine (SN, 2) and (B, 3) → New Node (BSN, 5)
- List: (AT., 3), (IL, 4), (Espace, 4), (BSN, 5)

8. **Combine Again**:
- Combine (AT., 3) and (Espace, 4) → New Node (AT.Espace, 7)
- List: (IL, 4), (BSN, 5), (AT.Espace, 7)

9. **Final Combine**:
- Combine (IL, 4) and (BSN, 5) → New Node (ILBSN, 9)
- Combine (ILBSN, 9) and (AT.Espace, 7) → Root Node

### Step 3: Generate Codes

Based on the Huffman tree constructed (not fully detailed here), you would assign binary
codes as follows:

- **B**: 00
- **I**: 010
- **L**: 011
- **E**: 10
- **A**: 1100
- **T**: 1101
- **S**: 11100
- **N**: 11101
- **.**: 11110
- **(space)**: 11111

### Step 4: Compress the String

Now, we can compress the string "BILL BEATS BEN." using the generated Huffman
codes.

**Original String**: BILL BEATS BEN.

**Compressed Representation**:
- B → 00
- I → 010
- L → 011
- L → 011
- (space) → 11111
- B → 00
- E → 10
- A → 1100
- T → 1101
- S → 11100
- (space) → 11111
- B → 00
- E → 10
- N → 11101
- . → 11110

**Complete Compressed Code**:


`00010001101111110010110011011100111110010111101`

This binary string represents the compressed version of "BILL BEATS BEN." using
Huffman coding.

11)Construct the Binary search tree for values 15, 20, 10, 8, 25, 12, 16.
Write the inorder successor of 12 and 15 from the constructed tree.
To construct a Binary Search Tree (BST) with the values 15, 20, 10, 8, 25, 12, and 16, we
will insert the values in the given order.

### Step 1: Insert Values into the BST

1. **Insert 15**:
- Tree:
```
15
```

2. **Insert 20**:
- Tree:
```
15
\
20
```

3. **Insert 10**:
- Tree:
```
15
/ \
10 20
```

4. **Insert 8**:
- Tree:
```
15
/ \
10 20
/
8
```
5. **Insert 25**:
- Tree:
```
15
/ \
10 20
/ \
8 25
```

6. **Insert 12**:
- Tree:
```
15
/ \
10 20
/\ \
8 12 25
```

7. **Insert 16**:
- Tree:
```
15
/ \
10 20
/\ \
8 12 25
/
16
```

### Final BST Structure


```
15
/ \
10 20
/\ \
8 12 25
/
16
```

### Step 2: Find Inorder Successors

**Inorder Successor**:
The inorder successor of a node is the node with the smallest key greater than the key of the
node.

1. **Inorder Successor of 12**:


- The inorder traversal of the tree yields: 8, 10, 12, 15, 16, 20, 25.
- The inorder successor of 12 is **15**.

2. **Inorder Successor of 15**:


- Continuing the inorder traversal, the successor of 15 is **16**.

### Summary
- **Inorder Successor of 12**: 15
- **Inorder Successor of 15**: 16 [ 5 Marks]

You might also like