Vous êtes sur la page 1sur 18

# 1.

## TOWERS OF HANOI USING RECURSION

# Function Definition
print("Towers of Hanoi Using Recursion")
def TowerOfHanoi(n,from_rod,to_rod,aux_rod):

if n==1:
print('Move disk 1','from rod',from_rod,'to rod',to_rod)
return
TowerOfHanoi(n-1,from_rod,aux_rod,to_rod)
print('Move disk',n,'from rod',from_rod,'to rod',to_rod)
TowerOfHanoi(n-1,aux_rod,to_rod,from_rod)

## n=input('Enter the Number Of Rods: ')

# Function Call
TowerOfHanoi(n, 'A', 'C', 'B')
2. CREATION OF STACK USING ARRAY (LIST)

## #Creation of Stack Using Array

print("Stack Using Array")
class stack:
def __init__(self):
self.items=[]

def isempty(self):
return self.items==[]

#Push Function
def push(self,items):
self.items.append(items)

#Pop Function
def pop(self):
return self.items.pop()
print ("pop sucessfully")

#Traverse Function
def traverse(self):
for i in range(len(self.items)-1,-1,-1):

print(self.items[i])

#Function call
s=stack()
s.push(100)
s.push(99)
s.push(88)

s.push(77)
s.push(66)
s.push(55)
s.push(44)
s.push(33)
s.push(22)
s.push(11)
print('The Stack Elements are:')
s.traverse()

s.push(10)
print('After inserting 10 into the Stack')
s.traverse()
s.push(9)
print('After inserting 9 into the Stack')
s.traverse()

s.push(8)
print('After inserting 8 into the Stack')
s.traverse()
s.pop()
print('After popping the last element from the Stack')
s.traverse()

#Queue Creation

class queue:

def __init__(self,initval=None):

self.val=initval

self.next=None

def isempty(self):

return self.val==None

#Enqueue

def enqueue(self,v):

if self.isempty():

self.val=v

else:

newnode=queue(v)

temp=self

while(temp.next != None):

temp=temp.next

temp.next=newnode

#Dequeue

def dequeue(self):

if self.isempty():

print("Nothing to delete")

elif(self.next==None):

self.val=None

else:

self.val=self.next.val
self.next=self.next.next

#Traverse

def traverse(self):

if self.isempty():

print("Nothing to show")

else:

temp=self

while(temp != None):

print(temp.val)

temp=temp.next

#Function call

q=queue()

q.enqueue(101)

q.enqueue(202)

q.enqueue(303)

q.enqueue(404)

q.enqueue(505)

q.traverse()

q.enqueue(606)

q.traverse()

q.dequeue()

## print('After Dequeue as per Queue(FIFO) Policy')

q.traverse()
4. MIN HEAP ANAYSIS

## print('Min Heap Analysis')

import heapq

class Heap:

#Initializing Heap

def __init__(self):

self.heapList=[0]

self.size=0

def SearchElement(self,item):

i=1

while(i<=self.size):

if item==self.heapList[i]:

return i

i+=1

#To percolate Up

def percolateUp(self,i):

while i//2>0:

if self.heapList[i]<self.heapList[i//2]:

tmp=self.heapList[i//2]

self.heapList[i//2]=self.heapList[i]

self.heapList[i]=tmp

i=i//2

#To Insert

def insert(self,k):

self.heapList.append(k)

self.size = self.size + 1

self.percolateUp(self.size)
def printHeap(self):

print(self.heapList[1:])

#Function Call

HOrig = Heap()

HOrig.insert(1)

HOrig.insert(3)

HOrig.insert(6)

HOrig.insert(7)

HOrig.insert(9)

HOrig.insert(8)

## print('Before Insertion Operation')

HOrig.printHeap()

HOrig.insert(5)

## print('After Inserting 5')

HOrig.printHeap()

## print('The Search Element 7 is Present in',HOrig.SearchElement(7))

5. SELECTION SORT

print('Selection sort')

## #Selection Sort Function

def selectionsort(A):

## for i in range (len(A)):

least = i

for k in range(i+1,len(A)):

if A[k]<A[least]:

least=k

## #Swap Function Call

swap(A,least,i)

#Swap Function

def swap(A,x,y):

temp=A[x]

A[x]=A[y]

A[y]=temp

A=[45,85,74,12,65,10,6,8,36]

print(A)

selectionsort(A)

## print('After selection sort')

print(A)
6. QUICK SORT

print(“Quick Sort”)

#Function Definition

import random

def QuickSort(A,low,high):

if low<high:

pivot=partition(A,low,high)

QuickSort(A,low,pivot-1)

QuickSort(A,pivot+1,high)

## #To Partition the List

def partition(A,low,high):

pivot=low

swap(A,low,high)

for i in range(low,high):

if A[i]<=A[high]:

swap(A,i,low)

low+=1

swap(A,low,high)

return low

def swap(A,x,y):

temp=A[x]

A[x]=A[y]

A[y]=temp

## print('The Elements to be Sorted are')

A=[43,32,22,78,63,57,91,13]

print(A)
#Quick Sort Function Call

QuickSort(A,0,len(A)-1)

## print('After Performing Quick Sort')

print(A)
7. BINARY SEARCH TREE

class Node:

## def __init__(self, key):

self.data = key

self.left = None

self.right = None

#Insert Function

## def insert(node, data):

if node is None:

return (Node(data))

else:

else:

return node

## #Finding Minimum Value

def minValue(node):

current = node

## while(current.left is not None):

current = current.left

return current.data

## #Finding Maximum Value

def maxValue(node):

current = node
while(current.right is not None):

current = current.right

return current.data

#Finding Height

def height(node):

if node is None:

return 0

else:

lheight = height(node.left)

rheight = height(node.right)

return lheight+1

else:

return rheight+1

## #Level Order Function

def levelorder(node):

h = height(node)

## for i in range (1, h+1):

printlevel(node, i)

if node is None:

return

if level == 1:

print(node.data)

## elif level > 1:

printlevel(node.left, level-1)

printlevel(node.right,level-1)

#Function Call

root = None

root = insert(root,27)

insert(root,14)

insert(root,35)

insert(root,10)

insert(root,19)

insert(root,31)

insert(root,42)

levelorder(root)

## print "\nMaximum value in BST is %d" %(maxValue(root))

8. DEPTH FIRST SEARCH

## from collections import defaultdict

class Graph(object):

def __init__(self):

self.graph = defaultdict(list)

self.graph[u].append(v)

## #Search the Element

def depth_first_search(self,node):

visited = []

stack = [node]

while stack:

node = stack.pop()

## if node not in visited:

print node

visited.append(node)

for i in self.graph[node]:

stack.append(i)

#Function Call

G = Graph()

G.depth_first_search(0)
9. MINIMUM COST SPANNING TREE

vertex = 0

## # initialize empty edges array and empty MST

MST = []

edges = []

visited = []

minEdge = [None,None,float('inf')]

## # mark this vertex as visited

visited.append(vertex)

## # add each edge to list of potential edges

for r in range(0, V):

# find edge with the smallest weight to a vertex that has not yet been visited

## if edges[e][2] < minEdge[2] and edges[e][1] not in visited:

minEdge = edges[e]

## # remove min weight edge from list of edges

edges.remove(minEdge)

## # push min edge to MST

MST.append(minEdge)

## # start at new vertex and reset min edge

vertex = minEdge[1]

minEdge = [None,None,float('inf')]

return MST

graph = [

[0,1,2],

[0,2,3],

[1,3,3],

[1,2,5],

[1,4,4],

[2,4,4],

[3,4,2],

[3,5,3],
[4,5,5]]