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

Micro Projects

Algorithm mini project

Uploaded by

ashayamal2003
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)
55 views

Micro Projects

Algorithm mini project

Uploaded by

ashayamal2003
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/ 6

Siddaganga Institute of Technology

(An autonomous institution affiliated to VTU, Belagavi, Approved by AICTE, New Delhi,
Accredited by NAAC with 'A++' grade & ISO 9001:2015 Certified)
Department of Computer Science & Engineering
(Program Accredited by NBA)

Course Title: Design and Analysis of Algorithms Course Code: S4CCS01


Semester: III CSE ‘A’ Faculty: Sowmya M N
Activity Based Learning 2023-24 Even Semester

Group Activity :– Micro projects

1. Teams will be formed comprising of 2 students.


2. Each team is assigned with a problem.
3. Teams will implement the problem given and present the same.
4. The report on the implementation will be submitted by each team.
Design and Simulate the Working of the following Algorithms
1. Euclid’s game (see [Bog]) starts with two unequal positive integers on the board. Two
players move in turn. On each move, a player has to write on the board a positive number
equal to the difference of two numbers already on the board; this number must be new, i.e.,
different from all the numbers already on the board. The player who cannot move loses the
game.
2. Locker doors there are n lockers in a hallway, numbered sequentially from 1 to n. Initially, all
the locker doors are closed. You make n passes by the lockers, each time starting with locker
#1. On the i th pass, i = 1, 2, . . . , n, you toggle the door of every ith locker: if the door is
closed, you open it; if it is open, you close it. After the last pass, which locker doors are open
and which are closed? How many of them are open?
3. Icosian Game
4. Graph-coloring problem
5. Gaussian elimination
6. Lighter or heavier? You have n > 2 identical-looking coins and a two-pan balance scale with
no weights. One of the coins is a fake, but you do not know whether it is lighter or heavier
than the genuine coins, which all weigh the same. Design a (1) algorithm to determine
whether the fake coin is lighter or heavier than the others.
7. Door in a wall You are facing a wall that stretches infinitely in both directions. There is a door
in the wall, but you know neither how far away nor in which direction. You can see the door
only when you are right next to it. Design an algorithm that enables you to reach the door by
walking at most O(n) steps where n is the (unknown to you) number of steps between your
initial position and the door.
8. von Neumann’s neighborhood Consider the algorithm that starts with a single square and on
each of its n iterations adds new squares all around the outside. How many one-by-one
squares are there after n iterations?
9. Frying hamburgers There are n hamburgers to be fried on a small grill that can hold only two
hamburgers at a time. Each hamburger has to be fried on both sides; frying one side of a
Siddaganga Institute of Technology
(An autonomous institution affiliated to VTU, Belagavi, Approved by AICTE, New Delhi,
Accredited by NAAC with 'A++' grade & ISO 9001:2015 Certified)
Department of Computer Science & Engineering
(Program Accredited by NBA)

hamburger takes 1 minute, regardless of whether one or two hamburgers are fried at the
same time. Consider the following recursive algorithm for executing this task in the
minimum amount of time. If n ≤ 2, fry the hamburger or the two hamburgers together on
each side. If n > 2, fry any two hamburgers together on each side and then apply the same
procedure recursively to the remaining n − 2 hamburgers.
10. Celebrity problem A celebrity among a group of n people is a person who knows nobody but
is known by everybody else. The task is to identify a celebrity by only asking questions to
people of the form “Do you know him/her?” Design an efficient algorithm to identify a
celebrity or determine that the group has no such person. How many questions does your
algorithm need in the worst case?
11. Fibonacci’s rabbits problem A man put a pair of rabbits in a place surrounded by a wall. How
many pairs of rabbits will be there in a year if the initial pair of rabbits (male and female) are
newborn and all rabbit pairs are not fertile during their first month of life but thereafter give
birth to one new male/female pair at the end of every month?
12. A network topology specifies how computers, printers, and other devices are connected
over a network. The three common topologies of networks: the ring, the star, and the fully
connected mesh.
You are given a boolean matrix A[0..n − 1, 0..n − 1], where n > 3, which is supposed to be the
adjacency matrix of a graph modeling a network with one of these topologies. Your task is to
determine which of these three topologies, if any, the matrix represents. Design a brute-
force algorithm for this task and indicate its time efficiency class.
13. stack of fake coins There are n stacks of n identical-looking coins. All of the coins in one of
these stacks are counterfeit, while all the coins in the other stacks are genuine. Every
genuine coin weighs 10 grams; every fake weighs 11 grams. You have an analytical scale that
can determine the exact weight of any number of coins.
a. Devise a brute-force algorithm to identify the stack with the fake coins and determine its
worst-case efficiency class.
b. What is the minimum number of weighings needed to identify the stack with the fake
coins?

14. Alternating disks You have a row of 2n disks of two colors, n dark and n light. They alternate:
dark, light, dark, light, and so on. You want to get all the dark disks to the right-hand end, and
all the light disks to the left-hand end. The only moves you are allowed to make are those that
interchange the positions of two neighboring disks. Design an algorithm for solving this puzzle
and determine the number of moves it takes.

15. Word Find A popular diversion in the United States, “word find” (or “word search”) puzzles
ask the player to find each of a given set of words in a square table filled with single letters. A
word can read horizontally (left or right), vertically (up or down), or along a 45 degree
diagonal (in any of the four directions) formed by consecutively adjacent cells of the table; it
may wrap around the table’s boundaries, but it must read in the same direction with no
Siddaganga Institute of Technology
(An autonomous institution affiliated to VTU, Belagavi, Approved by AICTE, New Delhi,
Accredited by NAAC with 'A++' grade & ISO 9001:2015 Certified)
Department of Computer Science & Engineering
(Program Accredited by NBA)

zigzagging. The same cell of the table may be used in different words, but, in a given word, the
same cell may be used no more than once.

16. Battleship game Write a program based on a version of brute-force pattern matching for
playing the game Battleship on the computer. The rules of the game are as follows. There are
two opponents in the game (in this case, a human player and the computer). The game is
played on two identical boards (10 × 10 tables of squares) on which each opponent places his
or her ships, not seen by the opponent. Each player has five ships, each of which occupies a
certain number of squares on the board: a destroyer (two squares), a submarine (three
squares), a cruiser (three squares), a battleship (four squares), and an aircraft carrier (five
squares). Each ship is placed either horizontally or vertically, with no two ships touching each
other. The game is played by the opponents taking turns “shooting” at each other’s ships. The
result of every shot is displayed as either a hit or a miss. In case of a hit, the player gets to go
again and keeps playing until missing. The goal is to sink all the opponent’s ships before the
opponent succeeds in doing it first. To sink a ship, all squares occupied by the ship must be hit.

17. Odd pie fight there are n ≥ 3 people positioned on a field (Euclidean plane) so that each has a
unique nearest neighbor. Each person has a cream pie. At a signal, everybody hurls his or her
pie at the nearest neighbor. Assuming that n is odd and that nobody can miss his or her target,
true or false: There always remains at least one person not hit by a pie.

18. Magic squares A magic square of order n is an arrangement of the integers from 1 to n2 in an
n × n matrix, with each number occurring exactly once, so that each row, each column, and
each main diagonal has the same sum.

a. Prove that if a magic square of order n exists, the sum in question must be equal to n(n2 + 1)/2.

19. Design an exhaustive-search algorithm for generating all magic squares of order n. 19.
Famous alphametic A puzzle in which the digits in a correct mathematical expression, such as a
sum, are replaced by letters is called cryptarithm; if, in addition, the puzzle’s words make sense, it
is said to be an alphametic. The most well-known alphametic was published by the renowned
British puzzlist Henry E. Dudeney (1857–1930):

S E ND
+MORE

MONEY
Two conditions are assumed: first, the correspondence between letters and decimal digits is one
to-one, i.e., each letter represents one digit only and different letters represent different digits.
Second, the digit zero does not appear as the left-most digit in any of the numbers. To solve an
alphametic means to find which digit each letter represents. Note that a solution’s uniqueness
cannot be assumed and has to be verified by the solver.
Siddaganga Institute of Technology
(An autonomous institution affiliated to VTU, Belagavi, Approved by AICTE, New Delhi,
Accredited by NAAC with 'A++' grade & ISO 9001:2015 Certified)
Department of Computer Science & Engineering
(Program Accredited by NBA)

a. Write a program for solving cryptarithms by exhaustive search. Assume that a given
cryptarithm is a sum of two words.

20. A graph is said to be bipartite if all its vertices can be partitioned into two disjoint subsetsX
and Y so that every edge connects a vertex in X with a vertex in Y . (One can also say that a graph
is bipartite if its vertices can be colored in two colors so that every edge has its vertices colored in
different colors; such graphs are also called 2-colorable.)

a. Design a DFS-based algorithm for checking whether a graph is bipartite.

b. Design a BFS-based algorithm for checking whether a graph is bipartite.

21. Three Jugs Sim´eon Denis Poisson (1781–1840), a famous French mathematician and
physicist, is said to have become interested in mathematics after encountering some version of
the following old puzzle. Given an 8-pint jug full of water and two empty jugs of 5- and 3-pint
capacity, get exactly 4 pints of water in one of the jugs by completely filling up and/or emptying
jugs into others. Solve this puzzle by using breadth-first search.

22. Ferrying soldiers A detachment of n soldiers must cross a wide and deep river with no bridge
in sight. They notice two 12-year-old boys playing in a rowboat by the shore. The boat is so tiny,
however, that it can only hold two boys or one soldier. How can the soldiers get across the river
and leave the boys in joint possession of the boat? How many times need the boat pass from
shore to shore?

23. Alternating glasses a. There are 2n glasses standing next to each other in a row, the first n of
them filled with a soda drink and the remaining n glasses empty. Make the glasses alternate in a
filled-empty-filled-empty pattern in the minimum number of glass moves.

b. Solve the same problem if 2n glasses—n with a drink and n empty—are initially in a random
order.

24. Marking cells Design an algorithm for the following task. For any even n, mark n cells on an
infinite sheet of graph paper so that each marked cell has an odd number of marked neighbors.
Two cells are considered neighbors if they are next to each other either horizontally or vertically
but not diagonally. The marked cells must form a contiguous region, i.e., a region in which there
is a path between any pair of marked cells that goes through a sequence of marked neighbors.

25. Team ordering You have the results of a completed round-robin tournament in which n teams
played each other once. Each game ended either with a victory for one of the teams or with a tie.
Design an algorithm that lists the teams in a sequence so that every team did not lose the game
with the team listed immediately after it.What is the time efficiency class of your algorithm?

26. Implement shellsort, straight insertion sort, selection sort, and bubble sort in the language of
your choice and compare their performance on random arrays of sizes 10n for n = 2, 3, 4, 5, and 6
as well as on increasing and decreasing arrays of these sizes.
Siddaganga Institute of Technology
(An autonomous institution affiliated to VTU, Belagavi, Approved by AICTE, New Delhi,
Accredited by NAAC with 'A++' grade & ISO 9001:2015 Certified)
Department of Computer Science & Engineering
(Program Accredited by NBA)

27. Fair attraction In olden days, one could encounter the following attraction at a fair. A light
bulb was connected to several switches in such a way that it lighted up only when all the switches
were closed. Each switch was controlled by a push button; pressing the button toggled the
switch, but there was no way to know the state of the switch. The object was to turn the light
bulb on. Design an algorithm to turn on the light bulb with the minimum number of button
pushes needed in the worst case for n switches.

28. Moldy chocolate Two players take turns by breaking an m × n chocolate bar, which has one
spoiled 1× 1 square. Each break must be a single straight line cutting all the way across the bar
along the boundaries between the squares. After each break, the player who broke the bar last
eats the piece that does not contain the spoiled square. The player left with the spoiled square
loses the game. Is it better to go first or second in this game?

29. Flipping pancakes There are n pancakes all of different sizes that are stacked on top of each
other. You are allowed to slip a flipper under one of the pancakes and flip over the whole stack
above the flipper. The purpose is to arrange pancakes according to their size with the biggest at
the bottom. Design an algorithm for solving this puzzle.

30. Tromino puzzle Atromino (more accurately, a right tromino) is an L-shaped tile formed by
three 1× 1 squares. The problem is to cover any 2n × 2n chessboard with a missing square with
trominoes. Trominoes can be oriented in an arbitrary way, but they should cover all the squares
of the board except the missing one exactly and with no overlaps. Design a divide-and-conquer
algorithm for this problem.

31. The Dutch national flag problem is to rearrange an array of characters R, W, and B (red, white,
and blue are the colors of the Dutch national flag) so that all the R’s come first, theW’s come
next, and the B’s come last. Design a linear in-place algorithm for this problem.

b. Explain how a solution to the Dutch national flag problem can be used in quicksort.

32. Nuts and bolts You are given a collection of n bolts of different widths and n corresponding
nuts. You are allowed to try a nut and bolt together, from which you can determine whether the
nut is larger than the bolt, smaller than the bolt, or matches the bolt exactly. However, there is
no way to compare two nuts together or two bolts together. The problem is to match each bolt
to its nut. Design an algorithm for this problem with average-case efficiency in _(n log n).

33. Given a set of n ≥ 3 points in the Cartesian plane, connect them in a simple polygon, i.e., a
closed path through all the points so that its line segments (the polygon’s edges) do not intersect
(except for neighboring edges at their common vertex). Design a reasonably efficient algorithm
for solving this problem and indicate its efficiency class.

34. Jealous husbands There are n ≥ 2 married couples who need to cross a river. They have a boat
that can hold no more than two people at a time. To complicate matters, all the husbands are
jealous and will not agree on any crossing procedure that would put a wife on the same bank
Siddaganga Institute of Technology
(An autonomous institution affiliated to VTU, Belagavi, Approved by AICTE, New Delhi,
Accredited by NAAC with 'A++' grade & ISO 9001:2015 Certified)
Department of Computer Science & Engineering
(Program Accredited by NBA)

of the river with another woman’s husband without the wife’s husband being there too, even
if there are other people on the same bank. Can they cross the river under such constraints?

a. Solve the problem for n = 2.

b. Solve the problem for n = 3, which is the classical version of this problem.

c. Does the problem have a solution for n ≥ 4? If it does, indicate how many river crossings it will
take; if it does not, explain why.

35. Least distance sorting There are 10 Egyptian stone statues standing in a row in an art gallery
hall. A new curator wants to move them so that the statues are ordered by their height. How
should this be done to minimize the total distance that the statues are moved? You may
assume for simplicity that all the statues have different heights.

36. Simulate Horspool’s algorithm(for DNA sequence).

37. Shortest-path counting A chess rook can move horizontally or vertically to any square in the
same row or in the same column of a chessboard. Find the number of shortest paths by which
a rook can move from one corner of a chessboard to the diagonally opposite corner. The
length of a path is measured by the number of squares it passes through, including the first
and the last squares. Solve the problem by a dynamic programming algorithm.

38. Jack Straws In the game of Jack Straws, a number of plastic or wooden “straws” are dumped
on the table and players try to remove them one by one without disturbing the other straws.
Here, we are only concerned with whether various pairs of straws are connected by a path of
touching straws. Given a list of the endpoints forn>1straws (as if they were dumped on a large
piece of graph paper), determine all the pairs of straws that are connected. Note that
touching is connecting, but also that two straws can be connected indirectly via other
connected straws(Dynamic programming approach).

39. Puzzle pegs This puzzle-like game is played on a board with 15 small holes arranged in an
equilateral triangle. In an initial position, all but one of the holes are occupied by pegs, as in
the example shown below. A legal move is a jump of a peg over its immediate neighbor into
an empty square opposite; the jump removes the jumped-over neighbor from the board.
Design and implement a backtracking algorithm for solving the following versions of this puzzle.

a. Starting with a given location of the empty hole, find a shortest sequence of moves that
eliminates 14 pegs with no limitations on the final position of the remaining peg.
b. Starting with a given location of the empty hole, find a shortest sequence of moves that
eliminates 14 pegs with the remaining peg at the empty hole of the initial board.

40. Simulate Travelling Salesman problem.

You might also like