Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professor, Computer Science and Engineering Department

Similar presentations


Presentation on theme: "Professor, Computer Science and Engineering Department"— Presentation transcript:

1 Professor, Computer Science and Engineering Department
ALGORITHMIC THINKING G.H.Joshi Professor, Computer Science and Engineering Department BVB College of Engineering and Technology Vidyanagar, Hubli. E_mail :

2 Problem 1 A peasant finds himself on a riverbank with a wolf, a goat, and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room for only the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage. Solve this problem for the peasant or prove it has no solution. (Note: The peasant is a vegetarian but does not like cabbage and hence can eat neither the goat nor the cabbage to help him solve the problem. And it goes without saying that the wolf is a protected species.)

3 Solution Let P, w, g, and c stand for the peasant, wolf, goat, and cabbage head, respectively. The following is principal sequences that solve the problem: Intial Final BANK B P G G P W G W P W C W C PWGC R I V E R P W C C P G C P G BANK A 1 2 3 7 4 6 5

4 Solution Initially P,W,G and C are on BANK A
Person P moves from BANK A to BANK B with Goat G . Person P moves from BANK B to BANK A. Person P moves from BANK A to BANK B with Wolf W. Person P moves from BANK B to BANK A with Goat G . Person P moves from BANK A to BANK B with Cabbage C . Person P moves from BANK B to BANK A skip to

5 Problem 2 There are four people who want to cross a bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, for example. Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must walk together at the rate of the slower person’s pace. For example, if person 1 and person 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. If person 4 returns the flashlight, a total of 20 minutes have passed and you have failed the mission. (Note: According to a rumor on the Internet, interviewers at a well-known software company located near Seattle have given this problem to interviewees.)

6 Solution Let 1, 2, 5, 10 be labels representing the persons of the problem, f represent the flashlight’s location, and the number in the parenthesis be the total amount of time elapsed. The following sequence of moves solves the problem: BANK B f,1,2 2 f,2,5,10 5,10 f,1,2,5,10 R I I V E R f,1,5,10 1 BANK A (0) (2) (3) (13) (15) (17) Time in minutes

7 Solution 1. Initially all the persons 1,2,5,10 and flashlight are on BANK A. 2. Persons 1,2 move with flashlight from BANK A to BANK B. 3. Person 1 moves with flashlight from BANK B to BANK A. 4. Persons 5,10 moves with flashlight from BANK A to BANK B. 5. Person 2 moves with flashlight from BANK B to BANK A Persons 1,2 moves with flashlight from BANK A to BANK B. skip to

8 Algorithm Definition 1: An algorithm is a precise, step-by-step set of instructions for solving a task. Definition 2: An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. An algorithm does not solve a task; it gives you a series of steps that, if executed correctly, will result in a solution to a task.

9 Characteristics of an algorithm
Finiteness terminates after a finite number of steps Definiteness rigorously and unambiguously specified Input valid inputs are clearly specified Output can be proved to produce the correct output given a valid input Effectiveness steps are sufficiently simple and basic Eg1. Eg2.

10 Algorithmic Thinking It is the ability to understand, execute, evaluate and create algorithms. To be an algorithmic thinker you need the ability to understand and execute algorithms. Some people find it easy to follow a series of precise instructions while other people find it very challenging. Some people seem to lack the patience and diligence required to follow a step-by-step plan. However, it is a valuable skill that all people should master. Algorithmic thinking requires patience because each instruction must be executed in its correct sequence without skipping ahead or "glossing over" some of the instructions. In addition, algorithmic thinking requires diligence and perseverance.

11 Algorithmic thinking also requires the ability to evaluate algorithms
Algorithmic thinking also requires the ability to evaluate algorithms. This involves determining if an algorithm really does solve a given task. This can be very challenging. For example, a "preflight check list" is an algorithm for preparing an aircraft for take off. And finally, Algorithmic thinking includes the ability to create new algorithms. This is probably the most challenging aspect of algorithmic thinking. Given a task, can you create a series of precise, step-by-step instructions that always solves the task correctly? Obviously the complexity of the task has a big impact on the complexity of an algorithm that will accomplish the task. Simple tasks can typically be accomplished with simple algorithms, while complex tasks typically require more complex algorithms.

12 What do you observe here ?
Problem 3 7 6 5 1 What do you observe here ?

13 magic constant Here, sums of each row, each column, and each main diagonal are same and this sum is called magic constant . And this square, a magic square

14 Introduction to Magic Square
A magic square of order n is an arrangement of the numbers from 1 to n2 in an n-by-n matrix, with each number occurring exactly once ,so that each row, each column, and each main diagonal has the same sum.

15 Magic Square for n = 3 6 1 8 7 5 3 9 4 2 magic constant=15

16 Magic constant If a magic square of order n exists, the magic constant in question must be equal to n(n2 + 1)/2.

17 Algorithm to generate Magic square
Start with the middle entry of the top row . Place number 1 in that cell. Move one cell up and one to the left cell to this, you have to assume that the top of the row “wraps round” to the bottom row and the left column “wraps round” to the right column. If this cell is empty, write the next highest number in the sequence. If the cell is not empty move down one cell within the same column. Repeat step 3 to 5 until all cells have been completed. End.

18 What are the limitations of this algorithm?

19 Problem 4 A tromino is an l-shaped tile formed by 1-by-1 adjacent squares. The problem is to cover any 2n-by- 2n chessboard with one missing square (anywhere on the board) with trominos. rominos should cover all the squares except the missing one with no overlaps.

20 Solution solution

21 Problem 5 Towers of Hanoi: We have n discs of different sizes and 3 pegs. Initially all the discs are on the first peg in order of size, the largest at the bottom, and the smallest on the top. Goal: To move all the discs to the third peg using second one as an auxiliary if necessary. Constraints: 1.Move only one disc at a time. 2.It is forbidden to place a larger disc on top of smaller one. Demonstration of Towers of Hanoi

22 Algorithmic Solution to Tower of Hanoi
Iterative solution for an even number of disks: make the legal move between pegs A and B make the legal move between pegs A and C make the legal move between pegs B and C repeat until complete Iterative solution for an odd number of disks: In each case, a total of 2n-1 moves are made.

23 Recursive solution for ToH
To move n discs from peg A to peg C: move n−1 discs from A to B. This leaves disc #n alone on peg A move disc #n from A to C move n−1 discs from B to C so they sit on disc #n

24 Another important algorithmic thinking paradigm is recursion

25 Problem 6 Welding Problem: To find the shortest tour for the welding robot, through a given set of n welding spots

26 The problem gets transformed into a graph theory problem

27 Travelling Sales Persons Problem
Definition: To find the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it started. 2 b a 8 5 3 7 c d 1

28 Solution to TSP Tour Length Remarks a->b->c->d->a
= 18 a->b->d->c->a = 11 Optimal a->c->b->d->a = 23 a->c->d->b->a = 11 a->d->b->c->a = 23 a->d->c->b->a = 18

29 AIRWAYS Hubli (HBX) to London(LON)
- Click here for online page. (Just enter the FROM & TO textbox as above) - For images. - First Possibility. - Second Possibility.

30 RAILWAYS Hubli (UBL) to Manglore(MAQ)
- Click here for online page. (Please enter the Source & Destination in the textbox) - For images. - First Possibility. Click here for maps. - Second Possibility. Click here for maps. -

31 Back

32 Back

33 Back

34 Back

35 HUBLI Back ARSIKERE MANGALORE

36 HUBLI Back BANGALURU MANGALORE

37 Such problems are typical graph problems for which there exist algorithmic solutions.

38 ?

39 Thank you


Download ppt "Professor, Computer Science and Engineering Department"

Similar presentations


Ads by Google