Skip to content

Commit d0569dd

Browse files
Merge pull request matthewsamuel95#366 from samarthsehgal97/br2
Added Activity Selection Greedy Algo
2 parents 1901238 + 63aadd8 commit d0569dd

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

BackTracking/RatInAMaze/ratmaze.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/* C/C++ program to solve Rat in a Maze problem using
3+
backtracking */
4+
#include<stdio.h>
5+
6+
// Maze size
7+
#define N 4
8+
9+
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);
10+
11+
/* A utility function to print solution matrix sol[N][N] */
12+
void printSolution(int sol[N][N])
13+
{
14+
for (int i = 0; i < N; i++)
15+
{
16+
for (int j = 0; j < N; j++)
17+
printf(" %d ", sol[i][j]);
18+
printf("\n");
19+
}
20+
}
21+
22+
/* A utility function to check if x,y is valid index for N*N maze */
23+
bool isSafe(int maze[N][N], int x, int y)
24+
{
25+
// if (x,y outside maze) return false
26+
if(x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)
27+
return true;
28+
29+
return false;
30+
}
31+
32+
/* This function solves the Maze problem using Backtracking. It mainly
33+
uses solveMazeUtil() to solve the problem. It returns false if no
34+
path is possible, otherwise return true and prints the path in the
35+
form of 1s. Please note that there may be more than one solutions,
36+
this function prints one of the feasible solutions.*/
37+
bool solveMaze(int maze[N][N])
38+
{
39+
int sol[N][N] = { {0, 0, 0, 0},
40+
{0, 0, 0, 0},
41+
{0, 0, 0, 0},
42+
{0, 0, 0, 0}
43+
};
44+
45+
if(solveMazeUtil(maze, 0, 0, sol) == false)
46+
{
47+
printf("Solution doesn't exist");
48+
return false;
49+
}
50+
51+
printSolution(sol);
52+
return true;
53+
}
54+
55+
/* A recursive utility function to solve Maze problem */
56+
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])
57+
{
58+
// if (x,y is goal) return true
59+
if(x == N-1 && y == N-1)
60+
{
61+
sol[x][y] = 1;
62+
return true;
63+
}
64+
65+
// Check if maze[x][y] is valid
66+
if(isSafe(maze, x, y) == true)
67+
{
68+
// mark x,y as part of solution path
69+
sol[x][y] = 1;
70+
71+
/* Move forward in x direction */
72+
if (solveMazeUtil(maze, x+1, y, sol) == true)
73+
return true;
74+
75+
/* If moving in x direction doesn't give solution then
76+
Move down in y direction */
77+
if (solveMazeUtil(maze, x, y+1, sol) == true)
78+
return true;
79+
80+
/* If none of the above movements work then BACKTRACK:
81+
unmark x,y as part of solution path */
82+
sol[x][y] = 0;
83+
return false;
84+
}
85+
86+
return false;
87+
}
88+
89+
// driver program to test above function
90+
int main()
91+
{
92+
int maze[N][N] = { {1, 0, 0, 0},
93+
{1, 1, 0, 1},
94+
{0, 1, 0, 0},
95+
{1, 1, 1, 1}
96+
};
97+
98+
solveMaze(maze);
99+
return 0;
100+
}

BackTracking/RatInAMaze/ratmaze.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
# Python3 program to solve Rat in a Maze
3+
# problem using backracking
4+
5+
# Maze size
6+
N = 4
7+
8+
# A utility function to print solution matrix sol
9+
def printSolution( sol ):
10+
11+
for i in sol:
12+
for j in i:
13+
print(str(j) + " ", end="")
14+
print("")
15+
16+
# A utility function to check if x,y is valid
17+
# index for N*N Maze
18+
def isSafe( maze, x, y ):
19+
20+
if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1:
21+
return True
22+
23+
return False
24+
25+
""" This function solves the Maze problem using Backtracking.
26+
It mainly uses solveMazeUtil() to solve the problem. It
27+
returns false if no path is possible, otherwise return
28+
true and prints the path in the form of 1s. Please note
29+
that there may be more than one solutions, this function
30+
prints one of the feasable solutions. """
31+
def solveMaze( maze ):
32+
33+
# Creating a 4 * 4 2-D list
34+
sol = [ [ 0 for j in range(4) ] for i in range(4) ]
35+
36+
if solveMazeUtil(maze, 0, 0, sol) == False:
37+
print("Solution doesn't exist");
38+
return False
39+
40+
printSolution(sol)
41+
return True
42+
43+
# A recursive utility function to solve Maze problem
44+
def solveMazeUtil(maze, x, y, sol):
45+
46+
#if (x,y is goal) return True
47+
if x == N - 1 and y == N - 1:
48+
sol[x][y] = 1
49+
return True
50+
51+
# Check if maze[x][y] is valid
52+
if isSafe(maze, x, y) == True:
53+
# mark x, y as part of solution path
54+
sol[x][y] = 1
55+
56+
# Move forward in x direction
57+
if solveMazeUtil(maze, x + 1, y, sol) == True:
58+
return True
59+
60+
# If moving in x direction doesn't give solution
61+
# then Move down in y direction
62+
if solveMazeUtil(maze, x, y + 1, sol) == True:
63+
return True
64+
65+
# If none of the above movements work then
66+
# BACKTRACK: unmark x,y as part of solution path
67+
sol[x][y] = 0
68+
return False
69+
70+
# Driver program to test above function
71+
if __name__ == "__main__":
72+
# Initialising the maze
73+
maze = [ [1, 0, 0, 0],
74+
[1, 1, 0, 1],
75+
[0, 1, 0, 0],
76+
[1, 1, 1, 1] ]
77+
78+
solveMaze(maze)
79+
80+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// C++ program for activity selection problem.
2+
// The following implementation assumes that the activities
3+
// are already sorted according to their finish time
4+
#include<stdio.h>
5+
6+
// Prints a maximum set of activities that can be done by a single
7+
// person, one at a time.
8+
// n --> Total number of activities
9+
// s[] --> An array that contains start time of all activities
10+
// f[] --> An array that contains finish time of all activities
11+
void printMaxActivities(int s[], int f[], int n)
12+
{
13+
int i, j;
14+
15+
printf ("Following activities are selected n");
16+
17+
// The first activity always gets selected
18+
i = 0;
19+
printf("%d ", i);
20+
21+
// Consider rest of the activities
22+
for (j = 1; j < n; j++)
23+
{
24+
// If this activity has start time greater than or
25+
// equal to the finish time of previously selected
26+
// activity, then select it
27+
if (s[j] >= f[i])
28+
{
29+
printf ("%d ", j);
30+
i = j;
31+
}
32+
}
33+
}
34+
35+
// driver program to test above function
36+
int main()
37+
{
38+
int s[] = {1, 3, 0, 5, 8, 5};
39+
int f[] = {2, 4, 6, 7, 9, 9};
40+
int n = sizeof(s)/sizeof(s[0]);
41+
printMaxActivities(s, f, n);
42+
return 0;
43+
}

Greedy/ActivitySelection/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Following is the problem statement.
2+
You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
3+
Example:
4+
5+
Example 1 : Consider the following 3 activities sorted by
6+
by finish time.
7+
start[] = {10, 12, 20};
8+
finish[] = {20, 25, 30};
9+
A person can perform at most two activities. The
10+
maximum set of activities that can be executed
11+
is {0, 2} [ These are indexes in start[] and
12+
finish[] ]
13+
14+
Example 2 : Consider the following 6 activities
15+
sorted by by finish time.
16+
start[] = {1, 3, 0, 5, 8, 5};
17+
finish[] = {2, 4, 6, 7, 9, 9};
18+
A person can perform at most four activities. The
19+
maximum set of activities that can be executed
20+
is {0, 1, 3, 4} [ These are indexes in start[] and
21+
finish[] ]
22+
23+
24+
The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as minimum finishing time activity.
25+
26+
1) Sort the activities according to their finishing time
27+
2) Select the first activity from the sorted array and print it.
28+
3) Do following for remaining activities in the sorted array.
29+
…….a) If the start time of this activity is greater than or equal to the finish time of previously selected activity then select this activity and print it.
30+
31+
In the following C implementation, it is assumed that the activities are already sorted according to their finish time

0 commit comments

Comments
 (0)