0% found this document useful (0 votes)
8 views25 pages

13.recusive Function

Uploaded by

malok9199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

13.recusive Function

Uploaded by

malok9199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Recursive function

CSE304- Python Programming with Web Frameworks

Dr.R.Anushiadevi/SoC/SASTRA

Text Book: Michael Urban, Joel Murach. Murach’s Python Programming, Mike Murach & Associates,

First Indian Reprint, 2017.


Recursion
recursion occurs when a function calls itself
As a result, recursion can be used to execute the same action repeatedly until a certain condition is
reached

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 2


01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 3
Sum of 0+1+2+3…………..+n

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 4


Sum of 0+1+2+3…………..+n

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 5


Factorial

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 6


01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 7
01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 8
Fibonacci series

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 9


Fibonacci series

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 10


Fibonacci series

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 11


Fibonacci series

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 12


01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 13
Towers of Hanoi

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 14


Towers of Hanoi

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 15


Towers of Hanoi

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 16


Towers of Hanoi

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 17


Towers of Hanoi

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 18


Extra Program

01/24/2025 CSE304 - Python Programming with Web Frameworks Dr.R.Anushiadevi 19


Binary Tree Traversal
tree_edges = {'Root': 'A','A':{'left':'B','right':'C'}, 'B':{'left':'D', 'right':'E'},
'C':{'left':None,'right':'F'}, 'D':{'left':'G','right':'H'},
'E':{'left':'I','right':None}, 'F':{'left':'J','right':'K'} }
def postorder(node):
if node:
if tree_edges.get(node, None):
postorder(tree_edges[node].get('left',None))
if tree_edges.get(node, None):
postorder(tree_edges[node].get('right', None))
print(node, end=' ')
postorder(tree_edges['Root'])

01/24/2025 PYTHON PROGRAMMING 20


Merge Sort
def mergesort(A, low, high):
if low < high:
mid = (low + high)//2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
merge(A, low, mid, high)

01/24/2025 PYTHON PROGRAMMING 21


Merge Sort
def merge(A, low, mid, high):
i, j, k = low, mid+1, 0
T = [0]*(high-low+1)
while i<=mid and j<=high: L = [41, 35, 53, 21, 62, 11, 98, 44, 73]
mergesort(L, 0, len(L)-1)
if A[i] < A[j]:
print(L)
T[k] = A[i]
i += 1
else:
T[k] = A[j]
j += 1
k += 1
if i > mid:
T[k:high-low+1] = A[j:high+1]
else:
T[k:high-low+1] = A[i:mid+1]
A[low:high+1] = T[0:high-low+1]
01/24/2025 PYTHON PROGRAMMING 22
Breadth First Search

G = {'A':['B', 'C', 'D'], 'B':['A', 'D'], while Q:


'C':['A', 'D'], 'D':['A', 'B', 'C']} u = Q.pop(0)
for v in G.get(u, None):
def BFS(G, s): if color[v] == 'WHITE':
keys = G.keys() dist[v] = dist[u] + 1
parent = dict.fromkeys(keys, None) parent[v] = u
color = dict.fromkeys(keys, 'WHITE') color[v] = 'GRAY'
dist = dict.fromkeys(keys, 9999999) Q.append(v)
Q = [s] color[u] = 'BLACK'
dist[s] = 0 return parent, dist
color[s] = 'GRAY' P, D = BFS(G, 'A')
for v in G:
print (v, ' ', P[v], ' ', D[v])
01/24/2025 PYTHON PROGRAMMING 23
Depth First Search
G = {'A':['B', 'C', 'D'], 'B':['A', 'D'], def DFS_Visit(u):
'C':['A', 'D'], 'D':['A', 'B', 'C']} global d, f, time, parent, color
time += 1
def DFS(G): d[u] = time
keys = G.keys() color[u] = 'GRAY'
global time, d, f, parent, color for v in G.get(u, None):
time = 0 if color[v] == 'WHITE':
color = dict.fromkeys(keys, 'WHITE') parent[v] = u
d = dict.fromkeys(keys, 0) DFS_Visit(v)
f = dict.fromkeys(keys, 0) time += 1
parent = dict.fromkeys(keys, None) f[u] = time
for u in G: color[u] = 'BLACK'
if color[u] == 'WHITE': P, D, F = DFS(G)
DFS_Visit(u) for v in G:
return parent, d, f print (v, ' ', P[v], ' ', D[v], ' ', F[v])

01/24/2025 PYTHON PROGRAMMING 24


Prim’s Algorithm for finding MWST
G = {'A':[('B', 3), ('C', 1), ('D', 5)], 'B':[('A', 3), ('D', parent[s] = None
6)], cost = 0
'C':[('A', 1), ('D', 2)], 'D':[('A', 5), ('B', 6), ('C', print ("Edges of Minimum Weight Spanning
2)]} Tree")
while Q:
def extract_min(Q): u = extract_min(Q)
v = min(Q, key=Q.get) visited[u] = True
Q.pop(v) if not u == s:
return v print (parent[u], "-->", u)
def PRIMS(G,s): cost += dist[u]
keys = G.keys() for (v, edg_w) in G.get(u, None):
parent = dict.fromkeys(keys, None) if not visited[v] and dist[v] > edg_w:
visited = dict.fromkeys(keys, False) dist[v] = edg_w
dist = dict.fromkeys(keys, 9999999) Q[v] = edg_w
Q = dict.fromkeys(keys, 9999999) parent[v] = u
Q[s] = 0 print("Total Weight: ", cost)
dist[s] = 0
visited[s] = True PRIMS(G, 'A')
01/24/2025 PYTHON PROGRAMMING 25

You might also like