Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ If you want to uninstall algorithms, it is as simple as:
$ pip3 uninstall -y algorithms

## List of Implementations

- [arrays](algorithms/arrays)
- [delete_nth](algorithms/arrays/delete_nth.py)
- [flatten](algorithms/arrays/flatten.py)
Expand Down Expand Up @@ -146,6 +145,8 @@ If you want to uninstall algorithms, it is as simple as:
- [word_break](algorithms/dp/word_break.py)
- [fibonacci](algorithms/dp/fib.py)
- [hosoya triangle](algorithms/dp/hosoya_triangle.py)
- [Greedy](algorithms/arrays)
- [fractionalKnapsack](algorithms/Greedy/fractionalKnapsack.py)
- [graph](algorithms/graph)
- [check_bipartite](algorithms/graph/check_bipartite.py)
- [strongly_connected](algorithms/graph/check_digraph_strongly_connected.py)
Expand Down
Empty file added algorithms/Greedy/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions algorithms/Greedy/fractionalKnapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Python3 program to solve fractional
# Knapsack Problem

class ItemValue:

"""Item Value DataClass"""

def __init__(self, wt, val, ind):
self.wt = wt
self.val = val
self.ind = ind
self.cost = val // wt

def __lt__(self, other):
return self.cost < other.cost

# Greedy Approach


class FractionalKnapSack:

"""Time Complexity O(n log n)"""
@staticmethod
def get_max_value(wt, val, capacity):
"""function to get maximum value """
iVal = []
for i in range(len(wt)):
iVal.append(ItemValue(wt[i], val[i], i))

# sorting items by value
iVal.sort(reverse=True)

totalValue = 0
for i in iVal:
curWt = int(i.wt)
curVal = int(i.val)
if capacity - curWt >= 0:
capacity -= curWt
totalValue += curVal
else:
fraction = capacity / curWt
totalValue += curVal * fraction
capacity = int(capacity - (curWt * fraction))
break
return totalValue


# Driver Code
if __name__ == "__main__":
wt = [10, 40, 20, 35]
val = [60, 40, 100, 120]
capacity = 50


# Function call
maxValue = FractionalKnapSack.get_max_value(wt, val, capacity)
print("Maximum value in Knapsack =", maxValue)


38 changes: 38 additions & 0 deletions algorithms/Greedy/graphColoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
colors = ['Red', 'Blue', 'Green', 'Yellow', 'Black', 'Brown', 'Purple']
nodes = ['0', '1', '2', '3', '4']
neighbors = {}

neighbors['0'] = ['1', '2']
neighbors['1'] = ['0', '2', '3']
neighbors['2'] = ['0', '1', '3']
neighbors['3'] = ['1', '2', '4']
neighbors['4'] = ['3']

colors_of_nodes = {}


def promising(node, color):
for neighbor in neighbors.get(node):
color_of_neighbor = colors_of_nodes.get(neighbor)
if color_of_neighbor == color:
return False
return True


def get_color_for_node(node):
for color in colors:
if promising(node, color):
return color


def main():
for node in nodes:
colors_of_nodes[node] = get_color_for_node(node)

print(colors_of_nodes)


main()
listOfValues = colors_of_nodes.values()
listOfValues = list(set(listOfValues))
print("Chromatic Number is : " + str(len(listOfValues)))
23 changes: 23 additions & 0 deletions tests/test_fractionalKnapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
from algorithms.Greedy import (
fractionalKnapsack
)


class TestfractionalKnapsack(unittest.TestCase):

def test_get_max_value(self):
result = fractionalKnapsack.FractionalKnapSack.get_max_value([10, 20, 30], [60, 100, 120], 50)
self.assertEqual(result, 240.0)

def test_get_max_value(self):
result = fractionalKnapsack.FractionalKnapSack.get_max_value([12, 32, 33, 5, 34], [100, 200, 50, 60, 150], 50)
self.assertEqual(result, 364.4117647058824)

def test_get_max_value(self):
result = fractionalKnapsack.FractionalKnapSack.get_max_value([10, 40, 20, 24], [100, 280, 120, 120], 60)
self.assertEqual(result, 440.0)


if __name__ == "__main__":
unittest.main()