All about programming in GNU/LINUX

Algorithms

Dynamic Programming : Making change for an given amount with least number of coins

Helloooooooooo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Its been sometime since the last blog … loads of em are struggling to come out of the drafts ūüėõ I’ve been working on Dynamic programming concepts for a while now , So i thought of sharing my insights on the same by taking up the problem of finding ¬†change for the given amount using ¬†least possible no of currencies , and in the process i would love to explain what Dynamic programming is all about . Without wasting lot of time lets get into solving the problem .

Let me take an example scenario . Lets say you gotto make change for  11 cents and you have coins of 1,2,5  cents.  Now the question is what are various ways(the last step)  to reach to 11 cents considering you have coins of 1,2 and 5 cents ??? hmmm, thats not tough!

Add a 2 cents coin after having change for ¬† 9 cents ¬†(9 + 2 )= 11 cents | ¬†add 1 cents after having change for ¬†10 cents ( 10 +1)=11cents | ¬†add 5 cents after having change for ¬†6 cents (6 + 5)= 11 cents ¬†. Now we have 3 ways ( 9 + 2 , 10 + 1 , 6 + 5 ) But amongst these 3 approach which one would consist of minimal number of currencies or coins to make it to 11 ?? Hmm ….

Thats tricky !!!!! Well , that inturn depends on number of coins in the change for 9 , 10 and 6 cents. we have to consider the minimum amongst the 3 cases below

case 1: No.of.coins to make it to 9 cents (x) +  one  coin of 2 ceeents

case 2: No.of.coins to make it to 10 cents(y) + one coin of 1 cents (1)

case 3: No.of.coins to make it to 6 cents (z) + one coin of 5 cents (1)

In short we have to consider min(x+1 , y+1, z+1)

Now lets set  arbitrary values for x,y,z to be {3,2,2} respectively and Lets translate this logic into code for this particular value of amount for which we are supposed to find change using coins of 1,2,5 cents

Here is the code and explanation follows it , The names of variable used reflects their purpose . If you quickly want to execute and mess around with the code here is link https://ideone.com/M9qq4f . Here is the Github link of the code

amntForWhichChangeIsToBeFound = 11

coinsWeHaveUsingwhichChangeHastoBeFound = [1,2,5]

LastcoinUsedToGetChangeArray = [0]*(amntForWhichChangeIsToBeFound+1)  

numberOfCoinsUsedForChange = [0]*(amntForWhichChangeIsToBeFound+1)

coinCount = 11
#In the worst the change will contain 11 coins with all of them ,obviously  lastCoinUsedForChange also  one being 1 cent
LastCoinUsedforChange = 1

numberOfCoinsUsedForChange[9] = 3
 
numberOfCoinsUsedForChange[10] = 2

numberOfCoinsUsedForChange[6] = 2 


for j in [c for c in coinsWeHaveUsingwhichChangeHastoBeFound if c<=amntForWhichChangeIsToBeFound]:
    #List comprehension in python , iterates only through values of array which satisfies if condition
    if numberOfCoinsUsedForChange[amntForWhichChangeIsToBeFound - j] + 1 < coinCount:
	    coinCount = numberOfCoinsUsedForChange[amntForWhichChangeIsToBeFound - j] + 1
	    LastCoinUsedforChange = j 
numberOfCoinsUsedForChange[amntForWhichChangeIsToBeFound] = coinCount 
LastcoinUsedToGetChangeArray[amntForWhichChangeIsToBeFound] = LastCoinUsedforChange 
print (numberOfCoinsUsedForChange)
print (LastcoinUsedToGetChangeArray)

In 5 and line 7 of the above code there is declaration and initialization to 0

LastcoinUsedToGetChangeArray = [0]*(amntForWhichChangeIsToBeFound+1)  

numberOfCoinsUsedForChange = [0]*(amntForWhichChangeIsToBeFound+1)

The array element numberOfCoinsUsedForChange[i] contains the minimum number of coins used to obtain change for i cents . Thats is , numberOfCoinsUsedForChange[1] contains the minimum number of coins used to obtain change for 1 cent , numberOfCoinsUsedForChange[6] contains the minimum number of coins used to obtain change for 6 cent and it follows . But for now i have assigned arbitrary values to numberOfCoinsUsedForChange[6] , numberOfCoinsUsedForChange[9] , numberOfCoinsUsedForChange[10] , but in practical these values are to be computed .

Also the LastcoinUsedToGetChangeArray[i] contains the last coin used to get the change for i cents . LastcoinUsedToGetChangeArray[11] contains the last coin used in the change for 11 cents .

This sounds Ok , but You have now hard coded and assigned arbitrary values for inumberOfCoinsUsedForChange[6] , numberOfCoinsUsedForChange[9] , numberOfCoinsUsedForChange[10]  , but in real how do i have to compute in runtime ,how do i compute them ??

Yes , There are two ways to compute them . It can be recursively computed using top-down approach or iteratively computed using bottom-up approach . In the complete code sample later in the article ill be using the iterative approach .So using one of these approach the values are computed and the arrays are filled .
So now in this case to compute minimum number of coins required for change of 11 cents we need minimum number of coins required for change of 10,9 and 6 cents and those values are ¬†in turn are dependent on others(10 is dependent on 10 – [1,2,5],9 is dependent on 9 – [1,2,5] and so on …..) , so to solve this inter dependency ,one amongst either recursive or ¬†iterative approach can be used .

That sounds cool , but Size of both the arrays used is equal to the amount for which im finding change for , LastcoinUsedToGetChangeArray = [0]*(amntForWhichChangeIsToBeFound+1) allocates and initializes array of size = (amntForWhichChangeIsToBeFound+1) , why are we wasting so much of memory ??

Yes , in the previous question above I spoke about the interdependencies , these arrays are used to store these interdependencies to avoid re-computation . These re-computations are avoided by storing these values and using them instead of recomputing them every-time . Yes , This needs more memory to be able to store these values , but this is traded off with the speed , This potentially can bring an exponentially time complexed algorithm down to polynomial and this is the core idea of Dynamic Programming . Yes the full program later First we’ll build the numberOfCoinsUsedForChange[] array from bottom-up approach . That is first we’ll compute ¬†numberOfCoinsUsedForChange[1] , numberOfCoinsUsedForChange[2], numberOfCoinsUsedForChange[3] and so on and these stored values are used to compute the later values because these serve as dependencies and recomputation is avoided .

Now analyze the output of the above program ,

In the output LastcoinUsedToGetChangeArray[11] contains value 1 , so 1 cent is the last coin used in the change . Now we know that the last coin is 1 cent , If we could find the last coin used in change for 10 cents (11 -1 )  this gives us one more coin in the minimum change for 11 cents , and this is stored in LastcoinUsedToGetChangeArray[10] , if this process is recursively followed all the coins used in getting the change can be obtained . Using this process PrintCoin function is written which prints out the coins used .

Phewwwwwwww!! Now i believe i spoke about enough of background work required to understand the logic easily , now lets scale it up to be able work with any value .

Here is the code which finds out the number of coins used and the coins used to find change for given amount with given set of coins . Again if you quickly want to execute and hack around here is the link of the code on cloud IDE https://ideone.com/eHhAcB. If you want to fork and mess around with the code here is the link of the code on Github Github link of the code

def printCoins(amntForWhichChangeIsToBeFound,LastcoinUsedToGetChangeArray):
    coin = amntForWhichChangeIsToBeFound
    while coin > 0:
        LastCoinForCoinCent = LastcoinUsedToGetChangeArray[coin]
        print(LastCoinForCoinCent)
        coin = coin - LastCoinForCoinCent

def main():
    amntForWhichChangeIsToBeFound = 68
    #edit it for the value you want it for 
    coinsWeHaveUsingwhichChangeHastoBeFound = [1,5,10,21,25]
    LastcoinUsedToGetChangeArray = [0]*(amntForWhichChangeIsToBeFound+1)  
    numberOfCoinsUsedForChange = [0]*(amntForWhichChangeIsToBeFound+1)
    for cents in range(amntForWhichChangeIsToBeFound+1):
    	#This loop starts finding change from 1 cent, then 2,3,4..amntForWhichChangeIsToBeFound
        coinCount = cents
        LastCoinUsedforChange = 1
        for j in [c for c in coinsWeHaveUsingwhichChangeHastoBeFound if c <= cents]:
            if numberOfCoinsUsedForChange[cents - j] + 1 < coinCount:
    	        coinCount = numberOfCoinsUsedForChange[cents - j] + 1
    	        LastCoinUsedforChange = j 
        numberOfCoinsUsedForChange[cents] = coinCount 
        LastcoinUsedToGetChangeArray[cents] = LastCoinUsedforChange 
    #print (numberOfCoinsUsedForChange)
    #print (LastcoinUsedToGetChangeArray)	
    print("Number of coins used: "+ str(numberOfCoinsUsedForChange[amntForWhichChangeIsToBeFound]))
    print("Here are the coins used ")
    printCoins(amntForWhichChangeIsToBeFound, LastcoinUsedToGetChangeArray)

main()

Finallllllllly!! I’ll following up with few more posts on Dynamic programming and Golang implementation of HTTP/2 in the further posts to come …. Till then , Happy Coding ūüėÄ


Algorithms using python:Graph Algorithms-1:Depth First search

Firstly hello to all the readers !!After the last and its previous post on node.js , Here is the first post of the series of posts to come related to algorithms using python.In this post Ill be discussing about popular tree traversal algorithm Depth First Search . Using python makes the implementation of the algorithm relatively easy because of the availability of numerous built in data structures like hashes(dictionaries) ……….In this this blog post ill be using dictionaries as the main data structure for most of the operations .I‚Äôve tried my best to enhance the degree of detailing in the post so that folks with even least acquaintance with python can understand the code . So then what are we waiting for??? Lets begin ūüėõ

1.REPRESENTATION OF GRAPH USING DICTIONARIES IN PYTHON

A graph will represented using a JSON like structure . here is an example …

Consider the following graph

Image

Here node A is connected to nodes B,C and E and this is represented as described below

{

‘A’:{‘B’:1,’C’:1 }

}

Using the similar approach here is the representation of the complete graph

{

‘A’:{‘B’:1,’C’:1,’E’:1},

‘B’:{‘A’:1,’D’:1,’F’},

‘C’:{‘A’:1,’G’:1},

‘D’:{},

‘E’:{‘A’:1,’F’:1},

‘F’:{‘B’:1,’E’:1},

‘G’:{}

}

  • Declaration of a dictionary

Declaring an empty dictionary in python very simple , the line below illustrates it

      example_dictionary = {}             

Now lets see given tuples containing the pair of codes between which there exists an edge , how to convert them into graph

representation given above and store them in dictionaries ,Here is the tuple representation of connected nodes of the above graph

[(‘A’,’B’),(‘A’,’C’),(‘A’,’E’),(‘B’,’D’),(‘B’,’F’),(‘C’,’G’),(‘E’,’F’)]

def make_link(G,node1,node2):
	if node1 not in G:
		G[node1]= { }
	(G[node1])[node2]= 1 
	if node2 not in G:
		G[node2]= {}
	(G[node2])[node1]= 1 
	return G 
anil_akg rediff

connections = [('a','g'),('a','d'),('d','g'),('g','c')]
G = {}
for x,y in connections:make_link(G,x,y)

print G 

         

Here are the key points that could help you understand the above operations on dictionary G

G= {} #initializes the empty dictionary
G['A'] = {} #Creates a key 'A' in the dictionaries and assigns the key to a value of another empty hash
(G['A'])['B'] = 1 #Creates a Sub-Hash for key 'A' of the hash,Sub-hash is {'B':1}
print G

Here is the screen shot with the above mentioned operations , this will help you understand the process of representing a Graph similar to JSON using dictionaries .

Image

2.Concept of Depth first search

As wikipedia quotes “Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.

Now let us traverse the above graph using Depth-First-Search.Lets start from node ‘A’ and then move to any of its neighbouring nodes,lets say ndoe ‘C’ , mark these nodes when you visit them for the first time , ‘C’ has only one neighbour ‘G’,so move to node ‘G’ .’G’ has no neighbours so now start backtracking,move to the previous node , that is node ‘C’,node has no more unvisited neighbours , ‘G’ has already been visited.So now backtrack to ‘A’ ,since ‘C’ has already been visited now move on to an unvisited node of ‘A’ , lets say ‘B’ , this process repeats till all the nodes of the graph are visited ,Here is the python code to achieve the some

def make_link(g,node1,node2): #function to construct the graph in JSOn like format 
	if node1 not in G:
		G[node1]={}
	(G[node1])[node2]=1
	if node2 not in G:
		G[node2]={}
	(G[node2])[node1]=1

G={} #initializing the empty grapgh
connections = [('A','B'),('A','C'),('A','E'),('B','D'),('B','F'),('C','G'),('E','F')] #tuples representing the connections

for x,y in connections:make_link(G,x,y) #constructing the graph using tuple representation 

print G

def dfs(G,node,traversed):
	traversed[node]=True #mark the traversed node 
	print "traversal:"+ node 
	for neighbour_nodes in G[node]: #take a neighbouring node 
		if neighbour_nodes not in traversed: #condition to check whether the neighbour node is already visited
			dfs(G,neighbour_nodes,traversed) #recursively traverse the neighbouring node 

def start_traversal(G):
	traversed = {} #dictionary to mark the traversed nodes 
	for node in G.keys(): #G.keys() returns a node from the graph in its iteration
		if node not in traversed: #you start traversing from the root node only if its not visited 
			dfs(G,node,traversed); #for a connected graph this is called only once 

start_traversal(G)

The comments in the code explains everything . The worst case time complexity of DFS is of order n*m , ‘n’ is the number of nodes and ‘m’ is no of edges .We’ll thats it for now,hope that this post helped you understand the implementation of D.F.S in python ūüėÄ see you folks soon with more exciting posts,this is the link to the code from my GITHUB profile


C Program to add elements of an array using Divide and Conquer approach

#include<stdio.h>
#include<stdio.h>
int add(int ,int ,int *);
int main()
{
    int *a,n,i,sum,mid;
¬†¬†¬† printf(“\NEnter the no.of.elements: “);
¬†¬†¬† scanf(“%d”,&n);
    a=(int *)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
¬†¬†¬†¬† scanf(“%d”,a+i);
    sum=add(0,n-1,a);
¬†¬†¬† printf(“\nSum=%d”,sum);
    return 0;

}

int add(int low,int high,int *a)
{
    int mid;
    if(high==low)
     return a[low];
    mid=(low+high)/2;
    return add(low,mid,a)+add(mid+1,high,a);
}