0% found this document useful (0 votes)
11 views16 pages

Practical Lp2

The document outlines various algorithms including Depth First Search (DFS) and Breadth First Search (BFS), detailing their implementations and applications in graph traversal. It also briefly discusses the A* algorithm, a heuristic search technique, and includes examples of selection sort. The document provides code snippets for each algorithm and explains their operational principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Practical Lp2

The document outlines various algorithms including Depth First Search (DFS) and Breadth First Search (BFS), detailing their implementations and applications in graph traversal. It also briefly discusses the A* algorithm, a heuristic search technique, and includes examples of selection sort. The document provides code snippets for each algorithm and explains their operational principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

DFS BFS Algorithm

① DFS - Depth First

search algorithm.
l A

① P r e o r d e r D F S

C
B

3 4
7


F
E


② I n o r d e r D F S A

G
r
B

3
7

O E
S

B p o s t o r d e r D F S v i s i t t h e a l l - C h i ld r e n s

A 7
F ir s t

B ⑥

E ⑤


D F S is im p le m e n te d u s i n g s t a c k

s t a c k ( L I F O

① R e c u r s iv e m e t h o d e
u n d i r e c t e d
g r a p h

A d j u n c y
g r a p h = {

L i s t
'A ': [ ' B ','C ' ] ,

' B ' : [ ' A ', ' D ' , ' E ' ] ,

' C ' : [ ' A ', ' F ' ] ,

'D ':[ 'B '] ,

' E ' : [ ' D ','F ' ] ,

' F ' : [ ' C ','E ' ]

7
le n d o
}

d e f d f s _ r e c u r s iv e ( g r a p h , n o d e , v is it e d = N o n e ) :
c le v e l s
B
if v is it e d is N o n e :

v is it e d = s e t ( )

v is it e d . a d d ( n o d e )
T
le v e l 2
p r in t ( n o d e , e n d = ' ')

f o r n e ig h b o r in g r a p h [ n o d e ] :

F le v e l 3

if n e ig h b o r n o t in v is it e d :

d f s _ r e c u r s iv e ( g r a p h , n e ig h b o r ,v is it e d )

o u t p u t →
p r in t ( " D F S T r a v e r s a l r e c u r s iv e : " )

A → B → D → E → F → C
d f s _ r e c u r s i v e ( g r a p h , 'A ' )

s e t o f v i s it e d n o d e s .

B r e a d t h F i s t s e a r c h

6
7
s 4

3
2
l

① V i s it a v e r t e x
B F S

@ e x p lo r e t h e v e r t e x

6
B F S : 1 , 2 14 ,5 , 7 , 6 13

E E s

T a k e a q u e u e
1 1 4 12 ,3 1 5 8 7 1 10 , 9 , 6 4 2
7

② 5 18 g

4 14 1 3 1 0 8

l 3

g
B F S s p a n n in g t r e e s . 10
l

- . _ . . - c r o s s e d g e s .

7
8
3
S

g
1 0
6
graph = {

is' :[ '3', '7'],

'31 : [12', '4'],


'7' ; [ '8'],

'21 : [] ,

141 : [ ' 8"],

18' : []

visited = [] # List

for visited graph

queue = [] #

initialize a queue.

def bfs ( g raph, node ):


visited. append (node)

queue. append (node)

while queue:

m = queue. pop (o)

print (m, end =" ")

for neighbour in graph

[m]:

if neighbour not in

visited:

visited-append (neighbour)

queue. append (neighbour)


bfs (graph, s)
A* Algorithm

• It is important algorithm in

heuristic or informed search

technique

• Goal state of mind A dose

and &

f (N) = 9 (N) th (N)

cost Actual cost estimate cost from

from start n to goal node

node to n

G
S

gen)
hen)

12
S
"
f
4 16

Goal
12
G Goal
S
start
3

14 S

"

7 2

fcs) = 0 + 14 = 14

SB → f SB-e

S → B s → C
5+4+11 4+12+4

4 + 12 = 16
2
3+1/= 14 = 2 = 2

so → d
so → e

3+7+6 = 16
3+10+4 3

= 17
Scd → e

3+7+2+4
4
= ⑯ ✓

scde → G

12 + S to

17
.
Example: .

① e a c h r o w - s Q u e e n m a x

② e a c h c o lu m n - I d u e e n m a x .

n e g a t i v e d i a g n o a l .

0 1 2 3 ( r - c ) = a lw a y s s a m e

- 1 2 in c r e a s i n g c o lu m n b y 2

- I
2
in c r e a s in g r o w b y 1 .
I

1
2

2
3
p o s i t i v e d i a g o n a l ,

0 1 2 3
@ + C ) = a l w a y s s a m e

I
3

3 4
S D e c i s i o n T r e e
3

W E

✓ I d
c a n n o t p la c e

in s a m e c o lu m n s * * 3 ¾

c a n n o t p la c e in

n e g a t i v e d ia g o n a l

Branch In bound
I 2 3

0 columns = I
0 1 2 3

0 1 2 3

cr owt col)
3
2

I 3
total diagonal =
3
S
3
4
2
2 2*n-1

6
S
if q ue e n is pu t in ( 112 ) 3 3 4

of W = 1 +2 = 3

m a ke ar r ay of an-1 s iz e .
so d o n 't p la c e the que e n
> s t or e s
wher e the r tw =3 nd = b o o le a n
v a lu e s .
I 2 3 4 S 6

0 I 2 3

-2 -3
G r o w - c o /+ b . 1 - 1 )
- I

I - I -2 t o ta l di agona l
l

2 2
- I 2n- 1

3 2
3
0 I 2 3

3 2 0
l

I 2
4 3 1

4
S 3 2
2

6 S
4 3
3

rd =

2 3 4 5 6 .

cg.

0 I 2 3
0 I 2 3 0 I 2 3

O 2
2 l O
3

l 2
I I 3 I 3
2

2 2 3 4 4 3 2
2 2

6 S
3 4
3 3 3
3

r - c + b. e - 1 .
rtc

O 3
2

c ✓ ✓

nd =

2 3 4 S 6

r d =

2 3 4 S 6

I n 3rd n ow we can not p l a ce th e que en s o ba ck tr a ck and

t ry to p la c e th e queen in d if f e r e n t p la c e .
selection sort

14, S , 1 , 2 , 3

4 13, 1, 2, 5

213€, 41s

2, 1, 3, 4, S.

1,21314£

Time complexity ◦ cm²)

Orig inal array: [6 4 , 25, 12, 22, 11]


d e f s e le c t io n _ s o r t _ b ig g e s t ( a r r ) :

n = le n ( a r r )

outer loop i(=4,3,2,110)

f o r i in r a n g e ( n - 1 , 0 , - 1 ) :

assume that the first index is maximum index.


m a x _ in d e x = 0

range decreases (1- 5), (1-4), (1-3), (1-2),


f o r j in r a n g e ( 1 , i+ 1 ) :

if a r r [ j] > a r r [ m a x _ in d e x ] :

m a x _ in d e x = j

a r r [ i] , a r r [ m a x _ in d e x ] = a r r [ m a x _ in d e x ] , a r r [ i]

p r in t ( f " A f t e r s t e p { n - i} : { a r r } " )

r e t u r n a r r

You might also like