You are on page 1of 7

Write an algorithm of sum of subsets.

Solve following problem and draw


portion of state space tree M = 35, W = (5, 7, 10, 12, 15, 18, 20)

Initially subset = {} Sum = 0 Description

5 5 Then add next element.

5, 7 12,i.e. 12 < 35 Add next element.

5, 7, 10 22,i.e. 22 < 35 Add next element.

5, 7, 10, 12 34,i.e. 34 < 35 Add next element.

5, 7, 10, 12, 15 49 Sum exceeds M = 35. Hence backtrack.

5, 7, 10, 12, 18 52 Sum exceeds M = 35. Hence backtrack.

5, 7, 10, 12, 20 54 Sum exceeds M = 35. Hence backtrack.

5, 12, 15 32 Add next element.

5, 12, 15, 18 50 Not feasible. Therefore backtrack

5, 12, 18 35 Solution obtained as M = 35


2.State n-queens problem and Explain 8-queens problem using
backtracking.
Ans.
8 -Queens problem solution:
One possible solution to the 8 queens problem using backtracking is
shown below. In the first row, the queen is at E8 square, so we have to make
sure no queen is in column E and row 8 and also along its diagonals.
Similarly, for the second row, the queen is on the B7 square, thus, we have to
secure its horizontal, vertical, and diagonal squares. The same pattern is
followed for the rest of the queens.
Output:

00001000
01000000
00010000
00000010
00100000
00000001
00000100
10000000
A Red-Black Tree is a self-balancing binary search tree that maintains
balance during insertions and deletions. It is named "Red-Black"
because each node in the tree has an extra attribute, which is a color
that can be either red or black. The color of the nodes is used to ensure
that the tree remains balanced.
Here are the key properties of a Red-Black Tree:
1. Node Color: Each node is colored either red or black.
2. Root Property: The root is always black.
3. Leaf Property: Every leaf (NIL or null node) is black.
4. Red Property: If a red node has children, then the children are
always black.
5. Depth Property: For each node, any simple path from this node to
any of its descendant leaves contains the same number of black
nodes.
These properties ensure that the tree is approximately balanced,
preventing the tree from becoming too skewed and maintaining a height
of O(log n), where n is the number of nodes in the tree.
Operations:
Insertion:
When a new node is inserted into the Red-Black Tree, it is initially
colored red. After the insertion, the tree may violate the Red-Black
properties, so a series of rotations and color adjustments are performed
to restore the balance.
1. Color the new node red.
2. Rebalance the tree:
• If the parent of the newly inserted node is black, the tree
remains valid.
• If the parent is red, and the uncle of the new node is also
red, recolor the parent and uncle to black, and recolor the
grandparent to red. Then, recursively check and fix the Red-
Black properties starting from the grandparent.
• If the parent is red, but the uncle is black or null, perform
rotations and color adjustments to restore balance.
Deletion:
When a node is deleted from the Red-Black Tree, it is replaced by its
successor or predecessor, and then the tree is rebalanced.
1. If the node to be deleted has no children or one child:
• Remove the node and replace it with its child (or null).
• If the removed node is black, additional adjustments are
needed to maintain the Red-Black properties.
2. If the node to be deleted has two children:
• Find its in-order successor (or predecessor).
• Replace the node to be deleted with its successor (or
predecessor).
• Recursively delete the successor (which has at most one
child).
• If the successor or original node removed was black,
additional adjustments are needed.
Red-Black Tree Diagrams:
Here are diagrams illustrating a Red-Black Tree during the insertion
process:
These diagrams demonstrate the Red-Black Tree properties being
maintained during the insertion of nodes. Similar diagrams can be
created for deletion operations.
Remember that these diagrams are simplified for illustration, and real-
world Red-Black Trees may have additional nodes and complexities

You might also like