CSC 102 Lecture 2
CSC 102 Lecture 2
LECTURE 2
Introduction to the core concepts of
computing: Problems and problem-solving.
Definition: Trying different solutions and ruling out those that don’t work.
Example: Fixing a broken printer by checking ink levels, paper tray, or
connections.
Characteristics:
Useful with limited options.
Not time-efficient but commonly used.
5
Algorithm and Heuristic
Algorithm:
• Step-by-step problem-solving formula (e.g., recipes, search engine processes).
• Always produces the same result when followed exactly.
Heuristic:
• General problem-solving framework or "rule of thumb“ (a conventional way of doing things).
• Saves time and energy but not always the most rational method.
Types of Heuristics:
• Working Backwards:
• Focus on the end result to solve the problem.
Example:
• Characteristics:
• Lack of sophistication; takes the most direct or obvious route.
• Does not minimize the number of operations required.
• Example:
• Searching through a list of candidates to find a desired object.
• Analogy: Finding a frozen pie in an unfamiliar grocery store by searching each aisle.
• Padlock Example:
• A padlock with 4 digits (0-9).
• Forgot combination: try all possible combinations from 0000 to 9999.
• Worst case: 104=10,00010^4 = 10,000104=10,000 tries.
10
Computational Approaches for Solving Problems
Divide and Conquer Approach
• Definition:: Problem is solved recursively in three steps: Divide, Conquer, and Combine.
• Divide: Split the problem into smaller sub-problems.
• Conquer: Solve the sub-problems recursively.
• Combine: Merge solutions of sub-problems to solve the whole problem.
• Example:
• Merge Sort Algorithm:
• 1. Divide an array into two halves.
• 2. Recursively sort each half.
• 3. Combine the sorted halves.
Advantages
• Solves difficult problems by dividing into solvable sub-problems.
• Can lead to discovering efficient algorithms.
• Uses memory caches effectively.
• Suited for parallel execution.
Disadvantages
• Recursion can be slow.
• Sometimes more complicated than iterative approaches.
11
Computational Approaches for Solving Problems
Dynamic Programming Approach
• Definition:
• Similar to divide-and-conquer but reuses results of sub-problems.
• Bottom-up technique; solves smallest sub-problems first.
• Example:
• Fibonacci Series:
• Sequence: 0, 1, 1, 2, 3, ...
• Each term is the sum of the two preceding ones.
Definition:
• At each step, makes the choice with the smallest immediate cost.
• Does not look ahead to find the global optimum.
• Characteristics:
• Simple and efficient for certain optimization problems.
Comparison
• Dynamic Programming:
• Finds optimal solution by solving sub-problems and combining results.
• Greedy Algorithms:
• Look for locally optimal solutions, hoping to find a global optimum.
13
Computational Approaches for Solving Problems
Randomized Approach
Randomized Approach
• Definition:
• Depends on input data and values from a random number generator.
• Often more effective when determining the optimal choice is difficult.
• Applications:
• Ensures fairness in game-theoretic situations.
• Widely used in computer security and games.
14
Discussion
15