Daas Solved 2019
Daas Solved 2019
Computer Engineering
2019 Pattern (Insem)
1. Importance of Correctness:
- Correctness ensures that the algorithm provides the right solution for every possible input.
- Without correctness, the algorithm might produce incorrect results, leading to errors in the system.
- Correct algorithms are reliable and can be trusted to perform the task they are designed for.
- By ensuring the loop invariant holds, we can prove that the algorithm is functioning as expected.
3. Example of Summing `n` Numbers Using Loop Invariant:
- Proof:
- Initialization: Before the loop, the sum is 0, which is correct for 0 numbers.
- Maintenance: If the sum is correct before an iteration, adding the next number keeps it correct.
- Termination: When the loop ends, the sum is correct for all `n` numbers.
1. Iterative Algorithm:
- An iterative algorithm repeats a set of instructions using loops until a condition is met.
- Example: Calculating the factorial of a number by multiplying all numbers from 1 to `n`.
2. Design Issues in Iterative Algorithms:
- Termination Condition: Must be clear when the loop should stop to avoid infinite loops.
- Efficiency: The algorithm should not perform unnecessary iterations to avoid wasting resources.
- Memory Usage:The algorithm should manage memory efficiently, especially in large loops.
- Correctness: The loop must maintain correctness throughout its execution by preserving loop invariants.
1. Proving Correctness:
- Prove that the algorithm works for all inputs by using methods like mathematical induction or loop
invariants.
- Start with a base case, then prove the algorithm works for the next step.
2. Using Counter Example:
- A counterexample is a specific case where the algorithm fails, proving that it is incorrect.
- Example: If a sorting algorithm doesn’t sort an unsorted array correctly, that unsorted array is a
counterexample.
Problem-Solving Strategies
- Solve problems by breaking them down into simpler subproblems and storing the results.
3. Greedy Algorithm:
- Make the best possible choice at each step with the hope of finding a global optimum.
- Example: Coin change problem.
4. Backtracking:
- Incrementally build solutions and backtrack when a solution fails to meet constraints.
- Example: Maze solving by trying different paths and backtracking when a dead end is reached.
What is Best, Average and Worst case Analysis of Algorithms? Analyse
j = i-1;
key = a[i];
a[j+1] = a[j];
j = j-1;
a[j+1] = key;
}
1. Introduction to Cases:
- Best Case: The minimum time an algorithm takes to complete. It occurs when the input is the most
favorable.
- Average Case: The expected time for the algorithm to complete, considering all possible inputs.
- Worst Case: The maximum time an algorithm takes. It occurs when the input is the least favorable.
1. P (Polynomial Time):
- Definition: Problems that can be solved by an algorithm in polynomial time, meaning the time it takes to
solve the problem grows at a reasonable rate as the size of the input increases.
- Example: Sorting a list of numbers using algorithms like merge sort or quicksort.
3. NP-Hard:
- Definition: Problems as hard as the hardest problems in NP. Solving an NP-hard problem means that all
NP problems can be solved in polynomial time.
- Example: The traveling salesman problem itself, as finding the shortest path is very challenging.
4. NP-Complete:
- Definition: Problems that are both in NP and NP-hard. If you can solve one NP-complete problem in
polynomial time, you can solve all NP problems in polynomial time.
- Example: The subset sum problem, where you determine if there’s a subset of numbers that adds up to
a given sum.
3-SAT Problem
1. Definition:
- A specific case of the Boolean satisfiability problem (SAT), where you have a Boolean expression in
Conjunctive Normal Form (CNF) with exactly three literals (variables or their negations) in each clause.
- Example: \((x_1 \vee \neg x_2 \vee x_3) \wedge (\neg x_1 \vee x_2 \vee \neg x_3)\), where you want to
determine if there's a way to assign true/false values to the variables that makes the entire expression true.
- It serves as a foundation for proving the complexity of other problems by reducing them to SAT .
Q4)
a) What is NP-complete class problem? How would you prove the vertex cover problem is NP-complete
class problem?
- Step 3: Conclusion:
Q4)
b) What is Best, Average, and Worst case Analysis of Algorithms? Analyze the following algorithm Best,
Average, and Worst case:
```java
int Linear-search(int a[], int n, int item) {
int i;
return a[i];
}
}
return -1;
}
```
- Best Case:
- Average Case:
- The item is in the middle of the array or the item is not present.
- Time Complexity: \( O(n) \) on average, where \( n \) is the number of elements in the array.
- Worst Case:
- The item is the last element in the array or not present at all.
- Time Complexity: \( O(n) \) because the algorithm has to check every element in the array.
In this way, the time complexity of the linear search is \( O(n) \) in both the average and worst cases, and
\( O(1) \) in the best case.