0% found this document useful (0 votes)
10 views

FDS Q

The document outlines rules for converting infix expressions to postfix using a stack, detailing the process and providing an example conversion. It also describes three types of recursion, explains infix, prefix, and postfix expressions, and evaluates a postfix expression. Additionally, it discusses priority queues, their applications, and advantages over normal queues, as well as the backtracking algorithmic strategy and its applications.

Uploaded by

2011zaidkhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

FDS Q

The document outlines rules for converting infix expressions to postfix using a stack, detailing the process and providing an example conversion. It also describes three types of recursion, explains infix, prefix, and postfix expressions, and evaluates a postfix expression. Additionally, it discusses priority queues, their applications, and advantages over normal queues, as well as the backtracking algorithmic strategy and its applications.

Uploaded by

2011zaidkhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q5 (a) - Rules to Convert Infix to Postfix Using Stack

To convert an infix expression to a postfix expression using a stack, follow these rules:

1. Operands are added directly to the output.

2. Left Parenthesis ( is pushed onto the stack.

3. Right Parenthesis ) causes pop operations until a left parenthesis is encountered.

4. Operators are pushed onto the stack based on precedence and associativity.

o Higher precedence operators are pushed first.

o Operators of the same precedence follow associativity (left-to-right for +, -, *, /; right-to-left


for ^).

5. At the end, pop all remaining operators from the stack.

Example Conversion

Convert (A * B – (C + D * E) ^ (F * G / H)) to postfix:

1. Convert C + D * E:

o D * E → DE*

o C + DE* → CDE*+

2. Convert F * G / H:

o F * G → FG*

o FG* / H → FG*H/

3. Convert exponentiation:

o CDE*+ ^ FG*H/ → CDE*+FG*H/^

4. Convert A * B - (...):

o A * B → AB*

o AB* CDE*+FG*H/^ - → AB*CDE*+FG*H/^ -

Final Postfix Expression:


AB*CDE*+FG*H/^ -
Q5 (b) - Three Types of Recursion

1. Direct Recursion:
A function calls itself directly.

Example:

2. Indirect Recursion:
Two or more functions call each other in a cyclic manner.

3. Tail Recursion:
The recursive call is the last operation in the function.

Example:

Q6 (a) - Infix, Prefix, and Postfix Expressions

1. Infix Expression: Operator appears between operands.


Example: (A + B) * C

2. Prefix Expression: Operator appears before operands.


Example: * + A B C (equivalent to (A + B) * C)

3. Postfix Expression: Operator appears after operands.


Example: A B + C * (equivalent to (A + B) * C)

Evaluation of Postfix Expression

Consider the expression: 2 3 + 5 *


(Assume + is addition, * is multiplication)

Steps:

1. 23+→5

2. 5 5 * → 25

Final Result: 25
Explain priority queue and state its Application :

A Priority Queue is a special type of queue where each element has a priority, and elements are served based
on their priority rather than the order of arrival (FIFO).

1. Higher Priority Elements are Served First

o Unlike a normal queue (FIFO), elements with a higher priority are dequeued before lower-
priority elements.

2. Equal Priority Elements Follow FIFO Order

o If two elements have the same priority, they are dequeued based on the order in which they
were inserted.

Applications of Priority Queues

1. CPU Scheduling in Operating Systems

o The OS schedules processes based on priority (e.g., real-time tasks get higher priority).

2. Dijkstra’s Algorithm (Shortest Path in Graphs)

o It uses a Min-Priority Queue to process the closest unvisited node first.

3. A Algorithm (Artificial Intelligence & Game Pathfinding)*

o A* search algorithm prioritizes paths based on cost and heuristic values.

4. Huffman Coding (Data Compression in Files, ZIP, MP3, etc.)

o Huffman coding uses a priority queue to construct optimal prefix codes.

5. Job Scheduling in Printers

o Documents with a higher priority (e.g., urgent reports) get printed before others.

6. Network Routing (Load Balancing & Bandwidth Allocation)

o Data packets with higher importance (like video streaming) are sent before less important
packets.

Q One type of priority in detail :

Min-Priority Queue (Ascending Order Priority Queue) - Detailed Explanation

Definition:

A Min-Priority Queue is a type of priority queue where the element with the smallest value (highest priority)
is always removed first.

How it Works:

1. Insertion: Elements are added with their respective priority.

2. Deletion: The smallest element (highest priority) is dequeued first.


3. Processing: If two elements have the same priority, they follow FIFO order.

Example Scenario: Task Scheduling in an Operating System

Imagine an operating system (OS) scheduler where processes have a priority based on their
execution time (shorter jobs get executed first).

Example: Process Scheduling

Consider 4 processes with different execution times:

Process Execution Time (Priority)


P1 15 ms
P2 5 ms
P3 25 ms
P4 10 ms

Operation Time Complexity

Insertion (push) O(log n)

Deletion (pop) O(log n)

Access Minimum (top) O(1)

Q Advantages of Circular Queue over Normal (Linear) Queue:

1. Efficient Memory Utilization

In a Circular Queue, the rear pointer wraps around when it reaches the end, allowing efficient reuse of empty
spaces.

2 Avoids "Queue Full" Condition Unnecessarily

Circular Queue allows insertion at the front if there is space available.

3 Faster Insertions and Deletions

Insertions and deletions are always O(1) due to rear and front pointers

4 Suitable for Real-Time Applications

Q What is backtracking algorithmic Strategy

Backtracking is a systematic and recursive algorithmic technique used to solve problems incrementally by
trying possible solutions and discarding those that fail to meet the required conditions.

Example of Backtracking: N-Queens Problem


Applications of Backtracking

Solving Puzzles – Sudoku, Crossword, N-Queens


Graph Problems – Hamiltonian Cycle, Graph Coloring
String Generation – Generating all valid parentheses
Combinatorial Optimization – Subset Sum, Knapsack

You might also like