Skip to content

Commit 9668d2c

Browse files
Merge pull request #741 from G7-TheKing/master
Knapsack Java and Sudoku Python implementation
2 parents ffac6ec + ddc55a8 commit 9668d2c

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# A Backtracking program in Python to solve Sudoku problem
2+
3+
4+
# A Utility Function to print the Grid
5+
def print_grid(arr):
6+
for i in range(9):
7+
for j in range(9):
8+
print arr[i][j],
9+
print ('n')
10+
11+
12+
# Function to Find the entry in the Grid that is still not used
13+
# Searches the grid to find an entry that is still unassigned. If
14+
# found, the reference parameters row, col will be set the location
15+
# that is unassigned, and true is returned. If no unassigned entries
16+
# remain, false is returned.
17+
# 'l' is a list variable that has been passed from the solve_sudoku function
18+
# to keep track of incrementation of Rows and Columns
19+
def find_empty_location(arr,l):
20+
for row in range(9):
21+
for col in range(9):
22+
if(arr[row][col]==0):
23+
l[0]=row
24+
l[1]=col
25+
return True
26+
return False
27+
28+
# Returns a boolean which indicates whether any assigned entry
29+
# in the specified row matches the given number.
30+
def used_in_row(arr,row,num):
31+
for i in range(9):
32+
if(arr[row][i] == num):
33+
return True
34+
return False
35+
36+
# Returns a boolean which indicates whether any assigned entry
37+
# in the specified column matches the given number.
38+
def used_in_col(arr,col,num):
39+
for i in range(9):
40+
if(arr[i][col] == num):
41+
return True
42+
return False
43+
44+
# Returns a boolean which indicates whether any assigned entry
45+
# within the specified 3x3 box matches the given number
46+
def used_in_box(arr,row,col,num):
47+
for i in range(3):
48+
for j in range(3):
49+
if(arr[i+row][j+col] == num):
50+
return True
51+
return False
52+
53+
# Checks whether it will be legal to assign num to the given row,col
54+
# Returns a boolean which indicates whether it will be legal to assign
55+
# num to the given row,col location.
56+
def check_location_is_safe(arr,row,col,num):
57+
58+
# Check if 'num' is not already placed in current row,
59+
# current column and current 3x3 box
60+
return not used_in_row(arr,row,num) and not used_in_col(arr,col,num) and not used_in_box(arr,row - row%3,col - col%3,num)
61+
62+
# Takes a partially filled-in grid and attempts to assign values to
63+
# all unassigned locations in such a way to meet the requirements
64+
# for Sudoku solution (non-duplication across rows, columns, and boxes)
65+
def solve_sudoku(arr):
66+
67+
# 'l' is a list variable that keeps the record of row and col in find_empty_location Function
68+
l=[0,0]
69+
70+
# If there is no unassigned location, we are done
71+
if(not find_empty_location(arr,l)):
72+
return True
73+
74+
# Assigning list values to row and col that we got from the above Function
75+
row=l[0]
76+
col=l[1]
77+
78+
# consider digits 1 to 9
79+
for num in range(1,10):
80+
81+
# if looks promising
82+
if(check_location_is_safe(arr,row,col,num)):
83+
84+
# make tentative assignment
85+
arr[row][col]=num
86+
87+
# return, if sucess, ya!
88+
if(solve_sudoku(arr)):
89+
return True
90+
91+
# failure, unmake & try again
92+
arr[row][col] = 0
93+
94+
# this triggers backtracking
95+
return False
96+
97+
# Driver main function to test above functions
98+
if __name__=="__main__":
99+
100+
# creating a 2D array for the grid
101+
grid=[[0 for x in range(9)]for y in range(9)]
102+
103+
# assigning values to the grid
104+
grid=[[3,0,6,5,0,8,4,0,0],
105+
[5,2,0,0,0,0,0,0,0],
106+
[0,8,7,0,0,0,0,3,1],
107+
[0,0,3,0,1,0,0,8,0],
108+
[9,0,0,8,6,3,0,0,5],
109+
[0,5,0,0,9,0,6,0,0],
110+
[1,3,0,0,0,0,2,5,0],
111+
[0,0,0,0,0,0,0,7,4],
112+
[0,0,5,2,0,6,3,0,0]]
113+
114+
# if sucess print the grid
115+
if(solve_sudoku(grid)):
116+
print_grid(grid)
117+
else:
118+
print "No solution exists"

Greedy/Knapsack/frac_knapsack.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Java program to solve fractional Knapsack Problem
2+
import java.util.Arrays;
3+
import java.util.Comparator;
4+
5+
//Greedy approach
6+
public class FractionalKnapSack {
7+
//Time complexity O(n log n)
8+
public static void main(String[] args){
9+
int[] wt = {10, 40, 20, 30};
10+
int[] val = {60, 40, 100, 120};
11+
int capacity = 50;
12+
13+
double maxValue = getMaxValue(wt, val, capacity);
14+
System.out.println("Maximum value we can obtain = "+maxValue);
15+
16+
}
17+
18+
// // function to get maximum value
19+
private static double getMaxValue(int[] wt, int[] val, int capacity){
20+
ItemValue[] iVal = new ItemValue[wt.length];
21+
22+
for(int i = 0; i < wt.length; i++){
23+
iVal[i] = new ItemValue(wt[i], val[i], i);
24+
}
25+
26+
//sorting items by value;
27+
Arrays.sort(iVal, new Comparator<ItemValue>() {
28+
@Override
29+
public int compare(ItemValue o1, ItemValue o2) {
30+
return o2.cost.compareTo(o1.cost) ;
31+
}
32+
});
33+
34+
35+
double totalValue = 0d;
36+
37+
for(ItemValue i: iVal){
38+
39+
int curWt = (int) i.wt;
40+
int curVal = (int) i.val;
41+
42+
if (capacity - curWt >= 0){//this weight can be picked while
43+
capacity = capacity-curWt;
44+
totalValue += curVal;
45+
46+
}else{//item cant be picked whole
47+
48+
double fraction = ((double)capacity/(double)curWt);
49+
totalValue += (curVal*fraction);
50+
capacity = (int)(capacity - (curWt*fraction));
51+
break;
52+
}
53+
54+
55+
}
56+
57+
return totalValue;
58+
59+
}
60+
61+
// item value class
62+
static class ItemValue {
63+
Double cost;
64+
double wt, val, ind;
65+
66+
// item value function
67+
public ItemValue(int wt, int val, int ind){
68+
this.wt = wt;
69+
this.val = val;
70+
this.ind = ind;
71+
cost = new Double(val/wt );
72+
}
73+
74+
}
75+
76+
}

0 commit comments

Comments
 (0)